/*================================================== JAVASCRIPT */
/*
    Javascript Document for Diageo Annual Review 2009
    Build: Julien Decaudin on behalf of SAS - 24/08/2009
*/
//Apply the Javascript-enabled styles (hidding expandable boxes for instance)
$('html').addClass('js'); 


//Global variables
var var_timer;
var pageId;

$(document).ready(function() {     
    // Fix IE6 background image caching problem
    if (jQuery.browser.msie) {
        try { 
            document.execCommand("BackgroundImageCache", false, true); 
        } catch(err) {}
    }
    
    //Fix IE6 PNGs
    $('#banner div.wrapper').ifixpng();

    //--------------------------------------------- Initialization
    //Smooth scroll
    $('#anchor_list, ul.anchor_nav').localScroll();
    
    //Nav current state
    pageId = $('body').attr('id').split('page_')[1];
    $('#nav_' + pageId).find('a').addClass('current');
    
    //Nav popup
    $('#main_nav a').hover(function(){
        $(this).find('+ div.nav_popup').show();    
    }, function(){
        $(this).find('+ div.nav_popup').hide();
    });
    
    //Brand item popup
    $('.brand_item a').hover(function(){
        $(this).parent().find('span').show();    
    }, function(){
        $(this).parent().find('span').hide();
    });
});


//--------------------------------------------- Forms
/* initialize the inputs which requires a 'default value' system */
function InitInputDefaultValue(input_class) {

    $(input_class).each(function(i) {
        var current_input_id = $(this).attr("id");
        var default_value = GetInputDefaultValue(current_input_id);
        if ($(this).val() == "") {
            $(this).val(default_value);
        }
        InitInputDefaultValueEvents("#" + current_input_id, default_value)
    });
}

/* set the focus and blur events for the 'default value' inputs */
function InitInputDefaultValueEvents(input_id, default_value) {

    $(input_id).click(function() {
        if ($(this).val() == default_value)
            $(this).val("");
    });

    $(input_id).blur(function() {
        if ($(this).val() == "")
            $(this).val(default_value);
    });
}

/* return the value of the label associated to the input */
function GetInputDefaultValue(input_id) {

    var input_default_value = "";
    $("label").each(function(j) {
        if ($(this).attr("for") == input_id) {
            input_default_value = $.trim($(this).text());
        }
    });
    return input_default_value;
}

/* clear the value of the input still using their 'default value' when the form is submitted */
function InitSubmitDefaultValue(submit_class, defaultvalue_class) {

    $(submit_class).click(function() {
        $(this).parent().find(defaultvalue_class).each(function(i) {
            var default_value = GetInputDefaultValue($(this).attr("id"));
            if ($(this).val() == default_value) {
                $(this).val("");
            }
        });
    });
}

function selectRedirect(dropdown) {
    if (dropdown.val() != '') {
        document.location.href = dropdown.val();
    }
    return false;
}

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 });
} 