var SITE_URL = "http://www.rentokil-initial.com";

$(document).ready(function(){    

    //Init main navigation menu
    init_main_nav();
        
    //Display current main navigation item
    display_current_main_nav_item();

    //Display Success story's caroussel
    caroussel_success_story();

    //form's scripts
    form_scripts();
    
    //Table highlighting
    init_table_highlighting();
    
    //Popup Links
    init_popup_links();
});

function init_main_nav()
{
    if ($("#main_navigation"))
    {
        $("#main_navigation li ul").hide();
    }
}

function display_current_main_nav_item()
{
    pageID = $("body").attr("id");
    if(pageID && pageID != "popup_table")
    {
        pageID = pageID.slice(4);        
        if (pageID != "0"){        
            current_item = $("#nav"+pageID);               
            current_item.addClass("current");
            current_item.find("a:eq(0)").addClass("current");
            expand_current_item(current_item);
        }
    } 
}

function expand_current_item(current)
{             
    current.find("ul:eq(0)").show();    
    if(current.parent("ul:eq(0)").attr("id") != "main_navigation")
    {        
        expand_current_item(current.parents("li:eq(0)"));
    }
    else
    {
        current.addClass("current");        
        if (current.find("li:eq(0)").attr("id") != null)
        {            
            current.find("a:eq(0)").addClass("parent");
        }
    }
}

function caroussel_success_story()
{
    if ($("#caroussel_success_story"))
    {
        /* Success story config: [0] = class name; [1] = title; [2] = page url; [3] = story image url; [4] = header image url; */
        sstory_array = new Array();
        
        sstory_array[0] = new Array();        
        sstory_array[0][0] = "theme_sstory_1";
        sstory_array[0][1] = "STRENGTHENING LEADERSHIP CAPABILITY";
        sstory_array[0][2] = "/annualreport2006/2006_overview/success_stories_1.shtml";
        sstory_array[0][3] = "/annualreport2006/_images/success_stories/story_home_01.jpg";
        sstory_array[0][4] = "/annualreport2006/_images/success_stories/story_header_01.gif";
        
        sstory_array[1] = new Array();
        sstory_array[1][0] = "theme_sstory_2";
        sstory_array[1][1] = "100 YEARS OF SERVICE";
        sstory_array[1][2] = "/annualreport2006/2006_overview/success_stories_2.shtml";
        sstory_array[1][3] = "/annualreport2006/_images/success_stories/story_home_02.jpg";
        sstory_array[1][4] = "/annualreport2006/_images/success_stories/story_header_02.gif";
        
        sstory_array[2] = new Array();
        sstory_array[2][0] = "theme_sstory_3";
        sstory_array[2][1] = "REINVENTING OUR PRODUCTS";
        sstory_array[2][2] = "/annualreport2006/2006_overview/success_stories_3.shtml";
        sstory_array[2][3] = "/annualreport2006/_images/success_stories/story_home_03.jpg";
        sstory_array[2][4] = "/annualreport2006/_images/success_stories/story_header_03.gif";
        
        sstory_array[3] = new Array();        
        sstory_array[3][0] = "theme_sstory_4";
        sstory_array[3][1] = "TAKING A NEW ROUTE";
        sstory_array[3][2] = "/annualreport2006/2006_overview/success_stories_4.shtml";
        sstory_array[3][3] = "/annualreport2006/_images/success_stories/story_home_04.jpg";
        sstory_array[3][4] = "/annualreport2006/_images/success_stories/story_header_04.gif";
        
        sstory_array[4] = new Array();
        sstory_array[4][0] = "theme_sstory_5";
        sstory_array[4][1] = "CREATING A MAJOR PLAYER";
        sstory_array[4][2] = "/annualreport2006/2006_overview/success_stories_5.shtml";
        sstory_array[4][3] = "/annualreport2006/_images/success_stories/story_home_05.jpg";
        sstory_array[4][4] = "/annualreport2006/_images/success_stories/story_header_05.gif";
        
        sstory_array[5] = new Array();
        sstory_array[5][0] = "theme_sstory_6";
        sstory_array[5][1] = "STIMULATING THE SENSES";
        sstory_array[5][2] = "/annualreport2006/2006_overview/success_stories_6.shtml";
        sstory_array[5][3] = "/annualreport2006/_images/success_stories/story_home_06.jpg";
        sstory_array[5][4] = "/annualreport2006/_images/success_stories/story_header_06.gif";
        
        sstory_array[6] = new Array();        
        sstory_array[6][0] = "theme_sstory_7";
        sstory_array[6][1] = "FIRST CLASS DELIVERY";
        sstory_array[6][2] = "/annualreport2006/2006_overview/success_stories_7.shtml";
        sstory_array[6][3] = "/annualreport2006/_images/success_stories/story_home_07.jpg";
        sstory_array[6][4] = "/annualreport2006/_images/success_stories/story_header_07.gif";
        
        sstory_array[7] = new Array();
        sstory_array[7][0] = "theme_sstory_8";
        sstory_array[7][1] = "GETTING CLOSER TO THE CUSTOMER";
        sstory_array[7][2] = "/annualreport2006/2006_overview/success_stories_8.shtml";
        sstory_array[7][3] = "/annualreport2006/_images/success_stories/story_home_08.jpg";
        sstory_array[7][4] = "/annualreport2006/_images/success_stories/story_header_08.gif";
        
        sstory_array[8] = new Array();
        sstory_array[8][0] = "theme_sstory_9";
        sstory_array[8][1] = "PUTTING OUR CUSTOMERS FIRST";
        sstory_array[8][2] = "/annualreport2006/2006_overview/success_stories_9.shtml";
        sstory_array[8][3] = "/annualreport2006/_images/success_stories/story_home_09.jpg";
        sstory_array[8][4] = "/annualreport2006/_images/success_stories/story_header_09.gif";
        
        sstory_array[9] = new Array();
        sstory_array[9][0] = "theme_sstory_10";
        sstory_array[9][1] = "LOOKING TO THE FUTURE";
        sstory_array[9][2] = "/annualreport2006/2006_overview/success_stories_10.shtml";
        sstory_array[9][3] = "/annualreport2006/_images/success_stories/story_home_10.jpg";
        sstory_array[9][4] = "/annualreport2006/_images/success_stories/story_header_10.gif";
                
        rndNo=Math.floor(Math.random()*(sstory_array.length));        
        
        $("#caroussel_success_story").removeClass(); 
        $("#caroussel_success_story").addClass(sstory_array[rndNo][0]);         
        $("#caroussel_success_story").find("#img_story_banner").attr("src", sstory_array[rndNo][3]);
        $("#caroussel_success_story").find("#img_story_banner").attr("alt", sstory_array[rndNo][1]);
        $("#caroussel_success_story").find("#img_story_header").empty();        
        $("#caroussel_success_story").find("#img_story_header").append("<img src='" + sstory_array[rndNo][4] + "' alt='"+ sstory_array[rndNo][1] +" header' />");
        $("#caroussel_success_story").find("a").attr("href", sstory_array[rndNo][2]);        
    }        
}

function form_scripts()
{
    //Search input
    $("#query_string").focus(function() {
       if (this.value == "Search our site")
         this.value = "";
     }); 
     
    $("#query_string").blur(function() {
        if (this.value == "")
         this.value = "Search our site";
     }); 
    
    //Email this page's Form
    $("#form_email_page").validate({
        event: "keyup",
        rules: {
            your_name: { required: true },
            your_email: { 
                required: true,
                email: true
            },
            recip_name: { required: true },
            recip_email: { 
                required: true,
                email: true
            }
        },
	    errorPlacement: function(error, element) {
		    error.appendTo( element.parent("label").find("span.error_message"));
	    }
    });
}

function init_table_highlighting()
{
    apply_tab_highlight("#tab_consolidated_balance_sheet");
    apply_tab_highlight("#tab_group_perf_review");  
    apply_tab_highlight("#tab_key_perf_indicators");
    apply_tab_highlight("#tab_download_centre");              
}

function apply_tab_highlight(table_id)
{
    if ($(table_id))
    {
        $(table_id + " tr").hover(function(){
            $(this).addClass("hover");
        },function(){
            $(this).removeClass("hover");
        });
    }
}

function init_popup_links()
{
    if($("#popup_kpi"))
    {
        link_hrefkpi = SITE_URL + $("#popup_kpi").attr("href");        
        $("#popup_kpi").click(function(){
            window.open(link_hrefkpi, "", "resizable=1, scrollbars=yes,width=550,height=450");
            $("#popup_kpi").attr("href", "#");
        });
    }
    
    if($("#popup_mi"))
    {
        link_hrefmi = SITE_URL + $("#popup_mi").attr("href");        
        $("#popup_mi").click(function(){
            window.open(link_hrefmi, "", "resizable=1, scrollbars=yes,width=640,height=350");
            $("#popup_mi").attr("href", "#");
        });
    }
    
    if($(".popup_pdf"))
    {
        $(".popup_pdf").each(function(index){
            $(this).click(function(){
                window.open(this, "", "resizable=1, scrollbars=yes,width=800,height=600");
                return false;
            });
        });
    }
}

