/*
Scripts for TabControl component
This component is AspLib Component Library part
Copyright © 2002-2006 Astron Digital Ltd., ALL RIGHTS RESERVED
*/



// config to save tabs layout for multirow control
function al_TabControl_Config()
{
    this.swapArray = "";
    this.isBottom = false;
    this.tabsArray = "";
    this.isMultirow = false;
    this.visibleCells = 0;
    this.currentModeIsSingleRow = false;
    this.currentTab = 0;
    this.oldTabWidth = 0;
    this.oldTabHeight = 0;
    this.replaceTabRows = true;
    this.rowsNumber = 0;
    this.rowHeight = "";
    this.rowsVisibleNumber = 0;
    this.swapCellString = '';
}



function al_TabControl_UpdateTab(isImg, ctrlID, index)
{
    if (isImg)
        al_TabControl_ImageUpdate(ctrlID, index);
    else
        al_TabControl_UpdateView(ctrlID, index);
}



function al_TabControl_Click(name, num, isImage,
                            eventObject, tabRowIndex, tabRowCount)
{
    if (eventObject && document.getElementsByName(name + "_currentTab")[0].value == num) return;
   
    var o = al_getVariable("al_TabControl_OnSelectedTabChange_" + name);
    // don't call if initializing
    if (o != null && eventObject)
        if (false == o(document.getElementsByName(name + "_currentTab")[0].value, num, eventObject))
            return;
   
    al_TabControl_UpdateTab(isImage, name, num);
       
    var oTab = al_TabControl_GetMultiRowCell(name, num);
    var oFrame = al_GE(name + "_iframe");
    var oPanel = al_TabControl_GetPanel(name, num);
   
    al_TabControl_HideAllPanels(name);

    //search the previous tab if current is hidden
    if (oTab == null)
    {
        return;
    }

    if (oTab.getAttribute("tabType") == "TabPanel")
    {
        if (oFrame)
            oFrame.style.display = "none";
        if (oPanel)
        {
            oPanel.style.display = "";
        }
    }
    else // TabFrame
    {
        if (oFrame)
        {
            oFrame.style.display = "";
            oFrame.src = oTab.getAttribute("tabSrc");
        }
    }
   
    document.getElementsByName(name + "_currentTab")[0].value = num;
   
    if (eval("TabControl_Config_" + name).replaceTabRows != true)
        return;

    eval("TabControl_Config_" + name).currentTab = num;
    var swapArray = eval("TabControl_Config_" + name).swapArray;

    setTotal();
   
    //swap rows in multirow control
    if (tabRowIndex && tabRowCount)
    {
        var rowIndex1 = tabRowIndex - 1;
       
        if (eval("TabControl_Config_" + name).isBottom != true)
            var rowIndex2 = tabRowCount - 1;
        else
            var rowIndex2 = 0;
           
        //back to original placement
        if (swapArray != "")
        {
            var ar = swapArray.split(",");
            index1 = ar[0];
            index2 = ar[1];
            al_TabControl_SwapRows(name, index2, index1);
        }
       
        swapArray = rowIndex1 + "," + rowIndex2;
        eval("TabControl_Config_" + name).swapArray = swapArray;
        
        //apply new placement
        al_TabControl_SwapRows(name, rowIndex1, rowIndex2);
       
	    al_TabControl_UpdateTab(isImage, name, num);
    }

}



function al_TabControl_GetPanel(ctrlID, panelIndex)
{
    return al_GE(ctrlID + "_panel" + panelIndex);
}



function al_TabControl_GetMultiRowCell(ctrlID, cellIndex)
{
    return al_GE(ctrlID + "__tab" + cellIndex);
}



function al_TabControl_GetSigleRowCell(ctrlID, cellIndex)
{
    return al_GE(ctrlID + "_singleCell_" + cellIndex);
}



function al_TabControl_GetSingleRowContainer(ctrlID)
{
    return al_GE(ctrlID + "_singleRowContainer");
}



function al_TabControl_GetMultiRowContainer(ctrlID)
{
    return al_GE(ctrlID + "_multiRowContainer");
}



function al_TabControl_SingleRowMode(ctrlID, bSingle, nWidth)
{   
    if (al_IsStringEmpty(nWidth))
        nWidth = "100%";

    //only for multirow control
    var isMultirow = eval("TabControl_Config_" + ctrlID).isMultirow;
    if (!isMultirow) {
        //return; 
        }

    var rowsN = eval("TabControl_Config_" + ctrlID).rowsVisibleNumber;
    var rowH = parseInt(eval("TabControl_Config_" + ctrlID).rowHeight);
    var err = 0;
      
    // if xhtml
    if (document.documentElement.clientHeight)
    {
        err = (rowsN-1)*(2+rowH);
    }
    else //html
    {
        err = 7 - (rowsN-2)*2;
    }

    var step = err;
    var panel_step = (rowsN-1)*rowH;
    var tab = al_GE(ctrlID);

    //do nothing if already in current mode
    if (eval("TabControl_Config_" + ctrlID).currentModeIsSingleRow == bSingle)
        return; 
	
    var tab_unit = "";
    if (tab.getAttribute("height").indexOf("%") == -1) //in px
        tab_unit = "px";
    else
        tab_unit = "%";

    if (tab.getAttribute("height").indexOf("%") == -1) //in px
    {
        //var hhh = tab.offsetHeight;
        if (bSingle)
        {
            eval("TabControl_Config_" + ctrlID).oldTabHeight = tab.getAttribute("height");
            tab.setAttribute("height", parseInt(tab.height) + step);
        }
        else
            tab.setAttribute("height", parseInt(tab.height) - step);
    }
    
    var isCreatedSingleRowCells = eval("TabControl_Config_" + ctrlID).isCreatedSingleRowCells;
    var visibleCells = parseInt(eval("TabControl_Config_" + ctrlID).visibleCells);
   
    var mr = al_TabControl_GetMultiRowContainer(ctrlID);
    var sr = al_TabControl_GetSingleRowContainer(ctrlID);
    var numTabs = tab.getAttribute("numberTabs");
   
    eval("TabControl_Config_" + ctrlID).currentModeIsSingleRow = bSingle;
   
    if (bSingle)
    {
        //save original size
        eval("TabControl_Config_" + ctrlID).oldTabWidth = tab.width;
       
        if (nWidth)
            tab.width = nWidth;
           
        //resize tabs
        var newwidth = tab.width;
        var newtabwidth = parseInt(newwidth) / visibleCells;

        if (newwidth.indexOf('%') != -1) //in percent
            newtabwidth += "%";
        else
            newtabwidth += "px";
           
        var swapCellString = eval("TabControl_Config_" + ctrlID).swapCellString;
        var swap_ar = swapCellString.split(";");

        //copys cells from multirow to singlerow
        var sc_counter = 0;
        if (swap_ar.length > 4) //multirow
        {
        for (var i = 0; i < swap_ar.length; i++)
        {
            var param = swap_ar[i].split("~~");
            var nn = param[0];
            var vv = param[1] == "true";
           
            if (vv)
            {
                var sc = al_TabControl_GetSigleRowCell(ctrlID, sc_counter);
                var mc = al_TabControl_GetMultiRowCell(ctrlID, nn);
               
                al_TabControl_CopyCells(sc, mc, false);
           
                sc.width = newtabwidth;

                sc_counter++;
            }
        }
        }

        /*for (var i = 0; i < visibleCells; i++)
        {
            var sc = al_TabControl_GetSigleRowCell(ctrlID, i);
            var mc = al_TabControl_GetMultiRowCell(ctrlID, i);
       
            al_TabControl_CopyCells(sc, mc, false);
           
            sc.width = newtabwidth;
        }*/
       
        if (mr || sr) {
			mr.style.display = "none";
			sr.style.display = "";
        }
       
        for (var j = 0; j < numTabs; j++)
        {
            var pan = al_TabControl_GetPanel(ctrlID, j);
            if (pan)
            {
                pan.style.width = nWidth;
                if (tab_unit != "%")
                    pan.style.height = (parseInt(pan.style.height) + panel_step) + "px";
            }
        }
    }
    else
    {

        //restore original size
        var old_w = eval("TabControl_Config_" + ctrlID).oldTabWidth;
        tab.width = old_w;

        //delete duplicate image id's
        if (visibleCells>3) //multirow
        {
			for (var i = 0; i < visibleCells; i++)
			{
				var sc = al_TabControl_GetSigleRowCell(ctrlID, i);
				sc.innerHTML = "";
			}
        }

        if (mr || sr) {
        mr.style.display = "";
        sr.style.display = "none";
        }
        
        for (var j = 0; j < numTabs; j++)
        {
            var pan = al_TabControl_GetPanel(ctrlID, j);
            if (pan)
            {
                pan.style.width = old_w;
                if (tab_unit != "%")
                    pan.style.height = (parseInt(pan.style.height) - panel_step) + "px";
            }   
        }

        var cur = eval("TabControl_Config_" + ctrlID).currentTab;
        var cur_mc = al_TabControl_GetMultiRowCell(ctrlID, cur);
        if (cur_mc)
        {
            var isImage = cur_mc.isImageTab;
            al_TabControl_UpdateTab(isImage, ctrlID, cur);
        }
    }
}



function al_TabControl_CopyCells(cellTo, cellFrom, bCopyID)
{
    if (!cellTo || !cellFrom) return; 

    cellTo.innerHTML = cellFrom.innerHTML;
    cellTo.onclick = cellFrom.onclick;
    cellTo.onmouseover = cellFrom.onmouseover;
    cellTo.onmouseout = cellFrom.onmouseout;
    cellTo.setAttribute("tabType", cellFrom.getAttribute("tabType"));
    cellTo.setAttribute("isEnabled", cellFrom.getAttribute("isEnabled"));
    cellTo.setAttribute("isImageTab", cellFrom.getAttribute("isImageTab"));
    cellTo.setAttribute("scOrderNumber", cellFrom.getAttribute("scOrderNumber"));
    cellTo.setAttribute("title", cellFrom.getAttribute("title"));
    cellTo.height = cellFrom.height;
    cellTo.width = cellFrom.width;
    cellTo.className = cellFrom.className;
    cellTo.align = cellFrom.align;
    cellTo.style.cursor = cellFrom.style.cursor;
   
    if (bCopyID)
	    cellTo.id = cellFrom.id;
}



// swap first row from t1 to t2
function al_TabControl_SwapRows(name, index1, index2)
{
    if (index1 == index2)
        return;

    //only for multirow control
    var isMultirow = eval("TabControl_Config_" + name).isMultirow;
    if (!isMultirow)
        return;
       
    var t1 = al_GE(name + "_tabRowTable_" + index1);
    var t2 = al_GE(name + "_tabRowTable_" + index2);
   
    if (!t1 || !t2)
        return;
   
    //adds new row to t2
    var r2 = t2.insertRow(-1);
    for (var i = 0; i < t1.rows[0].cells.length; i++)
    {
        var c = r2.insertCell(-1);
        var ci = t1.rows[0].cells[i];
       
        al_TabControl_CopyCells(c, ci, true);
    }

    var r1 = t1.insertRow(-1);
    for (var i = 0; i < t2.rows[0].cells.length; i++)
    {
        var c = r1.insertCell(-1);
        var ci = t2.rows[0].cells[i];
       
        al_TabControl_CopyCells(c, ci, true);
    }
   
    t2.deleteRow(0);
    t1.deleteRow(0);
}



function al_TabControl_HideAllPanels(name)
{
    var numTabs = al_GE(name).getAttribute("numberTabs");
    for (var i = 0; i < numTabs; i ++)
    {
        var o = al_TabControl_GetPanel(name, i);
        if (o != null)
            o.style.display = "none";
    }
}



//sc number depends on tab visible
function al_TabControl_GetSingleCellNumber(name, multiRowCellNumber)
{
    var oTab = al_TabControl_GetMultiRowCell(name, multiRowCellNumber);
    if (oTab)
    {
        return parseInt(oTab.getAttribute("scOrderNumber"));
    }
   
    return -1
}



function al_TabControl_UpdateView(name, num)
{
    var numTabs = al_GE(name).getAttribute("numberTabs");

    for (var i = 0; i < numTabs; i ++)
    {
        var sc_number = al_TabControl_GetSingleCellNumber(name, i);
        var sc = al_TabControl_GetSigleRowCell(name, sc_number);
       
        var oTab = al_TabControl_GetMultiRowCell(name, i);
        var oLink = al_GE(name + "__tabLink" + i);
       
        if (oTab == null) continue;
       
        if (parseInt(num) == i)
        {
            oTab.className = "C" + name + "__tabSelected";
            if (sc)
                sc.className = "C" + name + "__tabSelected";
            if (oLink)
                oLink.className = "C" + name + "__linkSelected";
        }
        else if (oTab.getAttribute("isEnabled") != "False")
        {
            oTab.className = "C" + name + "__tab";
            if (sc)
                sc.className = "C" + name + "__tab";
            if (oLink)
                oLink.className = "C" + name + "__link";
        }
       
        oTab.prevClassName = oTab.className;
        if (sc)
            sc.prevClassName = sc.className;
        if (oLink)
            oLink.prevClassName = oLink.className;
    }
}



function al_TabControl_ImageUpdate(name, num)
{
    al_TabControl_UpdateView(name, num);
   
    var numTabs = al_GE(name).getAttribute("numberTabs");
   
    for (var i = 0; i < numTabs; i ++)
    {
        var oImg = al_GE(name + "__tabImage" + i);
        var oSelImg = al_GE(name + "__tabSelImage" + i);
       
        if (oImg == null) continue;
       
        if (parseInt(num) == i)
        {
            oImg.style.display = "none";
            oSelImg.style.display = "";
        }
        else
        {
            oImg.style.display = "";
            oSelImg.style.display = "none";
        }
    }
}



function al_TabControl_HoverTab(name, index)
{
    var sc = al_TabControl_GetSigleRowCell(name, index);
    oTab = al_TabControl_GetMultiRowCell(name, index);
    oLink = al_GE(name + "__tabLink" + index);

    if (oTab.className == "C" + name + "__tabSelected") return;   
   
    oTab.prevClassName = oTab.className;
    if (sc)
        sc.prevClassName = sc.className;
    if (oLink)
        oLink.prevClassName = oLink.className;

    if (sc)
        sc.className = "C" + name + "__tabHover";
    oTab.className = "C" + name + "__tabHover";
    if (oLink)
        oLink.className = "C" + name + "__linkHover";
}



function al_TabControl_UnhoverTab(name, index)
{
    var sc = al_TabControl_GetSigleRowCell(name, index);
    oTab = al_TabControl_GetMultiRowCell(name, index);
    oLink = al_GE(name + "__tabLink" + index);
   
    if (oTab.className == "C" + name + "__tabSelected") return;   
   
    if (sc)
        sc.className = sc.prevClassName;
    oTab.className = oTab.prevClassName;
    if (oLink)
        oLink.className = oLink.prevClassName;
}



function al_TabControl_GetTabLink(ctrlId, tabIndex)
{
    return al_GE(ctrlId + "__tabLink" + tabIndex);
}



function al_TabControl_SetFocusOnTab(ctrlId, tabIndex)
{
    var a = al_TabControl_GetTabLink(ctrlId, tabIndex);
   
    if (a)
        a.focus();
}



function al_TabControl_TabOffset(ctrlId, tabIndex, offset)
{
    var a = al_TabControl_GetTabLink(ctrlId, tabIndex);
    var maxtab = parseInt(a.tabsVisibleCount);
    var newtab = tabIndex + offset;
   
    if (newtab < 0)
        newtab = 0;
       
    if (newtab > maxtab-1)
        newtab = maxtab - 1;
       
    al_TabControl_SetFocusOnTab(ctrlId, newtab);
}



function al_TabControl_KeyDown(evt, ctrlId, tabIndex)
{
    var code = al_GetKeyCode(evt);
    var a = al_TabControl_GetTabLink(ctrlId, tabIndex);
   
    var IsImageTab = a.tabIsImage;
    var tabRowIndex = a.tabRowIndex;
    var tabRowCount = a.tabRowCount;

    switch (code)
    {
        case UP_KEY: //up
        case LEFT_KEY: //left
        al_TabControl_TabOffset(ctrlId, tabIndex, -1);
        return false;
       
        case DOWN_KEY: //down
        case RIGHT_KEY: //right
        al_TabControl_TabOffset(ctrlId, tabIndex, 1);
        return false;
       
        case ENTER_KEY: //enter
        al_TabControl_Click(ctrlId, tabIndex, IsImageTab, evt, tabRowIndex, tabRowCount);
        al_TabControl_SetFocusOnTab(ctrlId, tabIndex);
        return false;

        default:
        break;
    }
   
    return true;
}

