// -----------------------------------------------------------------------------------
// 
// This page coded 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)
//
// -----------------------------------------------------------------------------------
// --- version date: 04/24/05 ------------------------------------------------------


// 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');

	// Preload some images
	MM_preloadImages('img/next_plus.gif','img/prev_minus.gif');
	
	// 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;
				} else {
					return;
				}
			}
			
			// Swap out loading animation to spare CPU cycles when hidden anyway
			API.LoadImg.setSrc("img/c.gif");
			
			// Fade photo in
			API.Photo.fadeIn(0,15,33);
		} else {
			return;
		}
	}
}

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

// Go to next photo
nextPhoto = function(){
	// 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');
	}
}

// Function to load subsequent photos in gallery
cyclePhoto = function(photoId){
			
	// Swap in loading animation
	API.LoadImg.setSrc("img/loading_ani.gif");
	
	// If menu is selected, deselect it
	if(document.getElementById('MenuLink').className == "selected"){
		changeElementClass('MenuLink','none');
	}
	
	// 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];

	// Event listeners for onload and onclick events
	document.getElementById('Photo').onload = initFade;
	
	// 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+1));
	API.PrevLink.setHref("#" + (photoId+1));

	// Block next/previous links until permitNextPrev() has fired
	document.getElementById('NextLink').onclick = falsify;
	document.getElementById('PrevLink').onclick = falsify;
	
	// Set caption
	(photoArray[photoId][3]) ? API.Caption.setInnerHtml('<p><span id=\"Counter\">'+(photoId)+'/'+photoNum+'</span>&nbsp;&nbsp;'+photoArray[photoId][3]+'</p>') : API.Caption.setInnerHtml("");
	
}