var bannerChanger = {
  animateSpeed: 600,
  bannerRevert: 1500,
  timer: null,
  init: function() {
	$('#banner, #topLeft')
	  .mouseenter(function() {
        bannerChanger.resetTimer();
      })
      .mouseleave(function() {
        bannerChanger.timer = window.setTimeout('bannerChanger.showBanner()', bannerChanger.bannerRevert);
      });
	$('#banner').append('<div id="bottomBanner" class="ui-corner-all" /><div id="topBanner" class="ui-corner-all" />');
	$('#topBanner').click(function(event) {
	  event.preventDefault();
	  window.location = $(this).attr('rel');
	});
    $('ul.homepageMenu li.tab a').hover(function() {
      bannerChanger.switchScene(this);
    }, function() {});
    this.showBanner();
  },
  resetTimer: function() {
    if (bannerChanger.timer) {
      window.clearTimeout(bannerChanger.timer);
      bannerChanger.timer = null;
    }
  },
  showBanner: function() {
    this.resetTimer();
    this.switchScene($('ul.homepageMenu li.banner:first a'));
  },
  switchScene: function(element) {
    var bgNew = $(element).attr('rel'),
      url = $(element).attr('href'),
      fadeBg = $('#topBanner').css('background-image');
    $('#bottomBanner')
      .css({
        'background-image': fadeBg,
        opacity: 1
      })
      .show();
    $('#topBanner')
      .hide()
      .attr('rel', url)
      .css({
        'background-image': "url('" + bgNew + "')",
        opacity: 0
      })
      .show()
      .stop()
      .animate({ opacity: 1 }, this.animateSpeed);
  }
};

$(function() {
  function blinkNextButton () {
  $('#nextButton a')
    .delay(400)
    .fadeTo(100, 0.7)
    .delay(100)
    .fadeTo(200, 1, function() { blinkNextButton(); });
  }
  $('a.actionSignIn').click(function() {
    $('#signIn').submit();
  });
  $('a.showHiddenContent').each(function() {
    $(this).click(function(event) {
      event.preventDefault();
      var anchor = $(this).attr('href').split('#')[1];
      hiddenContent = $(this).parent(':first').children('.' + anchor + ':hidden');
      if(hiddenContent.length){
        hiddenContent.removeClass('hidden');
        $(this).parent(':first').children('.' + anchor).show(0, $.fancybox.resize);
      };
    });
  });
  bannerChanger.init();
  blinkNextButton();
});

