//==========================================================================================================================================
//
//	Change History
//	==============
// 25/09/2009  DC   XX   Added the hash check on the Url requested to see if it is to toggle the tab which is viewed
// 11/09/2009  RT   XX    Refactored the selected tab update, and use a cookie to store the 'current' tab for a page
// 03/09/2009  RT   XX    Added facility for items with a class of 'tabSwitch' and an id of 'tabSwitch{n}' (where n is the id of the tab to open) to open a
//                                             tab.
//==========================================================================================================================================


jQuery(document).ready(function() {

    // See if there is a selected value in the cookie
    var selTab = GetCookie(GetCookieName());

    var selTabIndex = 0;
    if (selTab != null) {
        selTabIndex = parseInt(selTab);
    }

    var $tabs = $('#tabs').tabs({ selected: selTabIndex });

    $('#tabs li.ui-state-active').next('#tabs li').css('background-position', 'left bottom');
    $('#tabs li.ui-state-active').prev('#tabs li').children('a').css('background-position', 'right bottom');

    $('#tabs a').click(function() {
        UpdateSelectedTab();
        return false;
    });

    var $tabs = $('#tabs').tabs();

    /*
    The following checks the url for a hash ('#') entry, and inturn checks to see if it is a tab switch request
    */
    if (document.location.hash != '') {
        //get the URL hash
        tabSelect = document.location.hash.substr(1, document.location.hash.length);
        // Check to see if it is for switching tabs
        if (tabSelect.substr(0, 5) == "tabs-") {
            tabSelect = tabSelect.replace("tabs-", "");
            $tabs.tabs('select', tabSelect);
            UpdateSelectedTab();
        }
    }

    /*
    This function is used to toggle the tab visibility
    To use it, the toggle button/link should have a class of 'tabSwitch'
    and an id of tabSwitchN, where N is replaced with the index of the tab to open
    */
    $('.tabSwitch').click(function() { // bind click event to link.
        var tabIndex = parseInt(this.id.replace("tabSwitch", ""));
        $tabs.tabs('select', tabIndex); // switch to second tab
        UpdateSelectedTab();
        return false;
    });

    ///<summary>
    /// Returns the name of the cookie to use for storing the selected tab
    /// </summary>
    /// <returns>the name of the cookie to use for storing the selected tab</returns>
    function GetCookieName() {
        return document.title + "_selectedTab";
    }

    ///<summary>
    /// UpdateSelectedTab, updates the selected tabs visual appearance, and sets a cookie to store which one is selected
    /// </summary>
    function UpdateSelectedTab() {
        $('#tabs li').css('background-position', 'left top');
        $('#tabs li a').css('background-position', 'right top');

        $('#tabs li.ui-state-active').css('background-position', 'left -38px');
        $('#tabs li.ui-state-active a').css('background-position', 'right -38px');

        $('#tabs li.ui-state-active').next('#tabs li').css('background-position', 'left bottom');
        $('#tabs li.ui-state-active').prev('#tabs li').children('a').css('background-position', 'right bottom');

        var tabIndex = $tabs.tabs('option', 'selected');
        SetCookie(GetCookieName(), tabIndex);
    }
});