﻿var inButton = false;
var inPanel = false;

var current = 1;

var timerHide;
var timerNext;

function selectOffer(dir) {
  clearTimeout(timerNext);
  timerNext = setTimeout("animateToNext()", 8000);
     
  current = current + dir;
  if(current > offerCount) current = 1;
  if(current < 1) current = offerCount;
  
  $("#tempImage").attr("src", $("#offer_image_" + current + " img").attr("src"));
  //$("#button img").attr("src", $("#offer_image_" + current + " img").attr("src"));
  $("#buttonPanel .padding").html($("#offer_content_" + current).html());
  $("#button img").fadeOut("fast", function() {
  
    $("#button img").attr("src", $("#offer_image_" + current + " img").attr("src"));
    $("#button img").show();
  
  });
}

$(document).ready(function () {

  $("#footer .container").css("display","none");
  $("#content .leftContent .padding").css("display","none");
  $("#content .rightContent .padding").css("display","none");
  $("#utilNavi .container").css("display","none");
  
  if(swfobject.getFlashPlayerVersion().major < 9) {
    setTimeout("animateIn(1)", 3000);
    setTimeout("animateIn(1)", 4000);
  }

  $("#button img").attr("src", $("#offer_image_" + current + " img").attr("src"));
  $("#buttonPanel .padding").html($("#offer_content_" + current).html());

  $("#button").hover(function () {
    
    $("#buttonPanel").slideDown("normal");
  
    inButton = true;
    inPanel = false;
    clearTimeout(timerNext);
   }, function() { 
    timerNext = setTimeout("animateToNext()", 8000);    
    inButton = false;
    timerHide = setTimeout("timeOutHide()", 50);
  });
  
  $("#buttonPanel").hover(function () {
    $("#buttonPanel").slideDown("normal");
    inButton = false;
    inPanel = true;  
  }, function() { 
    timerNext = setTimeout("animateToNext()", 8000);
    inPanel = false;
    timerHide = setTimeout("timeOutHide()", 50);
  });

});

function timeOutHide(e) {
  if(!inPanel && !inButton) $("#buttonPanel").slideUp("normal");
}

function animateIn(step) {
  if(step == 1) {
    $("#content .leftContent .padding").fadeIn("fast");
  } else if(step == 2) {
    $("#content .rightContent .padding").fadeIn("fast");
    $("#content .imageContent").fadeIn("fast");
    setTimeout("animateIn(3)", 500);
    timerNext =  setTimeout("animateToNext()", 8000);
  } else if(step == 3) {
    $("#utilNavi .container").fadeIn("fast");
    $("#footer .container").fadeIn("normal");
  }
}

function animateToNext() {
  if(!inPanel && !inButton) {
    selectOffer(1);
  }
}



