var _tabss = [];
var effect_time = 200;//500ms;
var change_time = 18000;//18 second;
var current = 0;
var is_tabs = 0;
var _timer;
var _obj = false;
function initTabs()
{
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++)
	{
		if (sets[i].className.indexOf("tabset") != -1)
		{
			var tabs = [];
			var links = sets[i].getElementsByTagName("a");
			for (var j = 0; j < links.length; j++)
			{
				if (links[j].className.indexOf("tab") != -1)
				{
					tabs.push(links[j]);
					links[j].tabs = tabs;
					links[j]._index = j;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));
					c.mooeffect = new Fx.Morph(c, {duration: effect_time});
					//reset all tabs on start
					if (c) if (links[j].className.indexOf("active") != -1) {
						//c.style.display = "block";
						c.style.opacity = 0;
						c.className += " active";
						c.mooeffect.start({'opacity':[1]});
					}
					else {
						c.style.display = "none";
						c.style.opacity = 0;
					}
					links[j].onclick = function ()
					{
						if (current != this._index)
						{
							current = this._index;
							var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
							c.mooeffect = new Fx.Morph(c, {duration: effect_time});
							if (c)
							{
								//reset all tabs before change
								for (var i = 0; i < this.tabs.length; i++)
								{
									var anoth = document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1));
									if (anoth && anoth.className.indexOf('active')!=-1){
										//anoth.style.display = "none";
										anoth.className = anoth.className.replace("active", "");
										anoth.mooeffect.start({'opacity':0});
									}
									this.tabs[i].className = this.tabs[i].className.replace("active", "");
								}
								this.className += " active";
								c.className += " active";
								c.style.display = "block";
								//c.mooeffect.stop();
								//var _tabholder = $('tabholder');
								//_tabholder.style.height = c.offsetHeight + 'px';
								c.style.opacity = 0;
								_obj = c;
								setTimeout('_show()',effect_time);
								//c.mooeffect.start({'opacity':[0, 1]});
								return false;
							}
						}
						return false;
					}
				}
			}
			//_tabss[_tabss.length] = sets[i];
		}
	}
}
function initAutoTabs(){
	initTabs();
	_tab_ul = $("tabset");
	var _tabs = $$('.tab-hold div.tab');
	if (_tab_ul && _tabs.length){
		_tab_ul.onclick = function(){
			if (!is_tabs){
				is_tabs = 1;
				if (_timer) clearTimeout(_timer);
			}
		}
		_tabs.each(function(_el, i){
			_el.mooeffect = new Fx.Morph(_el, {duration: effect_time});
			_el.mooeffect.set({'opacity':0});
			_tabss.push(_el);
		})	
		current = 1;
		showTab(0,true);
	}
	
}

function showTab(_ids,_start){
	var _tab_a = $$('#tabset a.tab');
	//var _tabholder = $('tabholder');
	
	_tabss[_ids].style.display = "block";
	
	//_tabholder.style.height = _tabss[_ids].offsetHeight + 'px';
	_tab_a.each(function(_el, i){
		_el.className = _el.className.replace("active", "");
	})
	_tab_a[_ids].className += ' active';

	if(_tabss[current])
	{
		_tabss[current].mooeffect.start({'opacity':0});
	}

	if(!_start)
	{
		_obj = _tabss[_ids];
		setTimeout('_show()',effect_time);
		//_tabss[_ids].mooeffect.start({'opacity':[0, 1]});
	}
	else
	{
		_tabss[_ids].mooeffect.set({'opacity':1});		
	}

	current = _ids;
	var _next = _ids + 1;
	if (_next<_tabss.length){
		_timer = setTimeout('showTab('+_next+')', change_time);
	}
	else _timer = setTimeout('showTab('+0+')', change_time);
}

function initMenu()
{
	var nodes = document.getElementById("navigation").getElementsByTagName("li");
	for (var i=0; i<nodes.length; i++)
	{
		nodes[i].onmouseover = function()
		{
			this.className += " hover";
		}
		nodes[i].onmouseout = function()
		{
			this.className = this.className.replace(" hover", "");
		}
	}
}

function _show()
{
	if(_obj && _obj.mooeffect)
	{
		_obj.mooeffect.start({'opacity':[0, 1]});
	}
}

if (window.addEventListener)
{
	window.addEventListener("load", initAutoTabs, false);
}
else if (window.attachEvent)
{
	window.attachEvent("onload", initAutoTabs);
	window.attachEvent("onload", initMenu);
}