﻿
/* NAMESPACE Relational.Tabstrip */

/* Classes used by the Relational.Controls.Tabstrip control
/* Depends on ASP.NET Ajax Client-side framework (should be invoked through a ScriptManager object) */
/* Depends on Relational.Utility */

Type.registerNamespace('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 Relational.Utility.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
            Relational.Utility.changeClassName (tabElement, 'Off', 'On');
            Relational.Utility.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) Relational.Utility.setCookie(tabstripLinkElement.cookieName,i,Relational.Utility.daysInFuture(100));
        }
        else {
            // not this tab - turn it off
            Relational.Utility.changeClassName (tabElement, 'On', 'Off');
            Relational.Utility.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 = Relational.Utility.getElementsByClassName('a', tabstripElement.id + "_Next");
    for (j = 0; j < nextButtons.length; j++) {
        var nextButton = nextButtons[j];
        if (hideNextButtons == true) {
            Relational.Utility.changeClassName(nextButton, 'Visible', 'Hidden');
        }
        else {
            Relational.Utility.changeClassName(nextButton, 'Hidden', 'Visible');
        }
    }
    var hidePreviousButtons = Relational.Tabstrip.getSelectedIndex(tabstripElement) == 0;
    var previousButtons = Relational.Utility.getElementsByClassName('a', tabstripElement.id + "_Previous");
    for (j = 0; j < previousButtons.length; j++) {
        var previousButton = previousButtons[j];
        if (hidePreviousButtons == true) {
            Relational.Utility.changeClassName(previousButton, 'Visible', 'Hidden');
        }
        else {
            Relational.Utility.changeClassName(previousButton, 'Hidden', 'Visible');
        }
    }
    
    // old mentods which is now broken - looks for elements by id
    //    var nextButton = $get(tabstripElement.id + "_Next");
    //    if (nextButton) {
    //        if (Relational.Tabstrip.getSelectedIndex(tabstripElement) == Relational.Tabstrip.getTabCount(tabstripElement) - 1) {
    //            Relational.Utility.changeClassName(nextButton, 'Visible', 'Hidden');
    //        }
    //        else {
    //            Relational.Utility.changeClassName(nextButton, 'Hidden', 'Visible');
    //        }
    //    }
//    var previousButton = $get(tabstripElement.id + "_Previous");
//    if (previousButton) {
//        if (Relational.Tabstrip.getSelectedIndex(tabstripElement) == 0) {
//            Relational.Utility.changeClassName(previousButton, 'Visible', 'Hidden');
//        }
//        else {
//            Relational.Utility.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 Relational.Utility.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 Relational.Utility.eventCancel(evt);
}

// wireUpTabstrips
//
// Finds all tabstrips on the page, wire the buttons in them 
//
Relational.Tabstrip.wireUpTabstrips = function(cookieName, selectedIndex) {
    if (!document.getElementsByTagName) return false;
    // Find all tagstrips in the document and wire them up
    var tabstripElements = Relational.Utility.getElementsByClassName('div', 'Relational_Tabstrip');
    var i;
    for (i = 0; i < tabstripElements.length; i++) {
        var tabstripElement = tabstripElements[i];

        // Loop through link elements in the tabstrip to wire them up
        var tabstripLinkElements = tabstripElement.getElementsByTagName('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 nextButton = $get(tabstripElement.id + "_Next");
//        if (nextButton) {
//            $clearHandlers(nextButton);
//            $addHandler(nextButton, 'click', Function.createCallback(Relational.Tabstrip.nextButton_click, { "tabstripElement": tabstripElement }));
//        }
        var nextButtons = Relational.Utility.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 previousButton = $get(tabstripElement.id + "_Previous");
//        if (previousButton) {
//            $clearHandlers(previousButton);
//            $addHandler(previousButton, 'click', Function.createCallback(Relational.Tabstrip.previousButton_click, { "tabstripElement": tabstripElement }));
//        }
        var previousButtons = Relational.Utility.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); }

    }
} 

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();