// -----------------------------------------------------------------------------------
// This page is coded Lars Kirchhoff
// http://www.lars-kirchhoff.de
//
// adapted from the code by Scott Upton
// http://www.uptonic.com | http://www.couloir.org
//
// This work is licensed under a Creative Commons License
// Attribution-ShareAlike 2.0
// http://creativecommons.org/licenses/by-sa/2.0/
//
// Associated API copyright 2002, Travis Beckham (www.squidfingers.com)
//
// -----------------------------------------------------------------------------------

Detect=function(){var agent=navigator.userAgent.toLowerCase();this._mac=agent.indexOf('mac')!=-1;this._win=!this._mac;this._w3c=document.getElementById;this._iex=document.all;this._ns4=document.layers;}
Detect.prototype.getObj=function(name){if(this._w3c){return document.getElementById(name);}else if(this._iex){return document.all[name];}else if(this._ns4){return this.getObjNS4(document,name);}}
Detect.prototype.getObjNS4=function(obj,name){var d=obj.layers;var result,temp;for(var i=0;i<d.length;i++){if(d[i].id==name){result=d[i];}else if(d[i].layers.length){var temp=this.getObjNS4(d[i],name);}
if(temp){result=temp;}}
return result;}
Detect.prototype.getStyle=function(obj){return(this._ns4)?obj:obj.style;}
HTMLobj=function(name){if(name){this._inherit=Detect;this._inherit(name);this._id=name;this._el=this.getObj(this._id);this._css=this.getStyle(this._el);this._obj=name+'Object';eval(this._obj+'=this');this._timer=null;this._glideRunning=false;this._tweenRunning=false;this._fadeRunning=false;this._startFade=false;return this;}}
HTMLobj.prototype=new Detect();HTMLobj.prototype.getWidth=function(){if(this._ns4){return this._el.document.width;}else{return this._el.offsetWidth;}}
HTMLobj.prototype.getHeight=function(){if(this._ns4){return this._el.document.height;}else{return this._el.offsetHeight;}}
HTMLobj.prototype.setStyle=function(prop,val){if(!this._ns4){this._el.style[prop]=val;if(this._iex&&this._mac){this._el.innerHTML=this._el.innerHTML;}}}
HTMLobj.prototype.show=function(){this._css.visibility='visible';}
HTMLobj.prototype.hide=function(){this._css.visibility='hidden';}
HTMLobj.prototype.showhide=function(){if(this._css.visibility=='hidden'||this._css.visibility=='hide'){this._css.visibility='visible';}else{this._css.visibility='hidden';}}
HTMLobj.prototype.sizeTo=function(w,h){if(!this._ns4){this._css.width=w+'px';this._css.height=h+'px';}}
HTMLobj.prototype.tweenTo=function(method,start,end,time){if(!this._tweenRunning){this._tweenTime=0;var s='['+start.toString()+']';var e='['+end.toString()+']';this._timer=setInterval(this._obj+'.tweenTo('+method+','+s+','+e+','+time+')',33);this._tweenRunning=true;}
if(++this._tweenTime>time){this.cancelTween();}else{var w=method(this._tweenTime,start[0],end[0]-start[0],time);var h=method(this._tweenTime,start[1],end[1]-start[1],time);this.sizeTo(w,h);}}
HTMLobj.prototype.cancelTween=function(){clearInterval(this._timer);this._timer=null;this._tweenRunning=false;this._startFade=true;}
easeInQuad=function(t,b,c,d){t/=d;return c*t*t+b;}
HTMLobj.prototype.setOpacity=function(opacity){opacity=(opacity==100)?99.999:opacity;this._css.filter="alpha(opacity:"+opacity+")";this._css.KHTMLOpacity=opacity/100;this._css.MozOpacity=opacity/100;this._css.opacity=opacity/100;}
HTMLobj.prototype.fadeIn=function(opacity,change,speed){if(opacity<=100){this.show();this._fadeRunning=true;this.setOpacity(opacity);opacity+=change;setTimeout(this._obj+'.fadeIn('+opacity+','+change+','+speed+')',speed);}else{this._fadeRunning=false;this.setOpacity(100);}}
HTMLobj.prototype.displayShow=function(){this._css.display='block';}
HTMLobj.prototype.displayHide=function(){this._css.display='none';}
HTMLobj.prototype.displayShowHide=function(){if(this._css.display=='none'){this.displayShow();}else{this.displayHide();}}
HTMLobj.prototype.setSrc=function(target){this._el.src=target;}
HTMLobj.prototype.setHref=function(target){this._el.href=target;}
HTMLobj.prototype.setInnerHtml=function(content){this._el.innerHTML=content;}


// Create access to 'Detect' object and a place to put instances of 'HTMLobj'
API = new Detect();

// CREATE INSTANCES & LOAD
loadAPI = function() {
	// Instantiate HTMLobj
	API.Container		= new HTMLobj('Container');
	API.Photo			= new HTMLobj('Photo');
	API.PhotoContainer	= new HTMLobj('PhotoContainer');
	API.LinkContainer	= new HTMLobj('LinkContainer');
	API.MenuContainer	= new HTMLobj('MenuContainer');
	API.PrevLink		= new HTMLobj('PrevLink');
	API.NextLink		= new HTMLobj('NextLink');
	API.LoadImg			= new HTMLobj('LoadImg');
	API.Caption			= new HTMLobj('CaptionContainer');
	
	API.LoadImgSrc      = document.getElementById("LoadImg").src;
	
	// Show initial photo
	cyclePhoto(photoId);
}
onload = loadAPI;

// Fade in photo when it is loaded from the server
initFade = function(){
   
    // Show PhotoContainer again
	API.PhotoContainer.show();
	
	// Be certain the tween is complete before fading, too
	var fade_timer = setInterval('startFade()', 1000);
					
	// Fade photo in when ready and clear listener
	startFade = function(){
		if(API.Container._tweenRunning == false){
			clearInterval(fade_timer);
			
			// Be certain fade is done running before allowing next/previous links to work
			// This avoids rapid fade-in when users click next/previous links in quick succession
			var adv_timer = setInterval('permitNextPrev()', 500);
			
			// Permit next/previous links to function normally when fade is completed
			permitNextPrev = function(){
				if(API.Photo._fadeRunning == false){
					clearInterval(adv_timer);
					API.LinkContainer.displayShow();
					document.getElementById('NextLink').onclick = nextPhoto;
					document.getElementById('PrevLink').onclick = prevPhoto;
					
					for(var i=0;i<=photoNum;i++){
						restoreThumbLinks(i);
					}
				} else {
					return;
				}
			}
			
			// Swap out loading animation to spare CPU cycles when hidden anyway
			API.LoadImg.setSrc("/images/c.gif");
			
			// Fade photo in
			API.Photo.fadeIn(0,8,1);
		} else {
			return;
		}
	}
}

// Restore original onclick events to thumbnail links
restoreThumbLinks = function(thumbId){
	thumbId = thumbId;	
	tmp = thumbId;
	thisThumb = "Thumb"+tmp;
	
	// Create private function to pass into onclick event
	thisCyclePhoto = function(){
		cyclePhoto(thumbId);
		photoId = thumbId;
	}
	
	// Give onclick event back to thumbnail in question
	document.getElementById(thisThumb).onclick = thisCyclePhoto;
}

// Prevent next/previous
falsify = function(){
	return false;
}

// Go to next photo
nextPhoto = function(){
    if (photoId == 0) {
        photoId = 1;   
    } 
	// Go to next photo
	if(photoId == (photoArray.length-1)){
		photoId = 0;
	} else {
		photoId++;
	}
	cyclePhoto(photoId);
}

// Go to previous photo
prevPhoto = function(){
	// If at start, go back to end
	if(photoId == 0){
		photoId = photoArray.length - 1;
	} else {
		photoId--;
	}
	cyclePhoto(photoId);
}

// Alter class of elements
changeElementClass = function(objId,setClass) {        
	document.getElementById(objId).className = setClass;
}

// Toggle the menu appearance between selected/deselected
toggleActiveMenu = function(){		
	API.MenuContainer.displayShowHide();
	if(document.getElementById('MenuLink').className == "selected"){
		changeElementClass('MenuLink','none');
	} else {
		changeElementClass('MenuLink','selected');
	}
}

// Toggle the thumbnails between selected/deselected
toggleActiveThumb = function(newThumb){	
    
	var oldThumb = activeThumb;
	
	if (oldThumb && newThumb) changeElementClass(oldThumb,'');
	if (newThumb) changeElementClass(newThumb,'selected');
		
	activeThumb = newThumb;
}

// Function to load subsequent photos in gallery
cyclePhoto = function(photoId){
    
    // Swap in loading animation
    API.LoadImg.setSrc(API.LoadImgSrc);
	
	// Assign "selected" to active thumbnail in navigation	
	var newThumb = "Thumb"+photoId;
	toggleActiveThumb(newThumb);
	
	// Hide link and menu container if they are not already hidden
	API.MenuContainer.displayHide();
	API.LinkContainer.displayHide();
	
	// Hide photo container temporarily
	API.Photo.hide();
	API.Photo.setOpacity(0);
	
	// Get dimensions of photo
	var wNew = photoArray[photoId][1];
	var hNew = photoArray[photoId][2];
	
	// Start tween on a delay
	var wCur = API.Container.getWidth() - borderSize;
	var hCur = API.Container.getHeight() - borderSize;
	
	// Begin tweening on a short timer
	setTimeout('API.Container.tweenTo(easeInQuad, ['+wCur+', '+hCur+'], ['+wNew+','+hNew+'], 7)',500);
	setTimeout('API.LinkContainer.tweenTo(easeInQuad, ['+wCur+', '+hCur+'], ['+wNew+','+hNew+'], 7)',500);
	
	// Get new photo source
	var newPhoto = photoDir + photoArray[photoId][0];
	
	// Set source, width, and height of new photo
	API.Photo.setSrc(newPhoto);		
	API.Photo.sizeTo(wNew,hNew);
	
	// Set links to new targets based on photoId
	API.NextLink.setHref("#" + (photoId));
	API.PrevLink.setHref("#" + (photoId));
	
	setSliderHeight(hNew);
	
	// Event listeners for onload and onclick events
	document.getElementById('Photo').onload = initFade;
	
	// Block next/previous links until permitNextPrev() has fired
	document.getElementById('NextLink').onclick = falsify;
	document.getElementById('PrevLink').onclick = falsify;
	
	// Also block all thumbnail links until permitNextPrev() has fired
	for(var i=0;i<=photoNum;i++){	    
		document.getElementById('Thumb'+i).onclick = falsify;
	}
	
	
	// Set caption
	(photoArray[photoId][3]) ? API.Caption.setInnerHtml(''+photoArray[photoId][3]+'') : API.Caption.setInnerHtml("");
	
}

function setSliderHeight(hNew) {
     document.getElementById('NextLink').style.height = hNew + 'px';
     document.getElementById('PrevLink').style.height = hNew + 'px';
}
