//Apply the Javascript-enabled styles (hiding expandable boxes for instance)
$('html').addClass('js'); 

//Global variables
var var_timer;

$(document).ready(function () {
    // Fix IE6 background image caching problem
    if (jQuery.browser.msie) {
        try {
            document.execCommand("BackgroundImageCache", false, true);
        } catch (err) { }
    }

    //IE iframe background transparent fix    
    if ($.browser.msie) {
        var theframes = $('iframe');
        for (var i = 0; i < theframes.length; i++) {
            theframes[i].setAttribute("allowTransparency", "true");
        }
    }

    //Shadowbox init
    var options = {
        keysClose: ['c', 27], // c or esc 
        displayNav: false,
        modal: false
    };

    Shadowbox.init(options);

    //--------------------------------------------- Anchors smooth scroll
    $('.content').localScroll();

    //Forms        
    InitInputDefaultValue(".defaultvalue_input");
    InitSubmitDefaultValue(".defaultvalue_submit", ".defaultvalue_input");

    //--- Adjust landing page modules height to the deeper one
    AdjustModuleHeight();

    //Expandable items
    InitExpandableModules();

    //Language selector        
    $('#language_selector span').hover(function () {
        window.clearTimeout(var_timer);
        $('#language_selector li.language').show('fast');
    }, function () {
        var_timer = window.setTimeout('hideLanguages()', 800);
    });

    $('#language_selector li.language').hover(function () {
        window.clearTimeout(var_timer);
        $('#language_selector li.language').show('fast');
    }, function () {
        var_timer = window.setTimeout('hideLanguages()', 800);
    });

    //Homepage Flash alternative
    $('div.hp_flashalt_group').hover(function () {
        var current_group_id = $(this).attr('id');

        $('div.hp_flashalt_group').each(function () {
            if ($(this).attr('id') != current_group_id && $(this).attr('class') != "current") {
                $(this).find('a.hp_flashalt_link img.on').hide();
                $(this).find('a.hp_flashalt_link img.off').show();
            }
        });
    }, function () {
        $('div.hp_flashalt_group').find('a.hp_flashalt_link img.off').hide();
        $('div.hp_flashalt_group').find('a.hp_flashalt_link img.on').show();
    });

    //Locations Flash alternative
    $('div#module_container_locations div.module_text').hover(function () {
        $(this).find('img.off').hide();
        $(this).find('img.on').show();
    }, function () {
        $(this).find('img.on').hide();
        $(this).find('img.off').show();
    });

    // PDF lists
    $('.vertical .sections').each(function () {
        if ($(this).find('li ul').length) {
            $(this).find('li ul').hide();

            // Add more link
            $(this).after('<p class="more">...more...</p>');
        }
    });

    // More link
    $('.vertical h3').click(function () {
        var listItems = $(this).next().find('li ul');
        var currentItem = $(this);
        if (listItems.css('display') == 'none') {
            currentItem.siblings('.more').slideUp('fast');
            listItems.slideDown('fast', function () {
                currentItem.addClass('expanded');
            });

        } else {
            currentItem.siblings('.more').slideDown('fast');
            listItems.slideUp('normal', function () {
                currentItem.removeClass();
            });
        }
    });

    // Nav links on vacancies page
    $('#job-nav li a').click(function () {
        var href = $(this).attr('href');
        $('#frame').attr('src', href);
        return false;
    });

    //========= Accordion
    var accordion_default_index = 0;

    //check if there is an anchor set
    if (getURLAnchor() != '') {
        $('#accordion a.link_accordion').each(function (n) {
            if ($(this).attr('id') == getURLAnchor()) {
                accordion_default_index = n;
            }
        });
    }

    $('#accordion').accordion({
        collapsible: true,
        active: accordion_default_index,
        autoHeight: false
    });

    //console.log(accordion_default_index);

    //--------------------------------------------- Europe map    
    /* Map action handlers */
    $('map#europemap area').hover(function () {
        var area_id = $(this).attr('id').split('area_')[1];
        var area_alt = $(this).attr('alt');
        $('#img_europemap').attr('src', '/images/europemap/europemap_' + area_alt + '.png');
        $('#location_' + area_id).addClass('hover');

    }, function () {
        var area_id = $(this).attr('id').split('area_')[1];        
        $('#img_europemap').attr('src', '/images/europemap/europemap_default.png');
        $('#location_' + area_id).removeClass('hover');
    });
    
    /* Navigation action handlers */
    $('div.module_location').hover(function () {
        var area_id = $(this).attr('id').split('location_')[1];
        var area_alt = $('map#europemap area[id=area_' + area_id + ']').attr('alt');
        $('#img_europemap').attr('src', '/images/europemap/europemap_' + area_alt + '.png');
        $(this).addClass('hover');
    }, function () {
        $('#img_europemap').attr('src', '/images/europemap/europemap_default.png');
        $(this).removeClass('hover');
    });

    $('div.module_location').click(function () {
        var area_id = $(this).attr('id').split('location_')[1];
        document.location.href = $('#area_' + area_id).attr('href');
    });
});

function AdjustModuleHeight(){
    var max_module_height = 0;    
    
    //get the deeper module
    $('div.module_text_inner', 'div[class^=module_container_]').each(function(){
        if($(this).outerHeight() > max_module_height){
            max_module_height = $(this).outerHeight();
        }
    });
    
    //remove padding
    max_module_height = max_module_height - 20;
    
    //apply the same height to every modules
    if(max_module_height > 0){
        $('div.module_text_inner', 'div[class^=module_container_]').each(function(){            
            if($(this).attr('style') == ''){
                $(this).height(max_module_height);
            }    
        }); 
    }
}

function ScrollTo(target){
    $.scrollTo(target, 1000);
}

function InitExpandableModules() {
    $('.default_open').find('a.header_link').addClass("open");

    $("ul.expand_list li a.header_link").click(function() {
        var link = $(this);
        var module_holder = $(this).find("+ div.expand_content");                

        module_holder.slideToggle("fast", function() {                    
            link.toggleClass('open');
        });
        return false;
    });
}

function openPopup(url, targetName, width, height){    
    var options = "scrollbars=1, height=" + height + ", width=" + width + ", resizable=0";    
    window.open(url, targetName, options);  
    return false;  
}

function hideLanguages()
{
    $('#language_selector li.language').hide('fast');
}
 
//--------------------------------------------- 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 = $(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("");
            }
        });        
    });
}

function selectRedirect(dropdown)
{
    if (dropdown.val() != ''){
        document.location.href = dropdown.val();
    }
    return false;
}

function getURLAnchor(){
    var strReturn = "";
    var strHref = window.location.href;

    if ( strHref.indexOf("#") > -1 ){        
        var strReturn = strHref.substr(strHref.indexOf("#") + 1).toLowerCase();        
    }
    return strReturn;
}
