﻿// catches the tab parameter in the querystring and selects the corresponding tab on the page
// It also shows the correct pagers associated with the panel.
jQuery(function() {
    jQuery("#product_tabs").tabs();
    var selectedTabIndex = getTabIndex();

    if (selectedTabIndex == 0)
        SetPagerVisibilty(false);
    else
        SetPagerVisibilty(true)

    jQuery('#product_tabs').tabs('select', selectedTabIndex);
});

function getTabIndex(){
    var tabQueryValue = GetQueryParam('tab');
    return GetQueryParam('tab') != null ? parseInt(tabQueryValue) : 0;
}

// replaces the tab querystring parameter in the tabIndex parameter passed to this function
function gotoTabPage(tabIndex) {
    var newLocation;
    var oldTabIndex = getTabIndex();
    var currentLocation = window.location.toString();

    if (currentLocation.indexOf('?') == -1) {
        newLocation = currentLocation + '?tab=' + tabIndex;
    }
    else {
        if (currentLocation.indexOf('?tab=' + oldTabIndex) != -1)
            newLocation = currentLocation.replace('?tab=' + oldTabIndex, '?tab=' + tabIndex);
        else {
            if (currentLocation.indexOf('&tab=' + oldTabIndex) != -1)
                newLocation = currentLocation.replace('&tab=' + oldTabIndex, '&tab=' + tabIndex);
            else
                newLocation = currentLocation + '&tab=' + tabIndex;
        }
    }
    window.location = newLocation;
}

// Gets the value of the tab querystring parameter
function GetQueryParam(parameter) {
    var p = escape(unescape(parameter));
    var regex = new RegExp("[?&]" + p + "(?:=([^&]*))?", "i");
    var match = regex.exec(window.location.search);
    var value = null;
    if (match != null) {
        value = match[1];
    }
    return value;
}

// function that sets the display for the correct pager.
function SetPagerVisibilty(archived) {

    if (archived) {
        jQuery("#divBottomPagerRecent").hide();
        jQuery("#divTopPagerRecent").hide();
        jQuery("#divBottomPagerArchived").show();
        jQuery("#divTopPagerArchived").show();
    }
    else {
        jQuery("#divBottomPagerRecent").show();
        jQuery("#divTopPagerRecent").show();
        jQuery("#divBottomPagerArchived").hide();
        jQuery("#divTopPagerArchived").hide();
    }
}
