﻿/* GENERAL JAVASCRIPT */
/*
    Javascript Document for Home Retail Group - Corporate Responsibility 2007/2008
    Build: Julien Decaudin on behalf of SAS - 17/04/2008
*/

$(document).ready(function(){    

    //PNG fix
    $.ifixpng('_images/pixel.gif');
    $('div.shade_module_footer, div.tab_decoration, div.shade_content_footer, div.shade_content_wrapper').ifixpng();    
    
    //Navigation display
    Init_Nav("secondary_navigation");
    Display_CurrentNavItems(); 
    
    //What we've been doing navigation
    $("div.whatwevebeendoing_wrapper").hover(function () {
      $(this).find("div.rollover").fadeIn("fast");      
    },function(){
        $(this).find("div.rollover").fadeOut("fast");
    });
    
    //Home page flash alternative
    $("div.flash_alt_wrapper").hover(function () {
      $(this).find("div.rollover").fadeIn("fast");      
    },function(){
        $(this).find("div.rollover").fadeOut("fast");
    });  
            
    //Table stripping   
    //$("table.stripped_table tbody tr:nth-child(odd)").addClass("stripped");
    
    //Forms        
    InitInputDefaultValue(".defaultvalue_input");    
    InitSubmitDefaultValue(".defaultvalue_submit", ".defaultvalue_input");
        
    // Key Message Modules   
    $('div.rhm_keymessage').hover(function(){        
        $(this).find('.content_holder').addClass('rhm_keymessage_hover');
    }, function(){
        $(this).find('.content_holder').removeClass('rhm_keymessage_hover');
    });
    
    $('.main_keymessage').hover(function(){
        
        $(this).addClass('main_keymessage_hover');
    }, function(){
        $(this).removeClass('main_keymessage_hover');
    });
    
    $('div.rhm_keymessage, .main_keymessage').click(function(){
        if ($(this).find("a").attr("href") != null)
        {
            document.location = $(this).find("a").attr("href");             
        }
    });
    
    //Tabs AJAX
    //init the default tab    
    if ($('#tabs_container').size() > 0){
        LoadTabContent('#tabs_container li.current a');
        
        $('#tabs_container a').click(function(){        
                       
            var current_item = $(this).parent('li');
        
            //update the content                
            //LoadTabContent(this);
            var folder = $(this).attr('href');
            $('#column_content_middle div.content_holder').load(folder + 'main.shtml');
            $('#column_content_right').load(folder + 'column.shtml', function(){
                //highlight the tab
                $('#tabs_container li').removeClass('current');        
                current_item.addClass('current');                                
                includeScript("/cr/2008/_scripts/sifr-config.js");
            });            
            
            return false;
        });
    }
    
    //Expandable Modules
    InitExpandableModules();
    EventExpandableModules();   
});

//if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', FirefoxFixOrderedForm, false);    
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

//--------------------------------------------- Tabs
function LoadTabContent(link_target)
{
    var folder = $(link_target).attr('href');
    var specific_tab = getURLParam('tab');
    
    if (specific_tab != '')
    {
        folder = specific_tab + "/";
    }            
    
    var current_item = $('#tabs_container a[href=' + folder + ']').parent('li');
    
    $('#column_content_middle div.content_holder').load(folder + 'main.shtml');
    $('#column_content_right').load(folder + 'column.shtml', function(){        
        if (specific_tab != '')
        {
            $('#tabs_container li').removeClass('current');        
            current_item.addClass('current');    
        }
        includeScript("/cr/2008/_scripts/sifr-config.js");
    });
}

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 });
} 

//--------------------------------------------- Expandable Modules
function InitExpandableModules()
{
    //Close all modules that doesn't use the class 'open'
    $('a.exp_module_header').not('.open').find('+ div.exp_module_content').hide();              
    $('a.exp_module_header').not('.open').addClass('close');
    $('a.exp_module_header').not('.open').attr('title', 'Open');
    $('a.exp_module_header.open').attr('title', 'Close');
}
 
function EventExpandableModules()
{
    $("a.exp_module_header").click(function () { 
      var link = $(this);
      var module_body = $(this).find("+ div.exp_module_content");            
      
      module_body.slideToggle("fast", function(){
          //if the module is CLOSE and has to OPEN      
          if(module_body.css('display') == 'none'){
            link.attr('title', 'Open');            
          //if the module is OPEN and has to CLOSE
          }else{  
            link.attr('title', 'Close');
            includeScript("/cr/2008/_scripts/sifr-config.js");
          }
      });            
      $(this).toggleClass('close');
      $(this).toggleClass('open');                       
      
      return false;
    });
} 

//--------------------------------------------- Navigation
function Init_Nav(id_nav)
{
    if ($(id_nav))
    {
        $("#" + id_nav + " li ul").hide();
    }
}

function Display_CurrentNavItems()
{
    pageID = $("body").attr("id");
    if(pageID)
    {
        //remove the ID prefix
        pageID = pageID.slice(5);
        
        //split the id for the main navigation and for the secondary one
        pageID_array = pageID.split("_");
        
        //highlight the main navigation item
        mainID = pageID_array[0];                                            
        current_item = $("#nav_" + mainID);
        current_item.addClass("current");                
        
        //highlight and expand the deepest item in the list recursively to the top            
        if(pageID_array.length > 0)        
        {                                      
            Expand_CurrentItem("nav_" + pageID, pageID_array.length, false);            
        }
    }
}

function Expand_CurrentItem(current_ID, current_level, parent)
{
    if(parent)
    {
        $("#" + current_ID + " > a").addClass("current_parent");
    }else
    {
        $("#" + current_ID + " > a").addClass("current");    
    }            
    $("#" + current_ID + " > ul").show();
    
    if(current_level > 2)
    {
        Expand_CurrentItem($("#" + current_ID).parent("ul").parent("li").attr("id"), current_level - 1, true);
    }
}



//--------------------------------------------- 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("");
            }
        });        
    });
}

/* fix Firefox formating issue for Forms using ordered list */
function FirefoxFixOrderedForm(){
  // Hide forms
  $( 'form.ordered_form' ).hide().end();

  // Processing
  $( 'form.ordered_form' ).find( 'li label' ).each( function( i ){    
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = null;
    this.appendChild( labelSpan );
  } ).end();

  // Show forms
  $( 'form.ordered_form' ).show().end();
}

function getURLParam(strParamName){
    var strReturn = "";
    var strHref = window.location.href;

    if ( strHref.indexOf("?") > -1 ){        
        var strQueryString = strHref.substr(strHref.indexOf("?"));
        var aQueryString = strQueryString.split("&");
        for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
            if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }
        }
    }
    return strReturn;
}