//Settings
var banner_active = 1; //id of the current displayed banner
var next_banner = banner_active + 1; //id of the next banner to display
var carousel_duration = 5000;   //display time of each banner in ms
var activate_sifr = false;   //if sifr is required within the banner
var activate_fadein = true;   //activate fade effect between images (not supported by IE6)
var activate_image_carousel = true; //activate image carousel dynamically loaded. If deactivated the first image is used.
var module_link_text = "Read full report"; //Module Link text for the link to the section of the site. Used by all the modules.
var banner_xml_path = "/kbcs-media/js/hp_banner.xml";
var fadeSpeed = 2000;
var debug = false;

//Global variables
var nb_banner;  //total number of banner
var var_timer;
var stop_carousel = false;

//========= Launcher
//-------------------------------------------------------------------------------------
$(document).ready(function(){              
   
    var module_bg_container = $('div#hp_banner');
    var module_button = $('a#hp_banner_button');
    var module_progressbar = $('div#hp_banner_progressbar'); 
    var play_button = $('a#hp_banner_button');  
   
    if(getURLParam('nocarousel') == '1'){
        activate_image_carousel = false;
    }   
   
    //----------------------------------------- Home page banner  
    //empty the banner structure & remove the default banner image to start with a fade-in effect
    if(activate_image_carousel){
        module_bg_container.empty();
    }    
    module_progressbar.empty();    
          
    //populate module_progressbar & load the full set of banner & display the first banner
    initBanner(module_bg_container, module_progressbar);          
    
    //Initialize the play button
    play_button.click(function(){         
        if($(this).hasClass('pause')){       
            //Stop the carrousel  
            window.clearTimeout(var_timer);          
            stop_carousel = true;            
            $(this).removeClass('pause');            
        }else{
            //Start the carrousel
            stop_carousel = false;       
            $(this).addClass('pause');                         
            var_timer = window.setTimeout('loadBannerContent()', carousel_duration);
        }    
        return false;
    });      
});

//========= initBanner
//-------------------------------------------------------------------------------------
function initBanner(module_bg_container, module_progressbar){        
    module_progressbar.append("<ul></ul>");
    var module_progressbar_list = module_progressbar.find('ul');
        
    $.ajax({
        type: "GET",
        url: banner_xml_path,
        dataType: "xml",
            success: function(xml) {           
                $(xml).find('banner').each(function(n){
                    //-- Create the navigation link for each banner
                    var banner_id = $(this).attr('id');                    
                    var banner_bgimage = $(this).find('bgimage').text();                    
                    var current_class = "";
                    var current_style = "display:none;";
                                        
                    module_progressbar_list.append("<li id='" + banner_id + "'></li>");                    
                    var current_link = module_progressbar_list.find('li#' + banner_id);
                    
                    //First banner
                    if(n == 0){                       
                        current_link.attr('class', 'active');                        
                    }  
                                                          
                    //-- Content populate each banner with background image and copy
                    //The first banner is displayed and the following ones are hidden
                    
                    //First banner
                    if(n == 0){                       
                        current_class = "current";
                        current_style = "";
                    }
                    
                    module_bg_container.append("<img id='hp_banner_" + banner_id + "' src='" + banner_bgimage + "' alt='' class='" + current_class + "' style='" + current_style + "'/>");
                    
                    if(n == 0){
                        //Regenerate the sifr text      
                        if(activate_sifr){
                            includeScript("/_shared/sifr3/sifr-config_hp_banner.js");                       
                        }  
                    }    
                });                     
                nb_banner = $(xml).find('banner').length;                
             },
             error: function() {                    
                 //display error message     
                 //alert('Error loading content');            
             },
             complete: function() {                             
                //Carousel will start on the second banner
                var_timer = window.setTimeout('loadBannerContent(2)', carousel_duration);                
             }
         });                          
}

//========= loadBannerContent
//-------------------------------------------------------------------------------------
function loadBannerContent(banner_id){
    
    var module_bg_container = $('div#hp_banner');
    var module_button = $('a#hp_banner_button');
    var module_progressbar = $('div#hp_banner_progressbar');   
    var play_button = $('a#hp_banner_button');
    
    if(arguments.length == 0){
        banner_id = next_banner;
    }    
    
    //The first banner displays: Reset the progressbar
    if(banner_id == 1){
        module_progressbar.find('li').removeClass('active');
    }
    
    //Update the progressbar to the active state    
    module_progressbar.find('li#' + banner_id).addClass('active');
        
    //Update the background image
    if(activate_image_carousel){                                
        var current_bg = module_bg_container.find('img.current');
        var next_bg = module_bg_container.find('img#hp_banner_' + banner_id);
                
        current_bg.attr('class', 'previous');
        next_bg.attr('class', 'current');                        
        
        //Fade-in effect
        if(!activate_fadein){
            next_bg.show();            
            current_bg.hide();            
        }else{
            $('img#ajax_loader').show();
            next_bg.fadeIn(fadeSpeed, function(){                
                current_bg.hide();
                current_bg.removeClass('previous');
                                
                $('img#ajax_loader').hide();
            });
        }                                                      
    }
    
    //Carrousel functionnality (deactivated when the user click on a specific banner)
    if(!stop_carousel){        
        banner_active = banner_id;                        
        
        if (banner_active == nb_banner){
            next_banner = 1;
        }else{
            next_banner = parseInt(banner_active) + 1;
        }                
        
        //The first banner displays: stop the carrousel & show the play button
        if(banner_id == 1){
            stop_carousel = true;
            play_button.removeClass('pause');
            play_button.css('display','block');        
        }else{                
            var_timer = window.setTimeout('loadBannerContent(' + next_banner + ')', carousel_duration);
        }    
    }            
     
    //*** DEBUG     
    if(debug && !$.browser.msie){
        console.log("banner_active: " + banner_active);  
        console.log("next_banner: " + next_banner);  
    }    
}

//========= includeScript
//-------------------------------------------------------------------------------------
function includeScript(scriptUrl) {
    // Change requests to be sent synchronous
    $.ajaxSetup({ async: false });

    // Loads and executes a local JavaScript file
    $.getScript(scriptUrl);

    // Restore requests to be sent asynchronous
    $.ajaxSetup({ async: true });
} 

jQuery.preloadImages = function() {
    var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
    for(var i = a.length -1; i > 0; i--) {
        jQuery("<img>").attr("src", a[i]);
    }
}

/** function getURLParam(strParamName) 
* => get the value of a querystring parameter
* @strParamName: parameter name
* #RETURN: parameter value
*/
function getURLParam(strParamName){
    var strReturn = "";
    var strHref = window.location.href;

    if ( strHref.indexOf("?") > -1 ){        
        var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
        var aQueryString = strQueryString.split("&");
        for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
            if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }
        }
    }
    return strReturn;
}