// written by Tanny O'Haley, 28 Jul 2006
// http://tanny.ica.com
// Dependencies:
//	events.js, util.js

/*global addEvent addClass hasClass removeClass getElementsByClassName */

var tabs = {
	name: "tabs",
	obj: document,
	isMSIE: /*@cc_on!@*/false,

	init: function () {
		// Tell the CSS that tab items can be hidden from view since javascript is enabled. 
		addClass(document.body, "tabs");
		
		// Get all div elements with a class of "tabs"
		var els = getElementsByClassName("tabs", "div");
		for (var i = 0; i < els.length; i++) {
			this.processTab(els[i]);
		}

		// The format of a hash is #name. If there is a hash open the tab for that hash item.
		if (location.hash !== "") {
			this.openTabOfElement(document.getElementById(location.hash.substr(1)));
		}
	},

	processTab: function (el) {
		// Get the tabBody for this set of tabs.
		var elBody = el.nextSibling;
		while (!hasClass(elBody, "tabBody")) {
			elBody = elBody.nextSibling;
		}

		// Get an array of anchor elements in the tabs.
		var els = el.getElementsByTagName("a");

		for (var i = 0; i < els.length; i++) {
			// elParent is the li element that holds the anchor.
			var elParent = els[i].parentNode;

			// Set the tab parent for speed.
			elParent.tabDiv = el;

			// Set the parent node for speed.
			elParent.tabBody = elBody;

			// Set the tabNo for speed.
			elParent.tabNo = i;

			// Add the click events to the anchor and parent li elements.
			addEvent(els[i], "click", this.showTab);
			addEvent(elParent, "click", this.showTab);
			
			// For IE add a hover function for the LI element.
			if (this.isMSIE) {
				addEvent(elParent, "mouseover", function () { addClass(this, "sfhover"); });
				addEvent(elParent, "mouseout", function () { removeClass(this, "sfhover"); });
			}
		}
	},

	showTab: function (e) {
		var elSrc;
		
		// Stop the click event here.
		if (!e) {
			e = window.event;
		}

		e.cancelBubble = true;
		if (e.stopPropagation) {
			e.stopPropagation();
		}

		e.preventDefault();

		// Get the source element.
		if (e.target) {
			elSrc = e.target;
		} else if (e.srcElement) {
			elSrc = e.srcElement;
		}

		// Make sure that we are pointing at the li element.
		if (elSrc.tagName === "A") {
			elSrc.blur();
			elSrc = elSrc.parentNode;
		}

		tabs.setTab(elSrc);
	},

	setTab: function (elSrc) {
		// Get the tabs.
		var els = elSrc.tabDiv.getElementsByTagName("li");
		// Set the selected tab.
		for (var i = 0; i < els.length; i++) {
			if (elSrc === els[i]) {
				addClass(els[i], "selected");
			} else {
				removeClass(els[i], "selected");
			}
		}

		// Get the children of the tab body and set the selected tab item.
		var iTabs = 0;
		els = elSrc.tabBody.childNodes;
		for (i = 0; i < els.length; i++) {
			if (els[i].nodeName === "DIV" && hasClass(els[i], "tabItem")) {
				if (iTabs === elSrc.tabNo) {
					addClass(els[i], "selected");
				} else {
					removeClass(els[i], "selected");
				}

				iTabs++;
			}
		}

		return false;
	},
	
	openTabOfElement: function (el) {
		// Is this an object?
		if (!el) {
			return;
		}

		// Get the tabItem div of the passed element.
		var tabItem = el;
		while (tabItem) {
			if (tabItem.nodeName === "DIV" && hasClass(tabItem, "tabItem")) {
				break;
			}

			tabItem = tabItem.parentNode;
		}

		// Didn't find a tab item for the passed element.
		if (!tabItem) {
			return;
		}

		// Is the tab already open? If so, return.
//		if (hasClass(tabItem, "selected")) {
//			return;
//		}

		// Get the tabBody div.
		var tabBody = tabItem.parentNode;
		while (tabBody) {
			if (tabBody.nodeName === "DIV" && hasClass(tabBody, "tabBody")) {
				break;
			}

			tabBody = tabBody.parentNode;
		}

		// No tab body?
		if (!tabBody) {
			return;
		}

		// Get the children of the tab body and find the selected tab item.
		var iTabNo = 0;

		var tabItems = tabBody.childNodes;
		for (var i = 0; i < tabItems.length; i++) {
			if (tabItems[i].nodeName === "DIV" && hasClass(tabItems[i], "tabItem")) {
				if (tabItems[i] === tabItem) {
					break;
				}

				iTabNo++;
			}
		}

		// If you can't get then number, return.
		if (i === tabItems.length) {
			return;
		}
	
		// Get the tab.
		var tab = tabBody.previousSibling;
		while (tab.nodeName !== "DIV") {
			tab = tab.previousSibling;
		}

		// Get an array of li elements in the tab.
		var tabs = tab.getElementsByTagName("li");
		if (typeof tabs[iTabNo] === "undefined") {
			return;
		}
		
		// The tab is found, open the tab.
		this.setTab(tabs[iTabNo]);

		// Continue for embedded tabs.
		this.openTabOfElement(tab);
	}
};

addEvent(window, "DOMContentLoaded", function () { tabs.init(); });

