/*
This file contains functions that control mouse events on leftmenus, topmenus, level 2 and level 3's flying menus.
*/

//Flying Menus Position - Variables and Arrays

var leftAjust = -29;
var topAjust = 126;
var ON = 1;
var OFF = 0;
var leftmenusLock = OFF;
var enterCounter = 0;
var topColDistance = 30;
var objSelected;
var currentTopPosition;
var currentLeftPosition;
var initialTopPosition;
var topTimerOver = 25;	
var topShiftPixelOffset = 20; //0.25 sec. and 20 pixels for top menu down

var leftTimerOver = 0;
var leftShiftPixelOffset = 40; //0 sec. and 40 pixels for left menu down

var topFlyingTopVisible = topAjust;	//Position of top menu when navigator can see it
var topFlyingTopHidden = topHiddenPosition(allTopMenuId, topColDistance); //Position of top menu when navigator cannot see it
var topFlyingLeft = new Array(232, 330, 400, 500); //Left position of each top menu

var topInterval = 105;	
var topBasePosition = 212;
var leftFlyingTop = new Array(
		topBasePosition,
		topBasePosition + topInterval,
		topBasePosition + topInterval * 2,
		topBasePosition + topInterval * 3,
		topBasePosition + topInterval * 4);
var leftFlyingLeftVisiblePosition = 213 + leftAjust;
var leftFlyingLeftHiddenPosition = 25 + leftAjust;

//var lv3FlyingTop = new Array(leftFlyingTop[0], leftFlyingTop[1], leftFlyingTop[2], leftFlyingTop[3], 535);
var lv3FlyingTop = leftFlyingTop;
//2way 3way
//var lv3FlyingLeft = 347 + leftAjust;
var lv3FlyingLeft = 382 + leftAjust;

//var topFlyingOverColor = "#284b9e"
var lv2FlyingOverColor = "#143585";
var lv2FlyingOutColor = "#011954";

//var lv3FlyingOverColor = "#487bce";
var lv3FlyingOverColor = "#284b9e";
var lv3FlyingOutColor = lv2FlyingOverColor;

//var lv4FlyingOverColor = "#487bce";
var lv4FlyingOverColor = "#4065b8";
var lv4FlyingOutColor = lv3FlyingOverColor;

var lv5FlyingOverColor = "#487bce";
var lv5FlyingOutColor = lv4FlyingOverColor;

/* Place all top flying menus from variables above */
function postionTopFlying(topMenu, topFlyingTopHidden, topFlyingLeft, IsShowed)
{
	for (i = 0; i < topMenu.length; i++)
	{
		var vision = document.getElementById(topMenu[i]);
		vision.style.top = topFlyingTopHidden[i];
		vision.style.left = topFlyingLeft[i];
		vision.style.zIndex = 3;
		if (IsShowed)
			vision.style.visibility = "visible";
	}
}

/* Place all left flying menus */
function postionLeftFlying(leftMenu, leftFlyingTop, leftFlyingLeftHiddenPosition, IsShowed)
{
	for (i = 0; i < leftMenu.length; i++)
	{
		var vision = document.getElementById(leftMenu[i]);
		vision.style.top = leftFlyingTop[i];
		vision.style.left = leftFlyingLeftHiddenPosition;
		vision.style.zIndex = 3;
		//if (IsShowed)
		//	vision.style.visibility = "visible";
	}
}

/* Place all level 3 flying menus */
function postionLv3Flying(leftMenu, lv3FlyingTop, lv3FlyingLeft, IsShowed)
{
	for(i=0; i<leftMenu.length; i++)
	{
		var vision = document.getElementById(leftMenu[i] + lv2Postfix[0] +"Lv3");
		vision.style.top = lv3FlyingTop[i];
		if (i == 2) //Valves
			vision.style.left = lv3FlyingLeft + valveMenuLeftAjust;
		else
			vision.style.left = lv3FlyingLeft;
		if (IsShowed)
			vision.style.visibility = "visible";
	}
}

/* Call functions above to position all flying menus */
function positionAllFlyings(IsShowed)
{
	postionTopFlying(topMenu, topFlyingTopHidden, topFlyingLeft, IsShowed);
	postionLeftFlying(leftMenu, leftFlyingTop, leftFlyingLeftHiddenPosition, IsShowed);
	postionLv3Flying(leftMenu, lv3FlyingTop, lv3FlyingLeft, IsShowed);
}

/* Calculate top hidden positions for all top flying menus based on the number of elements in that topmenu */
function topHiddenPosition(allLevel2, topColDistance)
{
	var lv2Size = allLevel2.length;
	var returnArray = new Array(lv2Size);
	for (i=0; i<lv2Size; i++)
		returnArray[i] = topFlyingTopVisible-(allLevel2[i].length*topColDistance);
	return returnArray;
}

//Flying Menu Move Functions

/* Move top flying menu down when mouse over topmenu and move it up when mouse out topmenu */
function topMoveDown()
{
	if (currentTopPosition + topShiftPixelOffset < topFlyingTopVisible)
	{
		currentTopPosition += topShiftPixelOffset;
		objSelected.style.top = currentTopPosition;
		setTimeout("topMoveDown()", topTimerOver);
	}
	else
	{
		objSelected.style.top = topFlyingTopVisible;
		//objSelected.style.zIndex = 10;
	}
}
function topMoveUp()
{
	//objSelected.style.zIndex = 3;
	objSelected.style.visibility = "hidden";
}

/* Move left flying menu to right when mouse over leftmenu and move it to left when mouse out leftmenu */
function leftMoveOver()
{
	if(currentLeftPosition + leftShiftPixelOffset < leftFlyingLeftVisiblePosition)
	{
		currentLeftPosition += leftShiftPixelOffset;
		objSelected.style.left = currentLeftPosition;
		setTimeout("leftMoveOver()", leftTimerOver);
	}
	else
	{
		objSelected.style.left = leftFlyingLeftVisiblePosition;
		objSelected.style.zIndex = 6;
	}
}
function leftMoveOut()
{
	objSelected.style.zIndex = 3;
	objSelected.style.visibility = "hidden";
}

//Mouse Events Manipulation Functions

/* Hide all top flying menus, change topmenu's image and set a pointer to selected element */
function topOver(idName)
{
	postionTopFlying(topMenu, topFlyingTopHidden, topFlyingLeft, true);
	var source = "Index/Images/" + sSiteLang + "/TopMenu/" + idName + "Over.gif";
	document.images[idName].src = source;
	objSelected = document.getElementById(idName);
}
function topOut(idName)
{
	var source = "Index/Images/" + sSiteLang + "/TopMenu/" + idName + ".gif";
	document.images[idName].src = source;
}

/* when users mouse over or mouse out the topmenus,
the functions topMenuOver and topMenuOut will change (or change back in case of mouse out) the topmenus's images and
set their level 2's flying menus to visible (or hidden, in case of mouse out). */
function topMenuOver(idName)
{
	topOver(idName);
	var tempStr = objSelected.style.top;
	initialTopPosition = parseInt(tempStr.substring(0, tempStr.length-2));
	currentTopPosition = initialTopPosition;
	topMoveDown();
}
function topMenuOut(idName)
{
	topOut(idName);
	topMoveUp();
}

/* Action when mouse over top menu */
function topFlyingMenuOver(idName)
{
	topOver(idName);
	var tempStr = objSelected.style.top;
	initialTopPosition = parseInt(tempStr.substring(0, tempStr.length-2));
	objSelected.style.top = topFlyingTopVisible;
	//objSelected.style.zIndex = 10;
}
function topFlyingMenuOut(idName)
{
	topOut(idName);
	topMoveUp();
	//objSelected.style.zIndex = 3;
}

/* Hide all left flying menus, change leftmenu's image and set a pointer to selected element */
function leftOver(idName)
{
	postionLeftFlying(leftMenu, leftFlyingTop, leftFlyingLeftHiddenPosition, true);
	var source = "Index/Images/" + sSiteLang + "/LeftMenu/MainPage/" + idName + "Over.gif";
	document.images[idName].src = source;
	objSelected = document.getElementById(idName);
	objSelected.style.visibility = "visible";
}
function leftOut(idName)
{
	var source = "Index/Images/" + sSiteLang + "/LeftMenu/MainPage/" + idName+".gif";
	objSelected = document.getElementById(idName);
	objSelected.visibility = "hidden";
	document.images[idName].src = source;
	objSelected = document.getElementById(idName);
	objSelected.style.visibility = "hidden";	
}

/* when users mouse over or mouse out the leftmenus,
the functions leftMenuOver and leftMenuOut will change (or change back in case of mouse out) the leftmenus's images and
set their level 2's flying menus to visible (or hidden, in case of mouse out). */
function leftMenuOver(idName)
{
	if (leftmenusLock == OFF)
	{
		leftOver(idName);
		currentLeftPosition = leftFlyingLeftHiddenPosition;
		leftMoveOver();
	}
}
function leftMenuOut(idName)
{
	if(leftmenusLock == OFF)
	{
		leftOut(idName);
		leftMoveOut();
	}
}

function leftFlyingMenuOver(idName)
{
	leftOver(idName);
	objSelected.style.left = leftFlyingLeftVisiblePosition;
	objSelected.style.zIndex = 6;
}
function leftFlyingMenuOut(idName)
{
	leftOut(idName);
	leftMoveOut();
}

/* When users mouse over or mouse out one of options (link) in a flying menu,
the functions flyingOver and flyingOut change (or change back in case of mouse out) the background color of that specific option. */
function flyingOver(idName)
{
	var vision = document.getElementById(idName);
	vision.style.background = lv2FlyingOverColor;
}
function flyingOut(idName)
{
	var vision = document.getElementById(idName);
	vision.style.background = lv2FlyingOutColor;
}

function topFlyingOver(idName)
{
	flyingOver(idName);
	if (objSelected)
		objSelected.style.zIndex = 10;
}
function topFlyingOut(idName)
{
	flyingOut(idName);
	if (objSelected)
		objSelected.style.zIndex = 3;
}
		
/* cataFlyingOver and cataFlyingOut do exactly what flyingOver and flyingOut do, plus
they show (or hide, in case of mouse out) their corresponding level 3's flying table and
draw (or erase, in case of mouse out) the yellow line between level 2 and 3's flying table. */
function cataFlyingOver(idName)
{
	flyingOver(idName);
	var flyingName = idName.substring(0, idName.length - lv2Postfix[0].length);
	var vision2 = document.getElementById(flyingName);
	vision2.style.borderRightWidth = "0px";
	var vision = document.getElementById(idName + "Lv3");
	vision.style.visibility = "visible";
}
function cataFlyingOut(idName)
{
	flyingOut(idName);
	var flyingName = idName.substring(0, idName.length - lv2Postfix[0].length);
	var vision2 = document.getElementById(flyingName);
	vision2.style.borderRightWidth = "1px";
	var vision = document.getElementById(idName + "Lv3");
	vision.style.visibility = "hidden";
}

/* lv3TableOver and lv3TableOut are the combination of cataFlyingOver and leftFlyingMenuOver, or cataFlyingOut and lfetFlyingMenuOut. */
function lv3TableOver(idName)
{
	var leftName = idName + lv2Postfix[0];
	cataFlyingOver(leftName);
	leftFlyingMenuOver(idName);
}
function lv3TableOut(idName)
{
	var leftName = idName + lv2Postfix[0];
	cataFlyingOut(leftName);
	leftFlyingMenuOut(idName);
}

/*
When users mouse over or mouse out one of options (link) in a flying menu,
the functions lv3FlyingOver and lv3FlyingOut change (or change back in case of mouse out) the background color of that specific option.
*/
function lv3FlyingOver(idName)
{
	document.getElementById(idName).style.background = lv3FlyingOverColor;
}
function lv3FlyingOut(idName)
{
	document.getElementById(idName).style.background = lv3FlyingOutColor;
}