/* ====================================================================
  Gallery object
  Literal Notation
  ===================================================================== */
  
var Gallery = {
	
	//default config
	imageId : 'thephoto',
	descId : 'desc',
	loadId : 'loading',
	CSSselector : '',
	CSSselectedClass : 'selected',
	eventType : 'mouseover',
	changeDesc : true,
	startIndex : 0, // "random" | [number]
	enlarge : false,
	enlargeId : '',
	
	//private vars
	selectedImg : 0,
	timerRunning : 0,
	imageList : new Array (),
	links : new Array (),
	loaded : false,
	
	showImg : function (e, myId) {
		if (Gallery.loaded) {
			if (Gallery.timerRunning) {
				window.clearTimeout(tID);
				Gallery.timerRunning = false;
			}		
			if (myId !=-1) {
				tID = window.setTimeout("Gallery.showImage("+myId+")", 200);
				Gallery.timerRunning = true;
				//manage links
				if (Gallery.eventType =="click") {
					Gallery.manageLinks(myId);
				}
			}
		}
	},
	
	manageLinks : function (id) {
		//reset all the link styles
		for (i=0; i<Gallery.links.length; i++) {
			if (Gallery.links[i].className) {
				Gallery.links[i].className="";
			}
		}
		//set new class for the selected link (links[] starts at 0 but link ids start with 1, so -1)
		if (Gallery.links.length >1) {
			Gallery.links[id].className = Gallery.CSSselectedClass;
		}
		// hide enlargeButton if the selected image doesn't have an enlarged state
		if (document.getElementById) {
			var enlargeLink = document.getElementById(Gallery.enlargeId);
			
			if (Gallery.enlargeStates) {
				if (Gallery.enlargeStates[id] == "0") {
					enlargeLink.style.visibility = "hidden";
				} else {
					enlargeLink.style.visibility = "visible";
				}
			}
		}
	},
	
	swapImage : function (img) {
		var image = document.getElementById(Gallery.imageId);				
       	if (typeof(image) != "undefined") {
           	image.src = Gallery.imageList[img].src;
            image.alt = 'Image ' + (img);
            image.title = image.alt;
       	}
	},

	showImage : function (img) {	
		Gallery.timerRunning = false;

		if (Gallery.selectedImg != img) { // prevent flickering	
			if (document.getElementById) {
				var image = document.getElementById(Gallery.imageId);
				alphaControl.setOpacity(image, 0);
				image.style.visibility = "visible";
				Gallery.swapImage(img);
				//fade in
				alphaControl.fade(image,0,100,10);			      			
    		}
			Gallery.selectedImg = img;
    	}
   		if (Gallery.changeDesc) {
			Gallery.showDesc(img);
		}
	},

	showDesc : function (img) {
		document.getElementById(Gallery.descId).innerHTML = Gallery.description[img];
	},
	
	showImg_features : function (myId) {
  		return function(e) { 
    		Gallery.showImg(e, myId);
  		}
	},
	
	attachLinkBehavior : function () {
		Gallery.links = document.getElementsBySelector(Gallery.CSSselector);		
		var i;
		for (i=0;i<Gallery.links.length;i++) {
			//instead of passing a function reference to addEvent we execute a function
			// to return an anonymous function which includes the parameters we need
			// in this case we need to know the imgId for each link
			addEvent(Gallery.links[i],Gallery.eventType,Gallery.showImg_features(i),false);		
			if (Gallery.eventType =="click") {
				attachCancelClick(Gallery.links[i]);
			}
		}
		if (Gallery.enlarge) { // attach behaviour to enlarge button
			var enlargeLink = document.getElementById(Gallery.enlargeId);
			addEvent(enlargeLink,"click", Gallery.enlargeImg, false);
			if (Gallery.eventType =="click") {
				attachCancelClick(enlargeLink);
			}
		}
	},
	
	enlargeImg : function() {
		var enlargeLink = ""+document.getElementById(Gallery.enlargeId)+"";
		// strip 'img=' and following characters if it already exists in the url string
		if (enlargeLink.indexOf('&amp;img=')) {
			enlargeLink = enlargeLink.substring(0,enlargeLink.indexOf("&img="));
		}
		window.location.href = enlargeLink+"&img="+Gallery.selectedImg;
	},
	
	onPreload : function(aImages, nImages) {
		if (nImages == Gallery.imageSrc.length) { //all images preloaded
			//assign loaded images array to the gallery image array 
			Gallery.imageList = aImages;
			Gallery.loaded=true;
			Gallery.init();
		}
	},
	
	loadComplete : function (imgId) {	
		//change image
		Gallery.swapImage(imgId);     
        
        if (document.getElementById) {
        	// fade out loading div
       		var loading = document.getElementById(Gallery.loadId);
        	alphaControl.fade(loading,100,0,2);	
			Gallery.selectedImg = imgId;	
   			if (Gallery.changeDesc) {
   				Gallery.showDesc(imgId);
			}
		}
		
		//manage links
		//if (Gallery.eventType =="click") {
			Gallery.manageLinks(imgId);
		//}
	},
	
	init : function () {
		// all images preloaded, we're ready to go
		var imgId;
		if (Gallery.startIndex == "random") {
			imgId=Math.round(Math.random()*(Gallery.imageList.length));
		} else {
			imgId=Gallery.startIndex;
		}
   		Gallery.loadComplete(imgId);
	}
}
