var mouseX = 0;
var mouseY = 0;

var LEFT = 0;
var TOP = 1;
var RIGHT = 2;
var BOTTOM = 3;
var PARENTID = 4;
var GLOBALX = 5;
var GLOBALY = 6;

var iClearTimeout = 1000;

var iFocusID = null;
var iAllCleared = true;
var focusLayer = null;
var topFocusLayer = null;
var arrHistory = new Array();
var arrItemsToBeCleared = new Array();

function addToHistory(iItemID) {
	arrHistory[arrHistory.length] = iItemID;
}

function historyPop() {
	var arrTemp = new Array();
	for (var i = 0; i < arrHistory.length - 1; i++) 
		arrTemp[i] = arrHistory[i];
	arrHistory = arrTemp;
}

function evalClear() {
	var iLastItemID;
	for (var i = arrHistory.length - 1; i >= 0; i--) {
		iLastItemID = arrHistory[i];
		if (iLastItemID != iFocusID && iLastItemID != arrItemInfo[iFocusID][PARENTID]) {
			clearItem(iLastItemID);
			historyPop();
		} else {
			break;
		}
	}
}

function mouseAction(iItemID) {
	if (arrItemInfo == null) return 1;

	if (iItemID != iFocusID) {
									
		iFocusID = iItemID;
		evalClear();
		addToHistory(iItemID);

		if (arrItemInfo[iItemID][PARENTID] == -1) {
			var clipFocusLayer = topFocusLayer;
		} else {
			var clipFocusLayer = getLayer("panel" + arrItemInfo[iItemID][PARENTID] + "On");
		}
		clipLayer(clipFocusLayer, arrItemInfo[iItemID][LEFT], arrItemInfo[iItemID][TOP], 
									arrItemInfo[iItemID][RIGHT], arrItemInfo[iItemID][BOTTOM]);

		var unFocusLayer = getLayer("panel" + iItemID + "Off");
		var focusLayer = getLayer("panel" + iItemID + "On");
		var imgLayer = getLayer("panel" + iItemID + "Img");
		if (unFocusLayer != null) {
			if (arrItemInfo[iItemID][PARENTID] == -1) {
				var iSubX = arrItemInfo[iItemID][GLOBALX];
				var iSubY = arrItemInfo[iItemID][GLOBALY];
			} else {
				var iSubX = arrItemInfo[iItemID][GLOBALX];
				var iSubY = arrItemInfo[iItemID][GLOBALY];
			}
	
			clipLayer(focusLayer, 0, 0, 0, 0);

			moveLayerTo(unFocusLayer, iSubX, iSubY);
			moveLayerTo(focusLayer, iSubX, iSubY);
			moveLayerTo(imgLayer, iSubX, iSubY);
			showLayer(unFocusLayer);
			showLayer(focusLayer);
			showLayer(imgLayer);
		}
	}
}

function setFormat() {
	topFocusLayer = getLayer("toppanelOn");
	clipLayer(topFocusLayer, 0, 0, 0, 0);
	showLayer(topFocusLayer);
	if (isMinNS4)
		setTimeout("initClearItems();", 500);
	else
		initClearItems();
}

function clearItem(iItemID) {
	if (iItemID == -1) {
		topFocusLayer = getLayer("toppanelOn");
		clipLayer(topFocusLayer, 0, 0, 0, 0);
	} else {
		var unFocusLayer = getLayer("panel" + iItemID + "Off");
		var focusLayer = getLayer("panel" + iItemID + "On");
		var imgLayer = getLayer("panel" + iItemID + "Img");
		if (unFocusLayer != null) {
			moveLayerTo(unFocusLayer, -1000, -1000);
			moveLayerTo(focusLayer, -1000, -1000);
			moveLayerTo(imgLayer, -1000, -1000);
			hideLayer(unFocusLayer);
			hideLayer(focusLayer);
			hideLayer(imgLayer);
		}
	}
}

function getMousePosition(e) {
	if (isMinNS4) {
		mouseX = e.pageX;
		mouseY = e.pageY;
	}
	if (isMinIE4) {
		mouseX = event.clientX + document.body.scrollLeft;
		mouseY = event.clientY + document.body.scrollTop;
	}
	if (isMinMoz5) {
		mouseX = e.clientX;
		mouseY = e.clientY;
	}
	
	/*
	document.forms[0].mouseX.value = mouseX;
	document.forms[0].mouseY.value = mouseY;
	*/
	
	//evalMenuContents();
	return true;
}

function initClear(iItemID) {
	var l = arrItemsToBeCleared.length;
	arrItemsToBeCleared[l] = iItemID;	
}

function initClearItems() {
	for (var i = 0; i < arrItemsToBeCleared.length; i++) {
		clearItem(arrItemsToBeCleared[i]);
	}
}

function clearByPosition() {
	var parentKey;
	for (var i = arrHistory.length - 1; i >= 0; i--) {
		iLastItemID = arrHistory[i];
		iLastParentID = arrItemInfo[iLastItemID][PARENTID];
		parentKey = (iLastParentID != -1) ? iLastParentID : 'top'; 

		//window.status = arrPositionInfo[parentKey].toString();
		
		if (!(mouseY >= arrPositionInfo[parentKey][TOP] &&
			mouseY <= arrPositionInfo[parentKey][BOTTOM] &&
			mouseX >= arrPositionInfo[parentKey][LEFT] &&
			mouseX <= arrPositionInfo[parentKey][RIGHT])) {
			clearItem(iLastParentID);
			clearItem(iLastItemID);
		} else {
			break;
		}
	}
}	


function clearService() {
	if (arrHistory.length) {
		clearByPosition();
	}
	setTimeout("clearService();", iClearTimeout);
}

if (isMinNS4)
	document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMousePosition;


