var image_array_index = 0;
var image_array;
var rotate_one_image;
var image_rotation_speed = 5000;  //in milliseconds
var interval_id; //interval id for rotating images

//image reference names in the html
var rotatingImage="rotatingImage"
var rotatingImageTop="rotatingImageTop"
var rotatingImageBottom="rotatingImageBottom"

//stops the auto rotate and selects the previous image(s) - called by the user from the html page
function previous(){
	clearInterval(interval_id); //stops the auto rotate of the images
	previousImage();
}


//selects the previous image(s)
function previousImage(){
    if(rotate_one_image){
        //rotate one image
        if(image_array_index > 0){
            image_array_index--;
        }else{
            image_array_index=image_array.length-1;
        }
        change_image(rotatingImage,image_array_index);  
    }else{
        //rotate multiple images
        if(image_array_index > 0){
            image_array_index=image_array_index-2;
        }else{
            image_array_index=image_array.length-2;
        }
        change_image(rotatingImageTop,image_array_index);
        change_image(rotatingImageBottom,image_array_index+1);
    }
}


//stops the auto rotate and selects the next image(s) - called by the user from the html page
function next(){
	clearInterval(interval_id); //stops the auto rotate of the images
	nextImage();
}


//selects the next image(s)
function nextImage(){

    if(rotate_one_image){
        //rotate one image
        if(image_array_index < image_array.length-1){
            image_array_index++;
        }else{
            image_array_index=0;
        }
        change_image(rotatingImage,image_array_index);  
    }else{
        //rotate multiple images
        if(image_array_index+1 < image_array.length-1){
            image_array_index=image_array_index+2;
        }else{
            image_array_index=0;
        }
        change_image(rotatingImageTop,image_array_index);
        change_image(rotatingImageBottom,image_array_index+1);
    }

}

//changes the image
function change_image(imageName,image_array_index){
    $("#"+imageName).attr("src", image_array[image_array_index].src);
}


//used on pages where one image is displayed and rotated
function init_rotate_one_image(){
    preload_images();
    rotate_one_image=true;
    autoRotate();
}


//used on pages where two images are displayed and rotated
function init_rotate_two_images(){
    preload_images();
    rotate_one_image=false;
    autoRotate();
}


//preload all images when page loads
function preload_images(){

   image_array = new Array(image_count);
   if(document.images){    
        for (x=0; x<image_array.length; x++){
            var image_number=x+1;
            image_array[x]=new Image();
            image_array[x].src=image_path+image_number+".jpg";            
        }
    }

}

//rotate image based on image_rotation_speed
function autoRotate(){
	interval_id = setInterval("nextImage()",image_rotation_speed); 
}

