﻿menudisplayed = "";
arrowflag = 0;
menutop = 0;
topaon = 0;
bottomaon = 1;
hasmousemoved = 0;
oldmousey = 0;

document.onmousemove = mymousemove;
function mymousemove(myevent){
  var ev = (!myevent)?window.event:myevent;
	hasmousemoved = 1;
	if(menudisplayed != "" && isloaded == 1){
		theloc = getY("menu" + menudisplayed + "cell");
		topleftx = theloc.x;
		toplefty = theloc.y;
		toprightx = Number(topleftx) + Number(document.getElementById("menu" + menudisplayed + "cell").offsetWidth);
		//alert(topleftx + " " + toprightx);
		toprighty = toplefty;
		cellbottomy = Number(toplefty) + Number(document.getElementById("menu" + menudisplayed + "cell").offsetHeight);
		theloc = getY("menu" + menudisplayed);
		bottomleftx = theloc.x;
		menutopy = theloc.y;
		bottomlefty = Number(theloc.y) + Number(document.getElementById("menu" + menudisplayed).offsetHeight);
		//alert(Number(theloc.y) + " " + document.getElementById("menu" + menudisplayed).offsetHeight);
		bottomrightx = Number(bottomleftx) + Number(document.getElementById("menu" + menudisplayed).offsetWidth);
		bottomrighty = bottomlefty;
		cursorx = ev.clientX;
		cursory = ev.clientY;
		
		//document.getElementById("debug2").value = cursory + " " + cellbottomy;
		//document.getElementById("debug").value = cursorx + " " + bottomrightx;
		
		//alert(document.getElementById("menu" + menudisplayed + "div").offsetHeight);
		if(cursorx < topleftx || (cursorx > toprightx && cursory < cellbottomy) || (cursorx > bottomrightx && cursory > cellbottomy) || cursory < toplefty || cursory > bottomlefty){
			//alert();
			hide_menu(menudisplayed);
			//alert(cursorx + " " + topleftx + " " + toprightx + " " + cursory + " " + toplefty + " " + bottomlefty);
		}else{
			//checkarrows(cursory);
			//alert(menutop);
			if(cursory != oldmousey && cursory > (toplefty + 69) && cursory < (bottomlefty - 44) ){
				if((cursory > oldmousey && menutop > -(document.getElementById("menu" + menudisplayed + "div").offsetHeight - 190)) || (cursory <  oldmousey && menutop < 0)){
					thediff = cursory - oldmousey;
					themult = (document.getElementById("menu" + menudisplayed + "div").offsetHeight / 50) / 8;
					document.getElementById("menu" + menudisplayed + "div").style.top = (menutop - (thediff * themult)) + "px";
					menutop -= thediff * themult;
					//if(thediff > 100){
						//alert(cursory + " " + oldmousey)
					//}
					
				}
				oldmousey = cursory;
				
			}
			if(menutop < 0){
				document.getElementById("menu" + menudisplayed + "uptext").style.display = "inline";
				topaon = 1;
			}else if(menutop >= 0){
				document.getElementById("menu" + menudisplayed + "uptext").style.display = "none";
				topaon = 0;
			}
			if(menutop > -(document.getElementById("menu" + menudisplayed + "div").offsetHeight - 190)){
				document.getElementById("menu" + menudisplayed + "downtext").style.display = "inline";
				bottomaon = 1;
			}else if(menutop <= -(document.getElementById("menu" + menudisplayed + "div").offsetHeight - 190)){
				document.getElementById("menu" + menudisplayed + "downtext").style.display = "none";
				bottomaon = 0;
			}
			
		}
	}
}

function getY(tehobj){
	
	var objItem = document.getElementById(tehobj)
	//console.log(objItem.offsetParent.tagName + "\n");
	var objParent = null
	var intX = 0
	var intY = 0
	goahead = 1
	do
	{ // Walk up our document tree until we find the body
	 // and add the distance from the parent to our counter.
		intX += objItem.offsetLeft;
		intY += objItem.offsetTop;
		try { 
			objParent = objItem.offsetParent.tagName; 
			objItem = objItem.offsetParent;
			} 
			catch(e) 
			{         
			goahead = 0;
		 } 
		
		}
	 while(objParent != 'BODY' && goahead == 1)
	return {x:intX, y:intY};
}

function show_menu(menuid){
	if(menudisplayed != menuid){
		if(menudisplayed != ""){
			hide_menu(menudisplayed);
		}
		
	//}else{
		var theloc = getY("menu" + menuid + "cell");
		//alert("show_menu")
		document.getElementById("menu" + menuid).style.top = Number(theloc.y) + Number(document.getElementById("menu" + menuid + "cell").offsetHeight) + 2 + "px";
		//if(menuid == maxmenu){
			//alert((theloc.x);
			//document.getElementById("menu" + menuid).style.left = (theloc.x + (document.getElementById("menu" + menuid + "cell").clientWidth / 2)) + "px";
		//}else{ 
			document.getElementById("menu" + menuid).style.left = theloc.x + "px";
		//};
		document.getElementById("menu" + menuid).style.display = "";
		menudisplayed = menuid;
		oldmousey = Number(theloc.y) + 69;
		//alert("show_menu");
		//alert(document.getElementById("menu" + menuid + "div").offsetHeight + "px");
	}
}

function hide_menu(menuid){
	//theloc = getY("menu" + menuid);
	//alert("hide");
	document.getElementById("menu" + menuid).style.display = "none";
	document.getElementById("menu" + menuid + "div").style.top = "0px";
	menudisplayed = "";
	menutop = 0;
	arrowflag = 0;
	topaon = 0;
	bottomaon = 1;
	oldmousey = 0;
}

