/**

foliobox gallery/slideslow script
(C) Copyright 2009, John McClumpha

Not for distribution

filename:	foliobox.js
author:		John McClumpha <john@mcclumpha.org>
version:	0.1
date:		May 21st, 2009

**/

/* define the following settings */


// delay (in milliseconds) for slideshow
var imgplaydelay = 6000;
// time (in milliseconds) for image crossfade
var imgfadespeed = 1000;
// set to true to auto start the slideshow
var imgplaying = true;

var imageWidth = 700;
var imageHeight = 215;

// var imgpath = '/bdav/webdata/slideshows/'; // clients2
var imgpath = '/webdata/slideshows/'; // live
//var imgpath = '/bdav/website/dev/webdata/slideshows/'; // dev

/* end of user-defined settings */

var imgcurrent = -1;
var imgarray = new Array();
var imgtotal = 0;
var imgboxactive = 1;
var imgboxother  = 2;

// go to the specified image
function imgGo(imgindex){
	
	if(imgindex == imgcurrent) { return false; } // don't try to load the image we're already on

	imgcurrent = imgindex;

	$('#imgloading').fadeIn(imgfadespeed/2);
	
	// create the image preload object and define what to do once it's loaded
	imgpreload = new Image();
	imgpreload.onload = function(){	
	
		$('#img'+imgboxother).hide();
		$('#img'+imgboxother).html('<img src="'+imgpath+imgarray[imgcurrent].innerHTML+'" width="'+imageWidth+'" height="'+imageHeight+'" />');
//		$('#img'+imgboxother).css({'margin-left': '-' + ((imageWidth / 2) + parseInt($('.hero').css('padding-left'))) + 'px','margin-top': '-' + ((imageHeight / 2) + parseInt($('.hero').css('padding-top')))  + 'px'});
		$('#imgloading').hide();
		$('#img'+imgboxactive).fadeOut(imgfadespeed);
		$('#img'+imgboxother).fadeIn(imgfadespeed);
		$('#imgpos').html((imgcurrent+1)+' of '+imgtotal);
		// need to toggle all the variables
		(imgboxactive == 1) ? imgboxactive = 2 : imgboxactive = 1;
		(imgboxother == 1) ? imgboxother = 2 : imgboxother = 1;
		if(imgplaying){
			setTimeout('imgGoNext('+true+')', imgplaydelay);
		}

	}
	
	// preload the image
	imgpreload.src = imgpath+imgarray[imgcurrent].innerHTML;

}

// go to the next image
function imgGoNext(imgplayloop){
	if(imgplayloop && !imgplaying){ // check to see if we should continue playing
		return;
	}
	imgindex = imgcurrent + 1;
	if (imgindex == imgtotal) { imgindex = 0; }
	imgGo(imgindex);
}
// go to the previous image
function imgGoPrev(){
	imgindex = imgcurrent - 1;
	if (imgindex < 0) { imgindex = imgtotal - 1; }
	imgGo(imgindex);
}

function imgPlay() {
		if(imgplaying){
			setTimeout('imgGoNext('+true+')', imgplaydelay);
		}
}

