// CSS helper functions
CSS = {
    // Adds a class to an element.
    AddClass: function (element, class_name) {
        if (!element.className.match(new RegExp("\\b" + class_name + "\\b", "i")))
            element.className += (element.className ? " " : "") + class_name;
    },

    // Removes a class from an element.
    RemoveClass: function (element, class_name) {
        element.className = element.className.replace(new RegExp(" \\b" + class_name + "\\b|\\b" + class_name + "\\b ?", "gi"), "");
    }
};


// Functions for handling tabs.
Tabs = {
    // Changes to the tab with the specified ID.
    GoTo: function (contentIdentification, skipReplace) {
        // This variable will be true if a tab for the specified
        // content ID was found.
        var foundTab = false;

        // Get the TOC element.
        var toc = document.getElementById("toc");
        if (toc) {
            var list = toc.getElementsByTagName("li");
            for (var index = 0; index < list.length; index++) {
                var li = list[index];

                // Give the current tab link the class "current" and
                // remove the class from any other TOC links.
                var anchors = li.getElementsByTagName("a");
                for (var current = 0; current < anchors.length; current++) {
                    if (anchors[current].hash == "#" + contentIdentification) {
                        CSS.AddClass(li, "current");
                        foundTab = true;
                        break;
                    } else {
                        CSS.RemoveClass(li, "current");
                    }
                }
            }
        }

        // Show the content with the specified ID.
        var divsToHide = [];
        var divs = document.getElementsByTagName("div");
        for (var index = 0; index < divs.length; index++) {
            var div = divs[index];

            if (div.className.match(/\bproduct_part\b/i)) {
                if (div.id == "_" + contentIdentification)
                    div.style.display = "block";
                else
                    divsToHide.push(div);
            }
        }

        // Hide the other content boxes.
        for (var index = 0; index < divsToHide.length; index++)
            divsToHide[index].style.display = "none";

        // Change the address bar.
        if (!skipReplace) window.location.replace("#" + contentIdentification);
    },

    OnClickHandler: function (element) {
        // Stop the event (to stop it from scrolling or
        // making an entry in the history).
        if (!element) element = window.event;
        if (element.preventDefault) element.preventDefault(); else element.returnValue = false;

        // Get the name of the anchor of the link that was clicked.
        Tabs.GoTo(this.hash.substring(1), false);
    },

    Init: function () {
        if (!document.getElementsByTagName) return;

        // Attach an onclick event to all the anchor links on the page.
        var anchors = document.getElementsByTagName("a");
        for (var index = 0; index < anchors.length; index++) {
            var a = anchors[index];
            if (a.hash) a.onclick = Tabs.OnClickHandler;
        }

        var contentIdentification;
        if (window.location.hash) contentIdentification = window.location.hash.substring(1);

        var divs = document.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {
            var div = divs[i];

            if (div.className.match(/\bproduct_part\b/i)) {
                if (!contentIdentification) contentIdentification = div.id;
                div.id = "_" + div.id;
            }
        }

        if (contentIdentification) Tabs.GoTo(contentIdentification, true);
    }
};

// Hook up the OnLoad event to the tab initialization function.
window.onload = Tabs.Init;

// Hide the content while waiting for the onload event to trigger.
var contentIdentification = window.location.hash || "#Specification";

if (document.createStyleSheet) {
    var style = document.createStyleSheet();
    style.addRule("div.product_part", "display: none;");
    style.addRule("div" + contentIdentification, "display: block;");
} else {
    var head = document.getElementsByTagName("head")[0];
    if (head) {
        var style = document.createElement("style");
        style.setAttribute("type", "text/css");
        style.appendChild(document.createTextNode("div.product_part { display: none; }"));
		style.appendChild(document.createTextNode("div" + contentIdentification + " { display: block; }"));
        head.appendChild(style);
    }
}
