var fileLoadingImage = "/images/system/loading.gif";
var fileBottomNavCloseImage = "/images/system/closelabel.gif";
var animate = true;
var resizeSpeed = 7;
var borderSize = 10;
var slideshowTimeout = 4000; //
var scrollInc = 50; // Incriment in px for the scroll box
var overlayOpacity = 0.8;
var loadedFlag = false;
var isTransitioning = false;

//Adds and onMouseOver Event to Thumbnail Images. Must be set to True before Lightbox is created.
var thumbMouseOver = false;

//Added by Nick for New Image Server Settings
var thumbImageSettings = {
	width: null,
	height: null,
	maxwidth: null,
	maxheight: null,
	matte: null,
	smart: null,
	crop: null
};
var baseImageSettings = {
	width: null,
	height: null,
	maxwidth: null,
	maxheight: null,
	smart: null,
	matte: '!fff',
	crop: null
};

function parseImgURL(url, replaceSettings){
	// this is the old rubloff code that resizes the url, comment out for time being - nk
	/* 
	if(replaceSettings.width != null){
		if (url.match(/width\/\d+/) != null) {
			url = url.replace(/width\/\d+/, 'width/' + replaceSettings.width);
		} else {url += '/width/' + replaceSettings.width;}
	} else {url = url.replace(/\/width\/\d+\//, '');}
	if(replaceSettings.height != null){
		if (url.match(/height\/\d+/) != null) {
			url = url.replace(/height\/\d+/, 'height/'+ replaceSettings.height);
		} else {url += '/height/' + replaceSettings.height;}
	} else {url = url.replace(/\/height\/\d+\//, '');}
	if(replaceSettings.maxwidth != null){
		if (url.match(/maxwidth\/\d+/) != null) {
			url = url.replace(/maxwidth\/\d+/, 'maxwidth/'+ replaceSettings.maxwidth);
		} else {url += '/maxwidth/' + replaceSettings.maxwidth;}
	} else {url = url.replace(/\/maxwidth\/\d+\//, '');}
	if(replaceSettings.maxheight != null){
		if (url.match(/maxheight\/\d+/) != null) {
			url = url.replace(/maxheight\/\d+/, 'maxheight/'+ replaceSettings.maxheight);
		} else {url += '/maxheight/' + replaceSettings.maxheight;}
	} else {url = url.replace(/\/maxheight\/\d+\//, '');}
	if(replaceSettings.smart != null){
		if (url.match(/\/smart\/[a-zA-z0-9]+\//, '') != null) {
			url = url.replace(/\/smart\/[a-zA-z0-9]+\//, '/smart/'+ replaceSettings.smart);
		} else {url += '/smart/' + replaceSettings.smart;}
	} else {url = url.replace(/\/smart\/[a-zA-z0-9]+\//, '');}
	if(replaceSettings.crop != null){
		if (url.match(/crop\/[0-9]+,[0-9]+/) != null) {
			url = url.replace(/crop\/[0-9]+,[0-9]+/, 'crop/'+ replaceSettings.crop);
		} else {
			url += '/crop/' + replaceSettings.crop;}
	} else {url = url.replace(/\/crop\/[0-9]+,[0-9]+/, '');}
	if(replaceSettings.matte != null){
		//This only supports Hex Based Mattes. RGB support late if at all.
		if (url.match(/matte\/\![a-fA-F0-9]+/) != null) {
			url = url.replace(/matte\/\![a-fA-F0-9]+/, 'matte/'+ replaceSettings.matte);
		} else {url += '/matte/' + replaceSettings.matte;}
	} else {url = url.replace(/matte\/\![a-fA-F0-9]+/, '');}
	//url = url.replace(/crop\/[0-9]+,[0-9]+/, '');
	*/
	return url;
}

/*-------------------- DONT EDIT BELOW THIS LINE ----------------------*/

var scrollDir = 0; // 0 = horizontal, 1 = vertical
var mainImage; // Set with the setMainImage method
var sceneName; // Set with the setSceneName method
var startButton; // Set with the setStartBtn method
var pauseButton; // Set with the setPauseBtn method
var scrollPanel; // Set with the setScrollPanel method
var scrollUpButton; // Set with the setScrollUpButton method
var scrollDownButton; // Set with the setScrollUpButton method
var imageArray = new Array; // USED INTERNALLY
var slideshow = null; // USED INTERNALLY
var activeImage = 0; // USED INTERNALLY
var moveEffect; // USED INTERNALLY
var scrollBelt; // USED INTERNALLY
var hasExpand = 0;// USED INTERNALLY
var thumbsCanOpenBox = false;// USED INTERNALLY
var isPlaying = null;// USED INTERNALLY
var scrollDownButtonOff = null; 
var scrollDownButtonOn = null;
var scrollUpButtonOff = null;
var scrollUpButtonOn = null;
var navButtonColorOn = null;
var navButtonColorOff = null;
var nextButtonId = null;
var previousButtonId = null;
var doContinuousLoop = true;


if(animate == true){
	overlayDuration = 0.2;
	if(resizeSpeed > 10){ resizeSpeed = 10;}
	if(resizeSpeed < 1){ resizeSpeed = 1;}
	resizeDuration = (11 - resizeSpeed) * 0.15;
} else {
	overlayDuration = 0;
	resizeDuration = 0;
}

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";
	},
	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;
	}
});

Array.prototype.removeDuplicates = function () {
	for(i = 0; i < this.length; i++){
		for(j = this.length-1; j>i; j--){
			if(this[i][0] == this[j][0]){
				this.splice(j,1);
			}
		}
	}
}

Array.prototype.empty = function () {
	for(i = 0; i <= this.length; i++){
		this.shift();
	}
}

var Lightbox = Class.create();

Lightbox.prototype = {
	initialize: function() {
		var objBody = document.getElementsByTagName("body").item(0);

		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.setAttribute('style','display:none;');
		objOverlay.style.display = 'none';
		objOverlay.onclick = function() { lightBox.end(); }
		objBody.appendChild(objOverlay);

		var objLightbox = document.createElement("div");
		objLightbox.setAttribute('id','lightbox');
		objLightbox.setAttribute('style','display:none;');
		objLightbox.style.display = 'none';
		objLightbox.onclick = function(e) {
			if (!e) var e = window.event;
			var clickObj = Event.element(e).id;
			if ( clickObj == 'lightbox') {
				lightBox.end();
			}
		};
		objBody.appendChild(objLightbox);

		// TITLE BAR
		var objTitleContainer = document.createElement("div");
		objTitleContainer.setAttribute('id','lightBoxTitle');
		objTitleContainer.setAttribute('style','position:relative;color:#D1D1D1;height: 30px;background-color:#666;margin:0 auto;border-top:3px solid #666;boder-left:3px solid #666;border-right:3px solid #666;');
		objTitleContainer.style.color = '#D1D1D1';
		objTitleContainer.style.backgroundColor = '#666';
		objTitleContainer.style.height = '30px';
		objTitleContainer.style.margin = '0 auto';
		objTitleContainer.style.padding = '2px';
		objTitleContainer.style.whiteSpace = 'nowrap';
		objTitleContainer.style.borderTop = '3px solid #666';
		objTitleContainer.style.borderLeft = '1px solid #666';
		objTitleContainer.style.borderRight = '1px solid #666';
		objTitleContainer.style.position = 'relative';
		objLightbox.appendChild(objTitleContainer);

		// TITLE BAR - CLOSE BUTTON - IMAGE
		var objCloseButtonImage = document.createElement('div');
		objCloseButtonImage.setAttribute('style', 'position:absolute;top:5px;right:5px;cursor: pointer;background-image: url(/images/system/lightboxInterface/closeX.gif);width:15px;height:15px;');
		objCloseButtonImage.style.cursor = 'pointer';
		objCloseButtonImage.style.backgroundImage = 'url(/images/system/lightboxInterface/closeX.gif)';
		objCloseButtonImage.style.width = '15px';
		objCloseButtonImage.style.height = '15px';
		objCloseButtonImage.style.position = 'absolute';
		objCloseButtonImage.style.top = '5px';
		objCloseButtonImage.style.right = '5px';
		objCloseButtonImage.onclick = function(e) {
			lightBox.end();
		}
		objTitleContainer.appendChild(objCloseButtonImage);

		// UPPER BUTTON BAR
		var objButtonBar = document.createElement("div");
		objButtonBar.setAttribute('id', 'lightboxButtonBar');
		objButtonBar.setAttribute('style','border-left:3px solid #666;border-right:3px solid #666;margin:0 auto;background-color:#FFF;height:40px;');
		objButtonBar.style.borderLeft = '3px solid #666';
		objButtonBar.style.borderRight = '3px solid #666';
		objButtonBar.style.margin = '0 auto';
		objButtonBar.style.backgroundColor = '#FFF';
		objButtonBar.style.height = '40px';
		objButtonBar.style.position = 'relative';
		objLightbox.appendChild(objButtonBar);

		var objPrevBtn = document.createElement("div");
		objPrevBtn.setAttribute('id','prevTxt');
		objPrevBtn.setAttribute('style','position: absolute;top:15px;left:10px;cursor:pointer;');
		objPrevBtn.style.position = 'absolute';
		objPrevBtn.style.top = '15px';
		objPrevBtn.style.left = '10px';
		objPrevBtn.style.cursor = 'pointer';
		objPrevBtn.onclick = function(e) {
			lightBox.prevImage();
		}
		objButtonBar.appendChild(objPrevBtn);
		objPrevBtn.innerHTML = '<img src="/images/system/lightboxInterface/previous.gif"/>';

		var objNextBtn = document.createElement("div");
		objNextBtn.setAttribute('id','nextTxt');
		objNextBtn.setAttribute('style','position:absolute;top: 15px;right:10px;cursor:pointer;');
		objNextBtn.style.position = 'absolute';
		objNextBtn.style.top = '15px';
		objNextBtn.style.right = '10px';
		objNextBtn.style.cursor = 'pointer';
		objNextBtn.onclick = function(e) {
			lightBox.nextImage();
		}
		objButtonBar.appendChild(objNextBtn);
		objNextBtn.innerHTML = '<img src="/images/system/lightboxInterface/next.gif"/>';

		var objPauseBtn = document.createElement("div");
		objPauseBtn.setAttribute('style','position: relative;top:5px;cursor:pointer;');
		objPauseBtn.style.position = 'relative';
		objPauseBtn.style.top = '10px';
		objPauseBtn.style.width = '66px';
		objPauseBtn.style.height = '27px';
		objPauseBtn.style.margin = '0 auto';
		objPauseBtn.style.cursor = 'pointer';
		objPauseBtn.onclick = function(e) {
			lightBox.togglePlay();
		}
		objButtonBar.appendChild(objPauseBtn);
		objPauseBtn.innerHTML = '<img id="lightboxPlayPause" src="/images/system/lightboxInterface/play.gif"/>';

		// UPPER BUTTON BAR HR LINE
		var objButtonBarHRLine = document.createElement("div");
		objButtonBarHRLine.setAttribute('id', 'lightboxButtonBarHRLine');
		objButtonBarHRLine.setAttribute('style','border-left:3px solid #666;border-right:3px solid #666;margin:0 auto;background-color:#D1D1D1;height:1px;');
		objButtonBarHRLine.style.borderLeft = '3px solid #666';
		objButtonBarHRLine.style.borderRight = '3px solid #666';
		objButtonBarHRLine.style.margin = '0 auto';
		objButtonBarHRLine.style.backgroundColor = '#D1D1D1';
		objButtonBarHRLine.style.height = '1px';
		objLightbox.appendChild(objButtonBarHRLine);

		// MAIN IMAGE OUTER CONTAINER
		var objOuterImageContainer = document.createElement("div");
		objOuterImageContainer.setAttribute('id','outerImageContainer');
		objOuterImageContainer.setAttribute('style','border-left:3px solid #666; border-right:3px solid #666;');
		objOuterImageContainer.style.borderLeft = '3px solid #666';
		objOuterImageContainer.style.borderRight = '3px solid #666';
		objOuterImageContainer.style.borderBottom = '3px solid #666';
		objLightbox.appendChild(objOuterImageContainer);

		if(animate){
			Element.setWidth('outerImageContainer', 250);
			Element.setHeight('outerImageContainer', 250);
			Element.setWidth('lightBoxTitle', 250);
			Element.setWidth('lightboxButtonBar', 250);
			Element.setWidth('lightboxButtonBarHRLine', 250);
		} else {
			Element.setWidth('outerImageContainer', 1);
			Element.setHeight('outerImageContainer', 1);
			Element.setWidth('lightBoxTitle', 1);
			Element.setWidth('lightboxButtonBar', 1);
			Element.setWidth('lightboxButtonBarHRLine', 1);
		}
		var objImageContainer = document.createElement("div");
		objImageContainer.setAttribute('id','imageContainer');
		objOuterImageContainer.appendChild(objImageContainer);

		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() { lightBox.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';
		objImageDataContainer.style.display = 'none';
		objImageDataContainer.style.borderBottom = '3px solid #666';
		objImageDataContainer.style.borderLeft = '3px solid #666';
		objImageDataContainer.style.borderRight = '3px solid #666';
		objLightbox.appendChild(objImageDataContainer);

		var objImageData = document.createElement("div");
		objImageData.setAttribute('id','imageData');
		objImageDataContainer.appendChild(objImageData);

		var objImageDetails = document.createElement("div");
		objImageDetails.setAttribute('id','imageDetails');
		objImageDetails.style.width = '100%';
		objImageDetails.style.textAlign = 'center';
		objImageData.appendChild(objImageDetails);

		var objCaption = document.createElement("span");
		objCaption.setAttribute('id','caption');
		objImageDetails.appendChild(objCaption);

		var objNumberDisplay = document.createElement("div");
		objNumberDisplay.setAttribute('id','numberDisplay');
		objNumberDisplay.style.padding = '12px';
		objNumberDisplay.style.float = 'left';
		objNumberDisplay.style.fontWeight = 'bold';
		objNumberDisplay.style.fontSize = '22px';
		objNumberDisplay.style.textAlign = 'left';
		objTitleContainer.appendChild(objNumberDisplay);
		
		if (!document.getElementsByTagName){ return; }
		var images = document.getElementsByTagName('img');

		for (var i=0; i<images.length; i++){
			var image = images[i];

			var relAttribute = String(image.getAttribute('rel'));

			if (relAttribute.toLowerCase().match('lightbox')){
				imageArray.push(new Array(image.getAttribute('src'), getImageTitle(image)));
				image.onclick = function(){
					lightBox.changeImageByThumb(this.getAttribute('imgnum'));
					var preloadingImage = new Image();
					preloadingImage.src = parseImgURL(image.getAttribute('src'), thumbImageSettings);
				}
				if(thumbMouseOver){
					image.onmouseover = function(){
						lightBox.changeImageByThumb(this.getAttribute('imgnum'));
						var preloadingImage = new Image();
						preloadingImage.src = parseImgURL(image.getAttribute('src'), thumbImageSettings);
					}
				}
			}
			imageArray.removeDuplicates();
		}
		
	},

	scrollUpEvent: function(event){
		var max = 0 - getMaxScrollPos((imageArray.length-1), scrollInc);		
		
		
		if(scrollDir == 0){
		
			var pos = parseFloat(scrollPanel.style.top) - scrollInc;
			if(isNaN(pos)){pos = scrollInc;}
			if(pos >= max){
				new Effect.Move(scrollPanel, { y: pos, mode: 'absolute' });
			} else {
				new Effect.Move(scrollPanel, { y: 0, mode: 'absolute' });
			}
		} else {
			var pos = parseFloat(scrollPanel.style.left) - scrollInc;
			if(isNaN(pos)){
				pos = scrollInc;
			}
			if(pos >= max){
				new Effect.Move(scrollPanel, { x: pos, mode: 'absolute' });
				if(scrollDownButtonOn != null){scrollDownButton.src=scrollDownButtonOn;}
				if(scrollUpButtonOn != null){scrollUpButton.src=scrollUpButtonOn;}
			}else{
				new Effect.Move(scrollPanel, { x: max, mode: 'absolute' });
				if(scrollUpButtonOn != null){scrollUpButton.src=scrollUpButtonOff;}
			}
		}
	},

	scrollDownEvent: function(event){
		var max = getMaxScrollPos(imageArray.length, scrollInc);
		if(scrollDir == 0){			
			var pos = parseFloat(scrollPanel.style.top) + scrollInc;
			if(isNaN(pos)){pos = scrollInc;}
			if(pos <= (scrollPanel.scrollHeight - scrollPanel.clientHeight) && pos <= max){
				new Effect.Move(scrollPanel, { y: pos, mode: 'absolute' });
			}
		} else {
			var pos = parseFloat(scrollPanel.style.left) + scrollInc;
			if(isNaN(pos)){pos = scrollInc;}
			if(pos <= 0){
				new Effect.Move(scrollPanel, { x: pos, mode: 'absolute' });
				if(scrollUpButtonOn != null){scrollUpButton.src=scrollUpButtonOn;}
				if(scrollDownButtonOn != null){scrollDownButton.src=scrollDownButtonOn;}
			} else {
				if(scrollDownButtonOff != null){scrollDownButton.src=scrollDownButtonOff;}
				new Effect.Move(scrollPanel, { x: 0, mode: 'absolute' });
			}
		}
	},

	setDelay: function(delayVal) {
		slideshowTimeout = Number(delayVal);
	},

	setscrollInc: function(incVal) {
		scrollInc = Number(incVal);
	},
	
	setDoLoop: function(value) {
		doContinuousLoop = value;
	},

	setScrollBelt: function(elId) {
		if($(elId) != null){
			scrollBelt = $(elId);
		} else {
			throw('Element for scroll belt is invalid.');
		}
	},

	setMainImage: function(imgId) {
		if($(imgId) != null){
			mainImage = $(imgId);
			mainImage.style.cursor = 'pointer';
			mainImage.onclick = function() {
				lightBox.start();
			}
		} else {
			throw('Element for main image is invalid.');
		}
	},

	setSceneName: function(sceneId) {
		if($(sceneId) != null){
			sceneName = $(sceneId);
		} else {
			throw('Element for scene name is invalid.');
		}
	},

	setScrollDir: function(setting){
		if(setting == 0 || setting == 1){
			scrollDir = setting;
		} else {
			throw('Scroll direction either 0 for horizontal or 1 for vertical.');
		}
	},

	setBeltParentWidth: function(){
		if(scrollDir == 1 || scrollDir == 0 && scrollInc > 0){
			if(scrollDir == 0){
				scrollPanel.style.width = String(parseFloat(imageArray.length) * parseFloat(scrollInc)) + 'px';
			} else {
				scrollPanel.style.width = String(parseFloat(imageArray.length) * parseFloat(scrollInc)) + 'px';
			}
			return true;
		} else {
			throw('You must set scrollDir before calling setBeltParentWidth.');
			return false;
		}
	},

	setNextButton: function(btnId) {
		nextButtonId = $(btnId);
		if(nextButtonId != null){
			nextButtonId.onclick = function() {
				if(activeImage != (imageArray.length-1)){
					if(navButtonColorOn != null){
						previousButtonId.style.color = navButtonColorOn;
						nextButtonId.style.color = navButtonColorOn;
					}
					if(activeImage == (imageArray.length-2) && navButtonColorOff != null){nextButtonId.style.color = navButtonColorOff;}
					lightBox.changeImageByThumb(activeImage + 1);
				} else {
					if(doContinuousLoop){
						lightBox.changeImageByThumb(0);
					}else{
						if(navButtonColorOff != null){
							nextButtonId.style.color = navButtonColorOff;
						}
					}
				}
			}
		} else {
			throw('Element for next button is invalid.');
		}
		nextButtonId.style.cursor = 'pointer';
	},

	setPrevButton: function(btnId) {
		previousButtonId = $(btnId);
		if(previousButtonId != null){
			previousButtonId.onclick = function() {
				if(activeImage != 0){
					if(navButtonColorOn != null){
						previousButtonId.style.color = navButtonColorOn;
						nextButtonId.style.color = navButtonColorOn;
					}
					if(activeImage == (1) && navButtonColorOff != null){previousButtonId.style.color = navButtonColorOff;}
					lightBox.changeImageByThumb(activeImage - 1);
				} else {
					if(doContinuousLoop){
						lightBox.changeImageByThumb(imageArray.length);
					}else{
						if(navButtonColorOff != null){
							previousButtonId.style.color = navButtonColorOff;
						}
					}
				}
			}
		} else {
			throw('Element for next button is invalid.');
		}
		previousButtonId.style.cursor = 'pointer';
	},

	setScrollPanel: function(panelId) {
		scrollPanel = $(panelId);
	},

	setScrollUpButton: function(btnId) {
		scrollUpButton = $(btnId);
		scrollUpButton.style.cursor = 'pointer';
		Event.observe(scrollUpButton, 'click', this.scrollUpEvent.bindAsEventListener(this));
	},

	setScrollDownButton: function(btnId) {
		scrollDownButton = $(btnId);
		scrollDownButton.style.cursor = 'pointer';
		Event.observe(scrollDownButton, 'click', this.scrollDownEvent.bindAsEventListener(this));
	},
	
	setScrollUpSrc: function(btnoff,btnon) {
		scrollUpButtonOff = btnoff;
		scrollUpButtonOn = btnon;		
	},
	setScrollDownSrc: function(btnoff,btnon) {
		scrollDownButtonOff = btnoff;
		scrollDownButtonOn = btnon;		
	},
	
	setNavButtonColors: function(colorOff,colorOn){
		navButtonColorOff = colorOff;
		navButtonColorOn = colorOn;		
	},
	
	setStartBtn: function(btnId) {
		startButton = $(btnId);
		startButton.onclick = function() {
			slideshow = setTimeout("lightBox.nextImage();lightBox.setTimeoutAgain();isPlaying = 1;", slideshowTimeout);
		}
		startButton.style.cursor = 'pointer';
	},

	setTimeoutAgain: function() {
		slideshow = setTimeout("lightBox.automatedNextImage();lightBox.setTimeoutAgain();", slideshowTimeout);
		isPlaying = 1;
		isTransitioning = false;
		document.getElementById('lightboxPlayPause').src = "/images/system/lightboxInterface/pause.gif";
	},

	setPauseBtn: function(btnId) {
		pauseButton = $(btnId);
		pauseButton.onclick = function() {
			clearTimeout(slideshow);
			slideshow = null;
			isPlaying = null;
			document.getElementById('lightboxPlayPause').src = "/images/system/lightboxInterface/play.gif";
		}
		pauseButton.style.cursor = 'pointer';
	},
	
	automatedNextImage: function() {
		if(activeImage != (imageArray.length-1)){
			lightBox.changeImage(activeImage + 1);
		} else {
			lightBox.changeImage(0);
		}
	},

	togglePlay: function() {
		if(isPlaying == null){			
			lightBox.setTimeoutAgain();
			isPlaying = 1;
			document.getElementById('lightboxPlayPause').src = "/images/system/lightboxInterface/pause.gif";
		} else {
			clearTimeout(slideshow);
			slideshow = null;
			isPlaying = null;
			document.getElementById('lightboxPlayPause').src = "/images/system/lightboxInterface/play.gif";
		}
	},

	setHasExpandJS: function() {
		hasExpand = 1;
	},

// PRIVATE METHODS, NOT TO BE CALLED EXTERNALLY UNLESS YOU KNOW WHAT YOU'RE DOING

	nextImage: function(){
		if(isPlaying == 1){
			clearTimeout(slideshow);
			slideshow = null;
			isPlaying = null;
			document.getElementById('lightboxPlayPause').src = "/images/system/lightboxInterface/play.gif";
		}
		if(activeImage != (imageArray.length-1)){
			lightBox.changeImage(activeImage + 1);
		} else {
			lightBox.changeImage(0);
		}
	},

	prevImage: function(){
		if(isPlaying == 1){
			clearTimeout(slideshow);
			slideshow = null;
			isPlaying = null;
			document.getElementById('lightboxPlayPause').src = "/images/system/lightboxInterface/play.gif";
		}
		if(activeImage != 0){
			lightBox.changeImage(activeImage - 1);
		} else {
			lightBox.changeImage(imageArray.length - 1);
		}
	},

	start: function() {
		if(loadedFlag != true){
			for (var i=0; i<imageArray.length; i++){	
				var objLightboxImage = document.createElement("img");
				objLightboxImage.setAttribute('id','lightboxImage' + i);
				objLightboxImage.setAttribute('src',parseImgURL(imageArray[i][0], baseImageSettings));
				objLightboxImage.setAttribute('style','position:absolute;top:10px;left:10px;');
				objLightboxImage.style.position = 'absolute';
				objLightboxImage.style.top = '10px';
				objLightboxImage.style.left = '10px';
				objLightboxImage.style.display = 'none';
				$('imageContainer').appendChild(objLightboxImage);
			}
			loadedFlag = true;
		}
		hideSelectBoxes();
		hideFlash();
		var arrayPageSize = getPageSize();
		Element.setHeight('overlay', arrayPageSize[1]);
		new Effect.Appear('overlay', { duration: overlayDuration, from: 0.0, to: overlayOpacity });
		var arrayPageScroll = getPageScroll();
		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
		Element.setTop('lightbox', lightboxTop);
		Element.show('lightbox');
		this.changeImage(activeImage);
	},

	changeImageByThumb: function(imageNum) {
		activeImage = Number(imageNum);
		imgPreloader = new Image();
		if(activeImage == imageArray.length){
			activeImage -= 1;			
		}
		if(activeImage == (imageArray.length - 1) && navButtonColorOn != null && navButtonColorOff != null){
			previousButtonId.style.color = navButtonColorOn;
			nextButtonId.style.color = navButtonColorOff;
		}else if(activeImage == 0 && navButtonColorOn != null && navButtonColorOff != null){
			previousButtonId.style.color = navButtonColorOff;
			nextButtonId.style.color = navButtonColorOn;
		}else if(navButtonColorOn != null){
			previousButtonId.style.color = navButtonColorOn;
			nextButtonId.style.color = navButtonColorOn;
		}
		mainImage.src = parseImgURL(imageArray[activeImage][0], thumbImageSettings);
		if(sceneName != null){sceneName.innerHTML = imageArray[activeImage][1];}
		imgPreloader.onload=function(){
			mainImage.src = parseImgURL(imageArray[activeImage][0], thumbImageSettings);
		}
		imgPreloader.src = parseImgURL(imageArray[activeImage][0], thumbImageSettings);
		if(thumbsCanOpenBox == true) {
			lightBox.start();
		};
	},

	changeImage: function(imageNum) {
		if(isTransitioning != true){
			isTransitioning = true;
			activeImage = Number(imageNum);
			imgPreloader = new Image();
			imgPreloader.onload=function(){
				mainImage.src = parseImgURL(imageArray[activeImage][0], thumbImageSettings);
				lightBox.resizeImageContainer(imgPreloader.width, imgPreloader.height);
			}
			imgPreloader.src = parseImgURL(imageArray[activeImage][0], baseImageSettings);
			if($('numberDisplay') != null){$('numberDisplay').innerHTML = imageArray[activeImage][1];}	
		}		
	},

	resizeImageContainer: function( imgWidth, imgHeight) {

		this.widthCurrent = Element.getWidth('outerImageContainer');
		this.heightCurrent = Element.getHeight('outerImageContainer');

		var widthNew = (imgWidth  + (borderSize * 2));
		var heightNew = (imgHeight  + (borderSize * 2));;

		/*if(widthNew < 420){
			widthNew = 420;
			heightNew = Math.round((widthNew / (imgWidth  + (borderSize * 2))) * heightNew);
		}*/

		wDiff = this.widthCurrent - widthNew;
		hDiff = this.heightCurrent - heightNew;

		var imageScaleY = Math.round(((heightNew -(borderSize * 2)) / imgHeight) * 100);
		var imageScaleX = Math.round(((widthNew -(borderSize * 2)) / imgWidth) * 100);

		// new Effect.Scale('lightboxImage' + activeImage, imageScaleX, {scaleY: false, duration: resizeDuration, scaleMode: { originalHeight: imgHeight, originalWidth: imgWidth }});
		// new Effect.Scale('lightboxImage' + activeImage, imageScaleY, {scaleX: false, duration: resizeDuration, scaleMode: { originalHeight: imgHeight, originalWidth: imgWidth }});

		if(Element.getWidth('imageDataContainer') < 1){
			Element.setWidth( 'imageDataContainer', widthNew);
		}

		this.xScale = ( widthNew / this.widthCurrent) * 100;
		this.yScale = ( heightNew / this.heightCurrent) * 100;

		this.buttonBoxNewWidth = widthNew / Element.getWidth('imageDataContainer') * 100;
		this.doBoxResize(wDiff, hDiff, widthNew, heightNew);

		/*if(!( hDiff < 2)){
			new Effect.Scale('outerImageContainer', this.yScale, {scaleX: false, duration: resizeDuration, queue: 'front'});
		}*/

		if((hDiff == 0) && (wDiff == 0)){
			if (navigator.appVersion.indexOf("MSIE")!=-1){ pause(250); } else { pause(100);}
		}

		this.showImage();
	},

	doBoxResize: function(wDiff, hDiff, widthNew, heightNew){
		if(hDiff > 10){
			new Effect.Scale('outerImageContainer', this.yScale, {scaleX: false, duration: resizeDuration, queue: 'front'});
		} else {
			Element.setHeight('outerImageContainer', heightNew);
		}
		if(wDiff > 10){
			new Effect.Scale('outerImageContainer', this.xScale, {scaleY: false, duration: resizeDuration * 2});
			new Effect.Scale('lightboxButtonBar', this.xScale, {scaleY: false, duration: resizeDuration * 2, scaleContent:false});
			new Effect.Scale('lightBoxTitle', this.xScale, {scaleY: false, duration: resizeDuration * 2, scaleContent:false});
			new Effect.Scale('imageDataContainer', this.xScale, {scaleY: false, duration: resizeDuration * 2, scaleContent:false});
			new Effect.Scale('lightboxButtonBarHRLine', this.xScale, {scaleY: false, duration: resizeDuration * 2, scaleContent:false});
		} else {
			Element.setWidth('outerImageContainer', widthNew);
			Element.setWidth('lightboxButtonBar', widthNew);
			Element.setWidth('lightBoxTitle', widthNew);
			Element.setWidth('imageDataContainer', widthNew);
			Element.setWidth('lightboxButtonBarHRLine', widthNew);
		}
	},

	showImage: function(){
		Element.hide('loading');
		new Effect.Appear('lightboxImage' + activeImage, {
			duration: resizeDuration,
			afterFinish: function(){
				lightBox.updateDetails();
			}
		});
		if(imageArray.length > 1){
			if(activeImage > 0) {
				new Effect.Fade('lightboxImage' + (activeImage - 1), { duration: resizeDuration });
			} else {
				new Effect.Fade('lightboxImage' + (imageArray.length - 1), { duration: resizeDuration });
			}
			if(activeImage != (imageArray.length - 1)) {
				new Effect.Fade('lightboxImage' + (activeImage + 1), { duration: resizeDuration });
			} else {
				new Effect.Fade('lightboxImage' + (0), { duration: resizeDuration });
			}
		}
		this.preloadNeighborImages();
	},

	updateDetails: function() {
		// Element.show('caption');
		// Element.setInnerHTML( 'caption', imageArray[activeImage][1]);

		/*if(imageArray.length > 1){
			Element.show('numberDisplay');
			if(hasExpand == 1) {
				if(eval(activeImage + 1) > eval(imageArray.length/2)) {
					Element.setInnerHTML( 'numberDisplay', "Image " + eval(activeImage + 1 - imageArray.length/2) + " of " + eval(imageArray.length/2));
				} else {
					Element.setInnerHTML( 'numberDisplay', "Image " + eval(activeImage + 1) + " of " + eval(imageArray.length/2));
				}
			} else {
				Element.setInnerHTML( 'numberDisplay', "Image " + eval(activeImage + 1) + " of " + imageArray.length);
			}
		}*/
		// lightBox.updateNav();

		new Effect.Parallel(
			[ new Effect.Appear('imageDataContainer', { sync: true, duration: 500 }) ],
			{ duration: resizeDuration, afterFinish: function() {
				var arrayPageSize = getPageSize();
				Element.setHeight('overlay', arrayPageSize[1]);
				isTransitioning = false;
				}
			}
		);
	},

	updateNav: function() {
		if(activeImage != 0){
			// Element.show('prevLink');
			// Element.show('prevTxt');
			document.getElementById('prevLink').onclick = function() {
				lightBox.changeImage(activeImage - 1); return false;
			}
		}

		if(activeImage != (imageArray.length - 1)){
			// Element.show('nextLink');
			// Element.show('nextTxt');
			document.getElementById('nextLink').onclick = function() {
				lightBox.changeImage(activeImage + 1); return false;
			}
		}
		this.enableKeyboardNav();
	},

	enableKeyboardNav: function() {
		document.onkeydown = this.keyboardAction;
	},

	disableKeyboardNav: function() {
		document.onkeydown = '';
	},

	keyboardAction: function(e) {
		if (e == null) { // ie
			keycode = event.keyCode;
			escapeKey = 27;
		} else { // mozilla
			keycode = e.keyCode;
			escapeKey = e.DOM_VK_ESCAPE;
		}

		key = String.fromCharCode(keycode).toLowerCase();

		if((key == 'x') || (key == 'o') || (key == 'c') || (keycode == escapeKey)){	// close lightbox
			lightBox.end();
		} else if((key == 'p') || (keycode == 37)){	// display previous image
			if(activeImage != 0){
				lightBox.disableKeyboardNav();
				lightBox.changeImageByThumb(activeImage - 1);
			}
		} else if((key == 'n') || (keycode == 39)){	// display next image
			if(activeImage != (imageArray.length - 1)){
				lightBox.disableKeyboardNav();
				lightBox.changeImageByThumb(activeImage + 1);
			}
		}
	},

	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: function() {
		this.disableKeyboardNav();
		new Effect.Fade('imageDataContainer', { duration: resizeDuration/2 });
		// new Effect.Fade('lightboxImage', { duration: resizeDuration/2 });
		new Effect.Fade('overlay', { duration: overlayDuration});
		Element.hide('lightbox');
		showSelectBoxes();
		showFlash();
		clearTimeout(slideshow);
		// new Effect.Fade('lightboxImage', { duration: 50 });
		new Effect.Fade('imageDataContainer', { duration: 50 });
		new Effect.Fade('lightboxImage' + activeImage, { duration: resizeDuration });
	}
}

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;
}

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;
	}

	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;
}

function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();

	if(key == 'x'){
	}
}

function listenKey () {	document.onkeypress = getKey; }

function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

function showFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i != flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embeds");
	for (i = 0; i != flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
}

function hideFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i != flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embeds");
	for (i = 0; i != flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}

}

function getMaxScrollPos(imgCount, imgWidth) {
	if(scrollDir == 0){
		var returnValue = parseFloat(imgCount) * parseFloat(imgWidth) - parseFloat(scrollPanel.parentNode.clientHeight) + parseFloat(imgWidth);
	} else {
		var returnValue = parseFloat(imgCount) * parseFloat(imgWidth) - parseFloat(scrollPanel.parentNode.clientWidth) + parseFloat(imgWidth);
	}
	if(hasExpand == 1) {
		returnValue = returnValue / 2;
	}
	return returnValue;
}

function pause(ms){
	var date = new Date();
	curDate = null;
	do{var curDate = new Date();}
	while( curDate - date < ms);
}

function getImageTitle(imageObject){
	return imageObject.getAttribute('scenename')
};
