function configureGallery(id, thumbs)
{
	// assign each thumb its starting position as a property (1-indexed)
	for (var j in thumbs)
	{
		thumbs[j].num = parseInt(j) + 1;
	}
	
	var instance = new Object();
	var node = document.getElementById(id);
	instance.node = node;
	instance.thumbs = thumbs;
	instance.slots = node.getElementsByTagName("li");
	instance.prev = getElementsByClass("prev", node, "div")[0].firstChild;
	instance.next = getElementsByClass("next", node, "div")[0].firstChild;
	instance.index = 0;
	
	instance.setGallery = function()
	{
		for (var j = 0; j < this.slots.length; j++)
		{
			var img = this.slots[j].getElementsByTagName("img")[0];
			var a = this.slots[j].getElementsByTagName("a");
			
			if (j < this.thumbs.length)
			{
				img.src = this.thumbs[j].thumb;
				a[0].href = a[1].href = this.thumbs[j].src;
				a[1].innerHTML = this.thumbs[j].caption;
			}
			else
			{
				this.slots[j].innerHTML = '<div class="blank"></div><p></p>';
			}
		}
		
		if (this.thumbs.length > this.slots.length)
		{
			this.prev.style.display = this.next.style.display = "block";
		}
	}
	
	instance.goPrev = function(updateBrowser)
	{
		if (updateBrowser == null) updateBrowser = false;
		
		if (updateBrowser)
		{
			var inc = this.thumbs.pop();
			this.thumbs.unshift(inc);
			this.setGallery();
		}
		else
		{
			this.index = (this.index > 0) ? this.index-1 : this.thumbs.length-1;
		}
	}
	
	instance.goNext = function(updateBrowser)
	{
		if (updateBrowser == null) updateBrowser = false;
		
		if (updateBrowser)
		{
			var inc = this.thumbs.shift();
			this.thumbs.push(inc);
			this.setGallery();
		}
		else
		{
			this.index = (this.index < this.thumbs.length-1) ? this.index+1 : 0;
		}
	}
	
	instance.setFocusSlot = function(img)
	{
		for (var j = 0; j < this.thumbs.length; j++)
		{
			if (this.thumbs[j].src == img)
			{
				this.index = j
				break;	
			}
		}
	}
	
	instance.getFocusImage = function()
	{
		return this.thumbs[this.index].src;
	}
	
	instance.getFocusSurheader = function()
	{
		return this.thumbs[this.index].surheader;
	}
	
	instance.getFocusHeader = function()
	{
		return this.thumbs[this.index].header;
	}
	
	instance.getFocusSlideNum = function()
	{
		return this.thumbs[this.index].num;
	}
	
	instance.getFocusInfo = function()
	{
		return this.thumbs[this.index].info;
	}
	
	instance.getFocusCaption = function()
	{
		return this.thumbs[this.index].fullcaption;
	}
	
	instance.getSlideCount = function()
	{
		return this.thumbs.length;
	}
	
	instance.setGallery();
	instance.prev.href = instance.next.href = "javascript:void(0);";
	instance.prev.onclick = function() {instance.goPrev(true);};
	instance.next.onclick = function() {instance.goNext(true);};
	initBox(id, instance);
	
	return instance;
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null ) node = document;
	if ( tag == null ) tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp(searchClass);
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements.push(els[i]);
			j++;
		}
	}
	return classElements;
}





var focus_gallery;

// set things up
function initBox(node, gallery) {
	J('#'+node+' a.threebox').click(function() {
		//J('#media-video').attr('style', 'visibility: hidden;');
		J('#billboard-wrapper').attr('style', 'display: none;');
		if (J.browser.msie) 
		    J('#media-video').attr('style', 'height: 0; width: 0; overflow: hidden;');
    	else
    	    J('#media-video').attr('style', 'visibility: hidden;');
    	
		var lnk = J(this).attr("href");
		J('body').append('<div id="threebox"><div class="overlay" id="overlay"></div></div>');
		focus_gallery = gallery;
		focus_gallery.setFocusSlot(lnk);
		updateBox(lnk);
		return false;
	});
}

// Loading icon
function showLoading() {
	var imgPath = '../i/galleries/gallery-loading.gif';
	var imgHtml = '<img id="loading" src="' + imgPath + '" alt="Loading..." width="208" height="13" />';
	J("#threebox").append(imgHtml);
}

function hideLoading() {
	J("#loading").remove();
}

// update the boxes content
function updateBox(href) {
	J("#lightbox").remove();
	showLoading();
	getContent(href);
}

// Grab the file you need
function getContent(href) {
	hideLoading();
	
	var surheader = focus_gallery.getFocusSurheader() + "&nbsp;";
	var header = focus_gallery.getFocusHeader() + "&nbsp;";
	var slide = focus_gallery.getFocusSlideNum() + " of " + focus_gallery.getSlideCount();
	var info = focus_gallery.getFocusInfo();
	var caption = focus_gallery.getFocusCaption();
	
	var prevImg = '../i/galleries/gallery-prev.gif';
	var nextImg = '../i/galleries/gallery-next.gif';
	var closeImg = '../i/galleries/gallery-close.gif';
	var struct = '<div id="lightbox"><ul class="pagination"><li><a class="prev" href="#"><img src="/i/galleries/gallery-prev.gif" width="6" height="9" alt="prev" /></a></li><li>'+slide+'</li><li><a class="next" href="#"><img src="/i/galleries/gallery-next.gif" width="6" height="9" alt="next" /></a></li><li><a href="javascript:void(0);" class="close"><img src="/i/galleries/gallery-close.gif" width="9" height="9" alt="close" /></a></li></ul><h1>'+surheader+'</h1><h2>'+header+'</h2><div id="screenshot"><div class="photoinfo">'+info+'</div><img src="'+href+'" id="imgScreen" onload="configureView();" /></div><div id="caption">'+caption+'</div></div>'
	
	J("#threebox").append(struct);
	setHeight();
	navLinks();
	//configureView();
}


// assign previous and next link actions
function navLinks() {
	J('.pagination a.prev').click(function() {
		focus_gallery.goPrev(false);
		updateBox(focus_gallery.getFocusImage());
		return false;
	});
	
	J('.pagination a.next').click(function() {
		focus_gallery.goNext(false);
		updateBox(focus_gallery.getFocusImage());
		return false;
	});
	
	J('.pagination a.close').click(function() {
		clearView();
		return false;
	});
}


// set and reset the height of the window
function setHeight() {
	var ht = J("#page").height();
	var sHt = ht - 160;
	if( J("#imgScreen").height() >= sHt  ) {
		ht = J("#imgScreen").height() + 160;
	}
	else ht;
	J("#threebox .overlay").height(ht);
}

function configureView() {
	adjustView();
	//window.onscroll = adjustView;
	window.onresize = adjustView;
}

function clearView() {
    if (J.browser.msie) 
	    J('#media-video').attr('style', 'height: 246px; width: 448px; overflow: hidden;');
	else
	    J('#media-video').attr('style', '');
	J('#billboard-wrapper').attr('style', 'display: block;');
	J("#threebox").remove();
	
	//delete window.onscroll;
	delete window.onresize;
	
	/*/ reset overlay display
	var ol = document.getElementById("overlay");
	if (ol != null) {
		ol.style.left = "0px";
		ol.style.top = "0px";
		ol.style.width = "10px";
		ol.style.height = "10px";
	}*/
}

function adjustView() {
	var size = getViewableSize();
	var offset = getScrollOffset();
	
	var dsp = document.getElementById("lightbox");
	var dspW = dsp.offsetWidth;
	var dspH = dsp.offsetHeight;
	
	dsp.style.position = "absolute";
	dsp.style.left = (offset[0] + ((size[0] - dspW) / 2)) + "px";
	dsp.style.top = (offset[1] + ((size[1] - dspH) / 2) - 30) + "px";
}

function getViewableSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [ myWidth, myHeight ];
}

function getScrollOffset() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}