﻿/* GENERAL JAVASCRIPT */
/*
    Javascript Document for Home Retail Group - Corporate Responsibility 2008/2009
    Build: Julien Decaudin on behalf of SAS - 17/04/2008
    Update: Oliver Long on behalf of SAS - 04/2009
*/

$(document).ready(function(){

    //Coloured bar changer
    $("#main_navigation_container").addClass($('body').attr("class"));
    
//    //Link colour changer
//    var accentcolour = $('body').attr("class") + "_highlight";
//    alert(accentcolour);
//    
//    if ($("#main_navigation li").hasClass("current"))
//    {
//        $(this).addClass(accentcolour);
//    };

    //PNG fix
    $.ifixpng('_images/pixel.gif');
    $('div.shade_module_footer, div.tab_decoration, .home_sub_nav').ifixpng();    
    
   
    
    //Navigation display
    Init_Nav("secondary_navigation");
    Display_CurrentNavItems();   
            
    //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 .content_holder, .main_keymessage').hover(function(){
        $(this).addClass('rhm_keymessage_hover');
    }, function(){
        $(this).removeClass('rhm_keymessage_hover');
    });
    
    $('div.rhm_keymessage .content_holder, .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){
        //init
        LoadTabContent('#tabs_container li.current a', true);
        
             //Tooltips
   
               
        $('#tabs_container a').click(function(){                               
            var current_item = $(this).parent("li");        
            //update the content                
            LoadTabContent(this, false);                        
            return false;
        });
    }
    
   
    //Expandable Modules
    InitExpandableModules();
    EventExpandableModules();   
    
   
    //Highlight picker  
    if ($('#highlight_content').size() > 0){
    //Highlight picker

        LoadHighlightContent('#changer', true);
        
        $('#changer').change(function(){                                           
        
            //update the content                
            LoadHighlightContent($(this), false);                        
            return false;
        });
    }


});

//if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', FirefoxFixOrderedForm, false);    
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

//--------------------------------------------- Tabs

function LoadTabContent(link_target, page_load) {    
    var folder = $(link_target).attr('href');
    var specific_tab = getURLParam('tab');
    
    if (specific_tab != '' && page_load == true)
    {
        folder = specific_tab + "/";
    }

    var current_item = $('#tabs_container a[href=' + folder + ']').parent('li');
    
    $('#column_content_middle div.content_holder').load(folder + 'content.shtml', function(){                
        $('#tabs_container li').removeClass('current');        
        current_item.addClass('current');            
        includeScript("/ar/2009/_scripts/sifr-config.js");
    });

}

function LoadHighlightContent(link_target, page_load)
{
    var folder = 'highlights/' + $(link_target).val();     
    $('#highlight_content div.content_holder').removeClass('highlights');
    $('#highlight_content div.content_holder').removeClass('group');
    $('#highlight_content div.content_holder').removeClass('chairman');
    $('#highlight_content div.content_holder').removeClass('review');
    $('#highlight_content div.content_holder').removeClass('governance');
    $('#highlight_content div.content_holder').removeClass('financial');
    $('#highlight_content div.content_holder').removeClass('more');
    $('#highlight_content div.content_holder').addClass($(link_target).val());
    $('h2.module_home_left').text($(link_target).find('option[selected]').text());
    $('#highlight_content div.content_holder div.dynamic_content').load(folder + '/content.shtml', function(){                     
    });

}

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');
          }
      });            
      $(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;
}