var isOver = isOver_sub = animationActive = false;
var checkTO = checkTO_sub = null, currentTD = currentTD_sub = null;
var curWidth, currentLeft, currentIndex = 0;
var navMode = currentMode = "navlevel2";

function checkOver()
{
	if (!isOver && currentTD && !currentTD.is(".fix"))
	{
		currentTD.find('.navlevel2').hide();
		currentTD.find('.navlevel2_shape_left').hide();
		currentTD.find('.navlevel2_shape_right').hide();
		currentTD.removeClass("current");
		currentTD = null;
	}
}

function checkOver_sub()
{
	if (!isOver_sub && currentTD_sub)
	{
		currentTD_sub.find('.navlevel3').hide();
		currentTD_sub.find('.navlevel3_shape_left').hide();
		currentTD_sub.find('.navlevel3_shape_right').hide();
		currentTD_sub = null;
	}
}

function getRequiredNaviElemCount(startIndex)
{
	curWidth = 0;
	var curIdx = startIndex;
	var count = currentTD.find('.'+currentMode).children().length;
	var current = currentTD.find('.'+currentMode).children().eq(curIdx);
	while ((curWidth + current.width()) < 939)
	{
		current = currentTD.find('.'+currentMode).children().eq(curIdx);
		curWidth += current.width();
		curIdx++;
	}
	
	return curIdx - 1;
}

function getRequiredNaviElemCountBack(startIndex)
{
	curWidth = 0;
	var curIdx = startIndex;
	var count = currentTD.find('.'+currentMode).children().length;
	var current = currentTD.find('.'+currentMode).children().eq(curIdx);
	while (curIdx >= 0 && (curWidth + current.width()) < 939)
	{
		current = currentTD.find('.'+currentMode).children().eq(curIdx);
		curWidth += current.width();
		curIdx--;		
	}
	return curIdx + 1;
}

function getNaviElemTotalWidth()
{
	var tempWidth = 0;
	currentTD.find('.'+currentMode).children().each(function () {
		tempWidth += $(this).width();
	});
	return tempWidth;
}

function getNaviElemCurrentWidth(currentLeft)
{
	var tempWidth = 0;
	currentTD.find('.'+currentMode).children(":visible").each(function () {
		tempWidth += $(this).width();
	});
	return tempWidth - Math.abs(currentLeft);
}

function determineNaviArrowState()
{
	animationActive = false;
	
	if (currentTD)
	{
		currentMode = 'navlevel2';		
		currentTD.find('.'+currentMode+'_shape_left').hide();
		currentTD.find('.'+currentMode+'_shape_right').removeClass("just_shadow").hide();
	
		// left arrow	
		var curLeft = currentTD.find('.'+currentMode).css("left");
		curLeft = curLeft.split("p");
		curLeft = parseInt(curLeft[0]);
		if (curLeft < 0)
			currentTD.find('.'+currentMode+'_shape_left').addClass("arrow_shadow_left").show();

		// right arrow
		var curWidth = getNaviElemCurrentWidth(curLeft);
		if (curWidth >= 939)
			currentTD.find('.'+currentMode+'_shape_right').addClass("arrow_shadow_right").show();
		else
			currentTD.find('.'+currentMode+'_shape_right').addClass("just_shadow").show();
	}
	if (currentTD_sub)
	{
		currentMode = 'navlevel3';
		currentTD_sub.find('.navlevel3_shape_left').hide();
		currentTD_sub.find('.navlevel3_shape_right').removeClass("just_shadow").hide();
	
		// left arrow	
		var curLeft = currentTD_sub.find('.navlevel3').css("left");
		curLeft = curLeft.split("p");
		curLeft = parseInt(curLeft[0]);
		if (curLeft < 0)
			currentTD_sub.find('.navlevel3_shape_left').addClass("arrow_shadow_left").show();

		// right arrow
		var curWidth = getNaviElemCurrentWidth(curLeft);
		if (curWidth >= 939)
			currentTD_sub.find('.navlevel3_shape_right').addClass("arrow_shadow_right").show();
		else
			currentTD.find('.'+currentMode+'_shape_right').addClass("just_shadow").show();
	}	
}

function initializeNaviListener()
{
	$('.navi_td table td').mouseover(function () {
		if ($(this).find('.navlevel2').length > 0)
		{
			// clear old hovers first!
			checkOver();
		
			isOver = true;
			currentTD = $(this);
			currentTD.addClass("current");
			currentTD.find('.navlevel2').show();
			determineNaviArrowState();
		}
	});

	$('.navi_td table td').mouseout(function () {
		isOver = false;
		checkTO = window.setTimeout('checkOver()',1000);
	});

	$('.navlevel2 *').mouseover(function () {
		window.clearTimeout(checkTO);
		isOver = true;
	});

	if (navMode == "navlevel3")
	{
		$('.navi_td table td.fix .navlevel2 li').mouseover(function () {
			if ($(this).find('.navlevel3').length > 0)
			{
				// clear old hovers first!
				checkOver_sub();
			
				isOver_sub = true;
				currentTD_sub = $(this);
				currentTD_sub.find('.navlevel3').show();
				determineNaviArrowState();
			}
		});
	
		$('.navi_td table td .navlevel2 li').mouseout(function () {
			isOver_sub = false;
			checkTO_sub = window.setTimeout('checkOver_sub()',1000);
		});
	
		$('.navlevel3 *').mouseover(function () {
			window.clearTimeout(checkTO_sub);
			isOver_sub = true;
		});		
	}
		
	$('.navlevel2_shape_left, .navlevel3_shape_left').click(function () {
		if (!animationActive)
		{
			animationActive = true;

			if ($(this).is('.navlevel2_shape_left'))
				currentMode = "navlevel2";
			else
				currentMode = "navlevel3";
			
			$(this).blur();
			$(this).parent().find('.'+currentMode).find('li').removeClass("first");	

			// index of preceeding starting element
			var newIndex = getRequiredNaviElemCountBack(currentIndex);
			var newLeft = (-$(this).parent().find('.'+currentMode).find('li').eq(newIndex).position().left);
			newLeft = Math.round(newLeft) + 'px';		
		
			// set new currentIndex
			currentIndex = newIndex;
			$(this).parent().find('.'+currentMode).find('li').eq(newIndex).addClass("first");		
			$(this).parent().find('.'+currentMode).addClass("animating").animate({left: newLeft}, 800, "easeInOutQuint", determineNaviArrowState);
		}
	});
	
	$('.navlevel2_shape_right, .navlevel3_shape_right').click(function () {
		if (!$(this).is(".just_shadow") && !animationActive)
		{
			animationActive = true;
			
			if ($(this).is('.navlevel2_shape_right'))
				currentMode = "navlevel2";
			else
				currentMode = "navlevel3";
		
			$(this).blur();
			$(this).parent().find('.'+currentMode).find('li').removeClass("first");	
		
			// determine index of next starting element
			currentIndex = getRequiredNaviElemCount(currentIndex);
			var newLeft = (-$(this).parent().find('.'+currentMode).find('li:visible').eq(currentIndex).position().left);
			newLeft = Math.round(newLeft) + 'px';
		
			// set new currentLeft
			currentLeft = newLeft;
			$(this).parent().find('.'+currentMode).find('li:visible').eq(currentIndex).addClass("first");
			$(this).parent().find('.'+currentMode).addClass("animating").animate({left: newLeft}, 800, "easeInOutQuint", determineNaviArrowState);
		}
	});
}

$(document).ready(function () {
	// determine mode
	if ($('#navigation').is(".homepage"))
		navMode = "navlevel2";
	else
		navMode = "navlevel3";
		
	initializeNaviListener();	
});
