//indexOf function for Arrays
if(!Array.indexOf){
	    Array.prototype.indexOf = function(obj){
	        for(var i=0; i<this.length; i++){
	            if(this[i]==obj){
	                return i;
	            }
	        }
	        return -1;
	    }
	}


// CSS helper functions
CSS = {
    // Adds a class to an element.
    AddClass: function (e, c) {
        if (!e.className.match(new RegExp("\\b" + c + "\\b", "i")))
            e.className += (e.className ? " " : "") + c;
    },

    // Removes a class from an element.
    RemoveClass: function (e, c) {
        e.className = e.className.replace(new RegExp(" \\b" + c + "\\b|\\b" + c + "\\b ?", "gi"), "");
    }
};

// Functions for handling tabs.
Tabs = {
    // Changes to the tab with the specified ID.
    GoTo: function (contentId, 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 lis = toc.getElementsByTagName("li");
            for (var j = 0; j < lis.length; j++) {
                var li = lis[j];

                // Give the current tab link the class "current" and
                // remove the class from any other TOC links.
                var anchors = li.getElementsByTagName("a");
                for (var k = 0; k < anchors.length; k++) {
                    if (anchors[k].hash == "#" + contentId) {
                        CSS.AddClass(li, "current");
						li.setAttribute('aria-selected', 'true');
						anchors[k].setAttribute('tabindex', '0');
						anchors[k].tabIndex = 0;
                        foundTab = true;
                        break;
                    } else {
                        CSS.RemoveClass(li, "current");
						li.setAttribute('aria-selected', 'false');
						anchors[k].setAttribute('tabindex', '-1');
						anchors[k].tabIndex = -1;
                    }
                }
            }
        }

        // Show the content with the specified ID.
        var divsToHide = [];
        var divs = document.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {
            var div = divs[i];

            if (div.className.match(/\bcontent\b/i)) {
                if (div.id == "_" + contentId) {
                    div.style.display = "block";
					div.setAttribute('aria-hidden', 'false');	
                } else
                    divsToHide.push(div);
            }
        }

        // Hide the other content boxes.
        for (var i = 0; i < divsToHide.length; i++) {
            divsToHide[i].style.display = "none";
			divsToHide[i].setAttribute('aria-hidden', 'true');
		}	

        // Change the address bar.
        if (!skipReplace) window.location.replace("#" + contentId);
    },

    OnClickHandler: function (e) {
        // Stop the event (to stop it from scrolling or
        // making an entry in the history).
        if (!e) e = window.event;
        if (e.preventDefault) e.preventDefault(); else e.returnValue = false;

        // Get the name of the anchor of the link that was clicked.
        Tabs.GoTo(this.hash.substring(1));
    },

    Init: function () {
        if (!document.getElementsByTagName) return;

        // Attach an onclick event to all the tab anchor links and set up ARIA
		var toc = document.getElementById("toc");
		toc.setAttribute('role', 'tablist');
        var anchors = toc.getElementsByTagName("a");
		var tabs = new Array();
		for (var i = 0; i < anchors.length; i++) {
			var a = anchors[i];
			tabs.push(a.hash);
			a.setAttribute('tabindex', '-1');
			a.tabIndex = -1;
			a.setAttribute('role', 'tab');
		}
		anchors = document.getElementsByTagName("a");
		var tabAnchors = new Array();
		for (var i = 0; i < anchors.length; i++) {
			if (tabs.indexOf(anchors[i].hash) != -1) tabAnchors.push(anchors[i]);
		}
        for (var i = 0; i < tabAnchors.length; i++) {
            var a = tabAnchors[i];
            if (a.hash) {
				a.onclick = Tabs.OnClickHandler;
			}	
			
        }

        var contentId;
        if (window.location.hash && tabs.indexOf(window.location.hash) != -1) contentId = 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(/\bcontent\b/i)) {
                if (!contentId) contentId = div.id;
                div.id = "_" + div.id;
				div.setAttribute('aria-labelledby', div.id.substring(1)+"-tab");
				div.setAttribute('role', 'tabpanel');
            }
        }

        if (contentId) Tabs.GoTo(contentId, true);
		if (window.location.hash && tabs.indexOf(window.location.hash) == -1) {
			var node = document.getElementById(window.location.hash.substring(1));
			while (node.tagName!="DIV" && node.className.match(/\bcontent\b/i)==null) {
				node = node.parentNode;
			}
			Tabs.GoTo(node.id.substring(0,1)=="_"?node.id.substring(1):node.id, true);
			document.getElementById(window.location.hash.substring(1)).scrollIntoView(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 contentId = window.location.hash || "#Introduction";

// Set up keyboard shortcuts
function nextNode(el) {
	var n = el;
	do n = n.nextSibling;
	while (n && n.nodeType != 1);
	return n;
}

function previousNode(el) {
	var p = el;
	do p = p.previousSibling;
	while (p && p.nodeType != 1);
	return p;
}

function click(event, anchorObj) {
  if (anchorObj.click) {
    anchorObj.click()
  } else if(document.createEvent) {
    if(event.target !== anchorObj) {
      var evt = document.createEvent("MouseEvents"); 
      evt.initMouseEvent("click", true, true, window, 
          0, 0, 0, 0, 0, false, false, false, false, 0, null); 
      var allowDefault = anchorObj.dispatchEvent(evt);
      // you can check allowDefault for false to see if
      // any handler called evt.preventDefault().
      // Firefox will *not* redirect to anchorObj.href
      // for you. However every other browser will.
    }
  }
}

// Define keyboard shortcuts
document.onkeydown = function(e) {
	var key;
	if(!e) e = window.event;
	key = e.keyCode || e.which;
	el = e.target || e.srcElement;
	var par = (el == document ? document : el.parentNode);
	while (par != document && par.id != "toc" && !par.className.match(/\bcontent\b/)) {
		par = par.parentNode;
	}
	if(par == document) return true;
	var r = true;
	switch(key) {
		case 38:
		//Ctrl-Up
			if(e.ctrlKey && !e.shiftKey && !e.altKey) {
				if (e.preventDefault) e.preventDefault(); else e.returnvalue = false;
				document.getElementById((par.id.substring(0,1)=="_"?par.id.substring(1):par.id)+"-tab").getElementsByTagName("a")[0].focus();
				r = false;
				break;
			}
		case 37:
		// Left, Up
			if(par.id == "toc") {
				if (e.preventDefault) e.preventDefault(); else e.returnvalue = false;
				var prev;
				if(prev = previousNode(el.parentNode)) {
					var target = prev.getElementsByTagName("a")[0];
					target.focus();
					click(e, target);
				} else {
					var x = el.parentNode.parentNode.getElementsByTagName("a");
					var target = x[x.length-1];
					target.focus();
					click(e, target);
				}
				r = false;
			}
			break;
		case 39:
		case 40:
		// Right, Down
			if(par.id == "toc") {
				if (e.preventDefault) e.preventDefault(); else e.returnvalue = false;
				var next;
				if(next = nextNode(el.parentNode)) {
					var target = next.getElementsByTagName("a")[0];
					target.focus();
					click(e, target);
				} else {
					var target = el.parentNode.parentNode.getElementsByTagName("a")[0];
					target.focus();
					click(e, target);
				}
				r = false;
			}
			break;
		case 33:
		//Ctrl-PgUp
			if(e.ctrlKey && !e.shiftKey && !e.altKey) {
				if (e.preventDefault) e.preventDefault(); else e.returnvalue = false;
				var prev = (par.id!="toc"?previousNode(document.getElementById((par.id.substring(0,1)=="_"?par.id.substring(1):par.id)+"-tab")):previousNode(el.parentNode));
				if(prev) {
					var target = prev.getElementsByTagName("a")[0];
					target.focus();
					click(e, target);
				} else {
					var x = el.parentNode.parentNode.getElementsByTagName("a");
					var target = x[x.length-1];
					target.focus();
					click(e, target);
				}
				r = false;
			}
			break;
		case 34:
		//Ctrl-PgDn
			if(e.ctrlKey && !e.shiftKey && !e.altKey) {
				if (e.preventDefault) e.preventDefault(); else e.returnvalue = false;
				var next = (par.id!="toc"?nextNode(document.getElementById((par.id.substring(0,1)=="_"?par.id.substring(1):par.id)+"-tab")):nextNode(el.parentNode));
				if(next) {
					var target = next.getElementsByTagName("a")[0];
					target.focus();
					click(e, target);
				} else {
					var target = el.parentNode.parentNode.getElementsByTagName("a")[0];
					target.focus();
					click(e, target);
				}
				r = false;
			}
			break;
	}
	return r;
}

if (document.createStyleSheet) {
    var style = document.createStyleSheet();
    style.addRule("div.content", "display: none;");
    style.addRule("div" + contentId, "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.content { display: none; }"));
		style.appendChild(document.createTextNode("div" + contentId + " { display: block; }"));
        head.appendChild(style);
    }
}
