﻿var heroBannerTransitionLength = 1500;
var heroBannerTimeBetweenTransitions = 6000;
var heroInterval = 0;

var isCorporateSite = $('title').html() == "Biz Corporates";

var corpsReturnPoint = -174;

var animating = false;

$(function() {

    // dynamically preload over images
    jQuery(".Rollover").each(
        function() {
            jQuery("<img>").attr("src", AddOver(this.src));            
        }
    );

    // setup rollover actions
    jQuery(".Rollover").hover(
    function() {
        if (this.src.search("_Over") < 0) {
            this.src = AddOver(this.src);
        }
    },
    function() {
        this.src = this.src.replace("_Over", "");
    });
    
    setupHeroSlideshow();
});

function AddOver(i) {
    return i.replace(".gif", "_Over.gif").replace(".jpg", "_Over.jpg");
}

function setupHeroSlideshow(){
    var heroSlider = $("#HeroSlideshow");

    if (heroSlider != null) {
      heroInterval = setInterval( animateNextHeroFrame, heroBannerTimeBetweenTransitions );
    
      heroSlider.find("div").addClass("inactive");
      heroSlider.find("div:first-child").removeClass("inactive").addClass("active");
      
//      if( isCorporateSite ){
//        heroSlider.find("div:first-child").find("img").css("left", corpsReturnPoint);
//        
//        heroSlider.mouseenter( 
//          function(){
//            if( !animating ){
//              heroSlider.find(".active").find("img").animate(
//                { left: 0 },
//                500
//              );
//              
//              clearInterval( heroInterval );
//            }
//          }
//        );
//        
//        heroSlider.mouseleave(
//          function(){
//            heroSlider.find(".active").find("img").animate(
//              { left: corpsReturnPoint },
//              500
//            );
//            heroInterval = setInterval( animateNextHeroFrame, heroBannerTimeBetweenTransitions );
//          }
//        );
//      }
    
      
    }
}

function animateNextHeroFrame(){
    var heroSlider = $("#HeroSlideshow");
    var active = heroSlider.find(".active");
    
    var next = active.next();
    
    if( next.length == 0 ){
      next = heroSlider.find("div:first-child");
    }
    
    next.removeClass("inactive").addClass("next");
    next.fadeIn( "slow", 
        function(){
          active.removeClass("active").addClass("inactive");
          next.addClass("active").removeClass("next");    
        }
    );
    
    var imageWidth = next.find("img").width();
    var displayedWidth = heroSlider.width();
    
    var imageDistanceToTravel = imageWidth - displayedWidth;
    
    if( imageDistanceToTravel < 0 ){
      return;
    }
    
    next.find("img").css("left", "-" + imageDistanceToTravel + "px");

    animating = true;
    
    next.find("img").animate(
        { left: 0 },
        heroBannerTransitionLength,
        function(){
          animating = false;
        }
    );
}
