///////////////////////////////////////////////////////////////////////////////
//	Pinocchio Centre menu ver 1.0

//	configuration
var menuClensingTimeout = 250;	//	ms
var buttonsCount = 6;	//	ms

//	runtime state
var mouseLastX = -1;
var mouseLastY = -1;
var menuIsBusy = false;
var buttonsArray = new Array();

function menuMouseMoveHandler(eventTarget, offset)
{
	if(eventTarget.elementType == "menuButton")
	{
		if(eventTarget.state == "highlighted")
			return false;
		highlightMenuCell(eventTarget.id);
	}
	
	mouseLastX = offset.offsetLeft;
	mouseLastY = offset.offsetTop;
	return false;
}

function menuMouseClickHandler(eventTarget)
{
	if(eventTarget.elementType != "menuButton")
		return false;

	var link = document.getElementById(eventTarget.linkId);
	if(!NReflection.IsInstance(link))
		return false;
	
	document.location.href = link.href;
	return false;
}

function bindMenuEvents()
{
	for(var i = 1; i <= buttonsCount; i++)
	{
		var cell = document.getElementById("button0" + i + "cell");
		if(!NReflection.IsInstance(cell))
			continue;
		cell.onmouseover = NEventSinkService.MouseMoveHandler;
		cell.onclick = NEventSinkService.MouseClickHandler;
		cell.elementType = "menuButton";
		cell.linkId = "button0" + i + "link";
		cell.state = "normal";
		buttonsArray.push(cell);
	}
}

function highlightMenuCell(id)
{
	if(menuIsBusy)
		return;
		
	menuIsBusy = true;
	try
	{
		for(var i = 0; i < buttonsArray.length; i++)
		{
			var cell = buttonsArray[i];
			if(cell.id == id)
				continue;
			var link = document.getElementById(cell.linkId);
			if(!NReflection.IsInstance(link))
				continue;
			
			cell.state = "normal";
			cell.className = "mainMenuButton";
			link.className = "mainMenuButton";
		}

		var cell = document.getElementById(id);
		if(!NReflection.IsInstance(cell))
		{
			menuIsBusy = false;
			return;
		}
		var link = document.getElementById(cell.linkId);
		if(!NReflection.IsInstance(link))
		{
			menuIsBusy = false;
			return;
		}
		
		cell.state = "highlighted";
		cell.className = "mainMenuButton highlight";
		link.className = "mainMenuButton highlight";
		
		window.setTimeout( function() {clearHighlight(id)}, menuClensingTimeout );
		menuIsBusy = false;
	}
	catch(ex)
	{
		menuIsBusy = false;
	}
}

function clearHighlight(id)
{
	if(menuIsBusy)
		return;
		
	menuIsBusy = true;
	try
	{
		var cell = document.getElementById(id);
		if(!NReflection.IsInstance(cell))
		{
			menuIsBusy = false;
			return;
		}
			
		var isMouseOverCell = true;
		
		var cellOffset = NBrowserCaps.GetOffset(cell);
		
		if(mouseLastX < cellOffset.offsetLeft || mouseLastX > cellOffset.offsetLeft + cell.offsetWidth)
			isMouseOverCell = false;
		if(mouseLastY < cellOffset.offsetTop || mouseLastY > cellOffset.offsetTop + cell.offsetHeight)
			isMouseOverCell = false;
			
		if(!isMouseOverCell)
		{
			var link = document.getElementById(cell.linkId);
			if(!NReflection.IsInstance(link))
			{
				menuIsBusy = false;
				return;
			}
			
			cell.state = "normal";
			cell.className = "mainMenuButton";
			link.className = "mainMenuButton";
			menuIsBusy = false;
			return;
		}
			
		window.setTimeout( function() {clearHighlight(id)}, menuClensingTimeout );
		menuIsBusy = false;
	}
	catch(ex)
	{
		menuIsBusy = false;
	}
}