/* ------------------------------------------------------------------------------------------------------------------------------- */
var slideshowactive;
var slideshowimages;
var slideshowindex;
var slideshowobj;
var slideshowdelay	= 10;
var currentimagefile;
var currentdisplayid;
/* ------------------------------------------------------------------------------------------------------------------------------- */
function processSlideShowRequest(pid) {
	
	slideshowbutton	= document.getElementById('slideshowbutton');
	
	validateSlideShowRequest();

	if (slideshowactive) {
		slideshowactive			= false;
		if (slideshowbutton) {slideshowbutton.value	= 'Resume Slide Show'};
	} else {
		slideshowactive			= true;
		//slideshowbutton.value	= 'Pause Slide Show';
		if (slideshowbutton) {slideshowbutton.value	= 'Pause Slide Show'};

		if (slideshowobj) {
			slideshowindex	= slideshowobj.currentindex;
			
			doSlideShow(slideshowobj);
		} else {
			requestSlideShowImages(pid);
		}
	}

}
/* ------------------------------------------------------------------------------------------------------------------------------- */

/* ------------------------------------------------------------------------------------------------------------------------------- */
function validateSlideShowRequest() {
	// min interval is 5 seconds
	//var interval	= document.getElementById('interval');
	
	if (interval	= document.getElementById('interval')) {
		if (interval.value<8) {
			interval.value	= 8;
			alert("Please allow at least 8 seconds between each image");
		}
	}
	
}
/* ------------------------------------------------------------------------------------------------------------------------------- */

/* ------------------------------------------------------------------------------------------------------------------------------- */
function requestSlideShowImages(pid) {
	
	var url		= 'http://www.healingtaousa.com/cgi-bin/photos.pl';
	var loadnotice	= "Requesting slide show, please wait...";

	AjaxRequest.get(
		{	'url'		: url,
			'parameters':{ 'slideshow' : pid},
			'onSuccess'	:function(req){ setupSlideShow(req.responseText); },
			'onLoading'	:function(req){ writeToElement('ajaxmsg',loadnotice); }
		}
		);
}
/* ------------------------------------------------------------------------------------------------------------------------------- */

/* ------------------------------------------------------------------------------------------------------------------------------- */
function setupSlideShow(respobj) {

	//writeToElement('ajaxmsg','response received');
	eval("slideshowobj = ("+respobj+")");

	var msg	= '';
	if (slideshowobj) {
		slideshowindex	= slideshowobj.currentindex;
		
		imagesarray		= slideshowobj.images;
		imagecount		= imagesarray.length;

		msg	= 'Starting slide show...';
		writeToElement('ajaxmsg', msg);
		
		doSlideShow(slideshowobj);

		writeToElement('ajaxmsg', '');
	}

	//msg	= 'result:' + slideshowobj.images[4].caption;
	//msg	= 'result:' + imagecount;
	//writeToElement('ajaxmsg', msg);
	//writeToElement('captioncell', msg);
}
/* ------------------------------------------------------------------------------------------------------------------------------- */

/* ------------------------------------------------------------------------------------------------------------------------------- */
function imageLoaded() {
	
	slideshowindex++;
	
	msg	= '<br>loaded' + slideshowindex;
	writeToElement('ajaxmsg', msg,'a');
	
	
}
/* ------------------------------------------------------------------------------------------------------------------------------- */

/* ------------------------------------------------------------------------------------------------------------------------------- */
function doSlideShow(slideshowobj) {

	slideshowimages	= slideshowobj.images;
	imagecount		= slideshowimages.length;

	if (delayinterval	= document.getElementById('interval')) {
		slideshowdelay	= delayinterval.value;
	}

	if (slideshowindex<imagecount && slideshowactive) {
		slideshowindex++;

		if (slideshowimages[slideshowindex]) {
		} else {
			slideshowindex	= 0;
		}
		
		slideshowobj.currentindex	= slideshowindex;
		currentimgfilename			= slideshowimages[slideshowindex].filename;
		currentimgsrc				= slideshowimages[slideshowindex].imgsrc;
		currentimgcaption			= slideshowimages[slideshowindex].caption;
		currentimglink				= '<a href="'+currentimgsrc+'">'+currentimgfilename+'</a>';

		showImage(currentimgsrc);

		selectedinterval	= slideshowdelay*1000;
		timerid	= setTimeout( 'doSlideShow(slideshowobj)', selectedinterval );

	}
}
/* ------------------------------------------------------------------------------------------------------------------------------- */

/* ------------------------------------------------------------------------------------------------------------------------------- */
function showImage(imgsrc) {
	targetimg			= document.getElementById('mainimage');
	targetimg.onLoad	= refreshPage()
	targetimg.src		= imgsrc;
}
/* ------------------------------------------------------------------------------------------------------------------------------- */

/* ------------------------------------------------------------------------------------------------------------------------------- */
function refreshPage() {
	// delay displaying the new caption until the browser can load and render the image	
	timerid2	= setTimeout( 'updatePageElements()', 2500 );
}
/* ------------------------------------------------------------------------------------------------------------------------------- */

/* ------------------------------------------------------------------------------------------------------------------------------- */
function updatePageElements() {
	
	// update the caption
	writeToElement('captioncell', slideshowindex);

	writeToElement('captioncell', slideshowobj.images[slideshowindex].caption);
	
	// update the navigation
	prev			= document.getElementById('prevlink');
	next			= document.getElementById('nextlink');

	// manage the start and end	
	imgcount		= slideshowobj.images.length;
	nextid			= slideshowobj.images[slideshowindex+1] ? slideshowindex+1 : 0;
	previd			= slideshowobj.images[slideshowindex-1] ? slideshowindex-1 : imgcount-1;
	
	// set the nav href
	prev.href		= "http://www.healingtaousa.com/cgi-bin/photos.pl?photo=" + slideshowobj.images[previd].pid;
	next.href		= "http://www.healingtaousa.com/cgi-bin/photos.pl?photo=" + slideshowobj.images[nextid].pid;

	// debugging
	msg	= '<br>new nav: ' + slideshowobj.images[previd].pid + ' <-  ' + slideshowobj.images[slideshowindex].pid + '  -> ' + slideshowobj.images[nextid].pid;
	//writeToElement('ajaxmsg',msg);

}
/* ------------------------------------------------------------------------------------------------------------------------------- */
