﻿
var tabLinks = new Array();
var contentDivs = new Array();
var numTabs = 0;
function init() {

    // Grab the tab links and content divs from the page
    var tabListItems = document.getElementById('Tabs').childNodes;
    for (var i = 0; i < tabListItems.length; i++) {
        if (tabListItems[i].nodeName == "LI") {
            var tabLink = getFirstChildWithTagName(tabListItems[i], 'A');
            var id = getHash(tabLink.getAttribute('href'));
            tabLinks[id] = tabLink;
            contentDivs[id] = document.getElementById(id);
            numTabs += 1;
        }
    }

    // Assign onclick events to the tab links, and
    // highlight the first tab
    var i = 0;
    var LastActive = false;
    
    for (var id in tabLinks) {
        tabLinks[id].onclick = showTab;
        tabLinks[id].onfocus = function() { this.blur() };
        if (LastActive == true) {
            tabLinks[id].className = 'inactiveNext';
            LastActive = false;
        }
        else if (i == 0) {
            tabLinks[id].className = 'selectedFirst';
            LastActive = true;
        }
        else if (i == (numTabs - 1)) {
            tabLinks[id].className = 'inactiveLast';
        }
        else {
            tabLinks[id].className = 'inactive';
        }
        
        i++;
    }

    // Hide all content divs except the first
    var i = 0;

    for (var id in contentDivs) {
        if (i != 0) contentDivs[id].className = 'tabContent hide';
        i++;
    }
}

function showTab() {
    var selectedID = getHash(this.getAttribute('href'));
    var LastActive = false;
    var i = 0;

    // Highlight the selected tab, and dim all others.
    // Also show the selected content div, and hide all others.   
    for (var id in tabLinks) {
        if (id == selectedID) {
            // Active
            if (i == 0) {
                tabLinks[id].className = 'selectedFirst';
                LastActive = true;
            } else if (i == numTabs-1) {
                tabLinks[id].className = 'selectedLast';
            } else {
                tabLinks[id].className = 'selected';
                LastActive = true;
            }
            contentDivs[id].className = 'tabContent';
        } else {
            // Inactive
            if (i == 0) {
                tabLinks[id].className = 'inactiveFirst';
            } else if (i == numTabs-1) {
                tabLinks[id].className = 'inactiveLast';
            } else {
                tabLinks[id].className = 'inactive';
            }
            if (LastActive == true) {
                if (i == numTabs - 1) {
                    tabLinks[id].className = 'inactiveNextLast';
                } else {
                    tabLinks[id].className = 'inactiveNext';
                }
                LastActive = false;
            }
            contentDivs[id].className = 'tabContent hide';
        }
        i++;
    }
                
    return false;
}

function getFirstChildWithTagName(element, tagName) {
    for (var i = 0; i < element.childNodes.length; i++) {
        if (element.childNodes[i].nodeName == tagName) return element.childNodes[i];
    }
}

function getHash(url) {
    var hashPos = url.lastIndexOf('#');
    return url.substring(hashPos + 1);
}

