function configureGallery(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.thumbs = thumbs;
/*	instance.slots = node.getElementsByTagName("li"); */
	instance.slots = jQuery(".graphic .image");
	//instance.prev = getElementsByClass("prev", node, "div")[0].firstChild;
	//instance.next = getElementsByClass("next", node, "div")[0].firstChild;
	instance.index = 0;
	
	instance.setGallery = function()
	{
	    $slot_num = new RegExp("^image_([0-9]+)", "i");

		for (var j = 0; j < this.slots.length; j++)
		{
			$index = $slot_num.exec(this.slots[j].id)[1] - 1;
			var img = this.slots[j].getElementsByTagName("img")[0];
			var a = this.slots[j].getElementsByTagName("a");
			
			if (j < this.thumbs.length)
			{
				img.src = this.thumbs[$index].thumb;
				a[0].href = a[0].href = this.thumbs[$index].src;
//				a[0].innerHTML = this.thumbs[$index].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.getImgWidth = function() {
		return this.thumbs[this.index].imgwidth;
	}
	
	instance.getImgHeight = function() {
		return this.thumbs[this.index].imgheight;
	}
	
	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);};
	jQuery(".threebox").each(function(){
		initBox(this, 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) {
	jQuery(node).click(function() {
		var lnk = jQuery(this).attr("href");
		jQuery('body').append('<div id="threebox"><div class="overlay" id="overlay"></div></div>');
		focus_gallery = gallery;
		focus_gallery.setFocusSlot(lnk);
		updateBox(lnk);
		return false;
	});
	window.gallery_open = true;
}

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

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

// update the boxes content
function updateBox(href) {
	jQuery("#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 imgheight = focus_gallery.getImgHeight();
	var imgwidth = focus_gallery.getImgWidth();
	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>'
//  ORIGINAL FOLLOWS:
/*	var struct = '<div id="lightbox">';
struct +='					<ul class="pagination">';
struct +='						<li><a class="prev" href="#"><img src="/images/galleries/gallery-prev.gif" width="18" height="17" alt="prev" /></a></li>';
struct +='						<li>'+slide+'</li>';
struct +='						<li><a class="next" href="#"><img src="/images/galleries/gallery-next.gif" width="18" height="17" alt="next" /></a></li>';
struct +='						<li><a href="javascript:void(0);" class="close"><img src="/images/galleries/gallery-close.gif" width="41" height="9" alt="close" /></a></li>';
struct +='					</ul>';
struct +='					<h1>'+surheader+'</h1>';
struct +='					<h2>'+header+'</h2>';
struct +='					<div id="screenshot">';
struct +='						<div class="photoinfo">'+info+'</div>';
struct +='						<img src="'+href+'" id="imgScreen" onload="configureView();" />';
struct +='					</div>';
struct +='					<div id="caption">'+caption+'</div>';
struct +='		  </div>';
*/
	var struct = '<div id="lightbox">';
struct +='					<ul class="galleryCloseRow clearfix">';
struct +='						<li><a href="javascript:void(0);" class="close"><img src="/images/galleries/gallery-close.gif" width="41" height="9" alt="close" /></a></li>';
struct +='					</ul>';
struct +='					<ul class="pagination clearfix">';
struct +='						<li><span class="currentPosition">'+slide+'</span></li>';
struct +='						<li class="nextWrapper"><a class="next" href="#"><img src="/images/galleries/gallery-next.gif" width="18" height="17" alt="next" /></a></li>';
struct +='						<li class="prevWrapper"><a class="prev" href="#"><img src="/images/galleries/gallery-prev.gif" width="18" height="17" alt="prev" /></a></li>';
struct +='					</ul>';
struct +='					<h1>'+surheader+'</h1>';
struct +='					<h2>'+header+'</h2>';
struct +='					<div id="screenshot">';
struct +='						<div class="photoinfo">'+info+'</div>';
struct +='						<img src="'+href+'" height="' + imgheight + '" width="' + imgwidth + '" id="imgScreen" onload="configureView();" />';
struct +='					</div>';
struct +='					<div id="caption">'+caption+'</div>';
struct +='		  </div>';


	
	jQuery("#threebox").append(struct);
	setHeight();
	navLinks();
	//configureView();
}


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




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

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

function clearView() {
    if (jQuery.browser.msie) 
	    jQuery('#media-video').attr('style', 'height: 246px; width: 448px; overflow: hidden;');
	else
	    jQuery('#media-video').attr('style', '');
	jQuery('#billboard-wrapper').attr('style', 'display: block;');
	jQuery("#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";
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		dsp.style.left = (offset[0] + ((size[0] - dspW) / 2)) -5 + "px"; // minus zero used to be minus five
		dsp.style.top = offset[1] -11 + "px"; // minus zero used to be minus eleven
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		dsp.style.left = (offset[0] + ((size[0] - dspW) / 2)) -0 + "px"; // minus zero used to be minus five
		dsp.style.top = offset[1] -0 + "px"; // minus zero used to be minus eleven
	}
}

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 ];
}

