﻿var selected_index = null;
var this_index = null;
var position;
function initTabs()
{
	if($('over_lower_content_tabs'))
	{
		var tabs = getTabs();
		for(var i=0; i<tabs.length; i++)
		{
			if(!tabs[i].className)
				continue;
			if(tabs[i].className == "tab_center" || tabs[i].className == "tab_center_selected" )
			{
				var tab = tabs[i];
				
				tab.onclick = clickTab;
				tab.onmouseover = hoverTab;
				tab.onmouseout = unhoverTab;
			}
		}
	}
}
function clickTab()
{	
	this.className = "tab_center_selected";
	var selected_tab = $(findSelectedTab());
	selected_tab.className = "commenttab";
	var tabid = this.id.substr(0,this.id.lastIndexOf('_'));
	$(tabid).className = "commenttab_selected";
	var tabs = getTabs();
	switch(position)
	{
		case 'near_left':			tabs[this_index+1].className = "tab_right_of_selected";
									if(this_index-2 <= 0)
										tabs[0].className = "tab_left_selected";
									else
										tabs[this_index-1].className = "tab_left_of_selected";
									break;
		case 'near_right':			tabs[this_index-1].className = "tab_left_of_selected";
									if(this_index+2 == tabs.length)
										tabs[tabs.length-1].className = "tab_right_selected";
									else
										tabs[this_index+1].className = "tab_right_of_selected";
									break;
		case 'left':				tabs[this_index+1].className = "tab_right_of_selected";
									if(this_index-2 <= 0)
										tabs[0].className = "tab_left_selected";
									else
										tabs[this_index-1].className = "tab_left_of_selected";
									break;
		case 'right':				tabs[this_index-1].className = "tab_left_of_selected";
									if(this_index+2 == tabs.length)
										tabs[tabs.length-1].className = "tab_right_selected";
									else
										tabs[this_index+1].className = "tab_right_of_selected";
									break;
	}
	tabs[selected_index].className = "tab_center";
	if(position == "near_right")
	{
		if(selected_index == 1)
			tabs[selected_index-1].className = "tab_left";
		else
			tabs[selected_index-1].className = "tab_none_selected";
	}
	if(position == "right")
	{
		if(selected_index == 1)
			tabs[selected_index-1].className = "tab_left";
		else
			tabs[selected_index-1].className = "tab_none_selected";
		tabs[selected_index+1].className = "tab_none_selected";
	}
	if(position == "near_left")
	{
		if(selected_index == tabs.length-2)
			tabs[selected_index+1].className = "tab_right";
		else
			tabs[selected_index+1].className = "tab_none_selected";
	}
	if(position == "left")
	{
		if(selected_index == tabs.length-2)
			tabs[selected_index+1].className = "tab_right";
		else
			tabs[selected_index+1].className = "tab_none_selected";
		tabs[selected_index-1].className = "tab_none_selected";
	}
	resizeSpacer();
}
function getTabs()
{
	var real_tabs = $('over_lower_content_tabs').childNodes;
	if(Prototype.Browser.IE)
		return real_tabs;
	else
	{
		var tabs = new Array();
		for(var i=0;i<real_tabs.length;i++)
		{
			//Damit die Leerzeichen die Mozilla als childNodes zählt weg sind
			if(real_tabs[i].className)
			{
				tabs.push(real_tabs[i]);
			}
		}
		return tabs;
	}
}
function findPositions(id)
{
	//Wo liegt der Tab auf den der Event wirkt in Bezug im selektiertem Tab
	var tabs = getTabs();
	for(var i=0; i<tabs.length; i++)
	{
		if(tabs[i].className  == "tab_center_selected" && tabs[i].id != id)
			selected_index = i;		
	}
	for(var i=0; i<tabs.length; i++)
	{
		if(tabs[i].id == id)
		{
			this_index = i;
			if(this_index == selected_index-2)
				return "near_left";
			if(this_index < selected_index)
				return "left";
			if(this_index == selected_index+2)
				return "near_right";
			if(this_index > selected_index)
				return "right";			
		}
	}
}
function hoverTab()
{
	if(this.id == findSelectedTab()+"_button")
	{
		this.style.cursor = "default";
		return;
	}
	var tabs = getTabs();
	position = findPositions(this.id);
	switch(position)
	{
		case 'near_left':			tabs[this_index+1].className = "tab_both_selected_right";
									if(this_index-2 <= 0)
										tabs[0].className = "tab_left_selected";
									else
										tabs[this_index-1].className = "tab_left_of_selected";
									break;
		case 'near_right':			tabs[this_index-1].className = "tab_both_selected_left";
									if(this_index+2 == tabs.length)
										tabs[tabs.length-1].className = "tab_right_selected";
									else
										tabs[this_index+1].className = "tab_right_of_selected";
									break;
		case 'left':				tabs[this_index+1].className = "tab_right_of_selected";
									if(this_index-2 <= 0)
										tabs[0].className = "tab_left_selected";
									else
										tabs[this_index-1].className = "tab_left_of_selected";
									break;
		case 'right':				tabs[this_index-1].className = "tab_left_of_selected";
									if(this_index+2 == tabs.length)
										tabs[tabs.length-1].className = "tab_right_selected";
									else
										tabs[this_index+1].className = "tab_right_of_selected";
									break;
	}
	this.style.cursor = "pointer";
	this.className = "tab_center_selected";
}
function unhoverTab()
{
	if(this.id == findSelectedTab()+"_button")
		return;
	this.className = "tab_center";
	var tabs = getTabs();
	switch(position)
	{
		case 'near_left':		tabs[this_index+1].className = "tab_left_of_selected";
								if(this_index-2 <= 0)
									tabs[0].className = "tab_left";
								else
									tabs[this_index-1].className = "tab_none_selected";
								break;
		case 'near_right':		tabs[this_index-1].className = "tab_right_of_selected";
								if(this_index+2 == tabs.length)
									tabs[tabs.length-1].className = "tab_right";
								else
									tabs[this_index+1].className = "tab_none_selected";								
								break;
		case 'left':			tabs[this_index+1].className = "tab_none_selected";
								if(this_index-2 <= 0)
									tabs[0].className = "tab_left";
								else
									tabs[this_index-1].className = "tab_none_selected";
								break;
		case 'right':			tabs[this_index-1].className = "tab_none_selected";
								if(this_index+2 == tabs.length)
									tabs[tabs.length-1].className = "tab_right";
								else
									tabs[this_index+1].className = "tab_none_selected";
								break;
	}
}
function findSelectedTab()
{
	var tabs = $('lower_content_body').childNodes;
	for(var i=0; i<tabs.length; i++)
	{
		if(tabs[i].className == "commenttab_selected")
			return tabs[i].id;
	}
}
Event.observe(window,"load",initTabs);
