﻿$(function ()
{
    stretchMenu();
    setupMainMenuHover();
    setMainMenuDropdownWidths();
    addModalPopupToRSSLinks();
});

function stretchMenu()
{
    // stretch menu to 100% width
    var containerWidth = $("#hd .menu").width();
    var menuWidth = $("#hd .menu > ul").width();
    var diff = containerWidth - menuWidth;
    var menuItems = $("#hd .menu > ul > li > a");
    var extraPadding = parseInt(diff / (menuItems.length * 2));
    var leftOver = diff % (menuItems.length * 2)

    // add padding to all tabs
    $.each(menuItems, function (i, menuItem)
    {
        var paddingLeft = parseInt($(menuItem).css("padding-left"));
        var paddingRight = parseInt($(menuItem).css("padding-right"));
        $(menuItem).css("padding-left", paddingLeft + extraPadding + "px")
        $(menuItem).css("padding-right", paddingRight + extraPadding + "px");
    });

    // add left over padding to first tab
    var firstMenuItem = menuItems.eq(0);
    var paddingLeft = parseInt(leftOver / 2);
    var paddingRight = paddingLeft + (leftOver % 2);
    firstMenuItem.css("padding-left", parseInt(firstMenuItem.css("padding-left")) + paddingLeft + "px");
    firstMenuItem.css("padding-right", parseInt(firstMenuItem.css("padding-right")) + paddingRight + "px");

    //alert($("#hd .menu").width() - $("#hd .menu > ul").width());
    //alert($("#hd .menu > ul > li").length);
}

function setSelectedMenuItem(itemText)
{
    if (itemText != "")
    {
        $("#hd .menu > ul > li > a:contains(" + itemText + ")").parent().addClass("selected");
    }
}

function setupMainMenuHover()
{
    // use hover class instead of css ":hover" property for browser compatability
    $("#hd > .menu > ul > li").hover(function ()
    {
        $(this).addClass("hover");
        if ($(this).find("ul").length > 0)
        {
            $(this).find("ul").fadeIn(100);
        }
    },
    function ()
    {
        $(this).removeClass("hover");
        if ($(this).find("ul").length > 0)
        {
            $(this).find("ul").fadeOut(100);
        }
    });
}

function setMainMenuDropdownWidths()
{
    // set width of dropdown menus to the width of the parent menu item (except lawyers which is wider)
    $("#hd > .menu > ul > li").each(function ()
    {
        if ($(this).children("[href=/Lawyers.aspx]").length > 0)
        {
            $(this).find("ul li a").width(250); // there is 20px of padding in the css
        }
        else
        {
            $(this).find("ul li a").width($(this).width() - 20); // there is 20px of padding in the css
        }
    });
}

$.urlParam = function(name)
{
    // function to retrieve querystring variables
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) { return 0; }
    return results[1] || 0;
}

function showModalPopup()
{
    var popupWidth = 480;
    var popupHeight = 360;
    var redirectUrl = "/";

    $("#hiddenModalContent #btnYes").click(function ()
    {
        $.cookie("IsInACT", "1");
        tb_remove();
        return false;
    });
    $("#hiddenModalContent #btnNo").click(function ()
    {
        $.cookie("IsInACT", "0");
        window.location = redirectUrl;
        return false;
    });

    // don't show popup for googlebot
    if (navigator.userAgent.toLowerCase().indexOf('googlebot') == -1)
    {
        if ($.cookie("IsInACT") == null)
        {
            tb_show("", "#TB_inline?height=" + popupHeight + "&width=" + popupWidth + "&inlineId=hiddenModalContent&modal=true", "");
        }
        else if ($.cookie("IsInACT") == "0")
        {
            window.location = redirectUrl;
        }
    }
}

function addModalPopupToRSSLinks()
{
    $("[href*=RSS.aspx]").click(function ()
    {
        var popupWidth = 480;
        var popupHeight = 360;
        var redirectUrl = "/";
        var rssUrl = $(this).attr("href");

        $("#hiddenModalContent #btnYes").click(function ()
        {
            $.cookie("IsInACT", "1");
            tb_remove();
            window.location = rssUrl;
        });
        $("#hiddenModalContent #btnNo").click(function ()
        {
            $.cookie("IsInACT", "0");
            window.location = redirectUrl;
            return false;
        });

        // don't show popup for googlebot
        if (navigator.userAgent.toLowerCase().indexOf('googlebot') == -1)
        {
            if ($.cookie("IsInACT") == null)
            {
                tb_show("", "#TB_inline?height=" + popupHeight + "&width=" + popupWidth + "&inlineId=hiddenModalContent&modal=true", "");
            }
            else if ($.cookie("IsInACT") == "0")
            {
                window.location = redirectUrl;
            }
            else if ($.cookie("IsInACT") == "1")
            {
                return true;
            }
        }

        return false;
    });
}
