﻿$(function ()
{
    // add class 'first' to first item in side box lists
    $(".side_box li:first-child").addClass("first");

    // setup tab click event handler
    $("#tabs ul.tab_list a, #tabs ul.tab_list_bottom a").click(function ()
    {
        // reset tabs 
        $("#tabs ul li").removeClass("on");
        $("#tabs ul li").removeClass("first_on");
        $("#tabs ul li").removeClass("last_on");

        // update both sets of tabs when either is clicked
        $("a[href*=" + $(this).attr("href") + "]").each(function ()
        {
            var liParent = $(this).closest("li");
            liParent.closest("li").addClass("on");
            $("#tabs .tab_body").hide();
            $($(this).attr("href")).show();
            $(this).blur();

            // add curved bg image to first and last tabs
            if (liParent.hasClass("first"))
            {
                // first tab
                liParent.addClass("first_on");
            }
            else if (liParent.hasClass("last"))
            {
                // last tab
                liParent.addClass("last_on");
            }
        });

        return false;
    });

    // set active tab
    var activeTabID = location.href.substring(location.href.indexOf("#") + 1, location.href.length);
    $("#tabs ul.tab_list a[href=#tabs_" + activeTabID + "]").click();

    // update all tab body links to remember the active tab id
    $(".tab_body a").click(function ()
    {
        // store active tab id in querystring
        location.href = location.pathname + "#" + $(this).closest(".tab_body").attr("id").replace("tabs_", "");
    });

    // highlight selected side menu item
    $(".side_box a[href=" + decodeURI(location.pathname + location.search).replace(/'/g, "%27") + "]").addClass("active");

    // add class 'last' to last side button
    $(".side_button:last").addClass("side_button_last");

    // split footer service lists into 2 columns
    // personal service list
    var leftColHeight = 0;
    var rightColHeight = 0;
    $("#ft .personal_service_list li").each(function (i, e)
    {
        if (leftColHeight <= rightColHeight)
        {
            $(this).css({ "float": "left", "width": "212px" });
            leftColHeight += $(this).height();
        }
        else
        {
            $(this).css("float", "right");
            rightColHeight += $(this).height();
        }
    });
    // business service list
    leftColHeight = 0;
    rightColHeight = 0;
    $("#ft .business_service_list li").each(function (i, e)
    {
        if (leftColHeight <= rightColHeight)
        {
            $(this).css("float", "left");
            leftColHeight += $(this).height();
        }
        else
        {
            $(this).css({ "float": "right", "width": "172px" });
            rightColHeight += $(this).height();
        }
    });
    //$("#ft .personal_service_list li:odd").css("width", "212px");
    //$("#ft .business_service_list li:odd").css("width", "172px");

    // format quote in featured staff member box
    $(".featured_box .body .quote p:first").prepend("\"");
    $(".featured_box .body .quote p:last").css("margin-bottom", "0").append("\"");

    // format testimonial box
    $(".testimonial p:first").prepend("\"");
    $(".testimonial p:last").css("margin-bottom", "0").append("\"");

    // make dropdown menu at least as wide as parent menu item
    $("#main_menu .sub_menu").each(function ()
    {
        var submenuWidth = $(this).outerWidth();
        var paddingWidth = $(this).outerWidth() - $(this).width();
        var parentWidth = $(this).closest("li").width();
        if (parentWidth > submenuWidth)
        {
            $(this).width(parentWidth - paddingWidth - 14);
        }
    });
});
