﻿/* Tab scripts
--------------------------  */
var intialtab = true;

function initialisetabs() {
  
    // Get alll list items and process those marked as tabs
    var ularr = document.getElementsByTagName("UL");

    for (var i = 0; i < ularr.length; i++) {
        if (ularr[i].className == "cotabs") {
            //Go through each li and set styles, set visible content and apply scripts
            var liarr = ularr[i].getElementsByTagName("LI");
            
            for (var j = 0; j < liarr.length; j++) {
                // If not the first item then hide the content
                if (j > 0) {
                    document.getElementById(liarr[j].id.replace("li", "")).className = "hidden";
                }
                else {
                    // Set first item as selected
                    liarr[j].className = "selected";
                    ularr[i].setAttribute("current", liarr[j].id);
                    document.getElementById(liarr[j].id.replace("li", "")).className = "";
                }
                // Add the onclick scripts
                liarr[j].setAttribute("onclick", "selecttab(this);");
            }
        }
    }
}

function selecttab(o) {

    if (document.getElementById(o.id)) {
        while (o.tagName != "LI") {
            o = o.parentNode;
        }
        
        var ul = o;
        while (ul.tagName != "UL") {
            ul = ul.parentNode;
        }
        
        // Don't process the current tab
        if (ul.getAttribute("current") == o.id) {
            return false;
        }
        
        // Hide currently selected
        document.getElementById(ul.getAttribute("current")).className = "";
        document.getElementById(ul.getAttribute("current").replace("li", "")).className = "hidden";

        // Show newly selected
        o.className = "selected";
        document.getElementById(o.id.replace("li", "")).className = "";

        // Set Selected
        ul.setAttribute("current", o.id);
        
        intialtab = false;
    }
    
}

function closetab(o) {
    // Get the tab list item
    var li = o.parentNode;
    while (li.tagName != "LI") {
        li = li.parentNode;
    }
    // Select the previous tab if closing the current one
    var ul = o;
    while (ul.tagName != "UL") {
        ul = ul.parentNode;
    }
    if (li.id == ul.getAttribute("current")) {
        var ptab = li.previousSibling;
        while (ptab.tagName != "LI") {
            ptab = ptab.previousSibling
        }
        selecttab(ptab);
    }
    // Remove this tab from the list
    li.parentNode.removeChild(li);
    // Remove the content div from the page
    document.getElementById("mainbody").removeChild(document.getElementById(li.id.replace("li", "")));
}
