var phViewer = {
	slideShowPauseAmount : 4000,
	si : 0,
	currentImage : 0,
	prevImage : null,
	eventPhotosList : [],
	
	$ : function (id) {
		return document.getElementById(id);
	},	
	init : function() {
		if (!phViewer.$("eventPhotos"))
			return;
		var mainImage = phViewer.$("eventPhotos");
		var slidShow = phViewer.$("slideShow");
		var imgObjs = mainImage.getElementsByTagName("img");
                                           if (imgObjs.length == 0){
   return; 
  }

		for (var i=0; i < imgObjs.length; i++){
			phViewer.eventPhotosList.push(imgObjs[i]);//add all the event images to the array
			imgObjs[i].id = i;//set its ID to the array position
			phViewer.initImg(imgObjs[i]);
		}
		phViewer.setCurrentImage(phViewer.getCurrentImage());
		phViewer.setMainImg(phViewer.eventPhotosList[phViewer.currentImage]);
		
		// set toolbar events
		var obj = document.getElementById("mainImageContainer");
		phViewer.addEventListener(obj, 'mouseover', phViewer.showToolBar);
		phViewer.addEventListener(obj, 'mouseout', phViewer.hideToolBar);
		
		slidShow.style.visibility = "visible";
	},	
	initCat : function(obj) {
			//set the category caption in mainImageCatCap
			var category = obj.parentNode;
			if (category.firstChild.nodeName.toString().toLowerCase() != "div"){
				category.insertBefore(phViewer.newCopyEl("div",category.title),category.firstChild);
			}
	},
	initImg : function(obj) {
		obj.onclick = function(){
			try
			{
				phViewer.toggleSlideShow("paused");
				phViewer.setCurrentImage(Number(obj.id));
			}
			catch (e)
			{}
			phViewer.setMainImg(this);
		}
		phViewer.initCat(obj);
		phViewer.setSize(obj,50,40,true);
		obj.style.visibility = "visible";
		obj.style.cursor = "pointer";
		obj.className = "eventPhotosThumbImageInactive";			
	},
	setMainImg : function(obj) {
		var mainImageContainer = phViewer.$("mainImageContainer");	
		var mainImage = phViewer.$("mainImage");
		var mainImageCatCap = phViewer.$("mainImageCatCap");
		var mainImageImgCap = phViewer.$("mainImageImgCap");
		
			mainImageCatCap.style.visibility = "hidden";
			mainImageImgCap.style.visibility = "hidden"
		
			//remove any elements in the object
			while (mainImage.firstChild){
				mainImage.removeChild(mainImage.firstChild);
			}
			
			//create and set the new img as the main image
			var newImg=document.createElement("img");
			newImg.onload = function(){
				phViewer.setSize(this,500,400,false);
				
				//set the category caption in mainImageCatCap
				mainImageCatCap.replaceChild(phViewer.newCopyEl("span",obj.parentNode.title),mainImageCatCap.firstChild);
				
				//set the photo caption in mainImageImgCap
				mainImageImgCap.replaceChild(phViewer.newCopyEl("span",obj.title),mainImageImgCap.firstChild);
							
				mainImageContainer.style.width = newImg.width + 4 + "px";
				mainImage.style.border = "2px solid #535353";
				mainImage.appendChild(this);
				
				mainImageCatCap.style.visibility = "visible";
				mainImageImgCap.style.visibility = "visible"
			}			
			newImg.setAttribute("src",obj.src);
			newImg.style.display = "block";
	},
	setSize : function (obj,maxWidth,maxHeight,addPadding) {
		var ratio;
		if (obj.width > obj.height && (obj.width > maxWidth || obj.height > maxHeight)) {
			ratio = (obj.height / obj.width);
			obj.height = Math.floor(maxWidth * ratio);
			obj.width = maxWidth;
			if(obj.height < maxHeight && addPadding){
				var origHeight = obj.height;
				obj.style.paddingTop = Math.floor(maxHeight - origHeight) + "px";
			}
		}else if(obj.height > obj.width && (obj.width > maxWidth || obj.height > maxHeight)){
			ratio = (obj.width / obj.height);
			obj.width = Math.floor(maxHeight * ratio);			
			obj.height = maxHeight;
			if(obj.width < maxWidth && addPadding){
				var origWidth = obj.width;
				obj.style.paddingLeft = Math.floor((maxWidth - origWidth)/2) + "px";
				obj.style.paddingRight = maxWidth - origWidth - Math.floor((maxWidth - origWidth)/2) + "px";
			}
		}
		if (obj.width == 0 && obj.height == 0){
			obj.width = maxWidth;
			obj.height = maxHeight;
		}
	},
	toggleSlideShow : function (state){
		if(state == "on"){
			phViewer.startSlideShow();
		}else if(state == "paused"){
			phViewer.stopSlideShow();
		}else{
			if(phViewer.si == 0){
				phViewer.startSlideShow();
			}else{

				phViewer.stopSlideShow();
			}
		}		
	},
	stopSlideShow : function(){
		clearInterval(phViewer.si);
		phViewer.si = 0;
		var slideOnOffStatus = phViewer.$("slideOnOffStatus");
		slideOnOffStatus.replaceChild(phViewer.newCopyEl("span","Paused"),slideOnOffStatus.firstChild);
	},
	startSlideShow : function(){
		phViewer.si = setInterval("phViewer.autoRotate()",phViewer.slideShowPauseAmount);
		var slideOnOffStatus = phViewer.$("slideOnOffStatus");
		slideOnOffStatus.replaceChild(phViewer.newCopyEl("span","On"),slideOnOffStatus.firstChild);
	},
	newCopyEl : function(el,copy){
		var newEl = document.createElement(el);
		var newTxt = document.createTextNode(copy);
		newEl.appendChild(newTxt);
		return newEl;
	},
	incrementMainPhoto : function(i){
		phViewer.toggleSlideShow("paused");
		if (i == -1){
			phViewer.setCurrentImage(phViewer.getCurrentImage(-1));	
		}else if(i == 1){
			phViewer.setCurrentImage(phViewer.getCurrentImage(1));
		}	
		phViewer.setMainImg(phViewer.eventPhotosList[phViewer.currentImage]);
	},
	autoRotate : function(){
		phViewer.setCurrentImage(phViewer.getCurrentImage(1));
		phViewer.setMainImg(phViewer.eventPhotosList[phViewer.currentImage]);
	},
	getCurrentImage : function (i){
		var c;
		if (i == -1){
			c = phViewer.currentImage-1;
			if (c < 0){
				c = phViewer.eventPhotosList.length-1;
			}
		}else if (i == 1){
			c = phViewer.currentImage+1;
			if (c > phViewer.eventPhotosList.length-1){
				c = 0;
			}
		}else{
			c = phViewer.currentImage;
		}
		return c;
	},
	setCurrentImage : function (i){
		phViewer.prevImage = phViewer.currentImage;
		if (i < 0){
			phViewer.currentImage = phViewer.eventPhotosList.length-1;
		}else if (i > phViewer.eventPhotosList.length-1){
			phViewer.currentImage = 0;
		}else{
			phViewer.currentImage = i;
		}
		phViewer.$(phViewer.prevImage).className = "eventPhotosThumbImageInactive";
		phViewer.$(phViewer.currentImage).className = "eventPhotosThumbImageActive";	
	},
	showToolBar : function(evt){
		var obj = phViewer.$("toolBar"); 
		obj.className = "showToolBar";		
		var objEventPhotoDownloader = phViewer.$("flashPhotoDownloader");
	},
	hideToolBar : function(e){
		var obj = phViewer.$("toolBar");
		obj.className = "hideToolBar";
	},
	getPhotoUrl : function() {
		var mainImage = phViewer.$("mainImage");
		var photoUrl = unescape(mainImage.firstChild.src);
		if(photoUrl.indexOf("file:")>=0){
			alert("The URL below isn't valid.\n" + photoUrl);
		}
		return photoUrl;
	},
	addEventListener : function (el, event, func) {
		try {
			el.addEventListener(event, func, false);
		} catch (e) {
			try {
				el.detachEvent('on'+ event, func);
				el.attachEvent('on'+ event, func);
			} catch (e) {
				el['on'+ event] = func;
			}
		} 
	},
	removeEventListener : function (el, event, func) {
		try {
			el.removeEventListener(event, func, false);
		} catch (e) {
			try {
				el.detachEvent('on'+ event, func);
			} catch (e) {
				el['on'+ event] = null;
			}
		}
	}
};
phViewer.addEventListener(window, 'load', phViewer.init);