// -----------------------------------------------------------------------------------
//
//	Lightbox v2.02
//	by Lokesh Dhakar - http://www.huddletogether.com
//	3/31/06
//
//	For more information on this script, visit:
//	http://huddletogether.com/projects/lightbox2/
//
//	Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//	
//	Credit also due to those who have helped, inspired, and made their code available to the public.
//	Including: Scott Upton(uptonic.com), Peter-Paul Koch(quirksmode.org), Thomas Fuchs(mir.aculo.us), and others.
//
//
// -----------------------------------------------------------------------------------
/*

	Table of Contents
	-----------------
	Configuration
	Global Variables

	Extending Built-in Objects	
	- Object.extend(Element)
	- Array.prototype.removeDuplicates()
	- Array.prototype.empty()

	Lightbox Class Declaration
	- initialize()
	- start()
	- changeImage()
	- resizeImageContainer()
	- showImage()
	- updateDetails()
	- updateNav()
	- enableKeyboardNav()
	- disableKeyboardNav()
	- keyboardAction()
	- preloadNeighborImages()
	- end()
	
	Miscellaneous Functions
	- getPageScroll()
	- getPageSize()
	- getKey()
	- listenKey()
	- showSelectBoxes()
	- hideSelectBoxes()
	- pause()
	- initLightbox()
	
	Function Calls
	- addLoadEvent(initLightbox)
	
*/
// -----------------------------------------------------------------------------------

//
//	Configuration
//
var fileLoadingImage = "/images/lightbox/loading.gif";		
var fileBottomNavCloseImage = "/images/lightbox/blank.gif";
var fileTopNavCloseImage = "/images/lightbox/close_button.gif";
var transPixel = "/images/tpix.gif";
var capScaleY;
var I_ID;
var caption = "";
var moreInfo = "";

var resizeSpeed = 5;	// controls the speed of the image resizing (1=slowest and 10=fastest)
var borderTop= 70;
var borderLeft = 30; 
var borderRight = 30; 
var borderBottom = 15;	//if you adjust the padding in the CSS, you will need to update this variable
var navItemWidth = 34;
var bottomNavWidth = 330;
var captionlength = 0;
var moreInfoFlag = false;
var req; // AJAX request object...
var isIE = false;
// -----------------------------------------------------------------------------------

//
//	Global Variables
//
var imageArray = new Array;
var activeImage;
var infoArray = new Array;
if(resizeSpeed > 10){ resizeSpeed = 10;}
if(resizeSpeed < 1){ resizeSpeed = 1;}
resizeDuration = (11 - resizeSpeed) * 0.05;
var prevLink = new Array();
prevLink['a'] = new Image(34,54);
prevLink['a'].src = "/images/lightbox/previous_button.gif";
prevLink['o'] = new Image(34,54);
prevLink['o'].src = "/images/lightbox/previous_button_over.gif";
prevLink['c'] = new Image(34,54); 
prevLink['c'].src = "/images/lightbox/previous_button_click.gif";
prevLink['d'] = new Image(34,54); 
prevLink['d'].src = "/images/lightbox/previous_button_dis.gif";

var nextLink = new Array();
nextLink['a'] = new Image(34,54);
nextLink['a'].src = "/images/lightbox/next_button.gif";
nextLink['o'] = new Image(34,54);
nextLink['o'].src = "/images/lightbox/next_button_over.gif";
nextLink['c'] = new Image(34,54);
nextLink['c'].src = "/images/lightbox/next_button_click.gif";
nextLink['d'] = new Image(34,54);
nextLink['d'].src = "/images/lightbox/next_button_dis.gif";

var helpLink = new Array();
helpLink['a'] = new Image(34,54);
helpLink['a'].src = "/images/lightbox/help_button.gif";
helpLink['o'] = new Image(34,54);
helpLink['o'].src = "/images/lightbox/help_button_over.gif";
helpLink['c'] = new Image(34,54);
helpLink['c'].src = "/images/lightbox/help_button_click.gif";

var infoLink = new Array();
infoLink['a'] = new Image(34,54);
infoLink['a'].src = "/images/lightbox/more-info_button.gif";
infoLink['o'] = new Image(34,54);
infoLink['o'].src = "/images/lightbox/more-info_button_over.gif";
infoLink['c'] = new Image(34,54);
infoLink['c'].src = "/images/lightbox/more-info_button_click.gif";

var bookmarkLink = new Array();
bookmarkLink['a'] = new Image(34,54);
bookmarkLink['a'].src = "/images/lightbox/bookmark_button.gif";
bookmarkLink['o'] = new Image(34,54);
bookmarkLink['o'].src = "/images/lightbox/bookmark_button_over.gif";
bookmarkLink['c'] = new Image(34,54);
bookmarkLink['c'].src = "/images/lightbox/bookmark_button_click.gif";

var shopLink = new Array();
shopLink['a'] = new Image(34,54);
shopLink['a'].src = "/images/lightbox/shopping-cart_button.gif";
shopLink['o'] = new Image(34,54);
shopLink['o'].src = "/images/lightbox/shopping-cart_button_over.gif";
shopLink['c'] = new Image(34,54);
shopLink['c'].src = "/images/lightbox/shopping-cart_button_click.gif";

var signinLink = new Array();
signinLink['a'] = new Image(34,54);
signinLink['a'].src = "/images/lightbox/login_button.gif";
signinLink['o'] = new Image(34,54);
signinLink['o'].src = "/images/lightbox/login_button_over.gif";
signinLink['c'] = new Image(34,54);
signinLink['c'].src = "/images/lightbox/login_button_click.gif";
// -----------------------------------------------------------------------------------

//
//	Additional methods for Element added by SU, Couloir
//	- further additions by Lokesh Dhakar (huddletogether.com)
//
Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setLeft: function(element,l) {
	   	element = $(element);
    	element.style.left = l +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});

// -----------------------------------------------------------------------------------

//
//	Extending built-in Array object
//	- array.removeDuplicates()
//	- array.empty()
//
Array.prototype.removeDuplicates = function () {
	for(i = 1; i < this.length; i++){
		if(this[i][0] == this[i-1][0]){
			this.splice(i,1);
		}
	}
}

// -----------------------------------------------------------------------------------

Array.prototype.empty = function () {
	for(i = 0; i <= this.length; i++){
		this.shift();
	}
}

// -----------------------------------------------------------------------------------

//
//	Lightbox Class Declaration
//	- initialize()
//	- start()
//	- changeImage()
//	- resizeImageContainer()
//	- showImage()
//	- updateDetails()
//	- updateNav()
//	- enableKeyboardNav()
//	- disableKeyboardNav()
//	- keyboardNavAction()
//	- preloadNeighborImages()
//	- end()
//
//	Structuring of code inspired by Scott Upton (http://www.uptonic.com/)
//
var Lightbox = Class.create();

Lightbox.prototype = {
	
	// initialize()
	// Constructor runs on completion of the DOM loading. Loops through anchor tags looking for 
	// 'lightbox' references and applies onclick events to appropriate links. The 2nd section of
	// the function inserts html at the bottom of the page which is used to display the shadow 
	// overlay and the image container.
	//
	initialize: function() {
		
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');

		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];
			
			var relAttribute = String(anchor.getAttribute('rel'));
			
			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox'))){
				anchor.onclick = function () {myLightbox.start(this); return false;}
			}
		}

		var objBody = document.getElementsByTagName("body").item(0);
		
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.style.display = 'none';
		objOverlay.onclick = function() { myLightbox.end(); return false; }
		objBody.appendChild(objOverlay);
		
		var objLightbox = document.createElement("div");
		objLightbox.setAttribute('id','lightbox');
		objLightbox.style.display = 'none';
		objBody.appendChild(objLightbox);
	
		var objOuterImageContainer = document.createElement("div");
		objOuterImageContainer.setAttribute('id','outerImageContainer');
		objLightbox.appendChild(objOuterImageContainer);

/* --------------------------------------------------------------------------------*/
		var objBottomNav = document.createElement("div");
		objBottomNav.setAttribute('id','bottomNav');
		objBottomNav.setAttribute('class','clearfix');
		objOuterImageContainer.appendChild(objBottomNav);
		
		var objSigninLink = document.createElement("a");
		objSigninLink.setAttribute('id','signinLink');
		objSigninLink.setAttribute('class','bottomNavLink');
		objSigninLink.setAttribute('href','#');
		if(is.ie){
			objSigninLink.onmouseover = function(){objSigninLinkImage.src = signinLink['o'].src;}
			objSigninLink.onmouseout = function(){objSigninLinkImage.src = signinLink['a'].src;}
		}
		objBottomNav.appendChild(objSigninLink);
		
		if(is.ie){
			var objSigninLinkImage = document.createElement("img");
			objSigninLinkImage.setAttribute('src', signinLink['a'].src);
			objSigninLinkImage.setAttribute('id', 'signinLinkImg');
			
			objSigninLink.appendChild(objSigninLinkImage);
		}

		var objShopLink = document.createElement("a");
		objShopLink.setAttribute('id','shopLink');
		objShopLink.setAttribute('class','bottomNavLink');
		objShopLink.setAttribute('href','#');
		if(is.ie){
			objShopLink.onmouseover = function(){objShopLinkImage.src = shopLink['o'].src;}
			objShopLink.onmouseout = function(){objShopLinkImage.src = shopLink['a'].src;}
		}
		objBottomNav.appendChild(objShopLink);
		
		if(is.ie){
			var objShopLinkImage = document.createElement("img");
			objShopLinkImage.setAttribute('src', shopLink['a'].src);
			objShopLinkImage.setAttribute('id', 'shopLinkImg');
			objShopLink.appendChild(objShopLinkImage);
		}
		
		var objBookmarkLink = document.createElement("a");
		objBookmarkLink.setAttribute('id','bookmarkLink');
		objBookmarkLink.setAttribute('class','bottomNavLink');
		objBookmarkLink.setAttribute('href','#');
		if(is.ie){
			objBookmarkLink.onmouseover = function(){objBookmarkLinkImage.src = bookmarkLink['o'].src;}
			objBookmarkLink.onmouseout = function(){objBookmarkLinkImage.src = bookmarkLink['a'].src;}
		}
		objBottomNav.appendChild(objBookmarkLink);
		
		if(is.ie){
			var objBookmarkLinkImage = document.createElement("img");
			objBookmarkLinkImage.setAttribute('src', bookmarkLink['a'].src);
			objBookmarkLinkImage.setAttribute('id', 'bookmarkLinkImg');
			objBookmarkLink.appendChild(objBookmarkLinkImage);
		}
		
		var objInfoLink = document.createElement("a");
		objInfoLink.setAttribute('id','infoLink');
		objInfoLink.setAttribute('class','bottomNavLink');
		objInfoLink.setAttribute('href','#');
		if(is.ie){
			objInfoLink.onmouseover = function(){objInfoLinkImage.src = infoLink['o'].src;}
			objInfoLink.onmouseout = function(){objInfoLinkImage.src = infoLink['a'].src;}
		}
		objBottomNav.appendChild(objInfoLink);

		if(is.ie){
			var objInfoLinkImage = document.createElement("img");
			objInfoLinkImage.setAttribute('src', infoLink['a'].src);
			objInfoLinkImage.setAttribute('id', 'infoLinkImg');
			objInfoLink.appendChild(objInfoLinkImage);
		}
		
		var objHelpLink = document.createElement("a");
		objHelpLink.setAttribute('id','helpLink');
		objHelpLink.setAttribute('class','bottomNavLink');
		objHelpLink.setAttribute('href','#');
		if(is.ie){
			objHelpLink.onmouseover = function(){objHelpLinkImage.src = helpLink['o'].src;}
			objHelpLink.onmouseout = function(){objHelpLinkImage.src = helpLink['a'].src;}
		}
		objBottomNav.appendChild(objHelpLink);
		
		if(is.ie){
			var objHelpLinkImage = document.createElement("img");
			objHelpLinkImage.setAttribute('src', helpLink['a'].src);
			objHelpLinkImage.setAttribute('id', 'helpLinkImg');
			objHelpLink.appendChild(objHelpLinkImage);
		}
		
		var objPrevLink = document.createElement("a");
		objPrevLink.setAttribute('id','prevLink');
		objPrevLink.setAttribute('class','bottomNavLink');
		objPrevLink.setAttribute('href','#');
		if(is.ie){
			objPrevLink.onmouseover = function(){objPrevLinkImage.src = prevLink['o'].src;}
			objPrevLink.onmouseout = function(){objPrevLinkImage.src = prevLink['a'].src;}
		}
		objBottomNav.appendChild(objPrevLink);
		
		var objPrevLinkDis = document.createElement("img");
		objPrevLinkDis.setAttribute('src', prevLink['d'].src);
		objPrevLinkDis.setAttribute('id', 'prevLinkDis');
		objBottomNav.appendChild(objPrevLinkDis);
		
		if(is.ie){
			var objPrevLinkImage = document.createElement("img");
			objPrevLinkImage.setAttribute('src', prevLink['a'].src);
			objPrevLinkImage.setAttribute('id', 'prevLinkImg');
			objPrevLink.appendChild(objPrevLinkImage);
		}
		
		var objNextLink = document.createElement("a");
		objNextLink.setAttribute('id','nextLink');
		objNextLink.setAttribute('class','bottomNavLink');
		objNextLink.setAttribute('href','#');
		if(is.ie){
			objNextLink.onmouseover = function(){objNextLinkImage.src = nextLink['o'].src;}
			objNextLink.onmouseout = function(){objNextLinkImage.src = nextLink['a'].src;}
		}
		objBottomNav.appendChild(objNextLink);
		
		var objNextLinkDis = document.createElement("img");
		objNextLinkDis.setAttribute('src', nextLink['d'].src);
		objNextLinkDis.setAttribute('id', 'nextLinkDis');
		objBottomNav.appendChild(objNextLinkDis);
		
		if(is.ie){
			var objNextLinkImage = document.createElement("img");
			objNextLinkImage.setAttribute('src', nextLink['a'].src);
			objNextLinkImage.setAttribute('id', 'nextLinkImg');
			objNextLink.appendChild(objNextLinkImage);
		}
		
		var objTopNav = document.createElement("div");
		objTopNav.setAttribute('id','topNav');
		objOuterImageContainer.appendChild(objTopNav);
	
		var objTopNavCloseLink = document.createElement("a");
		objTopNavCloseLink.setAttribute('id','topNavClose');
		objTopNavCloseLink.setAttribute('href','#');
		objTopNavCloseLink.onclick = function() { myLightbox.end(); return false; }
		objTopNav.appendChild(objTopNavCloseLink);
	
		var objTopNavCloseImage = document.createElement("img");
		objTopNavCloseImage.setAttribute('src', fileTopNavCloseImage);
		objTopNavCloseLink.appendChild(objTopNavCloseImage);
/* --------------------------------------------------------------------------------*/

		var objImageContainer = document.createElement("div");
		objImageContainer.setAttribute('id','imageContainer');
		objOuterImageContainer.appendChild(objImageContainer);
	
		var objLightboxImage = document.createElement("img");
		objLightboxImage.setAttribute('id','lightboxImage');
		objImageContainer.appendChild(objLightboxImage);

		var objLoading = document.createElement("div");
		objLoading.setAttribute('id','loading');
		objImageContainer.appendChild(objLoading);
	
		var objLoadingLink = document.createElement("a");
		objLoadingLink.setAttribute('id','loadingLink');
		objLoadingLink.setAttribute('href','#');
		objLoadingLink.onclick = function() { myLightbox.end(); return false; }
		objLoading.appendChild(objLoadingLink);
	
		var objLoadingImage = document.createElement("img");
		objLoadingImage.setAttribute('src', fileLoadingImage);
		objLoadingLink.appendChild(objLoadingImage);

		var objImageDataContainer = document.createElement("div");
		objImageDataContainer.setAttribute('id','imageDataContainer');
		objImageDataContainer.className = 'clearfix';
		objLightbox.appendChild(objImageDataContainer);

		var objImageData = document.createElement("div");
		objImageData.setAttribute('id','imageData');
		objImageData.setAttribute('class','clearfix');
		objImageDataContainer.appendChild(objImageData);

		var objImageDetails = document.createElement("div");
		objImageDetails.setAttribute('id','imageDetails');
		objImageDetails.setAttribute('class','clearfix');
		objImageData.appendChild(objImageDetails);

		var objCaption = document.createElement("div");
		objCaption.setAttribute('id','caption');
		objImageDetails.appendChild(objCaption);
		
		var objmoreInfo = document.createElement("div");
		objmoreInfo.setAttribute('id','moreInfo');
		objImageDetails.appendChild(objmoreInfo);

	},
	
	//
	//	start()
	//	Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
	//
	start: function(imageLink) {	
		
		hideSelectBoxes();
		
		// stretch overlay to fill page and fade in
		var arrayPageSize = getPageSize();
		Element.setHeight('overlay', arrayPageSize[1]);
		if(is.ie) Element.setWidth('overlay', (arrayPageSize[0]+17));
		new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.8 });

		imageArray = [];
		imageNum = 0;		

		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');

		// if image is NOT part of a set..
		if((imageLink.getAttribute('rel') == 'lightbox')){
			// add single image to imageArray
			imageArray.push(new Array(imageLink.getAttribute('href'), imageLink.getAttribute('title'), imageLink.getAttribute('name'), imageLink.getAttribute('id')));			
		} else {
		// if image is part of a set..

			// loop through anchors, find other images in set, and add them to imageArray
			for (var i=0; i<anchors.length; i++){
				var anchor = anchors[i];
				if (anchor.getAttribute('href') && (anchor.getAttribute('rel') == imageLink.getAttribute('rel'))){
					imageArray.push(new Array(anchor.getAttribute('href'), anchor.getAttribute('title'), anchor.getAttribute('name'), anchor.getAttribute('id')));
				}
			}
			imageArray.removeDuplicates();
			while(imageArray[imageNum][0] != imageLink.getAttribute('href')) { imageNum++;}
		}

		// calculate top offset for the lightbox and display 
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
//		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);
		var lightboxTop = arrayPageScroll[1] + 20;

		Element.setTop('lightbox', lightboxTop);
		Element.show('lightbox');
		this.changeImage(imageNum);
	},

	//
	//	changeImage()
	//	Hide most elements and preload image in preparation for resizing image container.
	//
	changeImage: function(imageNum) {	
		
		activeImage = imageNum;	// update global var
		var tmp = imageArray[activeImage][0].split('/');
		
		I_ID = tmp[tmp.length-1];
		//alert(imageArray[activeImage][3]);
		// hide elements during transition
		Element.show('loading');
		Element.hide('lightboxImage');
		Element.hide('topNav');
		Element.hide('bottomNav');
		Element.hide('prevLink');
		Element.hide('prevLinkDis');
		Element.hide('nextLink');
		Element.hide('nextLinkDis');
		Element.hide('helpLink');
		Element.hide('infoLink');
		Element.hide('bookmarkLink');
		Element.hide('moreInfo');
		Element.hide('shopLink');
		Element.hide('signinLink');
		Element.hide('imageDataContainer');
		Element.setHeight('imageDataContainer',50);
		
		imgPreloader = new Image();
		
		// once image is preloaded, resize image container
		imgPreloader.onload=function(){
			Element.setSrc('lightboxImage', imageArray[activeImage][0]);
			myLightbox.resizeImageContainer(imgPreloader.width, imgPreloader.height);
			
			//alert(imageNum);
		}
		imgPreloader.src = imageArray[activeImage][0];
	},

	//
	//	resizeImageContainer()
	//
	resizeImageContainer: function( imgWidth, imgHeight) {
		borderWidth = borderLeft + borderRight;
		borderHeight = borderTop + borderBottom;
		// get current height and width
		this.wCur = Element.getWidth('outerImageContainer');
		this.hCur = Element.getHeight('outerImageContainer');
		this.navWidth = Element.getWidth('bottomNav');

		// scalars based on change from old to new
		if(imgWidth > 336) {
			wTmp = 500;
			capScaleY = 250;
		} else {
			wTmp = 336;
			capScaleY = 325;
		}
		this.xScale = ((wTmp  + borderWidth) / this.wCur) * 100;
		this.yScale = ((imgHeight  + borderHeight) / this.hCur) * 100;
		// calculate size difference between new and old image, and resize if necessary
		wDiff = (this.wCur - borderWidth) - wTmp;
		hDiff = (this.hCur - borderHeight) - imgHeight;
		
		if(!( hDiff == 0)){
			new Effect.Scale('outerImageContainer', this.yScale, {scaleX: false, duration: resizeDuration, queue: 'front'}); 
		}
		if(!( wDiff == 0)){
			new Effect.Scale('outerImageContainer', this.xScale, {scaleY: false, delay: resizeDuration, duration: resizeDuration}); 
		}

		// if new and old image are same size and no scaling transition is necessary, 
		// do a quick pause to prevent image flicker.
		if((hDiff == 0) && (wDiff == 0)){
			if(is.ie){ pause(250); } else { pause(100);} 
		}

		Element.setLeft('bottomNav', ((wTmp + borderWidth) - bottomNavWidth) / 2);
		Element.setLeft('topNav', ((wTmp + borderWidth) - 25));
		Element.setWidth('imageDataContainer', wTmp + borderWidth);
		if(imgWidth>336){
			if(is.ie5 || is.ie6){
				$('outerImageContainer').style.background = "url(/images/lightbox/lightbox_top_wide.gif)";
				$('imageDataContainer').style.background = "url(/images/lightbox/lightbox_bottom_wide.gif)";
			} else {
				$('outerImageContainer').style.background = "url(/images/lightbox/lightbox_top_wide.png)";
				$('imageDataContainer').style.background = "url(/images/lightbox/lightbox_bottom_wide.png)";
			}
		}else{
			if(is.ie5 || is.ie6){
				$('outerImageContainer').style.background = "url(/images/lightbox/lightbox_top_narrow.gif)";
				$('imageDataContainer').style.background = "url(/images/lightbox/lightbox_bottom_narrow.gif)";
			} else {
				$('outerImageContainer').style.background = "url(/images/lightbox/lightbox_top_narrow.png)";
				$('imageDataContainer').style.background = "url(/images/lightbox/lightbox_bottom_narrow.png)";
			}
		}
		$('outerImageContainer').style.backgroundRepeat = "no-repeat";
		$('outerImageContainer').style.backgroundPosition = "top left";
		$('imageDataContainer').style.backgroundRepeat = "no-repeat";
		$('imageDataContainer').style.backgroundPosition = "bottom left";
		this.showImage();
	},
	
	//
	//	showImage()
	//	Display image and begin preloading neighbors.
	//
	showImage: function(){
		Element.hide('loading');
		moreInfoFlag = false;
		new Effect.Appear('lightboxImage',{duration: 0.5, queue: 'end', afterFinish: function(){myLightbox.updateDetails();}});
		this.preloadNeighborImages();
	},

	//
	//	updateDetails()
	//	Display caption, image number, and bottom nav.
	//
	updateDetails: function() {
		Element.show('caption');
		getInfo(I_ID);
		new Effect.Parallel(
			[ new Effect.SlideDown( 'imageDataContainer', { sync: true, duration: resizeDuration + 0.25, from: 0.0, to: 1.0 }), 
			  new Effect.Appear('imageDataContainer', { sync: true, duration: 1.0 }) ], 
			{ duration: 0.65, afterFinish: function() { myLightbox.updateNav();} } 
		);
		
	},

	//
	//	updateNav()
	//	Display appropriate previous and next hover navigation.
	//
	updateNav: function() {

		Element.show('bottomNav');
		Element.show('topNav');
		Element.show('helpLink');
		
		$('helpLink').onclick = function() {
			document.location.href = "/help.php";
			return true;
		}
		
		
		var scaleY =  300;
		Element.show('infoLink');
		$('infoLink').onclick = function() {
			if(moreInfoFlag===false){
				new Effect.Scale('imageDataContainer', capScaleY, {
					scaleX: false, scaleContent: false, delay: resizeDuration, duration: resizeDuration, afterFinish: function() {
						Element.show('moreInfo');
					}
				});
				moreInfoFlag = true;
			}
			return false;
		}
		
/* :::::::::::::::::::::::::::::::::::: */
		Element.show('bookmarkLink');
		$('bookmarkLink').onclick = function() {
			var attr = "toolbar=0,menubar=0,scrollbars=1,resizable=1,dependent=1,status=1,width=525,height=525,left=10,top=10";
			var newwindow = window.open('',name,attr);
			var html = "";
			html += '<div align="center" style="width:500px;font-family:Georgia;font-size:12px;">';
			html += '<img src="'+imageArray[activeImage][0]+'" />';
			html += '<p>On a PC, right-click this image to bookmark. On a MAC, command(apple)-click this image to bookmark:<br />';
			
			html += '<a href="'+imageArray[activeImage][0]+'" onclick="alert(\'On a PC, right-click this image to bookmark. On a MAC, command(apple)-click this image to bookmark\');return false;">'+infoArray['iptc_title']+'</a></p>';
			html += '<p>'+infoArray['iptc_caption']+'</p>';
			html += '<div align="center"><p><a href="javascript:self.close();">Close</a></p></div>';
			html += '</div>';
		//	html += ;
		//	html += ;
		//	html += ;
		//	html += ;
			var tmp = newwindow.document;
			tmp.write(html);
			tmp.close();
			return false;
		//	popupWin(imageArray[activeImage][0],'popup',525,360);
			//return false;
		}
/* :::::::::::::::::::::::::::::::::::: */
		Element.show('shopLink');
		$('shopLink').onclick = function() {
			var url = "http://www.photoshelter.com/c/search/cart/cart-img-rights-form?PRF_TYPE=rm&I_ID="+I_ID+"&type=rm"
			//var url = infoArray['buy_rights_managed'];
			var attr = "toolbar=0,menubar=0,scrollbars=1,resizable=1,dependent=1,status=1,width=725,height=600,left=10,top=10";
			var newwindow = window.open(url,name,attr);
			if(!newwindow.focus) newwindow.focus();
			return false;
		}
		
		
		Element.show('signinLink');
		$('signinLink').onclick = function() {
			document.location.href = 'http://archive.anarchyimages.com/c/search/login';
			return true;
		}
		
		
		if(activeImage != 0){
			Element.show('prevLink');
			$('prevLink').onclick = function() {
				myLightbox.changeImage(activeImage - 1);
				Element.setHeight('imageDataContainer',50);
				return false;
			}
			
		} else {
			Element.show('prevLinkDis');
		}
		
		// if not last image in set, display next image button
		if(activeImage != (imageArray.length - 1)){
			Element.show('nextLink');
			$('nextLink').onclick = function() {
				myLightbox.changeImage(activeImage + 1);
				Element.setHeight('imageDataContainer',50);
				return false;
			}
			
		} else {
			Element.show('nextLinkDis');
		}
		
		this.enableKeyboardNav();
	},

	//
	//	enableKeyboardNav()
	//
	enableKeyboardNav: function() {
		document.onkeydown = this.keyboardAction; 
	},

	//
	//	disableKeyboardNav()
	//
	disableKeyboardNav: function() {
		document.onkeydown = '';
	},

	//
	//	keyboardAction()
	//
	keyboardAction: function(e) {
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}

		key = String.fromCharCode(keycode).toLowerCase();
		
		if((key == 'x') || (key == 'o') || (key == 'c')){	// close lightbox
			myLightbox.end();
		} else if(key == 'p'){	// display previous image
			if(activeImage != 0){
				myLightbox.disableKeyboardNav();
				myLightbox.changeImage(activeImage - 1);
			}
		} else if(key == 'n'){	// display next image
			if(activeImage != (imageArray.length - 1)){
				myLightbox.disableKeyboardNav();
				myLightbox.changeImage(activeImage + 1);
			}
		}

	},

	//
	//	preloadNeighborImages()
	//	Preload previous and next images.
	//
	preloadNeighborImages: function(){

		if((imageArray.length - 1) > activeImage){
			preloadNextImage = new Image();
			preloadNextImage.src = imageArray[activeImage + 1][0];
		}
		if(activeImage > 0){
			preloadPrevImage = new Image();
			preloadPrevImage.src = imageArray[activeImage - 1][0];
		}
	
	},

	//
	//	end()
	//
	end: function() {
		this.disableKeyboardNav();
		Element.hide('lightbox');
		new Effect.Fade('overlay', { duration: 0.2});
		showSelectBoxes();
	}
}

// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// -----------------------------------------------------------------------------------

//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the lightbox.
//
function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();
	
	if(key == 'x'){
	}
}

// -----------------------------------------------------------------------------------

//
// listenKey()
//
function listenKey () {	document.onkeypress = getKey; }
	
// ---------------------------------------------------

function showSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

// ---------------------------------------------------

//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

// ---------------------------------------------------
// AJAX CALL ADDED BY JEREMIAH FOR ANARCHY IMAGES 
// getInfo() - actual call to page that returns XML
// showResponse() - this picks out the desired data, formats it and inserts it into the caption element
function getInfo() {
//	example of URL that is called: http://dev.anarchyimages.com/process/getimg2.php?I_ID=I000097aYuw_HX_s&mode=search
	with (location) var url = '../process/getimg2.php?I_ID='+ I_ID + '&mode=search';
	loadXMLDoc(url);
}

function showError(t) {
    alert('Error ' + t.status + ' -- ' + t.statusText);
}

function showResponse(){	
	//var items = req.responseXML.getElementsByTagName("Item");
	var imgInfo = "";
	var imgMoreInfo = ""
	var lb = "<br />\n";
	var resp = req.responseXML;
	var rootNode = resp.documentElement;
	var itemNodes = rootNode.getElementsByTagName('Item');
	for(i=0;i<itemNodes.length;i++){
		if(itemNodes[i].childNodes.length > 1){
			infoArray[itemNodes[i].getAttribute("id")] = itemNodes[i].childNodes[1].nodeValue;
		} else {
			infoArray[itemNodes[i].getAttribute("id")] = itemNodes[i].firstChild.nodeValue;
		}
	}
	imgInfo += "<span class='imgTitle'>"+infoArray['iptc_title'] + "</span>" + lb;
	imgInfo += "Photographer: " + infoArray['iptc_copyright'] + lb;

	imgMoreInfo += "<span class='imgFileName'>" + infoArray['image_filename'] + "</span>" + lb;
	if(infoArray['iptc_caption'].length > 237){
		imgMoreInfo += infoArray['iptc_caption'].substring(0,237) + "...";
	} else {
		imgMoreInfo += infoArray['iptc_caption'];
	}
	captionlength = infoArray['iptc_caption'].length;
	Element.setInnerHTML('caption', imgInfo);
	Element.setInnerHTML('moreInfo', imgMoreInfo);
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}


function loadXMLDoc(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}
// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            showResponse();
         } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
         }
    }
}

function popupWin(url,name,w,h) { 
	attr = "toolbar=0,menubar=0,scrollbars=1,resizable=1,dependent=1,status=1,width=" + (w+15) + ",height=" + (h+40) + ",left=10,top=10";
	var newwindow = window.open(url,name,attr);
	if (!newwindow.opener) newwindow.opener = self;
	if (!newwindow.focus) newwindow.focus();
}
// ---------------------------------------------------

function initLightbox() { myLightbox = new Lightbox(); }
Event.observe(window, 'load', initLightbox, false);