/*================================================== GLOBAL JAVASCRIPT */
/*
Javascript Document for BBA Aviation
Build: Pete Robelou on behalf of SAS - 06/10/2009
*/

$('html').addClass('js');

//Global variables
var header_supportnav_timer;
var header_supportnav_timer_duration = 300;

var expand_speed = 250;

var fadein_speed = 200;
var fadeout_speed = 400;
var fadeout_opacity = 0.1;

var cs_fadeout_opacity = 0.2;

//Shadowbox setup
Shadowbox.loadSkin('classic', '/scripts/shadowbox/skin'); // use the "classic" skin
Shadowbox.loadLanguage('en', '/scripts/shadowbox/lang'); // use the English language
Shadowbox.loadPlayer(['img', 'iframe'], '/scripts/shadowbox/player'); // use iframe player


$(document).ready(function() {
    /*////////////////////////////////////// INIT */

    //------------------------ FIX IE6 BACKGROUND IMAGE CACHING PROBLEM
    if ($.browser.msie) {
        try {
            document.execCommand("BackgroundImageCache", false, true);
        } catch (err) { }
    }

    // INITIALISE SEARCH BOX
    InitInputDefaultValue(".defaultvalue_input");
    InitSubmitDefaultValue(".defaultvalue_submit", ".defaultvalue_input");

    // INITIALISE HEADER NAVIGATION DROPDOWN
    showDropNav();

    // INITIALISE EXPANDABLE MODULES
    InitExpandableModules();

    // INITIALISE SLIDING TABS
    slideTabs();

    // INITIALISE FADE CASE STUDY
    fadeCaseStudy();

    // INITIALISE FADE LOGO
    fadeLogo();

    // INITIALISE RADIO BUTTON REPLACEMENT
    $('input').filter('[type=radio]').checkBox();

    // INITIALISE ANCHOR SMOOTH SCROLL
    $('#master_container').localScroll();

    // INITIALISE SELECT BOX REPLACEMENT
	if (!$.browser.msie) {
	    $('.select_dropdown').sSelect({ddMaxHeight: '300px'});
	}

    // INITIALISE MAIN NAVIGATION DROPDOWN SCALING
    dropdownScale();

    // INITIALISE MODULE ALIGNMENT
    alignModules()
    //setTimeout("alignModules()", 300);

    // INITIALISE PEOPLE MODULE ALIGNMENT
    alignPeopleModules();

    // INITIALISE TABLE HIGHLIGHTING
    tableHighlight();

    // SHADOWBOX
    Shadowbox.init();

});


//--------------------------------------------- TABLE HIGHLIGHING
function tableHighlight() {
    $(".table_content table").find("tbody tr").hover(function() {
        $(this).addClass("hover");
    }, function() {
        $(this).removeClass("hover");
    });
}

//--------------------------------------------- MODULE ALIGNMENT
function alignModules() {
    var imgHeight = 148;
    var columnLeft = $('#column_aligned_left');
    var columnRight = $('#column_aligned_right');
    var nbModLeft = columnLeft.find('div.module_aligned_inner').length;
    var nbModRight = columnRight.find('div.module_aligned_inner').length;        
    
    //Select the column with the most modules
    var columnToParse = columnLeft;    
    var columnToCompare = columnRight;    
    if(nbModRight > nbModLeft) {
        columnToParse = columnRight;
        columnToCompare = columnLeft;
    }

    columnToParse.find('div.module_aligned_inner').each(function(n) {    
        var moduleToCompare = columnToCompare.find('div.module_aligned_inner').eq(n);

        if ($(this).outerHeight() > moduleToCompare.outerHeight()) {
            var alignedHeight = $(this).outerHeight();
            moduleToCompare.height(alignedHeight);
        } else {
            var alignedHeight = moduleToCompare.outerHeight();
            $(this).height(alignedHeight);
        }
        //alert(alignedHeight);
    });
}

//--------------------------------------------- PEOPLE MODULE ALIGNMENT
function alignPeopleModules() {
    $('div.content_holder .first').each(function () {
        var firstItem = $(this);
        var secondItem = firstItem.find('+ div').eq(0);
        var thirdItem = secondItem.find('+ div').eq(0);

        var moduleHeight = firstItem.find('.module_people_alignment').height();

        if (parseInt(secondItem.find('.module_people_alignment').height()) > moduleHeight) {
            moduleHeight = secondItem.find('.module_people_alignment').height();
        }

        if (parseInt(thirdItem.find('.module_people_alignment').height()) > moduleHeight) {
            moduleHeight = thirdItem.find('.module_people_alignment').height();
        }

        
        firstItem.find('.module_people_alignment').height(moduleHeight);
        secondItem.find('.module_people_alignment').height(moduleHeight);
        thirdItem.find('.module_people_alignment').height(moduleHeight);
    });
}

//--------------------------------------------- MAIN NAVIGATION DROPDOWN SCALING
function dropdownScale() {
    var mainNavWidth = $('#main_navigation').width() + 18;
    $('#main_navigation .droplink_navigation_container').width(mainNavWidth);   
    $('#main_navigation .droplink_navigation_container .content_holder').width(mainNavWidth - 38);  
    $('#main_navigation .droplink_navigation_container .content_holder .column_two_thirds').width(mainNavWidth - 224); 
}


//--------------------------------------------- FADE CASE STUDY
//function fadeCaseStudy() {
//	$('div.case_study').hover(function() {  
//	    var currentId = this.id;	    
//	    $('div.case_study').each(function(){
//	        if(this.id != currentId){
//	            $(this).find('img').stop().fadeTo(fadeout_speed, cs_fadeout_opacity);            
//	        }
//	    });
//    }, function() {               
//        $('div.case_study img').stop().fadeTo(fadein_speed, 1);    
//    });
//}
function fadeCaseStudy() {
    $('div.case_study').hover(
	    function() {
	        $(this).find('img').stop().fadeTo(fadeout_speed, cs_fadeout_opacity);
	    },
	    function() {
	        $(this).find('img').stop().fadeTo(fadein_speed, 1);
	    }
    );
}

//--------------------------------------------- FADE LOGO
function fadeLogo() {
	$('div.logo_rollover').hover(
	    function() { 		
	        $(this).find('img').stop().fadeTo(fadeout_speed, fadeout_opacity);  
	            },	        
	    function() {             
            $(this).find('img').stop().fadeTo(fadein_speed, 1);    
                }
    );
}


//--------------------------------------------- EXPANDABLE MODULES	
function InitExpandableModules() {   
    $(".accordion").accordion({ active: false, collapsible: true, autoHeight: false }); 
    $(".default_open").find("a.expand_link").addClass('close');
    $(".expandable a.expand_link").click(function() {
        var link = $(this);
        var link_classes = link.attr('class').split('open')[0].split('close')[0];
        var module_holder = $(this).parents('div.expandable').find("div.expand_content");
                              
        if ($(this).find('+ div.expand_content').length > 0) {
            module_holder = $(this).find("+ div.expand_content");
        }  
        module_holder.stop(true, true).slideToggle(expand_speed, function() {
            if (link.hasClass('close')) {
                //if the module was CLOSED and is OPENING                               
                if(link.parents('#footer').length > 0){
                    $('#footer a.expand_link').attr('class', link_classes + ' open');                   
                    //link.html("Click to hide list");
                }else{
                     link.attr('class', link_classes + ' open');
                }                                
            } else {
                //if the module was OPEN and is CLOSING
                if(link.parents('#footer').length > 0){
                    $('#footer a.expand_link').attr('class', link_classes + ' close');
                    //link.html("Click to show list");
                }else{
                     link.attr('class', link_classes + ' close');
                }  
            } 
        });

        return false;
    });
}


//--------------------------------------------- SLIDING TABS
function slideTabs() {
    $("#tab_navigation_container li a").hover(
    function(){            
        if (!$(this).parent().hasClass('current')){
            $(this).stop();
            $(this).animate({top:"0px",backgroundColor:"#b6afa3"}, 100);     
        }   
    },
    function(){ 
        if (!$(this).parent().hasClass('current')){        
            $(this).stop();  
            $(this).animate({top:"10px",backgroundColor:"#9e9484"}, 100);     
        } 
    })
};


//--------------------------------------------- HEADER NAVIGATION DROPDOWN
function showDropNav() {
    $('.dropdown_navigation > li').hover(    
	    function() {
	        window.clearTimeout(header_supportnav_timer);
	        hideDropNav();

	        $('ul', this).css('display', 'block');
	        $('div.droplink_navigation_container', this).css('display', 'block');
	        if ($(this).hasClass('droplink')) {
	            $(this).addClass('current');
	        }
	    }, function() {
	        if ($(this).hasClass('droplink')) {
	            header_supportnav_timer = window.setTimeout('hideDropNav(".dropdown_navigation li.droplink")', header_supportnav_timer_duration);
	        } else {
	            hideDropNav();
	        }
	    });
};

function hideDropNav(current_id) {
    if (arguments.length > 0) {
        $(current_id).find('ul').hide();
        $(current_id).find('div.droplink_navigation_container').hide();
        $(current_id).removeClass('current');
    } else {        
        $('#main_navigation li div.droplink_navigation_container').hide();
        $('.dropdown_navigation > li').removeClass('current');
    }
};


//--------------------------------------------- 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("");
            }
        });
    });
}
