/*================================================== JAVASCRIPT */
/*
    Javascript Document for BP Global Homepage
    Build: Julien Decaudin on behalf of SAS - 11/03/2009
*/
//Apply the Javascript-enabled styles (hidding expandable boxes for instance)
$('html').addClass('js'); 

//Global variables
var mainnav_timer;
var mainnav_timer_duration = 500;
var mainnav_padding_right = 14;

$(document).ready(function() {
    // Fix IE6 background image caching problem
    if (jQuery.browser.msie) {
        try { 
            document.execCommand("BackgroundImageCache", false, true); 
        } catch(err) {}
    }
    
    //--------------------------------------------- INITIALIZATION     
    initMainNav();
    initTabs();
    
    //--------------------------------------------- TWEAKS 
    //Add new window icon to link opening in a new window
    $('a[target=_blank]').each(function(){
        var link_title = $(this).attr('title');
        $(this).attr('title', link_title + ' - This link opens in a new window');
        
        //Do not add the icon if it's a link to a PDF
        if(!$(this).parent('li').hasClass('icon_pdf')){
            $(this).addClass('link_new_window').wrapInner("<span class='link_text'></span>").append("<span class='new_window'>&nbsp;</span>");
        }    
    });
    
    //Right-hand modules click handler
    $('div.rh_module h2').click(function(){
        document.location.href = $(this).find('+ a').attr('href');
    });
    
    //Set content container minimum height depending the floating column height
    //=> To avoid the floating column overlaping the footer and the footer to jump when using the tabs module
    var content_min_height = $('#floating_column_content').height() - 35;
    $('#content').css('min-height', content_min_height + 'px');
    
    if($.browser.msie && $.browser.version < 7){        
        $('#content').attr('style', 'min-height:' + content_min_height + 'px;' + 'height:auto !important;' + 'height:' + content_min_height + 'px');    
    }
   
});

//--------------------------------------------- Main navigation
function initMainNav()
{
    //Set the width of each subnav container
     $('#global_nav a.main_item').each(function(){
        if($(this).find('+ div.subnav_container').length){        
            var subnav_container = $(this).find('+ div.subnav_container');
            var subnav_container_width = 0;
            var subnav_container_holder_padding_left = parseInt(subnav_container.find('div.content_holder').css('padding-left').split('px')[0]);
            var subnav_container_holder_padding_right = parseInt(subnav_container.find('div.content_holder').css('padding-right').split('px')[0]);
            
            //$(this).addClass('with_subnav');
            
            subnav_container.find('div[class^=column_subnav]').each(function(){
                subnav_container_width = subnav_container_width + parseInt($(this).css('width').split('px')[0]) + mainnav_padding_right;
            });
            subnav_container.width(subnav_container_width + subnav_container_holder_padding_left + subnav_container_holder_padding_right);
        }
     });
     
     //Open the subnav on rollover    
     $('#global_nav > ul > li').hover(function() {  
        window.clearTimeout(mainnav_timer);        
        hideSubnav();        
        
        $(this).find('div.subnav_container').show();
        //$(this).find('div.subnav_container').slideDown('fast');
        $(this).addClass('current');
        
     }, function() {
        if($(this).find('div.subnav_container').length){
            mainnav_timer = window.setTimeout('hideSubnav("' + this.id + '")', mainnav_timer_duration);
        }else{
            $(this).removeClass('current');
        }    
     });     
}

function hideSubnav(current_id)
{
    if(arguments.length > 0){
        $('#' + current_id).find('div.subnav_container').hide();   
        //$('#' + current_id).find('div.subnav_container').slideUp('fast');   
        $('#' + current_id).removeClass('current');
    }else{
        $('#global_nav div.subnav_container').hide();        
        //$('#global_nav div.subnav_container').slideUp('fast');        
        $('#global_nav > ul > li').removeClass('current');
    }    
}

//--------------------------------------------- Tabs
function initTabs()
{
    $('ul.tabs_list li a').click(function(){
        if(!$(this).parent().hasClass('current')){
            var tab_id = $(this).parent().attr('id').split('tab_')[1];
            var tabs_list = $(this).parents('ul.tabs_list');
            var tabs_content = tabs_list.find('+ div.tabs_content');
            
            //update tabs
            tabs_list.find('li').removeClass('current');
            $(this).parent().addClass('current');
            
            //update tab content
            tabs_content.find('ul').hide();
            tabs_content.find('ul#link_list_tab_' + tab_id).show();
        }
    
        return false;
    });
}

//--------------------------------------------- BP Javascript Functions
function win2column(url2col){
    window.open(url2col,'BP','width=433,height=500,status=no,resizable=0,top=110,left=200,toolbar=no,menubar=no,scrollbars=yes')
}