﻿$(function ()
{
    formatFooterServiceList();
    formatMainMenuDropdowns();
    setupSearchOnEnter();
});

function formatFooterServiceList()
{
    // add a break to a few specific services
    var link;
    link = $("#ft .service_list a:contains(Commercial litigation)");
    link.html(link.html().replace(" and", "<br />and"));
    link = $("#ft .service_list a:contains(Accredited Specialists)");
    link.html(link.html().replace(" in", "<br />in"));
    link = $("#ft .service_list a:contains(Conveyancing and)");
    link.html(link.html().replace(" and", "<br />and"));

    // make all the items in the same row the same height so they wrap correctly
    var height = 0;
    var row = 0;
    $("#ft .service_list li").each(function (i, e)
    {
        if (height < $(e).height())
        {
            height = $(e).height();
        }
        if ((i + 1) % 4 == 0)
        {
            row = (i + 1) / 4;
            var li = $(e);
            for (var i = 0; i < 4; i++)
            {
                li.height(height);
                li = li.prev();
            }
            //alert("tallest in row " + row + " is " + height + "px");
            height = 0;
        }
    });
}

function formatMainMenuDropdowns()
{
    // add css class to first item of each submenu
    $("#main_menu li li:first-child").addClass("first");

    // set width of main menu dropdown items to 
    // be all equal to the widest item
    $("#main_menu li").each(function ()
    {
        var width = 0;
        $("li a", $(this)).each(function ()
        {
            if (width < $(this).width())
            {
                width = $(this).width();
            }
        });
        $("li a", $(this)).width(width);
    });
}

function setupSearchOnEnter()
{
    $('[id*=txtSearch]').keypress(function (e)
    {
        if (e.keyCode == 13)
        {
            window.location = $("[id*=btnSearch]").attr("href");
            return false;
        }
    });
}

$.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;
}
