var sliderInitialised = new Array;
var sliderLeft = new Array;
var sliderTargetLeft = new Array;
var sliderInterval = new Array;
var sliderWidth = new Array;
var sliderChange = new Array;
var sliderSpeed = new Array;
var sliderDiv = new Array;
var sliderDivCount = new Array;
var sliderAuto = new Array;
var sliderAutoTimeout = new Array;

function sliderGetObject( id )
{
  if ( document.getElementById )
    return document.getElementById( id );
  else if( document.layers )
    return document.layers[id];
  else if ( document.all )
    return document.all[id];

  return null;
}
function sliderSetAutoTimeout( id )
{
  if ( sliderAuto[id] > 0 )
  {
    clearTimeout( sliderAutoTimeout[id] );
    sliderAutoTimeout[id] = setTimeout( "sliderStart( true, '" + id + "' )", sliderAuto[id] * 1000 );
  }
}
function sliderInit( id, speed, auto )
{
  try
  {
    var i = 0;

    if ( typeof( auto ) == "undefined" )
      sliderAuto[id] = -1;
    else
      sliderAuto[id] = auto;

    // store the speed and div elements (identified by id)
    sliderSpeed[id] = speed;
    sliderDiv[id] = sliderGetObject( id );

    if ( sliderDiv[id] == null )
      return;

    // get the width of the slider div
    sliderWidth[id] = sliderDiv[id].offsetWidth;

    // set the initial left position to zero
    sliderLeft[id] = 0;//-975;

    // set the div count to zero (ready to count the number of divs)
    sliderDivCount[id] = 0;

    // loop through the slider div child nodes to find all the divs contained within
    for ( i in sliderDiv[id].childNodes )
    {
      // check if the node is a div
      if ( sliderDiv[id].childNodes[i].tagName == "DIV" )
      {
        // define the style of the current div so that it placed to the right of the
        // previous div (or left = 0 for the first div)
        sliderDiv[id].childNodes[i].style.position = "absolute";
        sliderDiv[id].childNodes[i].style.height   = "100%";
        sliderDiv[id].childNodes[i].style.width    = "100%";
        sliderDiv[id].childNodes[i].style.left     = sliderLeft[id] + "px";

        // increase the div count
        sliderDivCount[id]++;

        // increase the left position (used to set the left position of the next div)
        // by the width of the slider div
        sliderLeft[id] += sliderWidth[id];
      }
    }

    // set the overflow style of the slider div to visible (this should be set to hidden in
    // the html/css file so that only the first div within the slider div is visible)
    sliderDiv[id].style.overflow = "visible";

    // set the initial left and target left positions to 0 (first div within the slider div)
    sliderTargetLeft[id] = sliderLeft[id] = -970;

    // mark this slider as initialised
    sliderInitialised[id] = true;

    sliderSetAutoTimeout( id );
  }
  catch ( ex )
  {
    alert( ex );
  }
}
function sliderAnimate( forward, id )
{
  sliderSetAutoTimeout( id );
  
  if ( forward ) // the forward/next button was clicked
  {
    // move the sliding div left by the defined amount
    sliderLeft[id] -= sliderChange[id];

    // check to see if we have reached the target left position (if so set the
    // left position incase we have passed it)
    if ( sliderLeft[id] < sliderTargetLeft[id] )
      sliderLeft[id] = sliderTargetLeft[id];

    // update the slider div left to the new value
    sliderDiv[id].style.left = sliderLeft[id] + "px";

    // clear the interval if we have reached the target left position
    if ( sliderLeft[id] <= sliderTargetLeft[id] )
      clearInterval( sliderInterval[id] );
  }
  else // the backward/previous button was clicked
  {
    // move the sliding div right by the defined amount
    sliderLeft[id] += sliderChange[id];

    // check to see if we have reached the target left position (if so set the
    // left position incase we have passed it)
    if ( sliderLeft[id] > sliderTargetLeft[id] )
      sliderLeft[id] = sliderTargetLeft[id];

    // update the slider div left to the new value
    sliderDiv[id].style.left  = sliderLeft[id] + "px";

    // clear the interval if we have reached the target left position
    if ( sliderLeft[id] >= sliderTargetLeft[id] )
      clearInterval( sliderInterval[id] );
  }
}
function sliderStart( forward, id )
{
  if ( sliderInitialised[id] == null )
  {
    // the slider has not been initialised yet so attempt to delay the calling of the function
    // until the slider has been initialised
    setTimeout( "sliderStart( " +
      ( forward ? "true" : "false" ) + ", '" + id + "' )", 50 );

    return;
  }

  // calculate the distance the sliding div will more each time sliderAnimate is called
  sliderChange[id] = sliderWidth[id] * 0.03;

  if ( forward ) // the forward/next button was clicked
  {
    // set the target left position of the sliding div (sliderAnimate will move the sliding div
    // to this position) so that it moves left
    sliderTargetLeft[id] = sliderTargetLeft[id] - sliderWidth[id];

    // check the target left position doesn't exceeed the width of the sliding div
    if ( sliderTargetLeft[id] < -sliderWidth[id] * ( sliderDivCount[id] - 1 ) )
    {
      // target left position exceeeds the width of the sliding div so we need to
      // scroll back around to the start (i.e. head in the opposite direction)
      sliderDiv[id].style.left = "0px";
      sliderTargetLeft[id] = -sliderWidth[id];
      sliderLeft[id] = 0;
    }
  }
  else // the backward/previous button was clicked
  {
    // set the target left position of the div (sliderAnimate will move the sliding div
    // to this position) so that it moves right
    sliderTargetLeft[id] = sliderTargetLeft[id] + sliderWidth[id];

    // check the target left position doesn't exceeed the start of the sliding div
    if ( sliderTargetLeft[id] > 0 )
    {
      // target left position exceeeds the width of the sliding div so we need to
      // scroll back around to the start (i.e. head in the opposite direction)
      sliderTargetLeft[id] = -sliderWidth[id] * ( sliderDivCount[id] - 2 );
      sliderLeft[id] = -sliderWidth[id] * ( sliderDivCount[id] - 1 );
      sliderDiv[id].style.left = sliderLeft[id] + "px";

      // increase the distance the sliding div will more each time sliderAnimate is called
    }
  }

  // clear any previous intervals to eliminate weird behaviour which could occur if
  // the user clicks on the button again before the scrolling has finished
  clearInterval( sliderInterval[id] );

  // start the interval to slide the sliding div to the new target left position
  sliderInterval[id] = setInterval( "sliderAnimate( " +
    ( forward ? "true" : "false" ) + ", '" + id + "' )", sliderSpeed[id] );
}
