// Prepare Form Fields
function PrepareFormField(control, defaultValue)
{
	$(control).attr('value', defaultValue).focus(function() { if ($(this).attr('value') == defaultValue) { $(this).attr('value', ''); } }).blur(function() { if ($(this).attr('value') == '') { $(this).attr('value', defaultValue); } });
}

// Menu instellingen
$().ready(function()
{
	var liConfig = { sensitivity: 3, interval: 0, over: overLiFunction, timeout: 100, out: outLiFunction };
	var subLiConfig = { sensitivity: 3, interval: 0, over: overSubLiFunction, timeout: 100, out: outSubLiFunction };

	isFoldedOut = false;
	isAnimating = false;
	
	$('#MainMenuContainer').bind('mouseleave', function(e)
	{
		removeAllBlockedClasses();
		outBgFunction();
	});

	$('li.level1').map(function()
	{
		$(this).hoverIntent(liConfig);
	});

	$('li.level2').map(function()
	{
		$(this).hoverIntent(subLiConfig);
	});
});

function checkDivHeight()
{
	isFoldedOut = false;
	if($('#MainMenuContainer').css('height') == '173px')
	{
		isFoldedOut = true;
	}
}

function setMenuBackground(myHoveredLi, goingIn)
{
	if($(myHoveredLi).children('ul').length == 0 && $(myHoveredLi).parent('ul').attr('id') == 'MainMenu')
	{
		outBgFunction();
	}
	else
	{
		if(goingIn)
		{
			overBgFunction();
		}
	}
}

function overBgFunction()
{
	checkDivHeight();

	if(!isFoldedOut)
	{
		isAnimating = true;
		$('#MainMenuContainer').animate({ 
			height: '173px'
		}, 750, function(){
			isAnimating = false;
		});
	}
}

function outBgFunction()
{
	checkDivHeight();
	removeAllBlockedClasses();
	$('#MainMenuTitle').css('display', 'none');

	if(isFoldedOut)
	{
		isAnimating = true;
		$('#MainMenuContainer').animate({ 
			height: '33px'
		}, 750, function(){
			isAnimating = false;
		});
	}
}

function overLiFunction()
{
	var myId = $(this).children('ul').attr('id');
	$('#MainMenuTitle').css('display', 'none');

	if($(this).children('ul').length > 0)
	{
		if($('#MainMenuContainer').css('height') < '33px')
		{
			setMenuBackground(this, true);
		}
	}

	if(isAnimating)
	{
		$().everyTime(10, 'waitForAnimation', function()
		{
			if($('#MainMenuContainer').css('height') == '173px')
			{
				overLiTimer(this, myId);
				$().stopTime('waitForAnimation');
			}
		});
	}
	else
	{
		overLiTimer(this, myId)
	}
}

function overLiTimer(myLi, myId)
{
	setMenuBackground(myLi, true);
	childMenuWaitingroom(myId, '1', 100);
}

function outLiFunction()
{
	var myId = $(this).children('ul').attr('id');
	setMenuBackground(this, false);
}

function childMenuWaitingroom(myId, opacityIndex, animationSpeed)
{
	if(isAnimating)
	{
		$().oneTime(750, 'waitingroom', function()
		{
			setChildMenu(myId, opacityIndex, animationSpeed);
		});
	}
	else
	{
		setChildMenu(myId, opacityIndex, animationSpeed);
	}
}

function setChildMenu(myId, opacityIndex, animationSpeed)
{
	if(myId != null)
	{
		removeAllBlockedClasses();

		myId = '#' + myId;
		$(myId).css('display', 'block');
	}
}

function removeAllBlockedClasses()
{
	$('ul#MainMenu li ul').each(function()
	{
		$(this).css('display', 'none');
	});
}

function removeAllOtherBlockedClasses(myId)
{
	if(myId != undefined)
	{
		$('ul#MainMenu li ul').each(function()
		{
			if($(this).attr('id') != myId)
			{
				$(this).css('display', 'none');
			}
		});
	}
	else
	{
		removeAllBlockedClasses();
	}
}

function overSubLiFunction()
{
	if($(this).children('a').attr('menutitle') != null)
	{
		var offset = 15;
		var selectedSub = 0;
		$(this).parent().parent().parent().children('li').each(function()
		{
			selectedSub++;
			if($(this).children('ul').css('display') == 'block')
			{
				switch(selectedSub)
				{
					case 1:
						offset = 235;
						break;
					case 2:
						offset = 365;
						break;
					case 3:
						offset = 475;
						break;
					case 4:
						offset = 535;
						break;
					case 5:
						offset = 120;
						break;
					case 6:
						offset = 220;
						break;
					case 7:
						offset = 400;
						break;
				}
			}
		});

		$('#MainMenuTitle').html($(this).children('a').attr('menutitle'));
		$('#MainMenuTitle').css('opacity', '0');
		$('#MainMenuTitle').css('display', 'inline-block');
		$('#MainMenuTitle').css('left', offset + 'px');
		$('#MainMenuTitle').animate({ 
			opacity: '1'
		}, 250);
	}
}

function outSubLiFunction()
{
	
}