var WebtaTabs = new Class.create();

WebtaTabs.prototype = {
	hidetime:	0.05, // all times in seconds
	showtime:	0.1, 
	options: 	{},
	element: 	null,
	items: 		[],
	subitems:	[],
	activeTab:	null,
	cronid:		0,
	
	initialize: function(element) {
		var element = $(element);
		var options = Object.extend({
			tabClass:		"Tab",
			activeTabClass:	"Tab-Active",
			activeSubClass:	"Active",
			hidetime: 		this.hidetime,
			showtime: 		this.showtime
		}, arguments[1] || {});
		
		this.options = options;
		this.element = element;
	},
	
	setup: function() {
		var element = this.element;

		if (!element) return false;
		this.items = element.childNodes;
		for(var i = 0; i < this.items.length; i++)
		{
			var item = this.items[i];
			if (item.tagName == "LI")
			{
				var linkObj = this.getFirstChildByTagName(item);
				if (linkObj)
					linkObj.className = this.options.tabClass;
				
				var sub = this.getFirstChildByTagName(item, "UL");
				if (sub)
				{
					this.subitems[i] = sub;
					var subitems = sub.getElementsByTagName("LI");
					for (var j =0; j < subitems.length; j++)
					{
						subitems[j].callerObj = this;
						subitems[j].onmouseover = this.onSubitemOver;
						subitems[j].onmouseout = this.onSubitemOut;
					}
				}
				
				item.submenuNumber = i;
				item.callerObj = this;
				item.onmouseover = this.onItemOver;
				item.onmouseout	 = this.onItemOut;
			}
		}

		this.hideAllSubmenus();
	},
	
	getFirstChildByTagName: function(element, tagName) {
		var element = $(element);
		if (!element) return false;
		if (!tagName) tagName = "A";
		
		var elObj = element.getElementsByTagName(tagName);
		return (elObj.length > 0) ? elObj[0] : false;
	},
	
	hideAllSubmenus: function(num) {
		if (this.cronid)
			clearTimeout(this.cronid);

		if (num != null && this.activeTab == num)
			return false;
					
		$H(this.subitems).each(function(Item){
			try {
				Item[1].style.display = 'none';
			} catch (err) {}
		});
		
		this.activeTab = null;
	},
	
	onItemOver: function() {
		var callerObj = this.callerObj;
		
		var imgObj = callerObj.getFirstChildByTagName(this, "IMG");
		var source = imgObj.src.replace('-a.gif', '.gif');
		imgObj.src = source.replace('.gif', '-a.gif');
	
		callerObj.hideAllSubmenus(this.submenuNumber);
		callerObj.showSubmenu(this.submenuNumber);
	},
	
	onItemOut: function () {
		var callerObj = this.callerObj;
		var isactive = parseInt(this.getAttribute("isactive"));
		if (isactive) return false;
		
		var imgObj = callerObj.getFirstChildByTagName(this, "IMG");
		imgObj.src = imgObj.src.replace('-a.gif', '.gif');
	},
	
	onSubitemOver: function() {
		var callerObj = this.callerObj;
		
		var imgObj = callerObj.getFirstChildByTagName(this, "IMG");
		var source = imgObj.src.replace('-a.gif', '.gif');
		imgObj.src = source.replace('.gif', '-a.gif');
	},
	
	onSubitemOut: function() {
		var callerObj = this.callerObj;
		var isactive = parseInt(this.getAttribute("isactive"));
		if (isactive) return false;
		
		var imgObj = callerObj.getFirstChildByTagName(this, "IMG");
		imgObj.src = imgObj.src.replace('-a.gif', '.gif');
	},
	
	showSubmenu: function(num) {
		var callerObj = this;
		this.cronid = setTimeout(
			function() {
				try {
					callerObj.subitems[num].style.display = '';
					callerObj.activeTab = num;
				} catch (err) {}
			}, this.showtime * 1000);
	},
	
	hideSubmenu: function(num) {
		var callerObj = this;
		this.cronid = setTimeout(
			function() {
				try {
					callerObj.subitems[num].style.display = 'none';
				} catch (err) {}
			}, this.hidetime * 1000);
	},
	
	setActive: function(tabName, subtabName) { 
		for(var i = 0; i < this.items.length; i++)
		{
			var item = this.items[i];
			if (item.tagName == "LI")
			{
				var attr = item.getAttribute("tabName");
				if (tabName && attr == tabName)
				{
					item.setAttribute("isactive", 1);
					var linkObj = this.getFirstChildByTagName(item);
					
					try {
						linkObj.className = this.options.activeTabClass;
						var imgObj = this.getFirstChildByTagName(linkObj, "IMG");
						var source = imgObj.src.replace('-a.gif', '.gif');
						imgObj.src = source.replace('.gif', '-a.gif');
						
						var subItems = this.subitems[i].getElementsByTagName("LI");
						for (var j = 0; j < subItems.length; j++)
						{
							var sattr = subItems[j].getAttribute("tabName");
							if (subtabName && sattr == subtabName){
								subItems[j].setAttribute("isactive", 1);
								
								var link = this.getFirstChildByTagName(subItems[j]);
								link.className = this.options.activeSubClass;
								
								var imgObj = this.getFirstChildByTagName(link, "IMG");
								var source = imgObj.src.replace('-a.gif', '.gif');
								imgObj.src = source.replace('.gif', '-a.gif');
							}
						}
						
						this.showSubmenu(i);
					
					} catch (err) {}
					
				}
			}
		}
		
		
	}
};