/* script for rotating images */

/* author: Borislav Gizdov <borislav.gizdov@gmail.com> */

var TimeToFade = 1000.0;
var picId = 1; // first image
var isPaused = false;
var isLoaded = false;

// Usee this function to run image rotation, delay is in miliseconds
function startRotator(delay) {
    setInterval("rotate()", delay);
}

function loaded() {
    isLoaded = true;
}

addLoadEvent(loaded);


function rotatePause() {
    isPaused = true;
}

function rotateContinue() {
    isPaused = false;
}

function setTopSection(id) {
    document.getElementById('sectionTop').className = 'top' + id;
}

// Main function which rotate images
function rotate() {
    
    if (isPaused || !isLoaded) return;
    fade('overlay');
    if( document.getElementById('overlay') != null)
    document.getElementById('overlay').className ='top' + picId;
    setTimeout('setTopSection(' + picId +')', 900);
    highlightButton(picId);

    picId++;
    if (picId > 4) picId = 1;
}

function highlightButton(id) {
    document.getElementById('top1').style.backgroundPosition = '1 0px'; 
    document.getElementById('top2').style.backgroundPosition = '0 -65px';
    document.getElementById('top3').style.backgroundPosition = '0 -129px';
    document.getElementById('top4').style.backgroundPosition = '0 -193px';

    // highlight current button
    if (id == 1) {
        document.getElementById('top1').style.backgroundPosition = '200px 0px';
    } else if (id == 2) {
        document.getElementById('top2').style.backgroundPosition = '200px -65px';
    } else if (id == 3) {
        document.getElementById('top3').style.backgroundPosition = '200px -129px';
    } else if (id == 4) {
        document.getElementById('top4').style.backgroundPosition = '200px -193px';
    }

}
function hoverButton(el) {
   
    document.getElementById('top1').style.backgroundPosition = '1 0px'; 
    document.getElementById('top2').style.backgroundPosition = '0 -65px';
    document.getElementById('top3').style.backgroundPosition = '0 -129px';
    document.getElementById('top4').style.backgroundPosition = '0 -193px';
    
    if (el == 'top1')
     document.getElementById('top1').style.backgroundPosition = '200px 0px';
    if (el == 'top2')
   document.getElementById('top2').style.backgroundPosition = '200px -65px';
    if (el == 'top3')
    document.getElementById('top3').style.backgroundPosition = '200px -129px';
    if (el == 'top4')
     document.getElementById('top4').style.backgroundPosition = '200px -193px';

}





// Fade effect function when switching images
function fade(eid)
{
  var element = document.getElementById(eid);
  if(element == null)
    return;
   
  if(element.FadeState == null)
  {
    if(element.style.opacity == null
        || element.style.opacity == ''
        || element.style.opacity == '1')
    {
      element.FadeState = 2;
    }
    else
    {
      element.FadeState = -2;
    }
  }
   
  if(element.FadeState == 1 || element.FadeState == -1)
  {
    element.FadeState = element.FadeState == 1 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
  }
  else
  {
    element.FadeState = element.FadeState == 2 ? -1 : 1;
    element.FadeTimeLeft = TimeToFade;
    setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
  }  
}

function animateFade(lastTick, eid)
{  
  var curTick = new Date().getTime();
  var elapsedTicks = curTick - lastTick;
 
  var element = document.getElementById(eid);
 
  if(element.FadeTimeLeft <= elapsedTicks)
  {
    element.FadeState = 2;
    element.style.opacity = element.FadeState == 1 ? '1' : '0';
    element.style.filter = 'alpha(opacity = '
        + (element.FadeState == 1 ? '100' : '0') + ')';
    element.FadeState = element.FadeState == 1 ? 2 : -2;
    return;
  }
 
  element.FadeTimeLeft -= elapsedTicks;
  var newOpVal = element.FadeTimeLeft/TimeToFade;
  
  if(element.FadeState == 1)
    newOpVal = 1 - newOpVal;

  element.style.opacity = newOpVal;
  element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
 
  setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}