var firstSplash=true;
var currentSplash=-1;
var maxSplash=0;
var splashTransitionTime=7000;
var splashFadeInTime=700;
var splashTimer=null;

$(document).ready(function() {
  
  /* Splash Rotator Setup */
  $('#splash-nav li').append('<span class="progress-bar">&nbsp;</span>');
  $('#splash-nav li a img').not(':first').fadeTo(0,0.3);
  
  $('#splash-nav li').hover(
    function() {
      if(!$(this).hasClass('selected'))
        $('a img',this).stop().fadeTo(300,1);
    },
    function() {
      if(!$(this).hasClass('selected'))
        $('a img',this).stop().fadeTo(300,0.3);
    }
  );
  
  $('#splash-nav li a').each(function(i,row) {
    $(row).click(function() {
      gotoSplash(i);
      return false;
    });
  });
  
  /* Go go go go! */
  maxSplash=$('#splash li').length;
  nextSplash();
  
});

function nextSplash() {
  var prev_nav=$('#splash-nav li.selected');
  if($(prev_nav).length>0) {
    $('a img',prev_nav).stop().fadeTo(300,0.3);
    $('.progress-bar',prev_nav).stop().css('width',0);
    $(prev_nav).removeClass('selected');
  }
  
  var prev_splash=$('#splash li.selected');
  $('#splash li').not('.selected').css('z-index',90); //ensure none overlap
  if($(prev_splash).length>0)
    $(prev_splash).removeClass('selected').css('z-index',91);
  
  currentSplash+=1;
  if(currentSplash>=maxSplash)
    currentSplash=0;
  
  var next_nav=$('#splash-nav li:eq('+currentSplash+')');
  $(next_nav).addClass('selected');
  $('a img',next_nav).stop().fadeTo(300,1);
  $('.progress-bar',next_nav).stop().animate({width:['100%','linear']},splashTransitionTime);
  
  var next_splash=$('#splash li:eq('+currentSplash+')');
  if(firstSplash)
    $(next_splash).addClass('selected').css('z-index',92).fadeTo(splashFadeInTime,1);
  else
    $(next_splash).addClass('selected').stop().fadeTo(0,0).css('z-index',92).fadeTo(splashFadeInTime,1);
  firstSplash=false;
  
  splashTimer=setTimeout('nextSplash()',splashTransitionTime);
}

function gotoSplash(i) {
  if(currentSplash!=i) {
    clearTimeout(splashTimer);
    currentSplash=i-1;
    if(i<0)
      i=maxSplash;
    if(i>maxSplash)
      i=0;
    nextSplash();
  }
  return false;
}
