// JavaScript Document
// Tested on Firefox 3.6, IE 6/7, Safari 4, Chrome
// Version: 10.05.0526

$hlSelected=new Array();
$hlAutoTimer=new Array();
$hlAnimSlideTimer=new Array();
$hlNextZIndex=new Array();

$hlRestartDelay=45000;
$hlFlipDelay=5000;
$maxZIndex=100000;


//auto calculate steps to slide in within 1 second
$maxLeft=640;
$sliderStepDelay=30;
$slidingTime=1000;
$maxFrames=$slidingTime/$sliderStepDelay;
$sliderStepSize=($maxLeft / $maxFrames) * -1;
$sliderMaxLeft=$maxLeft + 'px';







function hlResetZIndex($boxId,$lastEle)
{
  $pg=1;
  $oldSeq='';
  while (document.getElementById($boxId + '_pg_' + $pg)) {
    $ele=document.getElementById($boxId + '_pg_' + $pg);
    $oldSeq=$oldSeq + $ele.id + 'o: ' + $ele.style.zIndex + ', n: ' +$pg + '\n';
    $ele.style.zIndex=$pg;
    $pg++;
  }
  
  //alert($oldSeq);
  //alert($oStr + 'Reset done');
  $lastEle.style.zIndex=$pg;
  
  return $pg+1;
}




function hlAnimSlide($boxId, $pg, $autoFlip)
{

  if ($hlSelected[$boxId]) {

    $pageId=$hlSelected[$boxId];
    $lastEle=document.getElementById($boxId + '_' + $pageId);
    $lastEle.style.display='block';
    $lastEle.style.left='0px';
    $lastLink=document.getElementById($boxId + '_pg_' + $pageId);
    $lastLink.className='';
    clearInterval($hlAnimSlideTimer[$lastEle.id]);
    //alert($pg + ' in, previous was ' + $lastEle.id);
  } else {
    $hlNextZIndex[$boxId]=new Array; //create zIndex array
    $hlNextZIndex[$boxId]['max']=0;
  }

  $showEle=document.getElementById($boxId + '_' + $pg);
  
    $hlNextZIndex[$boxId]++;
    
  if ($hlNextZIndex[$boxId]>$maxZIndex) {
    $hlNextZIndex[$boxId]=hlResetZIndex($boxId, $lastEle);
    window.status='ZIndex Reset to ' + $hlNextZIndex[$boxId];
  }
  

  $showEle.style.zIndex=$hlNextZIndex[$boxId];

  


  //alert('Showing ' + $showEle.id + ', current: ' + $showEle.style.zIndex + ', saved: ' + $hlZIndex[$boxId][$showEle.id] + ', max: ' + $hlZIndex[$boxId]['max']);
  
  $showEle.style.display='block';
  $showEle.style.left=$sliderMaxLeft;

  $activeLink=document.getElementById($boxId + '_pg_' + $pg);
  $activeLink.className='active';

  $hlSelected[$boxId]=$pg;
  $intervalStr="hlAnimSlideLoop('" + $showEle.id + "', " + $sliderStepSize + ", 0)";

  $hlAnimSlideTimer[$showEle.id]=setInterval($intervalStr, $sliderStepDelay);



  if ($autoFlip) {
    $nextPage=$pg+1;
    $nextEle=document.getElementById($boxId + '_' + $nextPage);

    if (!$nextEle) {
      $nextPage=1;
      $nextEle=document.getElementById($boxId + '_' + $nextPage);
    }

    if ($nextEle) {
      $timeOutStr="hlShow('" + $boxId + "', " + $nextPage + ",1, 1)";
      //alert('starting autoflip using ' + $timeOutStr + ' to next ele: ' + $nextEle);

      $hlAutoTimer[$boxId]=setTimeout($timeOutStr, $hlFlipDelay);
    }

  } else {
    clearTimeout($hlAutoTimer[$boxId]);
    $timeOutStr="hlShow('" + $boxId + "', " + $pg + ",0 ,1)";
    $hlAutoTimer[$boxId]=setTimeout($timeOutStr, $hlRestartDelay); //restart flipping after 45 sec
  }

}




function hlAnimSlideLoop($eleId, $xStep, $xLimit)
{
  $element=document.getElementById($eleId);
  $nextX=parseInt($element.style.left) + $xStep;
  //alert("Stepping  " + $eleId + " to " + $nextX + ' in ' + $xStep + ' pixels');
  
  if (($xStep<0 && $nextX<=$xLimit) ||
    ($xStep>0 && $nextX>=$xLimit))  {
    $nextX=$xLimit;
    clearInterval($hlAnimSlideTimer[$eleId]);
  }

  $element.style.left=$nextX + 'px';
}




function hlAnimAppear($boxId, $pg, $autoFlip)
{

  if ($hlSelected[$boxId]) {
    $lastEle=document.getElementById($boxId + '_' + $hlSelected[$boxId]);
    $lastEle.style.display='none';
    $lastEle.style.zIndex=1;

    $lastLink=document.getElementById($boxId + '_pg_' + $hlSelected[$boxId]);
    $lastLink.className='';
  }

  $showEle=document.getElementById($boxId + '_' + $pg);
  $showEle.style.zIndex=2;
  $showEle.style.display='block';

  $activeLink=document.getElementById($boxId + '_pg_' + $pg);
  $activeLink.className='active';

  $hlSelected[$boxId]=$pg;

  if ($autoFlip) {
    $nextPage=$pg+1;
    $nextEle=document.getElementById($boxId + '_' + $nextPage);

    if (!$nextEle) {
      $nextPage=1;
      $nextEle=document.getElementById($boxId + '_' + $nextPage);
    }

    if ($nextEle) {
      $timeOutStr="hlShow('" + $boxId + "', " + $nextPage + ",1)";
      //alert('starting autoflip using ' + $timeOutStr + ' to next ele: ' + $nextEle);

      $hlAutoTimer[$boxId]=setTimeout($timeOutStr, $hlFlipDelay);
    }

  } else {
    clearTimeout($hlAutoTimer[$boxId]);
    $timeOutStr="hlShow('" + $boxId + "', " + $pg + ",1)";
    $hlAutoTimer[$boxId]=setTimeout($timeOutStr, $hlRestartDelay); //restart flipping after 45 sec
  }

}



function hlShow($boxId, $pg, $autoFlip, $animationStyle)
{
  if ($pg!=$hlSelected[$boxId]) {
    switch ($animationStyle)
    {
      case 1 :
        hlAnimSlide($boxId, $pg, $autoFlip);
        break;

      default :
        hlAnimAppear($boxId, $pg, $autoFlip);
    }
    
  }
  
  return false;
}