/**
 * 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.
 *
 * need txtPath which is the location of the image txt file.
 * div block to publish the file to.
 * @author Dave
 */
function buildImageDIVs(textPath,div)
{
        //this function is automatically called in the new design templates.
        //It is possible that users will not use a slideshow in their landing
        //pages thus we need to make sure that the parameters passed contain
        //valid data, otherwise we simply do nothing.
        //var test = "1";
        if(textPath != "" && div != "")
        //if(test == 0)
        {
        //id of the containing div block
	var divID = div;
	//script that builds the html 
	var url = "/include/MainPageImageRotate_v2.php";
	
	new Ajax.Request(url,
	{		
		method:'get',
                parameters: {
			'path': textPath
                },
		onSuccess: function(transport){
		//connect to server, execute the php page and get the response html
		 var response = transport.responseText || "no response text";
		 //update the div block specified with the response html
                 //alert(response);
                 //only update the div block if we get valid data back.
                 //The Reddot container is configured to display a default image
                 //if nothing comes back or the params are not valid (i.e empty)
                 if(response != null && response != '0')
                 {
                    $(divID).update(response);
                 }//end if
		
		},

		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;

                //NB: I have decided not to catch an exception as I have indicated above, but I have
                //left those comments in so that I or someone else could do so in the future. Instead if
                //there is a problem getting data from the server I will just display a default img.
                var defaultImg = '<div><img src="images/universal/homepage_1.jpg"></img></div>';
		$(divID).update(defaultImg);
     	
   		}

	});

        }//end if

}//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 "area" defined in the variable ID. Contained
 * within that "area" will be the list of div blocks with content in 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 content to be rotated
 * through. 
 * 
 * @param {Object} listSize
 * @param {Object} id
 * 
 */
function rotateCentreImage2(listSize,id){
	//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 = id;
	
	//only execute the slideshow if the picName is defined.	
	if (picName != '') {
                //Effect.Fade('firstImage',{duration:2.0,from:1.0,to:0.0});
                
		//get the size of the list of images (divs)
		var size = listSize;
		start_slideshow(0, size - 1, 3000, picName);
		
	}//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:4.0,from:1.0,to:0.0});
        if (frame == end_frame)
        { 
        	frame = start_frame;
        }
        else
        {
        	frame = frame + 1;
        }
		Effect.Appear(picName + frame,{duration:4.0,from:0.0,to:1.0});
        setTimeout(switch_slides(frame, start_frame, end_frame, delay,picName), delay + 3000);    
    })
}
