/*================================================== GLOBAL JAVASCRIPT */
/*
Javascript Document for Hogan Lovells
Build: Pete Robelou on behalf of SAS - 26/07/2010
*/

// INITIATE CUFON
Cufon.replace('.dropshadow_padding p.title');
Cufon.replace('h1');
Cufon.replace('h2');
//Cufon.replace('h3');
//Cufon.replace('p.intro');
//Cufon.replace('a.expandable_link', {hover: true});
Cufon.replace('.title_rhm');

$('html').addClass('js');

// GLOBAL VARIABLES
var droplink_navigation_timer;
var droplink_navigation_timer_duration = 300;

$(document).ready(function () {
    /*////////////////////////////////////// INIT */
    //------------------------ FIX IE6 BACKGROUND IMAGE CACHING PROBLEM
    if (jQuery.browser.msie) {
        try {
            document.execCommand("BackgroundImageCache", false, true);
        } catch (err) { }
    }

    /* TOP NAVIGATION DROPDOWN */
    showDroplinkNavigation();
    showDroplinkNavigationPosition();
    highlightCurrentPage();

    /* FOOTER POPUP NAVIGATION */
    footerPopupNavigation();
    footerPopupNavigationPreview();

    /* INFORMATION SLIDER */
    informationSlider();

    /* EXPANDABLE CONTENT */
    expandableColorChange();
    expandableContent();

    /* TABS NAVIGATION */
    tabsNavigation();

    /* HOTSPOT POPUP */
    hotspotPopup();

    /* ANCHOR SMOOTH SCROLL */
    $('#master_container').localScroll({ offset: { left: 0, top: -90} });

    /* TRANSCRIPT */
    transcript();
});


//--------------------------------------------- TOP NAVIGATION DROPDOWN
function showDroplinkNavigation() {
    $('#top_navigation > li').hover(
	    function () {
	        window.clearTimeout(droplink_navigation_timer);
	        hideDroplinkNavigation();
	        $('.droplink_navigation_container', this).css('display', 'block');
	        $(this).addClass('selected');
	    },
		function () {
		    if ($(this).hasClass('selected')) {
		        droplink_navigation_timer = window.setTimeout('hideDroplinkNavigation("#top_navigation > li.selected")', droplink_navigation_timer_duration);
		    } else {
		        hideDroplinkNavigation();
		    }
		});
};

function hideDroplinkNavigation(current_id) {
    if (arguments.length > 0) {
        $(current_id).find('div.droplink_navigation_container').hide();
        $(current_id).removeClass('selected');
    } else {
        $('#top_navigation li div.droplink_navigation_container').hide();
        $('#top_navigation > li').removeClass('selected');
    }
};

function showDroplinkNavigationPosition() {
    $('#top_navigation > li').hover(function () {
        var linkPositionX = $("a", this).offset().left;
        var linkWidth = $("a", this).width();
        var viewPortWidth = $(window).width();
        var navigationWidth = $(".droplink_navigation_container", this).width();
        var availableWidth = viewPortWidth - linkPositionX;
        var safePosition = navigationWidth - linkWidth - 29;

        if (availableWidth < navigationWidth) {
            $(".droplink_navigation_container", this).css("left", "-" + safePosition + "px");
        };
    });
}

function highlightCurrentPage() {
    var pageId = $("body").attr("id");
    var subPageId = $("body").attr("id");
    var subSubPageId = $("body").attr("id");

    //top navigation
    if (pageId != "") {
        var pageId = pageId.slice(5, 7);
        if (pageId != "0") {
            var currentItem = "#top_nav_" + pageId;
            $("#top_navigation").find(currentItem).addClass("current");         
        }
    };
    //left hand navigation
    if (subPageId != "") {
        var subPageId = subPageId.slice(8, 10);
        if (subPageId != "0") {
            var currentItem = "#left_nav_" + subPageId;
            $("#left_navigation").find(currentItem).addClass("current");
        };
    };
    //left hand navigation - second level
    if (subSubPageId != "") {
        var subSubPageId = subSubPageId.slice(11);
        if (subSubPageId != "0") {
            var currentItem = ".sub_left_nav_" + subSubPageId;
            $("#left_navigation").find(currentItem).addClass("current");
        };
    };
}

//--------------------------------------------- FOOTER POPUP NAVIGATION
function footerPopupNavigation() {
    $("#init_slider").click(function () {
        $(this).toggleClass("initiated");
        $("#footer_popup_navigation_slider").stop(true, true).slideToggle("fast");
        return false;
    });
}

function footerPopupNavigationPreview() {
    var backgroundPositionIncrement = 92;
    $("#footer_popup_navigation li").hover(function () {
        var itemPosition = $(this).index();
        var backgroundPosition = itemPosition * backgroundPositionIncrement;
        $("#footer_popup_navigation_preview").stop().animate({ backgroundPosition: '0px -' + backgroundPosition + 'px' }, 200);
        $("a span", this).toggle();
    });
}


//--------------------------------------------- INFORMATION SLIDER
function informationSlider() {
    var slideOpenSpeed = 200;
    var slideCloseSpeed = 400;

    $("#content .image_holder").each(function () {

        //IE REQUIRES A HEIGHT FOR THE INFORMATION SLIDER TO WORK IN THE RIGHT HAND MODULES
        if (jQuery.browser.msie) {
            var holderHeight = $(this).height();
            $(this).css("height", holderHeight);
        };

        var infoHeight = $(".information_slider", this).height() + 1;
        $(".information_slider", this).css("bottom", "-" + infoHeight + "px");
    });

    $("#content .image_holder").hover(function () {
        $(this).find(".information_slider").stop().animate({ bottom: -1 }, slideOpenSpeed);
    }, function () {
        var infoHeight = $(this).find(".information_slider").height() + 1;
        $(this).find(".information_slider").stop().animate({ bottom: -infoHeight }, slideCloseSpeed);
    });

    $("#content .information_slider").click(function () {
        var location = $(this).parent(".image_holder").find("a").attr("href");
        document.location = location;
    });
}

//--------------------------------------------- EXPANDABLE CONTENT
function expandableColorChange() {
    var animateOpenSpeed = 150;
    var animateCloseSpeed = 150;

    var bgOpenColor = "#fff";
    if ($(".expandable_content_holder").parent().hasClass("tab_content")) {
        var bgOpenColor = "#e5e5e5";
    }

    var bgCloseColor = "#464646";
    var opacityOpen = 0;
    var opacityClose = 100;

    $(".expandable_content_container .expandable_link").hover(function () {
        if (jQuery.browser.msie !== true) {
            $(this).parents(".expandable_content_container").find(".expandable_wedge img").stop().animate({ opacity: opacityOpen }, animateOpenSpeed);
            $(this).parents(".expandable_content_container").find(".expandable_content_background").stop().animate({ backgroundColor: bgOpenColor }, animateOpenSpeed);
        }
        if (jQuery.browser.msie) {
            $(this).parents(".expandable_content_container").find(".expandable_content_background").css({ backgroundColor: bgOpenColor });
            if (jQuery.browser.version >= 8) {
                $(this).parents(".expandable_content_container").find(".expandable_wedge img").hide();
            }
        }
    }, function () {
        if (jQuery.browser.msie !== true) {
            $(this).parents(".expandable_content_container").find(".expandable_wedge img").stop().animate({ opacity: opacityClose }, 1000);
            $(this).parents(".expandable_content_container").find(".expandable_content_background").stop().animate({ backgroundColor: bgCloseColor }, animateCloseSpeed);
        }
        if (jQuery.browser.msie) {
            $(this).parents(".expandable_content_container").find(".expandable_content_background").css({ backgroundColor: bgCloseColor });
            if (jQuery.browser.version >= 8) {
                $(this).parents(".expandable_content_container").find(".expandable_wedge img").show();
            }
        }
    });
}

function expandableContent() {
    var animateSpeed = 200;

    $(".expandable_content_container .expandable_link").click(function () {
        $(this).parents(".expandable_content_container").find(".expandable_content").stop(true, true).slideToggle(animateSpeed);
        $(this).parents(".expandable_content_container").toggleClass("expanded");
        return false;
    });
}

//--------------------------------------------- TABS NAVIGATION
function tabsNavigation() {
    var tabToLoad = getURLParam('tab');
    if (tabToLoad != "") {
        loadTab(tabToLoad);
    }

    $('#tabs_navigation a').click(function () {
        var tab_id = $(this).parent("li").attr('id').split('tab_link_')[1];
        loadTab(tab_id);
        return false;
    });
}

function loadTab(tab_id) {
    $('#tabs_navigation li.current').removeClass('current');
    $('#tabs_navigation li#tab_link_' + tab_id).addClass('current');
    $('.tab_content.current').removeClass('current');
    $('#tab_content_' + tab_id).addClass('current');
}

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;
}

//--------------------------------------------- HOTSPOT POPUP
function hotspotPopup() {
    $(".hotspot").click(function () {
        $(".hotspot_content", this).show();
        $(this).css("z-index", "10");
        $(this).addClass("current");
        //return false; commented out as there is a href within the hotspot that uses an external link
    });
    $(".hotspot").mouseleave(function () {
        $(".hotspot_content", this).hide();
        $(this).css("z-index", "5");
        $(this).removeClass("current");
    });
    $(".hotspot_content").mouseleave(function () {     
        $(this).hide();
        $(this).parent('.hotspot').css("z-index", "5");
        $(this).parent('.hotspot').removeClass("current");
    });
}


//--------------------------------------------- TRANSCRIPT
function transcript() {
    var animateSpeed = 200;
    var link = $(".transcript_toggle");

    $(".transcript_toggle").click(function () {
        if (link.hasClass("open") == true) {            
            $(this).html("View transcript");
            $(this).removeClass("open");
            $(this).parent("li").find(".transcript_holder").stop(true, true).slideUp(animateSpeed);
        } else {            
            $(this).html("Close transcript");
            $(this).addClass("open");
            $(this).parent("li").find(".transcript_holder").stop(true, true).slideDown(animateSpeed);
        }
        return false;
    });
}
