/* Tab Navigator */

function TabNavigator(str)
{
	bindMethods(this);
	
	this.parent = $(str);
	this.tabs = [];
	
	this.init();
	this.select(this.tabs[0]);
}

TabNavigator.prototype.init = function()
{
	var arr = getElementsByTagAndClassName(null, 'tab', this.parent);
	forEach(arr, this.initTab);
}

TabNavigator.prototype.initTab = function(obj)
{
	var arr = getElementsByTagAndClassName(null, 'tab-content', this.parent);
	var content = arr[this.tabs.length];
	this.tabs.push(new Tab(obj, content, this));
}

TabNavigator.prototype.select = function(tab)
{
	tab.changeState('active');
	forEach(this.tabs, function(obj)
	{
		if (obj != tab)
			obj.reset();
	});
}


/* Tab Object */

function Tab(obj, cont, parent)
{
	bindMethods(this);
	
	this.el = obj;
	this.content = cont;
	this.nav = parent;
	
	this.reset();
}

Tab.prototype.reset = function()
{
	disconnectAll(this.el);
	connect(this.el, 'onmouseover', this.onMouseOver);
	connect(this.el, 'onmouseout', this.onMouseOut);
	connect(this.el, 'onclick', this.onClick);
	
	this.changeState('default');
}

Tab.prototype.changeState = function(str)
{
	switch (str) {
		case 'hover' :
			removeElementClass(this.el, 'tab-active');
			removeElementClass(this.el, 'tab-default');
			addElementClass(this.el, 'tab-hover');
			
			removeElementClass(this.content, 'tab-content-on');
			addElementClass(this.content, 'tab-content-off');
			break;
		case 'active' :
			removeElementClass(this.el, 'tab-hover');
			removeElementClass(this.el, 'tab-default');
			addElementClass(this.el, 'tab-active');
			
			removeElementClass(this.content, 'tab-content-off');
			addElementClass(this.content, 'tab-content-on');
			
			disconnectAll(this.el);
			break;
		default :
			removeElementClass(this.el, 'tab-active');
			removeElementClass(this.el, 'tab-hover');
			addElementClass(this.el, 'tab-default');
		
			removeElementClass(this.content, 'tab-content-on');
			addElementClass(this.content, 'tab-content-off');
			break;
	}
};

Tab.prototype.onMouseOver = function()
{
	this.changeState('hover');
};

Tab.prototype.onMouseOut = function()
{
	this.changeState('default');
};

Tab.prototype.onClick = function()
{
	this.nav.select(this);
};
