// JS FILE FOR RUNNING STANDARD SLIDESHOW UNDER MOSAIC
// ***************************************************
// Place slideshow's own images (ie. Autorun, Stop buttons etc..) in the /images folder
// *****
// ensure XSL provides following hidden fields between <form name="sideform"></form> tags:
//		(1) for total number of images:
//		<input type="hidden" value="IMAGE01.jpg" name="image10"></input>
//		<input type="hidden" value="Image 01 Caption" name="caption10"></input>
//		etc..
//		(2) relative path where the slideshow images are stored eg:
//		<input type="hidden" name="approot" value="slideshowimages/" />
//		(3) total number of (main-thumb) image pairs eg:
//		<input type="hidden" name="totpics" value="8/" />

var totpics;
var caption1, caption2, caption3, caption4, caption5, caption6, caption7, caption8, caption9, caption10;
var picnum=1;

var totthumbs = 5;
var thumboffset = 0;
var thumbstart,thumbfin;
thumbstart = 1;
thumbfin = totthumbs;

var spacerimage="images/spacer.gif";
var theTimeout;
var do_autorun = -1;

//Counter variables
var intCurrentCounterPosition;//Holds the position of the counter 1 to 10
var intTopCounterNumber;//Current top number to display
var intBottomCounterNumber;//current bottom number to display
var totcounter = 10;
var counteroffset = 0;
var counterstart,counterfin;
counterstart = 1;
counterfin = totcounter;
var tmpCounter;
intCurrentCounterPosition = 1;

//start initialise slide show
function initialise()  {
	doc=document.slideform;
	totpics=doc.totpics.value;
	document.getElementById('prevbutton').style.visibility="hidden"; 

	if (totpics>0) {	
		
		approot=doc.approot.value;
		
		if (totpics==1) {	
			document.getElementById('nextbutton').style.visibility="hidden"; 
			document.getElementById('autorunbutton').style.visibility="hidden";
		}
			
		if (totpics < 5) totthumbs=totpics;
		
		if (totpics < 10) totcounter=totpics;	

		// SET DATA: TOP CAPTION TEXT, HIDE CAPTIONS, IMAGES AND THUMBNAILS
		for (x=1;x<=totpics;x++) {
			ic=x-1;
			eval("caption"+x+"=doc.caption"+x+".value;");
			eval("imagefilename=\"SS_\"+doc.image"+x+".value;");
			eval("mainimage"+ic+"=new Image();"); 
			eval("mainimage"+ic+".src=approot+imagefilename;"); 

			//alert("mainimage"+ic+".src=approot+imagefilename;"); 
			eval("thumbfilename=\"ST_\"+doc.image"+x+".value;");
			eval("thumbimage"+ic+"=new Image();"); 
			eval("thumbimage"+ic+".src=approot+thumbfilename;"); 
			//alert("thumbimage"+ic+".src=approot+thumbfilename;"); 
			
			if (x<6) {
				eval("document.getElementById('thumb"+x+"').src=approot+thumbfilename;");
			}
			
			if (x==1) {
				document.getElementById('bottomnum1').style.color="#ffffff";
				document.getElementById('bottomTD1').style.backgroundColor="#990000";
				document.getElementById('bottomTD1').style.textAlign="center";
				document.getElementById('captiontext').innerHTML=caption1;
			}
			
			if (x > 1 & x <= 10) {
				eval("document.getElementById('bottomnum"+x+"').style.color='#000000';");
				eval("document.getElementById('bottomTD"+x+"').style.backgroundColor='#ffffff';");
				eval("document.getElementById('bottomTD"+x+"').style.textAlign='center';");
			}
		}
	}
	//Hide controls if no images
	else {
		document.getElementById('nextbutton').style.visibility="hidden"; 
		document.getElementById('autorunbutton').style.visibility="hidden";
		totthumbs=0;		
		document.getElementById('captiontext').innerHTML="Pictures coming soon !";
		document.getElementById('mainpic').src=spacerimage; 
	}

	// enclose first thumb in brackets and give dark border, give all other thumbs a white border
	for (x=1;x<=totthumbs;x++) {
		if (x==1) {
			eval("document.getElementById('thumb"+x+"').style.borderColor='#333333';");
			eval("document.getElementById('LHbrack_tn"+x+"').src='images/slide-LHbracket.gif';");
			eval("document.getElementById('RHbrack_tn"+x+"').src='images/slide-RHbracket.gif';");
		}
		else {
			eval("document.getElementById('thumb"+x+"').style.borderColor='#ffffff';"); 
		}
	}
}
//end initialise slide show





//-------------------------------------------------------------------------------
function toggle_autorun()  {
	do_autorun = 0 - Number(do_autorun);
	if (do_autorun == 1)   { 
		autorun(); 
		document.getElementById('autorunbutton').src="images/slide-autorun-stop.gif";
		document.getElementById('autorunbutton').alt="Click here to stop the slideshow";
		}
	else {
		stop_autorun();
	}
}
//-------------------------------------------------------------------------------


//-------------------------------------------------------------------------------
// run the slideshow !
function autorun() {
	// if next one is pic1 again need to do special bit of code since this action (going back to the beginning)
	// does not occur normally (Next Button would be hidden!) 
	if (picnum == totpics) { 
		
		var intamounttodisplay;
		if (totpics < 10) {
			intamounttodisplay = totpics;
		}
		else {
			intamounttodisplay = 10;
		}
		
		picnum = 1; 
		thumboffset = 0;
		
		
		intCurrentCounterPosition = 1;
		intTopCounterNumber = 10;
		intBottomCounterNumber = 1;
		totcounter = 10;
		counteroffset = 0;

		
		//Reset counters
		for (i = 1; i <= intamounttodisplay; i++) {
			eval("document.getElementById('bottomnum" + i + "').style.color='#000000';");
			eval("document.getElementById('bottomTD" + i + "').style.backgroundColor='#ffffff';");
			eval("document.getElementById('bottomnum" + i +"').innerHTML='<strong> " + i + "</strong>';");
		}
	
		eval("document.getElementById('LHbrack_tn"+totthumbs+"').src='"+spacerimage+"';");
		eval("document.getElementById('RHbrack_tn"+totthumbs+"').src='"+spacerimage+"';");
		
		eval("document.getElementById('bottomnum1').style.color='#ffffff';");
		eval("document.getElementById('bottomTD1').style.backgroundColor='#990000';");
		
		change_pic("num","1","");
	}
	else {
		change_pic("next",picnum,"");
	}
	theTimeout=setTimeout("autorun();",3000);
}
//-------------------------------------------------------------------------------


//-------------------------------------------------------------------------------
// func needed when user clicks on main pic to stop autorun!
function stop_autorun() {
	do_autorun=-1;
	clearTimeout(theTimeout);
	document.getElementById('autorunbutton').src="images/slide-autorun.gif";
	document.getElementById('autorunbutton').alt="Click here to run the slideshow!";
}
//-------------------------------------------------------------------------------


//-------------------------------------------------------------------------------
function change_pic(action,thepicnum,ctype) {

	//Only do something if there is more than one photo
	if (totpics > 1) { 
		
		var intamounttodisplay;
		if (totpics < 10) {
			intamounttodisplay = totpics;
		}
		else {
			intamounttodisplay = 10;
		}
		
		
		// need to take into account that the thumbnails will always request picnums 1-5!
		//If a thumb is clicked
		if (ctype == "thumb") {
			//Make all of the counters appear unselected
			for (i = 1; i <= intamounttodisplay; i++) {
				eval("document.getElementById('bottomnum" + i + "').style.color='#000000';");
				eval("document.getElementById('bottomTD" + i + "').style.backgroundColor='#ffffff';");
			}
			
			thepicnum = Number(thepicnum) + thumboffset;
			
			var strThumbString, strThumbString2;
			strThumbString2 = "<STRONG>" + thepicnum + "</STRONG>";

			for (i = 1; i <= intamounttodisplay; i++) {
				strThumbString = "bottomnum" + i;
				strThumbString = document.getElementById(strThumbString).innerHTML;

				if (strThumbString == strThumbString2) {			
					eval("document.getElementById('bottomnum" + i + "').style.color='#ffffff';");
					eval("document.getElementById('bottomTD" + i + "').style.backgroundColor='#990000';");
					intCurrentCounterPosition = i;
					break;
				}
			}
		}

		// take offset into account for thumbnail and bracket display
		ipicnum=picnum-thumboffset;
		// deselect all buttons for old pic and hide old pic 
		eval("document.getElementById('thumb"+ipicnum+"').style.borderColor='#ffffff';");
		eval("document.getElementById('LHbrack_tn"+ipicnum+"').src='"+spacerimage+"';");
		eval("document.getElementById('RHbrack_tn"+ipicnum+"').src='"+spacerimage+"';");
		
		// generate new picnum
		switch(action) {
			case "prev": picnum--; break;
			case "num": picnum=thepicnum; break;
			case "next": picnum++;
		}
		
		//Hide the 'prev' and 'next' buttons if necessary
		if (picnum==1) { 
			document.getElementById('prevbutton').style.visibility="hidden"; 
			document.getElementById('nextbutton').style.visibility="visible"; 
		}
		else {
			document.getElementById('prevbutton').style.visibility="visible"; 
			if (picnum==totpics) {
				document.getElementById('nextbutton').style.visibility="hidden";
			}
			else {
				document.getElementById('nextbutton').style.visibility="visible";
			}
		}
//////////////////////////////////////////////////////////////////////////////////
		//Work out what the bottom number should be
		if (picnum < 10) {
			intBottomCounterNumber = 1
		}
		else {
			intBottomCounterNumber = picnum - 9;
		}
		
		//Workout what the top number should be
		intTopCounterNumber = intBottomCounterNumber + 9;
		if (action == 'prev' && intCurrentCounterPosition == 1) {
			intTopCounterNumber--;
		}
		
		//Work out the offset
		if (picnum >= 10) {
			counteroffset = picnum - 10;
		}
		
		//Workout the current counter 1 to 10
		if (action == 'next') {
			intCurrentCounterPosition = picnum - counteroffset;
		}
		else if (action == 'prev' && intCurrentCounterPosition > 1) {
			intCurrentCounterPosition = intCurrentCounterPosition - 1;
		}
		
		//Reorder the counter numbers if current image is greater than 10
		if (action == 'next') {
			//Make all of the counters appear unselected
		
			for (i = 1; i <= intamounttodisplay; i++) {
				eval("document.getElementById('bottomnum" + i + "').style.color='#000000';");
				eval("document.getElementById('bottomTD" + i + "').style.backgroundColor='#ffffff';");
			}

			if (picnum > 10) {
				
				tmpCounter = intBottomCounterNumber;
				
				for (i = 1; i <= 10; i++) {
					eval("document.getElementById('bottomnum"+ i +"').innerHTML='<strong> " + tmpCounter + "</strong>';");
					tmpCounter++;
				}
			}
			//Make the current counter appear selected
			eval("document.getElementById('bottomnum" + intCurrentCounterPosition + "').style.color='#ffffff';");
			eval("document.getElementById('bottomTD" + intCurrentCounterPosition + "').style.backgroundColor='#990000';");
		}
		
		//
		if (action == 'prev') {
			
			//Make all of the counters appear unselected
			for (i = 1; i <= intamounttodisplay; i++) {
				eval("document.getElementById('bottomnum" + i + "').style.color='#000000';");
				eval("document.getElementById('bottomTD" + i + "').style.backgroundColor='#ffffff';");
			}

			if (intCurrentCounterPosition == 1 && picnum > 0) {
				tmpCounter = picnum;
				for (i = 1; i <= intamounttodisplay; i++) {
					eval("document.getElementById('bottomnum"+ i +"').innerHTML='<strong> " + tmpCounter + "</strong>';");
					tmpCounter++;
				}
			}

			//Make thew current counter appear selected
			eval("document.getElementById('bottomnum" + intCurrentCounterPosition + "').style.color='#ffffff';");
			eval("document.getElementById('bottomTD" + intCurrentCounterPosition + "').style.backgroundColor='#990000';");
		}
		
		//DEBUG
		//alert("Bottom: " + intBottomCounterNumber + " - Top: " + intTopCounterNumber + " - Current: " + intCurrentCounterPosition + " - Offset: " + counteroffset + " - Pic: " + picnum);
//////////////////////////////////////////////////////////////////////////////////	
		
		// repopulate thumb slideshowimages if new pic number is beyond current scope			
		if (Number(picnum)>thumbfin) {
			thumbfin=picnum;
			thumbstart=Number(picnum)-4;
			thumboffset=Number(thumbstart)-1;
			for (x=1;x<=totthumbs;x++) {
				selpic=x+thumboffset-1;
				selimage="thumbimage"+selpic+".src";
				eval("document.getElementById('thumb"+x+"').src="+selimage+";");
			}
		}
		else {
			if (Number(picnum)<thumbstart) {
				thumbstart=picnum;
				thumbfin=Number(picnum)+4;
				thumboffset=Number(thumbstart)-1;
				for (x=1;x<=totthumbs;x++) {
					selpic=x+thumboffset-1;
					selimage="thumbimage"+selpic+".src";
					eval("document.getElementById('thumb"+x+"').src="+selimage+";");
				}
			}
		}

		ipicnum=picnum-thumboffset;
		eval("document.getElementById('captiontext').innerHTML=caption"+picnum+";");
		selpic=picnum-1;
		selimage="mainimage"+selpic+".src";
		eval("document.getElementById('mainpic').src="+selimage+";");
		eval("document.getElementById('thumb"+ipicnum+"').style.borderColor='#333333';");
		eval("document.getElementById('LHbrack_tn"+ipicnum+"').src='images/slide-LHbracket.gif';");
		eval("document.getElementById('RHbrack_tn"+ipicnum+"').src='images/slide-RHbracket.gif';");
	}
}
//-------------------------------------------------------------------------------



//-------------------------------------------------------------------------------
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}} }
//-------------------------------------------------------------------------------
