var _tab_fade = false;
var _min_height = 400;
_t = false;

function initTabs()
{
	_t = document.getElementById('newtabs');
	var sets = document.getElementsByTagName("div");
	var _content = document.getElementById("content");
	var _max_height = 0;
	var __tabs = [];
	
	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;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

					//reset all tabs on start
					if (c)
					{
						_max_height = Math.max(_max_height,c.offsetHeight);
						if(c.parentNode.className.indexOf("tab-content") != -1)
						{
							c.className += " tab-fade";

							if (links[j].className.indexOf("active") != -1)
							{
								if(c.offsetHeight + 8 > _t.offsetHeight)
								{
									_t.style.height = c.offsetHeight + 8 + "px";
								}
								else
								{
									_t.style.height = _min_height + "px";									
								}

								c.style.display = "block";
								
							}
							else
							{
								c.style.display = "none";								
							}


						}						
						else
						{
							if (links[j].className.indexOf("active") != -1)
							{
								c.style.display = "block";
								c.style.zIndex = 10;
							}
							else
							{
								c.style.display = "none";
								c.style.zIndex = 9;
							}
						}
					}
					
					links[j]._index = tabs.length - 1;

					links[j].onclick = function ()
					{						
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
						if (c)
						{
							//reset all tabs before change
							for (var i = 0; i < this.tabs.length; i++)
							{
								if(_content.className.indexOf("tab-content") == -1)
								{
									var tab = document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1));	
									if (tab )
									{
										tab.style.display = "none";
									}
								}
								this.tabs[i].className = this.tabs[i].className.replace("active", "");
							}
							this.className += " active";

							if(_content.className.indexOf("tab-content") != -1)
							{								
								_tab_fade.slideTo(this._index);
								if(c.offsetHeight + 8 >= _t.offsetHeight)
								{
									_t.style.height = c.offsetHeight + 8 + "px";
								}
								else
								{
									_t.style.height = _min_height + "px";									
								}
							}
							else
							{
								c.style.display = "block";
							}
							return false;
						}
					}
				}
			}
			
		}

	}

/*
		_max_height += 8;
		if(_t.offsetHeight < _max_height)
		{
			_t.style.height = _max_height + "px";
		}
		for (var i = 0; i < __tabs.length; i++)
		{
			__tabs[i].style.height = _max_height + "px";
		}
*/		
		if(_content.className.indexOf("tab-content") != -1)
		{
			_tab_fade = new FadeSlideShow("div.slide-tab",{duration:500,_sindex: 2,
				onComplete: function()
				{
					this.elements.each(function(el, i){
						if(el.style.visibility == 'hidden')
						{
							el.style.display = 'none';
						}
						else
						{
							el.style.display = 'block';
						}
					})
				},
				onStart: function()
				{
					this.elements.each(function(el, i){
						el.style.display = 'block';
					})
				}

			});
		}
	
}

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