var currentIcon = 0;
var previousIcon = 0;
var repeater;
var size;

$(document).ready(function() {

  $("#thumb-1 .rollover").css("visibility", "visible");
  $("#thumb-1").css("opacity", "1.0");

  $("#slider > .thumb").click( function() {
    showSlide($("#slider > .thumb").index(this));
  });

  $("#slides .close").click( function() {
    $("#slides .banner").fadeTo("fast", 0);
  });

  $("#thumbs").mouseenter(function(){
    repeater.stop();
  }).mouseleave(function() {
    repeater.reset(6000);
  });

  $("#slider > .thumb").mouseenter(function(){
    $(this).fadeTo("fast", 1);
  }).mouseleave(function() {
    if($("#slider > .thumb").index(this) != currentIcon) {
      $(this).fadeTo("fast", 0.65);
    }
  });
  
  $("#thumbs").mousemove(function(e) {
    var offset = $(this).offset();
    var y = e.pageY - offset.top;
    var h = $("#slider").height();
    $("#slider").css("top", (h - 450) * y / -450);
  });  
  
});

function showSlide(index) {    
  previousIcon = currentIcon;
  currentIcon = index;

  $("#thumb-" + (previousIcon + 1) + "> .rollover").css("visibility", "hidden");
  $("#thumb-" + (currentIcon + 1) + "> .rollover").css("visibility", "visible");
  $("#thumb-" + (previousIcon + 1)).fadeTo("fast", 0.65);
  $("#thumb-" + (currentIcon + 1)).fadeTo("fast", 1);
  
  if (currentIcon != previousIcon) {
    $("#slide-" + (previousIcon + 1)).fadeOut(900);
    $("#slide-" + (currentIcon + 1)).fadeIn(900);
  }
}

function nextSlide(size) {
  var index = 0;
  
  if(currentIcon < size - 1) {
    index = currentIcon + 1;
  } else {
    index = 0;
  }

  var h = $("#slider").height();
  var move = -1 * (h - 450) * (index / size);
  $("#slider").animate({top: "" + move + "px"}, "slow");

  showSlide(index);
}

function closeIntro() {
  $('#flash-container').hide();
  $('body').css('overflow', 'auto');
  size = $("#slider > .thumb").length;
  repeater = jQuery.timer(6000, function(timer) { nextSlide(size); });
}



