/**
 * buildImageDIVs
 * 
 * This function uses ajax via the Prototype.js library to call a php
 * page which grabs all the images and builds the div blocks for each image
 * to be used in the slideshow.
 * 
 * @author Dave
 */
function buildImageDIVs(imagePath,divID,firstImage,randomImage)
{
	//script that builds the html 
	var url = "/include/RotateCentreImage/buildImageDIVs.php?var1="+imagePath+"&var2="+divID+"&var3="+firstImage+"&var4="+randomImage;
	
	new Ajax.Request(url,
	{		
		method:'get',
		onSuccess: function(transport){
		//connect to server, execute the php page and get the response html
		 var response = transport.responseText || "no response text";
		 var theContent = $(divID);
		 //update the div block specified with the response html
		 theContent.update(response);	 
		
		},

		onFailure: function(){},
		onException: function(transport, exception) {

		/**
		Of course, this handler will now only restrict its activities to TypeError exceptions. All other errors will be ignored.
		The JavaScript 1.5 specification defines six primary error types, as follows:	
		EvalError - raised when the eval() functions is used in an incorrect manner;
		RangeError - raised when a numeric variable exceeds its allowed range;	
		ReferenceError - raised when an invalid reference is used;		
		SyntaxError - raised when a syntax error occurs while parsing JavaScript code;
		TypeError - raised when the type of a variable is not as expected;	
		URIError - raised when the encodeURI() or decodeURI() functions are used in an incorrect manner;
		excerpt from http://www.alise.lv/ALISE/technolog.nsf/0/53d693d7d1a4f198c2257066003d6153?OpenDocument
		**/

		var ename = exception.name;
		var emsg = exception.message;
		var theContent = $(divID);
		theContent.update("ERROR: "+ename+", "+emsg);
     	
   		}

	});
			
}//end buildImageDIVs
/**
 * rotateCentreImage2
 * 
 * This function rotates through a set of images on a web page. The images
 * must be layed out as a set of individual div blocks. This function will
 * iterate through the splashImg "area" or the rightImage "area". Contained
 * within that "area" will be the list of div blocks with an image each.
 * 
 * There are 3 parameters. The first is the size of the list of div blocks
 * (images). The second is the div ID that contains the images to be rotated
 * through. The third parameter is a flag that tells the function to not 
 * iterate through the images but rather just display a random image.
 * 
 * @param {Object} listSize
 * @param {Object} id
 * @param {Object} randomImageFlag
 */
function rotateCentreImage2(listSize,id,randomImageFlag){
	//used as part of the div id for each image. Each image has its own
	//unique id but they ALL start with either centre OR rightt.
	var picName = ''; 
	//test to determine which "area" this function is going to display
	//the images in.
	if (id == 'splashImg')
	{
		picName = 'centre';
	}//end if
	else if (id == 'rightImage')
	{
			picName = 'rightt';
	}//end else if
	//if I can't determine the id then set the random image flag to 1
	//and just display a random image because something is not right.
	else
	{
		randomImageFlag = 1;
	}//end else
	//only execute the slideshow if the picName is defined.
	if (picName != '') {
		//get the size of the list of images (divs)
		var size = listSize;
		//do we just pick a random image?
		if (randomImageFlag == 1) {
			//pick a random number 
			var randomnumber=Math.floor(Math.random()*size);
			//by default the first picture to display is the first one so set
			//that to not display.
			$('centre0').setStyle({display:'none'});
			//display the picture that has been randomly selected.			
			$(picName+randomnumber).setStyle({display:'block',opacity:1.0,filter:'alpha(opacity=100)'});			
		}//end if
		else {
			start_slideshow(0, size - 1, 5000, picName);
		}//end else.
	}//end if

}
function start_slideshow(start_frame, end_frame, delay,picName)
{

    setTimeout(switch_slides(start_frame,start_frame,end_frame, delay,picName), delay);
}
function switch_slides(frame,start_frame,end_frame,delay,picName)
{
    return (function()
    {
        Effect.Fade(picName + frame,{duration:2.0,from:1.0,to:0.0});
        if (frame == end_frame) { frame = start_frame; } else { frame = frame + 1; }
		Effect.Appear(picName + frame,{duration:2.0,from:0.0,to:1.0});
        setTimeout(switch_slides(frame, start_frame, end_frame, delay,picName), delay + 5000);    
    })
}
