﻿$(function ()
{
    // add padding to main content area if there are no tabs
    if ($(".tabs").length == 0)
    {
        $("#main-content").css("padding-top", "1em");
    }
    else
    {
        // add click event handler to tabs
        $(".tabs li a").click(function ()
        {
            $(".tabs li").removeClass("selected");
            $(this).closest("li").addClass("selected");
            $(".tab").hide();
            $($(this).attr("href")).show();
            return false;
        });

        // preselect tab if tab id passed in querystring
        var tabId = $.urlParam('t');
        if (tabId != "")
        {
            $(".tabs li").removeClass("selected");
            $(".tabs li:nth-child(" + tabId + ")").closest("li").addClass("selected");
            $(".tab").hide();
            $("#tab" + tabId).show();
        }

        // show non-empty tabs
        for (var i = 1; i <= 7; i++)
        {
            var tabHtml = $("#tab" + i).html();
            if (tabHtml != null && tabHtml.indexOf('<') != -1)
            {
                $(".tabs a[href=#tab" + i + "]").closest("li").show();
            }
        }
    }
});

// function to get querystring parameters
$.urlParam = function (name)
{
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) { return 0; }
    return results[1] || 0;
}
