// <![CDATA[

// -----------------------------------------------------------------------------------
// 
// 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/30/05 ------------------------------------------------------

//

var photoDir = "/galery/"; // Location of photos for gallery
var borderSize = 6;	 // = 2x CSS border size

// get current photo id from URL
var thisURL = document.location.href;
var splitURL = thisURL.split("#");
var photoId = splitURL[1] - 1;
var bSlideShow = true;
var galeryMax;// = 1000;
var photoArray = new Array();
var doc;
var photoNum;

function sendSlideShowReq(url, params, httpMethod){
	if(!httpMethod) httpMethod = 'GET';
	doc = initXMLHttpRequest();
	if (doc) {
		doc.onreadystatechange = onReadySlideShow;
		doc.open(httpMethod, url, true);
		doc.send(params);
	}
}

// handle onreadystatechange event of doc object
function onReadySlideShow() {
	var ready = doc.readyState;
	var data = null;
    // only if doc shows "loaded"
    if (ready == 4) {// only if "OK"        
		if (doc.status == 200) {
			iniSlideShow();
		}else{
			alert("There was a problem retrieving the XML data:\n "+doc.statusText);
		}
	}
}

function iniSlideShow(){

	var g = doc.responseXML.getElementsByTagName("num");
	galeryMax = g[0].childNodes[0].nodeValue;

	// if no id in query string then set to 0
	photoId = (!photoId)? 0:photoId;
		
	// Define each photo's name, height, width, and caption dinamic
	for(var n=0; n<galeryMax; n++){
		if(n == (galeryMax-1)){
			photoArray[n] = new Array("pic"+(n+1)+".jpg", "800", "600");
		}else{
			photoArray[n] = new Array("pic"+(n+1)+".jpg", "800", "600");
		}
	};

	// Number of photos in this gallery
	photoNum = photoArray.length;
	
	// Show initial photo
	cyclePhoto(photoId);
}



// Create access to 'Detect' object and a place to put instances of 'HTMLobj'
API = new Detect();

// CREATE INSTANCES & LOAD
loadAPI = function(){
	// Instantiate HTMLobj
	API.Galery = new HTMLobj('galery');
	API.Photo = new HTMLobj('photo');
	API.PhotoContainer	= new HTMLobj('photoContainer');
	API.GaleryNavigation = new HTMLobj('galeryNavigation');
	API.PrevLink = new HTMLobj('prevLink');
	API.NextLink = new HTMLobj('nextLink');
	API.CounterDisplay = new HTMLobj('counterDisplay');
	API.Counter = new HTMLobj('counter');
	API.LoadImg = new HTMLobj('LoadImg');
	
	sendSlideShowReq('/xml/galery.php', null, '');
}

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.Galery._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);
					
					// Only show links if there is more than one photo in array
					if(photoNum > 1){
						//API.GaleryNavigation.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("/images/slideshow/start.gif");
			
			// Show caption again
			//API.counterDisplay.show();
			
			// Fade photo in
			API.Photo.fadeIn(0,30,30);
		} else {
			return;
		}
	}
}

// Prevent next/previous
falsify = function(){
	return false;
}


// Go to next photo
nextPhoto = function(){
	clearTimeout(toSlideShow);
	if(photoId == (photoArray.length - 1)){
		photoId = 0;
	} else {
		photoId++;
	}
	cyclePhoto(photoId);
}

// Go to previous photo
prevPhoto = function(){
	// If at start, go back to end
	clearTimeout(toSlideShow);
	if(photoId == 0){
		photoId = photoArray.length - 1;
	} else {
		photoId--;
	}
	cyclePhoto(photoId);
}

// Play Slide Show
slideShow = function(){
	if(photoId == (photoArray.length - 1)){
		photoId = 0;
	} else {
		photoId++;
	}
	cyclePhoto(photoId);
}

// Alter class of elements
changeElementClass = function(objId,setClass) {
	document.getElementById(objId).className = setClass;
}

var toSlideShow;

// Function to load subsequent photos in gallery
cyclePhoto = function(photoId){
	// Swap in loading animation
	API.LoadImg.setSrc("/images/slideshow/loading_ani2.gif");
	
	// Hide link container if it is not already hidden
	//API.GaleryNavigation.displayHide();
	
	// Hide photo container and caption temporarily
	API.Photo.hide();
	API.Photo.setOpacity(0);
	//API.CounterDisplay.hide();
	
	// Get dimensions of photo
	var wNew = photoArray[photoId][1];
	var hNew = photoArray[photoId][2];
	
	// Start tween on a delay
	var wCur = API.Galery.getWidth() - borderSize;
	var hCur = API.Galery.getHeight() - borderSize;
	
	// Begin tweening on a short timer
	setTimeout('API.Galery.tweenTo(easeInQuad, ['+wCur+', '+hCur+'], ['+wNew+','+hNew+'], 7)',100);
	
	// 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+1));
	API.PrevLink.setHref("#" + (photoId+1));
	API.Counter.setInnerHtml((photoId+1)+"_"+photoNum);
	
	// 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;
	
	
	toSlideShow = setTimeout('slideShow()', 6000);

}
// ]]>


