/*
imagerotation.js
*/

///////////////////////////////////////////////
// CONFIGURABLE PARAMETERS

// this variable defines the 
// interval between image 
// rotations in seconds
var rotation_interval = 6;

// these variables define the html
// id of the anchor and image tags
// which comprise rotating system
var ID_ROTLINK = 'rotating_link';
var ID_ROTIMAGE = 'rotating_image';

// END OF CONFIGURABLE PARAMETERS
///////////////////////////////////////////////




// declare and initialize
// the index indicating which
// image is to be displayed
var rotation_index = 0;

// the locations and link configurations of
// all images in the slide show are defined
// external but we will prepare an array of
// objects to make external definition as
// simple as possible for administrator
var slide = new Array();
for (var i=0; i<51; i++)
  slide[i] = new Object();

// this variable is used internally
// to represent a condensed and ordered
// list of all externally defined images 
var image_list = new Array();




function rotate_forward()
{
  // this function will advance the proper document
  // elements to display the next image in the show
  // then call the looping mechanism to advance again
  // first retrieve the elements from the host page
  var img = document.getElementById(ID_ROTIMAGE);
  var a = document.getElementById(ID_ROTLINK);
  
  // increment the index
  // so we will display
  // the next picture
  rotation_index++;
  
  // perform validation on the incremented index ensuring it is positive
  // and properly looping to the first image if we have reached the end
  if ((rotation_index >= image_list.length) || (rotation_index < 0))
    rotation_index = 0;
  
  // now set the document elements to display
  // the next image in the slide show rotation
  img.src = image_list[rotation_index].src;
  if (image_list[rotation_index].link)
      a.href = image_list[rotation_index].link;
  else
    {
      // for an undefined or empty link
      // we will disable the anchor
      a.removeAttribute('href');
    }
    
  // when the target attribute of the link is 
  // specifically provided use the value otherwise
  // set a blank target to open in new window
  if (image_list[rotation_index].target)
    a.target = image_list[rotation_index].target;
  else a.target = '_blank';
  
  
  // call the same function again after the proper interval
  // to continue the slide show effect and display next image
  setTimeout("rotate_forward()", (rotation_interval*1000));
}




function imagerotation()
{
  // this function is designed to be called after the host
  // page has been loaded via the onload property most likely
  // it is the main application function and will perform 
  // necessary setup tasks and start the images rotating  
  
  // first determine if there are images
  // defined and dont proceed without them
  if (!(slide instanceof Array)) return;
  
  // re-initialize the image list
  // just be sure we have empty
  // array before we proceed
  image_list = new Array();
  var j = 0;
  
  // now filter only defined images
  // loop through all objects in the 
  // slide array and copy those with 
  // a definition to the image list
  for (var i=0; i<slide.length; i++)
    if (slide[i].src)
    {
      // create a object and set the
      // properties as defined in the
      // external slide array object
      image_list[j] = new Object();
      image_list[j].src = slide[i].src;
      image_list[j].link = slide[i].link;
      image_list[j].target = slide[i].target;
      j++;
    }
  
  // use the standard technique
  // for preloading all images 
  // that are a part of show
  var d = document;
  if (d.images)
  {
    if (!(d.ROT_imgs instanceof Array)) d.ROT_imgs = new Array();
    var j = d.ROT_imgs.length;
    for (var i=0; i<image_list.length; i++)
    {
      document.ROT_imgs[j] = new Image();
      document.ROT_imgs[j].src = image_list[i].src;
      j++;
    }
  }
  
  // start the show with the 
  // first image by setting
  // the index to invalid 
  // value and call the 
  // rotation mechansim
  rotation_index = -1;
  rotate_forward();
}