//Settings
var banner_active = 1; //id of the current displayed banner
var carousel_duration = 10000;   //display time of each banner in ms
var activate_sifr = true;   //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.

//Global variables
var nb_banner;  //total number of banner
var var_timer;
var bg_images_preload = new Array();
var stop_carousel = false;

//========= Launcher
//-------------------------------------------------------------------------------------
$(document).ready(function(){              
   
    var module_bg_container = $('div#home_welcome_container div.hp_banner');
    var module_content = $('div#home_welcome_container div.module_content');
    var module_nav = $('div#home_welcome_container div.module_nav div.content_container');   
   
    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_content.empty();
    module_nav.empty();    
          
    //populate module_nav & load the full set of banner & display the first banner
    initBanner(module_bg_container, module_content, module_nav);        
});

//========= initBanner
//-------------------------------------------------------------------------------------
function initBanner(module_bg_container, module_content, module_nav){        
    module_nav.append("<ul></ul>");
    var module_nav_list = module_nav.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_title = $(this).find('title').text();
                    var banner_copy = $(this).find('copy').text();
                    var banner_link = $(this).find('link').text();
                    var banner_link_text = $(this).find('linkText').text();
                    var banner_color = $(this).find('color').text();
                    var banner_bgimage = "/_images/hp_banner/banner_" + banner_id + ".jpg";                    
                    var current_class = "";
                    var current_style = "display:none;";
                                        
                    module_nav_list.append("<li><a href='#' title='" + banner_title + "' id='hpb_" + banner_id + "' rel='color_" + banner_color + "'>&nbsp;</a></li>");
                    
                    var current_link = module_nav_list.find('a#hpb_' + banner_id);
                    
                    //First banner
                    if(n == 0){                       
                        current_link.attr('class', 'current');
                        current_link.css('background-color', banner_color);
                    }
                    
                    //init the nav events                    
                    current_link.hover(function(){                        
                        $(this).css('background-color', banner_color);
                        
                        $('#home_welcome_container div.module_nav span').text(banner_title);
                        $('#home_welcome_container div.module_nav span').css('color', banner_color);
                        $('#home_welcome_container div.module_nav span').show();
                    },function(){
                        if(!$(this).hasClass('current')){
                            $(this).css('background-color', 'transparent');
                        }                    
                        $('#home_welcome_container div.module_nav span').hide();
                    });
                    
                    current_link.click(function(){                                                                                              
                        window.clearTimeout(var_timer);
                        stop_carousel = true;
                        if(banner_active != banner_id){                            
                            loadBannerContent(banner_id);
                            banner_active = banner_id;                            
                        }                        
                        return false;
                    });  
                                        
                    //-- 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 + "'/>");
                    module_content.append("<div id='module_content_" + banner_id + "' class='content_container' style='" + current_style + "'><h1>" + banner_title + "</h1><p>" + banner_copy + "</p><a href='" + banner_link + "'>" + banner_link_text + "</a></div>");
                    
                    if(n == 0){
                        //Regenerate the sifr text      
                        if(activate_sifr){
                            includeScript("/_shared/sifr3/sifr-config_hp_banner.js");                       
                        }  
                    }    
                });                     
                nb_banner = $(xml).find('banner').length;                
                //Preload background images & ajax loader gif
                /*
                
                $.preloadImages("/_images/ajax-loader.gif"); 
                if(activate_image_carousel){
                    $.preloadImages(bg_images_preload);
                }
                
                */
             },
             error: function() {                    
                 //display error message     
                 //alert('Error loading content');            
             },
             complete: function() {                             
                module_nav.append("<span></span>");
    
                //loadBannerContent(1);                           
                //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#home_welcome_container div.hp_banner');
    var module_content = $('div#home_welcome_container div.module_content');
    var module_nav = $('div#home_welcome_container div.module_nav div.content_container');
    
    $('div#home_welcome_container div.module').attr('id', 'module_banner_' + banner_id);
    
    //Reset all the nav link state/color
    $('div.module_nav li a').removeClass('current');
    $('div.module_nav li a').css('background-color', 'transparent');
    
    //Set the nav link to the current state    
    var banner_color = module_nav.find('a#hpb_' + banner_id).attr('rel').split("color_")[1];
    module_nav.find('a#hpb_' + banner_id).addClass('current');
    module_nav.find('a#hpb_' + banner_id).css('background-color', banner_color); 
        
    //Change the background image then display the new content
    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 (deactivated on IE6)
        //if(!activate_fadein || ($.browser.msie && $.browser.version < 7)){
        if(!activate_fadein){
            next_bg.show();            
            current_bg.hide();            
            
            loadModuleContent(banner_id, module_content);
                                                             
        }else{
            module_content.find('div.content_container').hide();
            $('img#ajax_loader').show();
            next_bg.fadeIn('slow', function(){                
                current_bg.hide();
                current_bg.removeClass('previous');
                                
                $('img#ajax_loader').hide();
                loadModuleContent(banner_id, module_content);
            });
        }                                                      
    }else{
        loadModuleContent(banner_id, module_content);
    }
    
    //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;
        }                
        
        var_timer = window.setTimeout('loadBannerContent(' + next_banner + ')', carousel_duration);
    }       
}

//========= loadModuleContent
//-------------------------------------------------------------------------------------
function loadModuleContent(banner_id, module_content){    
    //Display the right content
    module_content.find('div.content_container').hide();
    module_content.find('div.content_container[id=module_content_' + banner_id + ']').show();             
    
    //Regenerate the sIFR text      
    if(activate_sifr){
        includeScript("/_shared/sifr3/sifr-config_hp_banner.js");                       
    }  
}

//========= 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;
}




/************************************* OLD FUNCTIONS ******************************************************/
/************************************* OLD FUNCTIONS ******************************************************/
/************************************* OLD FUNCTIONS ******************************************************/

function old_loadBannerContent(banner_id){   

    var module_content = $('div.module_content .content_container');
    var module_nav = $('div.module_nav .content_container');
     
    $.ajax({
        type: "GET",
        url: banner_xml_path,
        dataType: "xml",
            success: function(xml) {           
                var current_banner = $(xml).find('banner[id=' + banner_id + ']');                                 
                var banner_title = current_banner.find('title').text();
                var banner_copy = current_banner.find('copy').text();
                var banner_color = current_banner.find('color').text();
                var banner_link = current_banner.find('link').text();
                var banner_bgimage = "/_images/hp_banner/banner_" + banner_id + ".jpg";
                
                if(activate_image_carousel){                
                    $('div.hp_banner').addClass('hp_banner_prev');
                    $('#home_welcome_container').prepend("<div class='hp_banner hp_banner_new'></div>");
                    $('div.hp_banner_new').hide();
                    $('div.hp_banner_new').css('background-image', 'url(' + banner_bgimage + ')');
                }                                    
                
                $('div.module').attr('id', 'module_banner_' + banner_id);                                                
                
                //Reset all the nav link state/color
                $('div.module_nav li a').removeClass('current');
                $('div.module_nav li a').css('background-color', 'transparent');
                
                //Set the nav link to the current state
                module_nav.find('a#hpb_' + banner_id).addClass('current');
                module_nav.find('a#hpb_' + banner_id).css('background-color', banner_color);
                
                //Clear the module content
                module_content.find('h1').remove();
                module_content.find('p').empty();
                module_content.find('a').empty();
                
                if(activate_image_carousel){
                    //Fade-in effect (deactivated on IE6)
                    if(!activate_fadein || ($.browser.msie && $.browser.version < 7)){
                        $('div.hp_banner_new').show();
                        $('div.hp_banner_prev').remove();
                        $('div.hp_banner').removeClass('hp_banner_new');
                        loadModuleContent(module_content, banner_title, banner_copy, banner_link);
                                                                         
                    }else{
                        module_content.append("<img id='ajax_loader' src='/_images/ajax-loader.gif' alt=''>");
                        $('div.hp_banner_new').fadeIn('slow', function(){
                            $('div.hp_banner_prev').remove();
                            $(this).removeClass('hp_banner_new');
                            
                            $('#ajax_loader').remove();
                            loadModuleContent(module_content, banner_title, banner_copy, banner_link);
                        });
                    }
                }else{
                    loadModuleContent(module_content, banner_title, banner_copy, banner_link);
                }    
             },
             error: function() {                    
                 //display error message     
                 //alert('Error loading content');            
             },
             complete: function() {  
                //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;
                    }                
                    var_timer = window.setTimeout('loadBannerContent(' + next_banner + ')', carousel_duration);
                }
             }
         });
}

function old_loadModuleContent(module_content, banner_title, banner_copy, banner_link){    
    //Populate the module with the new content
    module_content.prepend("<h1>" + banner_title + "</h1>");
    module_content.find('p').text(banner_copy);
    module_content.find('a').attr('href', banner_link);
    module_content.find('a').text(banner_link_text); 
    
    //Regenerate the sifr text      
    if(activate_sifr){
        includeScript("/_shared/sifr3/sifr-config_hp_banner.js");                       
    }
}

