bbcjs.ui.LightBox.prototype.positionBox = function()
{
	bbcjs.trace('bbcjs.ui.LightBox.positionBox()');
	
	if (this.opts.top)
	{
		this.elemOuter.style.top = this.opts.top;
	}
	else if (window.innerHeight)
	{
		var newTop = (((window.innerHeight - this.elemOuter.offsetHeight)/2) + window.pageYOffset); //CHANGED
		newTop = newTop < 0 ? 0 : newTop; //CHANGED
		this.elemOuter.style.top = newTop; //CHANGED
		//alert (window.pageYOffset);
	}
	else
	{
		var newTop = (((document.body.clientHeight - this.elemOuter.offsetHeight)/2) + document.body.scrollTop); //CHANGED
		newTop = newTop < 0 ? 0 : newTop; //CHANGED
		this.elemOuter.style.top = newTop; //CHANGED
		//alert (document.body.scrollTop);
	}
	
	if (this.opts.left)
	{
		this.elemOuter.style.left = this.opts.left;
	}
	else if (window.innerWidth)
	{
		this.elemOuter.style.left = ((window.innerWidth - this.elemOuter.offsetWidth)/2);
	}
	else
	{
		this.elemOuter.style.left = ((document.body.clientWidth - this.elemOuter.offsetWidth)/2);
	}
};

bbcjs.addOnLoadItem(function() {
	OneShowLightbox.initLightboxes();
	OneShowGalleries.init();
	CommentSwitcher.initAll();
});

function $q(selector, container) {
	container = container || document;
	var aReturn = [];
	var aParams = selector.split(".");
	if (aParams[0] == "") {
		aParams[0] = "*";
	}
	var aElements = container.getElementsByTagName(aParams[0]);
	
	if (!aParams[1]) {
		return aElements;
	}
	for (var n = 0; n < aElements.length; n++) {
		if (bbcjs.dom.hasClassName(aElements[n], aParams[1])) {
			aReturn[aReturn.length] = aElements[n];
		}
	}
	return aReturn;
}

function isWithin(oInner, oOuter) {
	if (!oInner || !oOuter) return false;
	while ((oInner = oInner.parentNode) && oInner != oOuter);
	return oInner == oOuter;
}

/*TOOLTIP OBJECT*/

var Tooltip = function(oOpts) {
	this.element;
	this.opts = {
		offsetX: 5,
		offsetY: 20,
		followMouse: true,
		delay: 300,
		delayWhileStatic: false,
		className: ""
	}
	this.showInterval; //holds delay interval
	this.moveEventId = null;
	this.overEventId = null;
	this.isShown = false;
	this.isActive = false;
	
	//set options
	if (oOpts) {
		this.opts = Object.extend(this.opts, oOpts);
	}
	//create element
	var tmpElement = document.createElement("div");
	tmpElement.innerHTML = '<div class="uiTooltip ' + this.opts.className + '"></div>';
	this.element = tmpElement.childNodes[0];
	document.body.appendChild(this.element);
	//add to array
	Tooltip.tooltips[Tooltip.tooltips.length] = this;
}
Tooltip.tooltips = []; //stores all tooltips
Tooltip.hideAll = function() {
	for (var n = 0; n < Tooltip.tooltips.length; n++) {
		Tooltip.tooltips[n].hide();
	}
}
Tooltip.prototype = {
	setContent: function(newContent) {
		this.element.innerHTML = newContent;
	},
	
	activate: function() {
		if (this.isActive) {
			return;
		}
		this.isActive = true;
		this.addMoveEvent();
		var currentInstance = this;
		window.clearInterval(this.showInterval);
		this.showInterval = window.setInterval(function() {
			currentInstance.show();
		}, this.opts.delay);
	},
	
	show: function() {
		this.element.style.display = "block";
		window.clearInterval(this.showInterval);
		this.isShown = true;
		if (!this.opts.followMouse) {
			this.addOverEvent();
		}
	},
	
	hide: function() {
		window.clearInterval(this.showInterval);
		this.element.style.display = "";
		this.isShown = false;
		this.isActive = false;
		this.removeMoveEvent();
		this.removeOverEvent();
	},
	
	addMoveEvent: function() {
		this.moveEventId = bbcjs.dom.addEventListener(document, 'mousemove', this.moveEvent, this);
	},
	
	removeMoveEvent: function() {
		if (this.moveEventId !== null) {
			bbcjs.dom.removeEventListener(this.moveEventId);
			this.moveEventId = null;
		}
	},
	
	addOverEvent: function() {
		this.overEventId = bbcjs.dom.addEventListener(this.element, 'mouseover', this.hide, this);
	},
	
	removeOverEvent: function() {
		if (this.overEventId !== null) {
			bbcjs.dom.removeEventListener(this.overEventId);
			this.overEventId = null;
		}
	},
	
	moveEvent: function(e) {
		if (!this.opts.followMouse && this.isShown) {
			this.removeMoveEvent();
			if (this.opts.delay > 0) return;
		}
		
		this.element.style.left = (e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft))) + this.opts.offsetX + "px";
		this.element.style.top = (e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop))) + this.opts.offsetY + "px";
		
		//move off the right hand side
		var difference;
		if ((difference = (e.clientX + this.element.offsetWidth + this.opts.offsetX) - document.body.clientWidth) > 0) {
			this.element.style.left = (this.element.style.left.substr(0, this.element.style.left.length - 2) - difference) + "px";
		}
		
		if (!this.isShown && this.opts.delayWhileStatic) {
			window.clearInterval(this.showInterval);
			var currentInstance = this;
			this.showInterval = window.setInterval(function() {
				currentInstance.show();
			}, this.opts.delay);
		}
	}
};

/*add gallery mouseovers*/
var OneShowGalleries = {
	init: function() {
		var aGalleries = $q("ul.gallery");
		
		for (var n = 0; n < aGalleries.length; n++) {
			OneShowGalleries.initGallery(aGalleries[n]);
		}
	},
	
	initGallery: function(oGallery) {
		var aImages = $q("div.boxThumb", oGallery);
		var tooltipNode;
		
		for (var n = 0; n < aImages.length; n++) {
			tooltipNode = $q("img", aImages[n])[0];
			aImages[n].oTooltip = new Tooltip();
			aImages[n].oTooltip.setContent('<div class="shadow"></div><div class="inner">' + tooltipNode.alt + '</div>');
			
			aImages[n].onmouseover = function(e) {
				e = e || window.event;
				var relatedTarget = e.relatedTarget || e.fromElement;
				if (this != relatedTarget && !isWithin(relatedTarget, this)) {
					this.oTooltip.activate();
				}
			}
			aImages[n].onmouseout = function(e) {
				e = e || window.event;
				var relatedTarget = e.relatedTarget || e.toElement;
				if (this != relatedTarget && !isWithin(relatedTarget, this)) {
					this.oTooltip.hide();
				}
			}
		}
	}
}

/*lightbox*/
var myInterval;

var OneShowLightbox = {
	lightbox: null,	//holds the light box for the page

	template: '' + 
	
		'<div class="bbcjsUiLightBoxDefault lightbox">' +
		'<div class="contenDetailBG">' +
			'<a href="javascript:void(0)" id="bbcjsUiLightBoxClose"><img src="/arts/summerexhibition/images/lightboximages/close3.gif" width="74" height="20" alt="close" /></a><br />' +
			'<!-- <h2>{title}</h2> -->' +
			'<ul class="imageMeta">' +
				'<!-- <li>by {author}</li> -->' +
				'<!-- <li class="last">uploaded {uploadDate}</li> -->' +
			'</ul>' +
			'<img id="lbImg" src="{src}" width="{width}" height="{height}" alt="{title}" />' +
			'<!-- <h3>Description</h3> -->' +
			'<div class="description">{description}</div>' +
			'<div class="biogbox">{biog}</div>'+
'</div>' +
			'<ul id="bbcjsUiLightBoxOpts">' +
				'<li class="prevBtn"><a href="javascript:void(0)" id="bbcjsUiLightBoxPrevious"><span><img src="/arts/summerexhibition/images/previousbutton.gif" width="117" height="18" alt="Previous image" border="0"></span></a></li>' +
				'<li class="nextBtn"><a href="javascript:void(0)" id="bbcjsUiLightBoxNext"><span><img src="/arts/summerexhibition/images/nextbutton.gif" width="117" height="18" alt="Next image" border="0"></span></a></li>' +
				'<li id="bbcjsUiLightBoxPlace"><span></span></li>' +
			'</ul>' +
			'<!-- <div class="copyrightDetails">{copyright}</div> -->' +
		'</div>',
		
	initLightboxes: function() {
		//return if bbcjs.ui isn't included 
		if (!bbcjs.ui) return;
		
		var oLightboxContent;
		if (oLightboxContent = $("lightboxContent")) {
			oLightboxContent.style.display = "none";
		}
		var aObjs = OneShowLightbox.gatherDataObjects();
		
		OneShowLightbox.lightbox = new bbcjs.ui.LightBox(aObjs, {template:OneShowLightbox.template, width:function() {
			$("bbcjsUiLightBoxPlace").innerHTML = bbcjs.ui.LightBox.instances[0].current + 1 + " / " + bbcjs.ui.LightBox.instances[0].content.length + " images";
			window.setTimeout(function() {
				$("lbImg").src = $("lbImg").src;
			}, 0);
			return 580;
		}, borderWidth: 0});
		//add events
		for (var n = 0; n < aObjs.length; n++) {
			aObjs[n].linkNode.lightboxIndex = n;
			aObjs[n].linkNode.onclick = function() {
				OneShowLightbox.lightbox.open(this.lightboxIndex);
				$("bbcjsUiLightBoxClose").focus();
				Tooltip.hideAll();
				return false;
			}
			
			aObjs[n].altLink.lightboxIndex = n;
			aObjs[n].altLink.onclick = function() {
				OneShowLightbox.lightbox.open(this.lightboxIndex);
				$("bbcjsUiLightBoxClose").focus();
				Tooltip.hideAll();
				return false;
			}
		}
	},
	
	gatherDataObjects: function() {
		var aObjs = []; //array of objects to return
		var aDivs = $q("div.useLightbox");
		for (var n = 0; n < aDivs.length; n++) {
			aObjs[aObjs.length] = OneShowLightbox.gatherDataObject(aDivs[n].getElementsByTagName("a")[0], aDivs[n].getElementsByTagName("a")[1]);
		}
		return aObjs;
	},
	
	gatherDataObject: function(linkNode, altLink) {
		/*
			returns object:
			{
				linkNode: node of link tag
				altLink: an additional link which points to the same lightbox
				title: 
				description: 
				author: of image
				uploadDate: of image
				width: of image
				height: of image
				src: of image
				copyright: details
			}
		*/
		var obj = {linkNode: linkNode, altLink: altLink} // obj to return
		
		//get node containing data
		var oContent = $(linkNode.hash.substring(1));
		//throw error if doesn't exist
		if (!oContent) {
			linkNode.parentNode.style.background = "#f00";
			eval('throw new Error("No data div relating to a thumbnail box")');
		}
		//get title
		obj.title = oContent.getElementsByTagName("h2")[0].innerHTML;
		//cycle through lists
		var aLists = oContent.getElementsByTagName("ul");
		var aListItems;
		for (var n = 0; n < aLists.length; n++) {
			//get image meta data
			if (bbcjs.dom.hasClassName(aLists[n], "imageMeta")) {
				//get list items in meta list
				aListItems = aLists[n].getElementsByTagName("li");
				for (var m = 0; m < aListItems.length; m++) {
					//catch author
					if (/by\s+(.*)/i.test(aListItems[m].innerHTML)) {
						obj.author = /by\s+(.*)/i.exec(aListItems[m].innerHTML)[1];
					//catch upload date
					} else if (/uploaded\s+(.*)/i.test(aListItems[m].innerHTML)) {
						obj.uploadDate = /uploaded\s+(.*)/i.exec(aListItems[m].innerHTML)[1];
					} else if (/width:\s+(\d+)/i.test(aListItems[m].innerHTML)) {
						obj.width = /width:\s+(\d+)/i.exec(aListItems[m].innerHTML)[1];
					} else if (/height:\s+(\d+)/i.test(aListItems[m].innerHTML)) {
						obj.height = /height:\s+(\d+)/i.exec(aListItems[m].innerHTML)[1];
					}
				}
			}
		}
		
		//get info stored in divs
		var aDivs = oContent.getElementsByTagName("div");
		for (var n = 0; n < aDivs.length; n++) {
			//get description
			if (bbcjs.dom.hasClassName(aDivs[n], "description")) {
				obj.description = aDivs[n].innerHTML;
			//get copyright details
			} 
			else if (bbcjs.dom.hasClassName(aDivs[n], "biogbox")) {
				obj.biog = aDivs[n].innerHTML;
			}else if (bbcjs.dom.hasClassName(aDivs[n], "copyrightDetails")) {
				obj.copyright = aDivs[n].innerHTML;
			}
		}
		
		//get image location
		obj.src = oContent.getElementsByTagName("img")[0].parentNode.href;
		
		return obj;
		
	}
};

function CommentSwitcher(oElement /*element to enhance*/) {
	this.oElement = oElement; //element on page holding quote & switcher
	this.oQuoteHolder; //element which contains the current quote & meta data
	this.aComments = []; //array of CommentSwitcher.Comment objects
	this.currentComment = null; //index of current comment
	this.sourceUrl;
	
	
	this.init();
}
CommentSwitcher.prototype = {
	init: function() {
		//get link which points to quotes page
		var oLink = $q("a.toQuotes", this.oElement)[0];
		if (!oLink) {
			eval('throw new Error("Couldn\'t find link with class \'toQuotes\' inside comment switcher box")');
		}
		this.souceUrl = $q("a.toQuotes", this.oElement)[0].href;
		
		var currentInstance = this;
		
		//fetch qutoes page
		bbcjs.http.get(this.souceUrl, {
			onLoad:function(request) {
				currentInstance.processCommentsPage(request).enhanceNode();
			}
		});
		return this;
	},
	
	processCommentsPage: function(request) {
		//get contents of body tag
		var bodyContent;
		var rMatchBody = /<body[^>]>([\s\S]*)<\/body>/i;
		if (rMatchBody.test(request.text())) {
			bodyContent = rMatchBody.exec(request.text())[1];
		} else {
			bodyContent = request.text();
		}
		
		//process as dom object
		var tmpElement = document.createElement("div");
		tmpElement.innerHTML = bodyContent;
		
		//get list items within ol.comments
		var quoteNodes = $q("ol.comments", tmpElement)[0].getElementsByTagName("li");
		for (var n = 0; n < quoteNodes.length; n++) {
			this.aComments[this.aComments.length] = CommentSwitcher.Comment.FromHtml(quoteNodes[n]);
		}
		return this;
	},
	
	enhanceNode: function() {
		//add template
		this.oElement.innerHTML = CommentSwitcher.Templates.Main;
		//show first comment
		this.showComment(0);
		
		var currentInstance = this;
		//add events to buttons
		$q("a.next", this.oElement)[0].onclick = function() {
			currentInstance.showNext();
			return false;
		};
		$q("a.prev", this.oElement)[0].onclick = function() {
			currentInstance.showPrev();
			return false;
		}
		return this;
	},
	
	showComment: function(commentIndex) {
		$q("div.quoteHolder", this.oElement)[0].innerHTML = CommentSwitcher.Templates.Quote.supplant(this.aComments[commentIndex]);
		this.currentComment = commentIndex;
		this.updateNav();
		return this;
	},
	
	showNext: function() {
		this.hasNext() ? this.showComment(++this.currentComment) : this.showComment(this.currentComment = 0);
		return this;
	},
	
	showPrev: function() {
		this.hasPrev() ? this.showComment(--this.currentComment) : this.showComment(this.currentComment = this.aComments.length - 1);
		return this;
	},
	
	hasNext: function() {
		return !!this.aComments[this.currentComment + 1];
	},
	
	hasPrev: function() {
		return !!this.aComments[this.currentComment - 1];
	},
	
	updateNav: function() {
		//$q("a.next", this.oElement)[0].style.visibility = this.hasNext() ? "" : "hidden";
		//$q("a.prev", this.oElement)[0].style.visibility = this.hasPrev() ? "" : "hidden";
		return this;
	}
}
CommentSwitcher.Templates = {};
CommentSwitcher.Templates.Main = '' +
	'<div class="quoteHolder"></div>' +
	'<ul class="nav">' +
		'<li><a href="#" class="next">Next</a></li>' +
		'<li><a href="#" class="prev">Last</a></li>' +
		'<li class="main">' +
			'<div>' +
				'<a href="#">' +
					'Join in the conversation on the comment above' +
				'</a>' +
			'</div>' +
		'</li>' +
	'</ul>';
CommentSwitcher.Templates.Quote = '' +
	'<div class="quote1">' +
		'<blockquote>' +
			'{quote}' +
		'</blockquote>' +
	'</div>' +
	'<p class="quoteMeta">' +
		'<cite>{author}, {location}</cite> Posted {postTimeStr}' +
	'</p>';
CommentSwitcher.initAll = function() {
	var oNodes = $q("div.jsQuoteSwitcher");
	var oCommentSwitcher = null;
	//itterate through nodes
	for (var n = 0; n < oNodes.length; n++) {
		oCommentSwitcher = new CommentSwitcher(oNodes[n]);
	}
}
CommentSwitcher.Comment = function(quote, author, location, postTimeStr) {
	this.quote = quote || ""; //content
	this.author = author || ""; //name of poster
	this.location = location || ""; //location of poster
	this.postTimeStr = postTimeStr || ""; //time of post (formatted string)
}
CommentSwitcher.Comment.FromHtml = function(fragment /*node or html string*/) {
	if (typeof fragment == "string") {
		var oNode = document.createNode("div");
		oNode.innerHTML = fragment;
		fragment = oNode;
	}
	return new CommentSwitcher.Comment(
		$q("blockquote", fragment)[0].innerHTML,
		$q("span.author", fragment)[0].innerHTML,
		$q("span.location", fragment)[0].innerHTML,
		$q("span.postTime", fragment)[0].innerHTML
	);
}

//add vote button left float for lighbox:
document.write('<style> .LBVote {float:left;} </style>');