document.onmouseout = hide;

var current_sub = '';
var current_element = '';
var current_img = '';
var current_imgX = 0;
var current_imgY = 0;
var x = 0;
var y = 0;
var img_height = 30;
var img_width = 5;
var divoffset = 5;
var mouseIsOut = false;

function show(imgID, sub_menu, img_height, divoffset, end_items){
	current_sub = sub_menu;
	current_imgX = getCoorX(imgID);
	current_imgY = getCoorY(imgID);
	current_element = document.getElementById(sub_menu);
			
	current_element.style.left = current_imgX - divoffset + 'px';
	current_element.style.top = current_imgY + img_height + 'px'; 		
    current_element.style.visibility = "visible";
	
	for(i=1;i<end_items;++i){
		if(sub_menu != "flyout"+i){
			var subElement = "flyout"+i;
			document.getElementById(subElement).style.visibility = 'hidden';
		}
	}
}

function hide(e) {
	if(!e) var e = window.event;
	
	if(current_sub != ''){
		var img_x = current_imgX - img_width;
		var img_y = current_imgY + img_height;
		var areaX1 = img_x + img_width - divoffset;//finding upper corner
		var areaY1 = img_y;//second coordinate in finding upper corner (x,y)
		var mouseX;
		var mouseY;	
		var areaX2;
		var areaY2;
						
		if(document.all){ 
			mouseX = event.x;
			mouseY = event.y + document.body.scrollTop;
		}else{ 
			mouseX = e.pageX;
			mouseY = e.pageY;
		}
		
		areaX2 = areaX1 + parseInt(document.getElementById(current_sub).style.width);
		areaY2 = areaY1 + parseInt(document.getElementById(current_sub).style.height);		
		
		//if mouse is > than (x,y) and <  (x,y)
		if(!(mouseX > (areaX1-2) && mouseX < areaX2 && mouseY > (areaY1-3) && mouseY < areaY2) && !(mouseX > (img_x-2) && mouseX < (img_x + img_width) && mouseY > (img_y-3) && mouseY < (img_y + img_height+1))){
			document.getElementById(current_sub).style.visibility = "hidden";
			current_sub = '';
			the_sub = '';
		}
	}
}							

function hideall(end) {
	for(c=1;c<end;++c){
		var subElement = "flyout"+c;
		document.getElementById(subElement).style.visibility = 'hidden';
	}
}

function getCoorX(imgElem) {
	xPos = eval(imgElem).offsetLeft;
	tempEl = eval(imgElem).offsetParent;
  	while (tempEl != null) {
  		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
  	}
	return xPos;
}
		
function getCoorY(imgElem) {
	yPos = eval(imgElem).offsetTop;
	tempEl = eval(imgElem).offsetParent;
	while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	return yPos;
}
