Relational.Tabstrip = {};

// tabstripLinkElement_click 
//
// Handles the click event for each tab
//
Relational.Tabstrip.tabstripLinkElement_click = function(evt, context) {
    // Find the tabStrip element (we know it's the parent element of this link)
    var tabstripElement = this.parentNode;
    Relational.Tabstrip.showTabByLink(tabstripElement, this);
    return $r.eventCancel(evt);
}

// Relational.Tabstrip.showTabByLink 
//
// Sets visibility for the panels as needed, and turns the tabs On/Off 
//
Relational.Tabstrip.showTabByLink = function(tabstripElement, tabstripLinkElement) {
    // Fi  nd all our sibling tabs (all links inside the tabStrip)
    var tabElements = tabstripElement.getElementsByTagName('a');
    // Loop through tabs & panels to show/hide
    var i;
    for (i = 0; i < tabElements.length; i++) {
        var tabElement = tabElements[i];
        // Find the corresponding tabPanel (the id of the Tabstrip link was built by prepending "RelationalTab_" to the ClientID of the tabPanel)
        var tabPanelId = tabElement.id.replace(new RegExp("(.*?)RelationalTab_(.*)"), "$2");
        var tabPanel = $get(tabPanelId);
        if (tabElement.id == tabstripLinkElement.id) {
            // it's this tab - turn it on
            $r.changeClassName(tabElement, 'Off', 'On');
            $r.changeClassName(tabPanel, 'Hidden', 'Visible');
            Relational.Tabstrip.setSelectedIndex(tabstripElement, i);
            // if this tabstrip supports persistence update our cookie with the currently selected tab index
            if (tabstripLinkElement.cookieName != null) $r.setCookie(tabstripLinkElement.cookieName, i, $r.daysInFuture(100));
        }
        else {
            // not this tab - turn it off
            $r.changeClassName(tabElement, 'On', 'Off');
            $r.changeClassName(tabPanel, 'Visible', 'Hidden');
        }
    }
    Relational.Tabstrip.setNextPrevVisibility(tabstripElement);
}

Relational.Tabstrip.showTabByName = function(tabstripElement, name) {
    if (typeof (tabstripElement) != 'object') { tabstripElement = $get(tabstripElement) } // Accept ID of element (string) or the element itself
    var tabElements = tabstripElement.getElementsByTagName('a');
    var i;
    for (i = 0; i < tabElements.length; i++) {
        var tabElement = tabElements[i];
        if (tabElement.innerHTML.length > 0 && tabElement.innerHTML.indexOf(name) > -1) { Relational.Tabstrip.showTabByLink(tabstripElement, tabElement) };
    }
}

Relational.Tabstrip.setSelectedIndex = function(tabstripElement, i) {
    // Find our currentPanelHolder hidden field (it should be the only input element in the tabstrip)
    var currentPanelHolder = tabstripElement.getElementsByTagName('input')[0];
    // Store selected panel's index in our hidden field
    currentPanelHolder.value = parseInt(i);
}

Relational.Tabstrip.getSelectedIndex = function(tabstripElement) {
    // Get selected panel's index from our hidden field
    var currentPanelHolder = tabstripElement.getElementsByTagName('input')[0];
    return parseInt(currentPanelHolder.value);
}

Relational.Tabstrip.getTabCount = function(tabstripElement) {
    return tabstripElement.getElementsByTagName('a').length;
}

Relational.Tabstrip.showTabByIndex = function(tabstripElement, i) {
    var tabElements = tabstripElement.getElementsByTagName('a');
    if (i >= 0 && i < tabElements.length) {
        Relational.Tabstrip.showTabByLink(tabstripElement, tabElements[i]);
    }
}

Relational.Tabstrip.setNextPrevVisibility = function(tabstripElement) {
    var hideNextButtons = Relational.Tabstrip.getSelectedIndex(tabstripElement) == Relational.Tabstrip.getTabCount(tabstripElement) - 1;
    var nextButtons = $r.getElementsByClassName('a', tabstripElement.id + "_Next");
    for (j = 0; j < nextButtons.length; j++) {
        var nextButton = nextButtons[j];
        if (hideNextButtons == true) {
            $r.changeClassName(nextButton, 'Visible', 'Hidden');
        }
        else {
            $r.changeClassName(nextButton, 'Hidden', 'Visible');
        }
    }
    var hidePreviousButtons = Relational.Tabstrip.getSelectedIndex(tabstripElement) == 0;
    var previousButtons = $r.getElementsByClassName('a', tabstripElement.id + "_Previous");
    for (j = 0; j < previousButtons.length; j++) {
        var previousButton = previousButtons[j];
        if (hidePreviousButtons == true) {
            $r.changeClassName(previousButton, 'Visible', 'Hidden');
        }
        else {
            $r.changeClassName(previousButton, 'Hidden', 'Visible');
        }
    }

}

Relational.Tabstrip.nextButton_click = function(evt, context) {
    Relational.Tabstrip.showTabByIndex(context.tabstripElement, Relational.Tabstrip.getSelectedIndex(context.tabstripElement) + 1);
    window.scrollTo(0, 0);
    return $r.eventCancel(evt);
}

Relational.Tabstrip.previousButton_click = function(evt, context) {
    Relational.Tabstrip.showTabByIndex(context.tabstripElement, Relational.Tabstrip.getSelectedIndex(context.tabstripElement) - 1);
    window.scrollTo(0, 0);
    return $r.eventCancel(evt);
}

// wireUpTabstrips
//
// Finds all tabstrips on the page, wire the buttons in them 
//
Relational.Tabstrip.wireUpTabstrip = function(elementId, cookieName, selectedIndex) {
    // Find all tagstrips in the document and wire them up
        var tabstripElement = $("#"+ elementId)[0];
        // Loop through link elements in the tabstrip to wire them up
        var tabstripLinkElements = $(tabstripElement).find("a");
        var j;
        for (j = 0; j < tabstripLinkElements.length; j++) {
            var tabstripLinkElement = tabstripLinkElements[j];
            // Check whether to store state in a cookie
            if (cookieName && cookieName != '') {
                tabstripLinkElement.cookieName = cookieName
            };
            // Wire up handler
            $addHandler(tabstripLinkElement, 'click', Function.createCallback(Relational.Tabstrip.tabstripLinkElement_click, {}));
        }
        // Add next button handler 
        var nextButtons = $r.getElementsByClassName('a', tabstripElement.id + "_Next");
        for (j = 0; j < nextButtons.length; j++) {
            var nextButton = nextButtons[j];
            $clearHandlers(nextButton);
            $addHandler(nextButton, 'click', Function.createCallback(Relational.Tabstrip.nextButton_click, { "tabstripElement": tabstripElement }));
        }
        // Add previous button handler 
        var previousButtons = $r.getElementsByClassName('a', tabstripElement.id + "_Previous");
        for (j = 0; j < previousButtons.length; j++) {
            var previousButton = previousButtons[j];
            $clearHandlers(previousButton);
            $addHandler(previousButton, 'click', Function.createCallback(Relational.Tabstrip.previousButton_click, { "tabstripElement": tabstripElement }));
        }

        Relational.Tabstrip.setNextPrevVisibility(tabstripElement);
        if (selectedIndex >= 0) { Relational.Tabstrip.showTabByIndex(tabstripElement, selectedIndex); }

}

$("._TabstripLink").live("click", function(event) {
    var tabstripName = $(this).attr("_TabstripName");
    var tabName = $(this).attr("_TabName");
    Relational.Tabstrip.showTabByName(tabstripName, tabName);
    event.preventDefault();
});

