//////////////////////////////////////////////////////////////////////////////////
// TabSupport.js v5.0.0                                                          //
// Copyright ADVIZOR Solutions, Inc. 2005.                                      //
//                                                                              //
// Copying, modifying or distributing all or part of the contents of this file  //
// without express written permission is strictly prohibited.                   //
//////////////////////////////////////////////////////////////////////////////////

//
// Referenced Classes/Rules:
//   Persp_ListFrame     (TabSupport_Init)
//   Persp_List          (TabSupport_Init)
//   Persp_Tab           (TabSupport_Init, GetTabWidth, GetTabLeft)
//   Persp_ActiveTab     (TabSupport_Init)
//

//
// Referenced Element IDs:
//   Page_Pane           (TabSupport_ResizeTabFrame)
//   Persp_ListBtnLeft   (TabSupport_ResizeTabFrame, RefreshScrollArrows, GetVisibleWidthForTabs)
//   Persp_ListBtnRight  (TabSupport_ResizeTabFrame, RefreshScrollArrows)
//   Persp_ListFrame     (TabSupport_ResizeTabFrame, GetVisibleWidthForTabs)
//   Persp_List          (TabSupport_Init, TabSupport_ResizeTabFrame,
//                        GetTabListPos, ScrollTabList, ScrollTabListTo)
//

var m_ScrollSpeed = 1;
var m_StepSize = 10;
var m_ScrollAmt = 200;

var m_ScrollableTabs = false;
var m_TabNodes;

function TabSupport_Init() {
    var divNode;
    var numTabNodes = 0;
    var tabList = "";

    var i = 0;
    var j = 0;

    // Find the number of perspective tabs
    var divNodes = document.getElementsByTagName("div");
    for (i = 0; i < divNodes.length; i++) {
        divNode = divNodes[i];
        if (divNode.className) {
            if (divNode.className == "Persp_ActiveTab" || divNode.className == "Persp_Tab" || divNode.className == "Separator_Tab") {
                numTabNodes++;
                if (numTabNodes == 1) {
                    tabList += divNode.id;
                }
                else {
                    tabList += "|" + divNode.id;
                }
            }
        }
    }

    // Create an array for the tab nodes
    m_TabNodes = new Array(numTabNodes);

    // Save tab nodes in an array
    var tabIDArray = tabList.split("|");
    for (j = 0; j < numTabNodes; j++) {
        divNode = document.getElementById(tabIDArray[j]);
        m_TabNodes[j] = divNode;
    }

    // Determine if tab list is scrollable (based on css style settings)
    var listFrameRule = ServerSupport_GetRule(".Persp_ListFrame");
    if(listFrameRule) {
        if (listFrameRule.style.overflow.toLowerCase() == "hidden") {
            var listRule = ServerSupport_GetRule(".Persp_List");
            if(listRule) {
                if (listRule.style.position.toLowerCase() == "relative") {
                    m_ScrollableTabs = true;
                }
            }
        }
    }

    // Must set initial width for tab list or tabs with wrap
    var perspList = document.getElementById("Persp_List");
    perspList.style.width = GetTabListWidth();
}

function TabSupport_ResizeTabFrame() {
    var perspListFrame = document.getElementById("Persp_ListFrame");
    var pagePane = document.getElementById("Page_Pane");

    var leftBtn = document.getElementById("Persp_ListBtnLeft");
    var rightBtn = document.getElementById("Persp_ListBtnRight");
    perspListFrame.style.pixelWidth = 0;

    leftBtn.style.display = "block";
    rightBtn.style.display = "block";

    var relLeft = 0;
    if (perspListFrame.currentStyle.left) {
        relLeft = perspListFrame.currentStyle.left;
        relLeft = relLeft.replace("px", "");
        relLeft = parseInt(relLeft);
        if(isNaN(relLeft)) {
            relLeft = 0;
        }
    }
    if (!m_ScrollableTabs) {
        var perspList = document.getElementById("Persp_List");
        perspList.style.pixelWidth = pagePane.offsetWidth - relLeft - leftBtn.offsetWidth*2;
    }
    perspListFrame.style.pixelWidth = pagePane.offsetWidth - relLeft - leftBtn.offsetWidth*2;

    leftBtn.style.pixelLeft = perspListFrame.offsetLeft + perspListFrame.offsetWidth;
    rightBtn.style.pixelLeft = leftBtn.offsetLeft + leftBtn.offsetWidth;

    leftBtn.style.pixelTop = perspListFrame.offsetTop + ((perspListFrame.offsetHeight - leftBtn.offsetHeight) / 2);
    rightBtn.style.pixelTop = leftBtn.style.pixelTop;

    RefreshScrollArrows(CanScrollRight(), CanScrollLeft());
}

function RefreshScrollArrows(canScrollLeft, canScrollRight) {
    var leftBtn = document.getElementById("Persp_ListBtnLeft");
    var rightBtn = document.getElementById("Persp_ListBtnRight");

    if (canScrollLeft) {
        if (leftBtn.onmouseover == null) {
            leftBtn.onmouseover = function(){lightup('leftArrow');};
            leftBtn.onmouseout = function(){turnoff('leftArrow');};
            leftBtn.onmousedown = function(){DoScrollLeft();};
            leftBtn.onmouseup = function(){lightup('leftArrow');};
            turnoff('leftArrow');
        }
    }
    else {
        leftBtn.onmouseover = null;
        leftBtn.onmouseout = null;
        leftBtn.onmousedown = null;
        leftBtn.onmouseup = null;
        disable('leftArrow');
    }

    if (canScrollRight) {
        if (rightBtn.onmouseover == null) {
            rightBtn.onmouseover = function(){lightup('rightArrow');};
            rightBtn.onmouseout = function(){turnoff('rightArrow');};
            rightBtn.onmousedown = function(){DoScrollRight();};
            rightBtn.onmouseup = function(){lightup('rightArrow');};
            turnoff('rightArrow');
        }
    }
    else {
        rightBtn.onmouseover = null;
        rightBtn.onmouseout = null;
        rightBtn.onmousedown = null;
        rightBtn.onmouseup = null;
        disable('rightArrow');
    }
}

function DoScrollLeft() {
    clickdown('leftArrow');
//    TabRight();
    ScrollRight();
}

function DoScrollRight() {
    clickdown('rightArrow');
//    TabLeft();
    ScrollLeft();
}

// Scroll left an amount equal to the currently visible tab area
function PageLeft() {
    var scrollAmt = 0;
    var visibleWidth = GetVisibleWidthForTabs();
    var hiddenWidth = GetHiddenTabListWidth();
    scrollAmt = visibleWidth;
    if (hiddenWidth < visibleWidth) {
        scrollAmt = hiddenWidth;
    }
    scrollAmt = scrollAmt * -1;
    ScrollTabList(scrollAmt);
}

// Scroll right an amount equal to the currently visible tab area
function PageRight() {
    var scrollAmt = 0;
    var visibleWidth = GetVisibleWidthForTabs();
    scrollAmt = visibleWidth;
    ScrollTabList(scrollAmt);
}

function ScrollLeft() {
    var scrollAmt = m_ScrollAmt;
    var hiddenWidth = GetHiddenTabListWidth();

    if (hiddenWidth < scrollAmt) {
        scrollAmt = hiddenWidth;
    }

    scrollAmt = scrollAmt * -1;
    ScrollTabList(scrollAmt);
}

function ScrollRight() {
    var scrollAmt = m_ScrollAmt;
    ScrollTabList(scrollAmt);
}

function TabLeft() {
    var scrollAmt = 0;

    var visibleWidth = GetVisibleWidthForTabs();
    var tabListWidth = GetTabListWidth();
    
    var tab = GetLeftMostVisibleTab();
    var tabLeft = GetTabLeft(tab);
    var tabWidth = GetTabWidth(tab);
    var tabListPos = GetTabListPos();
    tabListPos = tabListPos * -1;

    scrollAmt = tabLeft + tabWidth - tabListPos;
    var hiddenWidth = GetHiddenTabListWidth();

    if (hiddenWidth < scrollAmt) {
        scrollAmt = hiddenWidth;
    }

    scrollAmt = scrollAmt * -1;
    ScrollTabList(scrollAmt);
}

function TabRight() {
    var scrollAmt = 0;

    var tab = GetLeftMostVisibleTab();
    var tabLeft = GetTabLeft(tab);
    var tabListPos = GetTabListPos();
    tabListPos = tabListPos * -1;

    scrollAmt = tabListPos - tabLeft;
    if (scrollAmt == 0) {
        var prevTab = GetPrevTab(tab);
        if (prevTab) {
            scrollAmt = GetTabWidth(prevTab);
        }
    }

    ScrollTabList(scrollAmt);
}

function CanScrollLeft() {
    var hiddenWidth = GetHiddenTabListWidth();

    if (hiddenWidth > 0) {
        return true;
    }
    else {
        return false;
    }
}

function CanScrollRight() {
    var tabListPos = GetTabListPos();

    if (tabListPos < 0) {
        return true;
    }
    else {
        return false;
    }
}

function GetVisibleWidthForTabs() {
    var visibleWidth = 0;

    var listFrame = document.getElementById("Persp_ListFrame");
    var leftBtn = document.getElementById("Persp_ListBtnLeft");

    //visibleWidth = listFrame.clientWidth - leftBtn.offsetWidth*2;
    visibleWidth = listFrame.clientWidth;

    return visibleWidth;
}

function GetTabWidth(tab) {
    var tabWidth = 0;
    var marginWidth = 0;

    var tabRule = ServerSupport_GetRule(".Persp_Tab");
    if(tabRule) {
        if (tabRule.style.marginRight) {
            marginWidth = tabRule.style.marginRight;
            marginWidth = marginWidth.replace("px", "");
        }
    }

    tabWidth = tab.offsetWidth + (parseInt(marginWidth) * 2);

    return tabWidth;
}

function GetTabLeft(tab) {
    var tabLeft = 0;
    var marginWidth = 0;

    var tabRule = ServerSupport_GetRule(".Persp_Tab");
    if(tabRule) {
        if (tabRule.style.marginRight) {
            marginWidth = tabRule.style.marginRight;
            marginWidth = marginWidth.replace("px", "");
        }
    }

    tabLeft = tab.offsetLeft - parseInt(marginWidth);
    
    return tabLeft;
}

function GetTabListWidth() {
    var leftMost = 0;
    var tabListWidth = 0;
    
	if ( m_TabNodes !== undefined ) {
	    for (i = 0; i < m_TabNodes.length; i++) {
	        tab = m_TabNodes[i];
	        if (m_ScrollableTabs) {
	            tabListWidth += GetTabWidth(tab);
	        }
	        else {
	            if (GetTabLeft(tab) > leftMost) {
	                leftMost = GetTabLeft(tab);
	                tabListWidth = leftMost + GetTabWidth(tab);
	            }
	        }
	    }
	}

    return tabListWidth;
}

// Returns offset of tab list
// This value will always be less than zero (if scrolled left) or
// zero if scrolled right back to start position.
function GetTabListPos() {
    var tabListPos = 0;
    var perspList = document.getElementById("Persp_List");
    tabListPos = perspList.offsetLeft;
    return tabListPos;
}

function GetLeftMostVisibleTab() {
    var tab = null;

    var tabListPos = GetTabListPos();
    tabListPos = tabListPos * -1;

    for (i = 0; i < m_TabNodes.length; i++) {
        tab = m_TabNodes[i];
        if (GetTabLeft(tab) <= tabListPos) {
            if (GetTabLeft(tab) + GetTabWidth(tab) > tabListPos) {
                return tab;
            }
        }
    }

    return tab;
}

function GetPrevTab(tab) {
    var prevTab = null;
    var curTab;
    var tabEnd;

    var tabLeft = GetTabLeft(tab);

    for (i = 0; i < m_TabNodes.length; i++) {
        curTab = m_TabNodes[i];
        tabEnd = GetTabLeft(curTab) + GetTabWidth(curTab);

        if (tabEnd == tabLeft) {
            return curTab;
        }
    }

    return prevTab;
}

function GetHiddenTabListWidth() {
    var tabListWidth = GetTabListWidth();
    var visibleWidth = GetVisibleWidthForTabs();
    var tabListPos = GetTabListPos();
    tabListPos = tabListPos * -1;

    var hiddenWidth = (tabListPos + visibleWidth >= tabListWidth ? 0 : tabListWidth - tabListPos - visibleWidth);

    return hiddenWidth;
}

// Smooth scroll by the number of pixels specified in scrollVal param
// Positive scrollVal will scroll right
// Negative scrollVal will scroll left
function ScrollTabList(scrollVal) {
    var fCall;
    var endVal;
    var perspList = document.getElementById("Persp_List");

    endVal = perspList.style.pixelLeft + scrollVal;
    if (scrollVal > 0) {
        fCall = "ScrollTabListTo(" + endVal + ", " + m_StepSize + ")";
    }
    else {
        fCall = "ScrollTabListTo(" + endVal + ", " + (m_StepSize * -1) + ")";
    }
    setTimeout(fCall, m_ScrollSpeed)
}

// Smooth scroll to the specified end point
function ScrollTabListTo(endVal, stepSize, complete) {
    var fCall;
    var done = false;
    var perspList = document.getElementById("Persp_List");

    if (complete) {
        RefreshScrollArrows(CanScrollRight(), CanScrollLeft());
        return;
    }

    if (stepSize > 0) {
        if (perspList.style.pixelLeft + stepSize > endVal) {
            perspList.style.pixelLeft = endVal;
            done = true;
        }
        if (perspList.style.pixelLeft + stepSize > 0) {
            perspList.style.pixelLeft = 0;
            done = true;
        }

        if (done) {
            fCall = "ScrollTabListTo(0, 0, true)";
        }
        else {
            perspList.style.pixelLeft = perspList.style.pixelLeft + stepSize;
            fCall = "ScrollTabListTo(" + endVal + ", " + stepSize + ")";
        }
    }
    else {
        if (perspList.style.pixelLeft + stepSize < endVal) {
            perspList.style.pixelLeft = endVal;
            done = true;
        }

        if (done) {
            fCall = "ScrollTabListTo(0, 0, true)";
        }
        else {
            perspList.style.pixelLeft = perspList.style.pixelLeft + stepSize;
            fCall = "ScrollTabListTo(" + endVal + ", " + stepSize + ")";
        }
    }
    setTimeout(fCall, m_ScrollSpeed);
}
