var _timeSwith = 5000; //mls = 5sec
function initTabs()
{
	var links;
	var sets = document.getElementsByTagName("div");
	var _timer = setTimeout(function(){autoSwith()},_timeSwith);
	function autoSwith(){
		if ($('div.tab:visible').length) {
			var _l;
			if ( $('div.tab:visible').next().is('div.tab') ) {
				$('div.tab:visible').fadeOut(800, function(){
					$(this).next('div.tab').fadeIn(800);
					_l = $(this).next('div.tab').attr('id');
					$(links).removeClass('active');
					$('a[href*='+_l+']').addClass('active');
				});
			} else {
				$('div.tab:visible').fadeOut(800, function(){
					$('div.tab').eq(0).fadeIn(800);
					_l = $('div.tab').eq(0).attr('id');
					$(links).removeClass('active');
					$('a[href*='+_l+']').addClass('active');
				});
			}
		}
		_timer = setTimeout(function(){autoSwith()},_timeSwith+800);
	}
	var _tabClick = false;
	$('div.tab').hover(function(){
		if (_timer) clearTimeout(_timer);
	}, function(){
		if (!_tabClick) _timer = setTimeout(function(){autoSwith()},_timeSwith);
	});
	for (var i = 0; i < sets.length; i++)
	{
		if (sets[i].className.indexOf("tabset") != -1)
		{
			var tabs = [];
			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;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

					//reset all tabs on start
					if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
					else c.style.display = "none";

					links[j].onclick = function ()
					{
						_tabClick = true;
						var _this = this;
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
						if (c)
						{
							//reset all tabs before change
							if (_timer) clearTimeout(_timer);
							$('a.tab').removeClass('active');
							$(_this).addClass('active');
							if ($('div.tab:visible').length) {
								$('div.tab:visible').not(c).fadeOut(800, function(){
									$(c).fadeIn(800);
								});
							} else $(c).fadeIn(800);
							//_timer = setTimeout(function(){autoSwith()},_timeSwith);
							return false;
						};
					}
				}
			}
		}
	}
}

if (window.addEventListener) window.addEventListener("load", initTabs, false);
else if (window.attachEvent) window.attachEvent("onload", initTabs);