/*================================================== GLOBAL JAVASCRIPT */
/*
    Javascript Document for Macfarlanes
    Build: Pete Robelou on behalf of SAS - 24/09/2009
*/

$('html').addClass('js');

$(document).ready(function () {
    /*////////////////////////////////////// INIT */
    //------------------------ FIX IE6 BACKGROUND IMAGE CACHING
    if (jQuery.browser.msie) {
        try {
            document.execCommand("BackgroundImageCache", false, true);
        } catch (err) { }
    }

    //------------------------ SLIDING RED LINE ON NAVIGATION
    $(".lavaLamp").lavaLamp({ fx: "swing", speed: 300, startItem: 0 });

    //------------------------ WIDTH CALCULATIONS FOR SECTION TITLE UNDERLINE
    var section_title_width = $("#section_title_container h1").width() - 20;
    $('#section_title_underline').width(section_title_width);

    var subsection_title_width = $("#subsection_title_container h2").width() - 20;
    $('#subsection_title_underline').width(section_title_width);

    //------------------------ WIDTH CALCULATIONS FOR CONTENT NAVIGATION UNDERLINE
    $("#content_navigation li a").each(function () {
        var content_nav_width = $(this).width() - 7;
        $(this).find('span').width(content_nav_width);
    });

    //------------------------ FORMS         
    InitInputDefaultValue("#form_globalsearch .input_text", ".button_submit", "Search");
    InitInputDefaultValue("#form_peoplesearch .input_text", ".button_submit", "Search by name");
    InitInputDefaultValue("#form_publicationssearch .input_text", ".button_submit", "Keyword");

    //"on change" Dropdown handlers    
    $('.select_practice').sSelect({ ddMaxHeight: '300px' }).change(function () {
        
        //console.log($(this).fieldValue());
        if ($(this).parents('#form_select_expertise').length > 0) {
            selectRedirect('#' + $(this).attr('id'));
        } else {
            var current_form = $(this).parents('form');
            if ($(this).fieldValue() != '0') {
                //Clear input value
                current_form.find('input[type=text]').attr('value', '');

                //submit form        
                current_form.submit();                
            }
        }        
    });

    //People search submit: clear dropdown value
    $('.form_lh_search button[type=submit]').click(function () {
        if ($('.form_lh_search select').fieldValue() != '') {
            $('.form_lh_search select').val('');
        }
    });

    //Registration form submission
    $('div.event_registration_container form button').click(function () {
        //$('#form_validation_container').show();
    });

    //Registration form: Select all link
    $('a.select_all').click(function () {
        $(this).parents('div.expand_module').find('input[type=checkbox]').attr('checked', 'checked');
        return false;
    });

    //------------------------ EXPANDING MODULES
    InitExpandableModules();

    $('div.shadow_module').hover(function () {
        $(this).addClass('hover');
    }, function () {
        $(this).removeClass('hover');
    });

    //------------------------ TABLE ROLLOVER
    $('.richTextFormat tbody tr').hover(function () {
        $(this).addClass('highlight');
    }, function () {
        $(this).removeClass('highlight');
    });

});

//--------------------------------------------- EXPANDABLE MODULES
function InitExpandableModules() {
    $(".expand_link_holder").click(function() {
        var link = $(this).find('a.expand_link');        
        var module_parent_holder = $(this).parents('div.expand_module_holder');
        var module_expand = module_parent_holder.find("div.expand_module");                
        var reveal_item = module_parent_holder.find('p.reveal_expand_item');

        module_expand.slideToggle("fast", function() {                    
            if (link.hasClass('closed')) {
                //if the module was CLOSE and is OPENING
                link.attr('class', 'expand_link opened');                 
                link.find('span.link_open').hide();
                link.find('span.link_close').show();
            } else {
                //if the module was OPEN and is CLOSING
                link.attr('class', 'expand_link closed');                                
                link.find('span.link_open').show();
                link.find('span.link_close').hide();
            }
        });
        
        //reveal item
        if (link.hasClass('closed')) {
                //if the module was CLOSE and is OPENING
                reveal_item.slideDown('fast');
            } else {
                //if the module was OPEN and is CLOSING
                reveal_item.slideUp('fast');
            }
        
        module_parent_holder.toggleClass('open');        

        return false;
    });
}

//--------------------------------------------- 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("");
        }
    });
}

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;
}