/*=============================    
common.css
LCP Corporate
Julien Decaudin @ SAS - 06/2010
=============================*/

$('html').addClass('js');

$(document).ready(function () {
    /*////////////////////////////////////// INIT */

    //------------------------ FIX IE6 BACKGROUND IMAGE CACHING
    if (jQuery.browser.msie) {
        try {
            document.execCommand("BackgroundImageCache", false, true);
        } catch (err) { }
    }

    //------------------------ FORMS         
    InitInputDefaultValue("#form_search .input_text", ".button", "Search");

    //------------------------ EXPANDING MODULES
    InitExpandableModules();
    InitExpandableServicesModules();

    //------------------------ MAIN NAVIGATION DROPDOWN

    $(function () {
        $("#main_nav>li").hover(function () {
            //$(this).addClass("hover");
            $(this).find('a').addClass("highlight");
            $('div', this).css('display', 'block');
        }, function () {
            //$(this).removeClass("hover");
            $(this).find('a').removeClass("highlight");
                if (!($(this).find('a').hasClass("current"))) {
                    $(this).find('a').removeClass("current");
                } 
            $('div', this).css('display', 'none');
            Cufon.replace('ul#main_nav>li>a', { hover: true });
        });
    });



    //------------------------ TABS NAVIGATION
    //InitTabsNavigation();

    //------------------------ TABLE ROLLOVER
    /*$('.richTextFormat tbody tr').hover(function () {
        $(this).addClass('highlight');
    }, function () {
        $(this).removeClass('highlight');
    });*/

    //------------------------ IE6 ROLLOVERS
    $('#frm_submit, .intro_content, .input_submit_holder button').mouseover(function () {
        $(this).addClass('hover');
    });
    $('#frm_submit, .intro_content, .input_submit_holder button').mouseout(function () {
        $(this).removeClass('hover');
    });

    //------------------------ THEME SWITCHER (DEBUG)
    /*$('body').prepend("<div id='theme_switcher'><ol><li><a id='theme_blue'>Blue</a></li><li><a id='theme_brown'>Brown</a></li><li><a id='theme_red'>Red</a></li><li><a id='theme_green'>Green</a></li><li><a id='theme_purple'>Purple</a></li><li><a id='theme_teal'>Teal</a></li><li><a id='theme_gold'>Gold</a></li><li><a id=''>Generic</a></li></ol></div>");

    $('#theme_switcher  ol li a').click(function () {
        var theme = $(this).attr('id');
        $('body').attr('class', theme);
    });*/

    //------------------------ LOCATION MAP
    var location_open = false;

    $('.office_location').hover(function () {
        $(this).addClass('hover');
        $(this).css('z-index', '1000');
        $(this).find('.spot').css('z-index', '1001');
        var answer = $(this).css("left").split("px")[0];
        var width_answer = $('.locationsmap img').css("width").split("px")[0];
        var gap_space = width_answer - answer;
        var tooltip_width = $('.tooltip').css("width").split("px")[0];
        if (gap_space > tooltip_width) {
            if ($('div.tooltip').hasClass('left_tooltip')) {
                $('div.tooltip').removeClass('left_tooltip');
            }
        }
        else {
            $('div.tooltip').addClass('left_tooltip');
            $(this).find('a').addClass('spot_left');
            $(this).find('a').removeClass('spot');
        }
        $(this).find('.tooltip').show();
    }, function () {
        $(this).removeClass('hover');
        $(this).find('a').removeClass('spot_left');
        $(this).find('a').addClass('spot');
        $(this).css('z-index', '10');
        $(this).find('.tooltip').hide();
    });
    // hide all profiles	
    $('div[id^=container_]').hide();
    $('div[id^=spot_] div.tooltip').hide();
    // open profile function	
    $('div[id^=spot_] a').click(function () {
        var location_clicked = $(this).parent().attr('id').split('spot_')[1];
        $('#container_' + location_clicked).fadeIn('fast');
        location_open = true;
        return false;
    });
    // close profile functions	
    $(document).click(function (event) {
        var target = $(event.target);
        if (location_open && !target.parents('div[id^=container_]').length) {
            $('div[id^=container_]').fadeOut('fast');
            location_open = false;
            return false;
        }
    });
    $('a.close_popup').click(function (event) {
        $(this).parents('div[id^=container_]').fadeOut('fast');
        location_open = false;
        return false;
    });

});

//--------------------------------------------- EXPANDABLE MODULES
function InitExpandableModules() {

    $("a.header_link").click(function () {
        var link = $(this);

        if (link.hasClass('open')) {
            link.parents("div.expanding_module").find("div.container").slideDown("medium");
            link.addClass("close");
            link.removeClass("open");
            Cufon.refresh('.module h2:not(.clean)');
        } else {
            link.parents("div.expanding_module").find("div.container").slideUp("medium", function () {
                link.addClass("open");
                link.removeClass("close");
                Cufon.refresh('.module h2:not(.clean)');
            });
        }
        return false;

    });
}
//--------------------------------------------- EXPANDABLE SERVICES MODULES
function InitExpandableServicesModules() {

    $("#services_expanders div.intro_content").click(function () {
        var link = $(this).parent(".expanding_module");

        if (link.hasClass('close')) {
            //if the module was OPEN and is CLOSING
            link.find("div.additional_content").slideUp("medium", function () {
                link.removeClass("close");
                link.find("a.more_detail_link span").text("More detail");
                Cufon.refresh('.intro_content h2');
            });
        } else {
            //if the module was CLOSED and is OPENING
            link.find("div.additional_content").slideDown("medium");
            link.addClass("close");
            link.find("a.more_detail_link span").text("Less detail");
            Cufon.refresh('.intro_content h2');
        }
        return false;
    });
}

//--------------------------------------------- TABS NAVIGATION
function InitTabsNavigation() {
    var tabToLoad = getURLParam('tab');
    if (tabToLoad != "") {
        loadTab(tabToLoad);
    } else {
        loadTab(1);
    }

    $('#tab_links a').click(function () {
        var tab_id = $(this).attr('id').split('tab_link_')[1];
        loadTab(tab_id);
        return false;
    });
}

function loadTab(tab_id) {
    /*	var tabContentPresent = document.getElementById('tabs_content_container');
    if (tabContentPresent == null) {
    $('#tab_links a.current').addClass('current_page');
    }

    $('#tab_links a.current').removeClass('current');
    $('#tab_links a#tab_link_' + tab_id).addClass('current');
    $('.tab_content.current').removeClass('current');
    $('#tab_content_container_' + tab_id).addClass('current'); */

}

//--------------------------------------------- FORMS
/* initialize the inputs which requires a 'default value' system */
function InitInputDefaultValue(input_class, submit_class, default_value) {
    var input_text = $(input_class);

    //Init the input field
    if (input_text.val() == "") {
        input_text.val(default_value);
    }

    //Add input field events
    input_text.click(function () {
        if ($(this).val() == default_value)
            $(this).val("");
    }).blur(function () {
        if ($(this).val() == "")
            $(this).val(default_value);
    });

    //Add form submit event
    $(submit_class).click(function () {
        if (input_text.val() == default_value) {
            //input_text.val("");
            return false;
        }
    });
}

function getURLParam(strParamName) {
    var strReturn = "";
    var strHref = window.location.href;

    if (strHref.indexOf("?") > -1) {
        var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
        var aQueryString = strQueryString.split("&");
        for (var iParam = 0; iParam < aQueryString.length; iParam++) {
            if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1) {
                var aParam = aQueryString[iParam].split("=");
                strReturn = aParam[1];
                break;
            }
        }
    }
    return strReturn;
}

function selectRedirect(select_id) {
    if ($(select_id).val() != '') {
        document.location.href = $(select_id).val();
    }
    return false;
}

function includeScript(scriptUrl) {
    // Loads and executes a local JavaScript file
    $.getScript(scriptUrl);
} 
