/* V-Quad.com home page navigation area JavaScript.
 *
 * Copyright 2007 Spenlen Media (http://spenlen.com)
 */

var homeNavAnimationsOut  = [];
var homeNavAnimationIndex = 0;
var homeNavAnimator       = null;

function homeNavInitAnimation ()
{
  homeNavAnimationsOut[0] = new Fx.Style($('home-nav-background-1'), 'opacity', {duration: 333, fps: 30, transition: Fx.Transitions.quadIn});
  homeNavAnimationsOut[1] = new Fx.Style($('home-nav-background-2'), 'opacity', {duration: 333, fps: 30, transition: Fx.Transitions.quadIn});
  homeNavAnimationsOut[2] = new Fx.Style($('home-nav-background-3'), 'opacity', {duration: 333, fps: 30, transition: Fx.Transitions.quadIn});
  homeNavAnimationsOut[3] = new Fx.Style($('home-nav-background-4'), 'opacity', {duration: 333, fps: 30, transition: Fx.Transitions.quadIn});

  Event.observe($('home-nav-link-1'), 'mouseover', homeNavOnMouseOverHandler.bindAsEventListener($('home-nav-link-1')));
  Event.observe($('home-nav-link-2'), 'mouseover', homeNavOnMouseOverHandler.bindAsEventListener($('home-nav-link-2')));
  Event.observe($('home-nav-link-3'), 'mouseover', homeNavOnMouseOverHandler.bindAsEventListener($('home-nav-link-3')));
  Event.observe($('home-nav-link-4'), 'mouseover', homeNavOnMouseOverHandler.bindAsEventListener($('home-nav-link-4')));

  Event.observe($('home-nav-link-1'), 'mouseout', homeNavOnMouseOutHandler.bindAsEventListener($('home-nav-link-1')));
  Event.observe($('home-nav-link-2'), 'mouseout', homeNavOnMouseOutHandler.bindAsEventListener($('home-nav-link-2')));
  Event.observe($('home-nav-link-3'), 'mouseout', homeNavOnMouseOutHandler.bindAsEventListener($('home-nav-link-3')));
  Event.observe($('home-nav-link-4'), 'mouseout', homeNavOnMouseOutHandler.bindAsEventListener($('home-nav-link-4')));

  homeNavAnimator = new PeriodicalExecuter(homeNavDoAnimation, 5);
}

function homeNavDoAnimation ()
{
  var toIndex = homeNavAnimationIndex + 1;
  if (toIndex >= homeNavAnimationsOut.length) {
    toIndex = 0;
  }
  homeNavTransitionTo(toIndex);
}

function homeNavOnMouseOverHandler()
{
  if (homeNavAnimator) {
    homeNavAnimator.stop();
    homeNavAnimator = null;
  }
  var toIndex = parseInt(this.id.slice(-1), 10) - 1;
  homeNavTransitionTo(toIndex);
}

function homeNavOnMouseOutHandler()
{
  if (homeNavAnimator) {
    homeNavAnimator.stop();
  }
  homeNavAnimator = new PeriodicalExecuter(homeNavDoAnimation, 5);
}

function homeNavTransitionTo(toIndex)
{
  if (homeNavAnimationIndex === toIndex) {
    return;
  }
  
  $('home-nav-background-' + (homeNavAnimationIndex + 1)).setStyle({'z-index': 5});
  
  $('home-nav-background-' + (toIndex + 1)).setStyle({'z-index': 0});
  $('home-nav-background-' + (toIndex + 1)).setStyle({opacity: 1});
  $('home-nav-background-' + (toIndex + 1)).setStyle({visibility: 'visible'});
  
  homeNavAnimationsOut[homeNavAnimationIndex]._start(1, 0);

  homeNavAnimationIndex = toIndex;
}

Event.observe(window, 'load', function()
{
  homeNavInitAnimation();
}
);

