var currentPromoIndex = 0;
var numPromos = 5;
var promoRefreshSeconds = 4;
var promoFadeSpeedMilliSeconds = 1000;
var promoIntervalID = -1;

function showPromoElement(index) {
    if (promoIntervalID != -1) {
        clearInterval(promoIntervalID);
        promoIntervalID = -1;              
    }
    for (var i = 0; i < numPromos; ++i) {
        if (i == index) {
            $("#home_promo_splash_fg").addClass("home_promo_splash_"+i); 
            $("#home_promo_splash_bg").removeClass("home_promo_splash_"+i);                                      
        } else if (i == currentPromoIndex) {
            $("#home_promo_splash_bg").addClass("home_promo_splash_"+i);                   
            $("#home_promo_splash_fg").removeClass("home_promo_splash_"+i);                                      
        } else {
            $("#home_promo_splash_fg").removeClass("home_promo_splash_"+i);                                      
            $("#home_promo_splash_bg").removeClass("home_promo_splash_"+i);                                      
        }
    }    
    currentPromoIndex = index;
    $("#home_promo_splash_bg").show().fadeOut(promoFadeSpeedMilliSeconds);
    $("#home_promo_splash_fg").hide().fadeIn(promoFadeSpeedMilliSeconds);
    promoIntervalID = setInterval(advanceSplashPromo, promoRefreshSeconds*1000);             
}

function advanceSplashPromo()
{
    var nextPromoIndex = (Number(currentPromoIndex)+1) % numPromos;
    showPromoElement(nextPromoIndex);             
}

function preload_splash_images()
{
    var image1 = $('<img />').attr('src', 'images/home_bpre.jpg');
    var image2 = $('<img />').attr('src', 'images/home_hsp.jpg');
    var image3 = $('<img />').attr('src', 'images/home_bcm.jpg');
    var image4 = $('<img />').attr('src', 'images/home_ksi.jpg');
    var image5 = $('<img />').attr('src', 'images/home_assassin.jpg');
}

$(document).ready(function() {
  preload_splash_images();
  $("#home_promo_splash_bg").hide();
  showPromoElement(currentPromoIndex);

  $("#home_promo_splash").click(function(event) {
       event.preventDefault();
       switch(currentPromoIndex)
       {
           case 0:
               window.location = 'product_development.shtml';
           break;
           case 1:
               window.location = 'products/haley_strategic.shtml';
           break;
           case 2:
               window.location = 'products/bcm.shtml';
           break;
           case 3:
               window.location = 'products/ksi_global.shtml';
           break;
           case 4:
               window.location = 'products/assassin.shtml';
           break;

       }     
       //var element_id = "#home_promo_nav_link_"+currentPromoIndex;
       //window.location = $(element_id).attr('href');
  });
});
