//Settings
var slide_active = 1; //id of the current displayed slide
var next_slide = slide_active + 1; //id of the next slide to display
var carousel_duration = 5000;   //display time of each slide in ms
var activate_sifr = false;   //if sifr is required within the slide
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 slide_xml_path = "/assets/bp_internet/globalbp/globalbp_uk_english/homepage/STAGING/local_assets/bp_homepage/javascript/slideshow.xml";
var fadeSpeed = 1000;
var debug = false;

//Global variables
var nb_slide;  //total number of slide
var var_timer;
var stop_carousel = false;
var block_slide = false;

//========= Launcher
//-------------------------------------------------------------------------------------
$(document).ready(function(){              
   
    var module_bg_container = $('div#slideshow_module');
    //var module_button = $('a#slideshow_button');
    var module_progress = $('div#slideshow_progress'); 
    //var play_button = $('a#slideshow_button');  
   
    if(getURLParam('nocarousel') == '1'){
        activate_image_carousel = false;
    }   
   
    //----------------------------------------- Home page slide  
    //empty the slide structure & remove the default slide image to start with a fade-in effect
    if(activate_image_carousel){
        module_bg_container.empty();
    }    
          
    //populate module_progress & load the full set of slide & display the first slide
    initslide(module_bg_container, module_progress);          
});

//========= initslide
//-------------------------------------------------------------------------------------
function initslide(module_bg_container, module_progress){        
        
    $.ajax({
        type: "GET",
        url: slide_xml_path,
        dataType: "xml",
            success: function(xml) {           
                $(xml).find('slide').each(function(n){
                    //-- Create the navigation link for each slide
                    var slide_id = $(this).attr('id');                    
                    var slide_bgimage = $(this).find('bgimage').text();                    
                    var current_class = "";
                    var current_style = "display:none;";
                                                            
                    //First slide
                    if(n == 0){                       
                    //    current_link.attr('class', 'active');                        
                    }  
                                                          
                    //-- Content populate each slide with background image and copy
                    //The first slide is displayed and the following ones are hidden
                    
                    //First slide
                    if(n == 0){                       
                        current_class = "current";
                        current_style = "";
                    }
                    
                    module_bg_container.append("<img id='hp_slide_" + slide_id + "' src='" + slide_bgimage + "' alt='' class='" + current_class + "' style='" + current_style + "'/>");
                });                     
                nb_slide = $(xml).find('slide').length;                
             },
             error: function() {                    
                 //display error message     
                 //alert('Error loading content');            
             },
             complete: function() {   
             
                //Start the slideshow only if more than one slide are available
                if(nb_slide > 1){    
					//Show the slideshow nav
					$('#slideshow_nav').show();

                    //Update nav total slide number
                    $('#total_slide_nb').text(nb_slide);
                        
                    //Carousel will start on the second slide                
                    var_timer = window.setTimeout('loadslideContent(2)', carousel_duration);  
                    
                    //Nav next and previous action handlers
                    $('#slideshow_button_next').click(function(){
                        if(!block_slide){
                            //Stop the carrousel
                            window.clearTimeout(var_timer);   
                            stop_carousel = true;
                            
                            if(slide_active < nb_slide){
                                loadslideContent(slide_active + 1);
                            }else{
                                loadslideContent(1);
                            }    
                        }    
                        return false;
                    });
                    
                    $('#slideshow_button_prev').click(function(){
                        if(!block_slide){
                            //Stop the carrousel
                            window.clearTimeout(var_timer);
                            stop_carousel = true;                   
                            
                            if(slide_active > 1){
                                loadslideContent(slide_active - 1);
                            }else{
                                loadslideContent(nb_slide);
                            }        
                        }    
                        return false;
                    });         
                 }else{
                    $('#slideshow_nav').hide();
                 }        
             }
         }); 
    
    
}

//========= loadslideContent
//-------------------------------------------------------------------------------------
function loadslideContent(slide_id){
    
    block_slide = true;
        
    var module_bg_container = $('div#slideshow_module');
    var module_progress = $('div#slideshow_progress'); 
    
    if(arguments.length == 0){
        slide_id = next_slide;
    }    
    
    //Update the nav counter
    $('#current_slide_nb').text(slide_id);
        
    //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_slide_' + slide_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();
                block_slide = false;
            });
        }                                                      
    }    
    slide_active = slide_id;

    //Carrousel functionnality (deactivated when the user click on a specific slide)
    if(!stop_carousel){        
        if (slide_active == nb_slide){
            next_slide = 1;
        }else{
            next_slide = parseInt(slide_active) + 1;
        }                        
        var_timer = window.setTimeout('loadslideContent(' + next_slide + ')', carousel_duration);  
    }            
     
    //*** DEBUG     
    if(debug && !$.browser.msie){
        console.log("slide_active: " + slide_active);  
        console.log("next_slide: " + next_slide);  
    }    
}

//========= 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 });
} 

$.preloadImages = function() {
    var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
    for(var i = a.length -1; i > 0; i--) {
        $("<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;
}