function initTabs() {
	new TabSet("landingTabs");
}

function TabSet(id) {
	this.init(id);
}

TabSet.prototype = {
	init: function(id) {
		this.id = id;
		this.currTabId = null;
		this.container = document.getElementById(id);
		var tabContainer = YAHOO.util.Dom.getElementsByClassName("tabs", "ul", this.container)[0];
		var li = tabContainer.getElementsByTagName("li");
		this.tabs = {};
		
		for (x=0; x < li.length; x++) {
			var tab = new Tab(li[x], this);
			this.tabs[tab.id] = tab;
			if (tab.isShowing) {
				this.currTabId = tab.id;
			}
		}
		// let's see if there is another tab that should be highlighted
		var switchTab = document.location.hash.replace("#tabId=", "");
		if (switchTab != "" && switchTab != this.currTabId) {
			this.switchTab(null, this.tabs[switchTab]);
		}
	},
	switchTab: function(e, tab) {
		if (e) {
			YAHOO.util.Event.stopEvent(e);
		}
		if (!tab.showing && tab.id != this.currTabId) {
			this.tabs[this.currTabId].hide();
			tab.show();
			this.currTabId = tab.id;
			var url = document.location.href;
			url = url.replace(/#.*$/, "");
			document.location.replace(url + "#tabId=" + this.currTabId);
		}
	}
}

function Tab(li, tabset) {
	this.init(li, tabset);
}

Tab.prototype = {
	init: function(li, tabset) {
		this.li = li;
		this.tabset = tabset;
		this.link = li.getElementsByTagName("a")[0];
		this.id = this.link.hash.replace("#", "");
		this.content = document.getElementById(this.id);
		this.isShowing = YAHOO.util.Dom.hasClass(this.li, "selected");
		this.animation = null;
			
		YAHOO.util.Event.on(this.link, "mouseover", tabset.switchTab, this, tabset);
	},
	show: function() {
		if (!this.isShowing) {
			var fade = new YAHOO.util.Anim(this.content, {opacity: {from: 0, to: 1} }, .3);
			fade.onStart.subscribe(this.showBegin, this, true);
			fade.onComplete.subscribe(this.showComplete, this, true);
			// this is just a little safety precaution, in case someone can press back and forth
			// between tabs faster than 3/10 of a second, it will piggy back this animation event
			// on the last one.  The hope is that they can't sustain the toggling for an extended
			// period of time.  OK, OK, it must be late, who worries about crap like this anyway?
			if (this.animation != null) {
				this.animation.onComplete.subscribe(fade.animate, fade, true);
			} else {
				this.animation = fade;
				fade.animate();
			}
		}
	},
	hide: function() {
		if (this.isShowing) {
			var fade = new YAHOO.util.Anim(this.content, {opacity: {from: 1, to: 0} }, .3);
			fade.onStart.subscribe(this.hideBegin, this, true);
			fade.onComplete.subscribe(this.hideComplete, this, true);
			// see comment above  in the show() method for explanation.
			if (this.animation != null) {
				this.animation.onComplete.subscribe(fade.animate, fade, true);
			} else {
				this.animation = fade;
				fade.animate();
			}
		}
		
	},
	fireSecondAnimation: function(secondAnimation) {
		this.animation = secondAnimation;
		secondAnimation.animate();
	},
	showBegin: function() {
		YAHOO.util.Dom.addClass(this.li, "selected");
		this.content.style.display = "block";
		this.isShowing = true;
	},
	showComplete: function() {
		this.animation = null;
	},
	hideBegin: function() {
		YAHOO.util.Dom.removeClass(this.li, "selected");
		this.isShowing = false;
	},
	hideComplete: function() {
		this.content.style.display = "none";
		this.animation = null;
		this.isShowing = false;
	}
}
YAHOO.util.Event.onDOMReady(initTabs);