﻿var atgTab = new function() {
    // Initalize variables
    var atgCurrentTab = "atgCurrentTab";
    var atgTabSeperator = "atgTabSeperator";
    var atgMap = new Array();

    function setTabColor(tdElement, color, bgColor) {
        tdElement.style.backgroundColor = bgColor;
        tdElement.style.color = color;
        tdElement.style.borderBottomColor = bgColor;
        //tdElement.style.a.color = "White";
    }

    this.setup = function(tabId, color, bgColor, hoverColor, hoverBgColor) {
        var mapData = new Array(); mapData["color"] = color; mapData["bgColor"] = bgColor; mapData["hoverColor"] = hoverColor; mapData["hoverBgColor"] = hoverBgColor;
        atgMap[tabId] = mapData;
        var row; var col; var table = document.getElementById(tabId);
        var className;
        if (!table) { alert("No tab name found with: " + tabId); return; }
        for (var i = 0; row = table.rows[i]; i++) {
            for (var j = 0; col = row.cells[j]; j++) {
                className = col.className;
                if (className != null) {
                    if (className == atgTabSeperator) {
                        col.setAttribute("style", "border-bottom: solid 1px Black");
                    }
                    else {
                        col.setAttribute("style", "border-left: solid 1px black; border-top: solid 1px black; border-right: solid 1px black; border-bottom: solid 1px;");
                        col.setAttribute("onmouseout", "if (this.className != atgCurrentTab) { setTabColor(this, '" + color + "', '" + bgColor + "');}");
                        col.setAttribute("onmouseover", "setTabColor(this, '" + hoverColor + "', '" + hoverBgColor + "');");
                        col.setAttribute("align", "center");
                        //col.setAttribute("onclick", "tabOnClicked(this, '#', '" + color + "','" + bgColor + ");");

                        // Is the current tab selected?
                        if (className == atgCurrentTab) {
                            setTabColor(col, hoverColor, hoverBgColor);
                        }
                        else {
                            setTabColor(col, color, bgColor);
                        }
                    }
                }
            }
        }
    }

    this.setCurrentTab = function(tabId, selectedTabName) {
        var mapData = atgMap[tabId];
        if (mapData == null) { alert("Tab has not been initialized."); return; }
        var table = document.getElementById(tabId);
        if (!table) { alert("No tab name found with: " + tabId); return; }
        var tabName = "";
        for (var i = 0; row = table.rows[i]; i++) {
            for (var j = 0; col = row.cells[j]; j++) {
                tabName = col.getAttribute("name");
                if (tabName != null) {
                    if (tabName != atgTabSeperator) {
                        if (tabName == selectedTabName) {
                            setTabColor(col, mapData["hoverColor"], mapData["hoverBgColor"]);
                            col.className = atgCurrentTab;
                        }
                        else {
                            setTabColor(col, mapData["color"], mapData["bgColor"]);
                        }
                    }
                }
            }
        }
    }
    
}



