﻿function fMenu(instanceName, divName) {
	var menuWidth = 500;
	var menuHeight = 300;
	var sizeIncrement = 80;
	var menuVisible = false;
	var expandTimeOut = 0;
	var collapseTimeOut = 0;
	var hideTimeOut = 0;
	var iName = instanceName;
	var menudivwrapper = document.getElementById(divName);
	var iframewrapper = document.getElementById('collapsablediviframe');
	var firstMouseOverX = -1;
	var firstMouseOverY = -1;
	var expandDir = "down";
	var isRelative = true;
	
	this.showMenu = showMenu;
	this.hideMenu = hideMenu;
	this.expandMenu = expandMenu;
	this.collapseMenu = collapseMenu;
	this.hideMenuHelper = hideMenuHelper;
	this.setSize = setSize;
	this.setIncrement = setIncrement;
	this.setX = setX;
	
	function setX(evt) {
	    var Ypos;
	    if (window.pageYOffset)
        Ypos=window.pageYOffset;
        else if (document.documentElement && document.documentElement.scrollTop)
        Ypos=document.documentElement.scrollTop;
        else if (document.body)
        Ypos=document.body.scrollTop;

	    try {
	        //Netscape and firefox use this
	        firstMouseOverX = evt.screenX;
	        firstMouseOverY = evt.screenY + Ypos; 
	        // this is a bit of a hack, as it seems this value is not correct, unless you divide by 2
	        firstMouseOverY = firstMouseOverY /2;
	   } catch (ex) {
	      //IE does this
	      firstMouseOverX = window.event.clientX;
	      firstMouseOverY = window.event.clientY + Ypos;
	 	} 
	    document.onmousemove = ''; 
	}
	
	function setSize(x, y) {
		menuWidth = x;
		menuHeight = y;
	}
	
	function setIncrement(inc) {
		sizeIncrement = inc;
	}
	
	function showMenu(dorefresh, relative, dir) {
	   if(relative != null) {
	       isRelative = relative;
	   }
	  if(dir != null) {
	      expandDir = dir;
	  } 
	   
	  if(isRelative) { 
	   if((firstMouseOverX == -1) || (dorefresh)) {
	       document.onmousemove = eval(iName+".setX");
	    }
	   }   
		clearTimeout(hideTimeOut);
		hideTimeOut = 0;
		if(expandTimeOut == 0) {
			expandMenu();
		}	
	}
	function hideMenu() {
		if(hideTimeOut == 0) {
			hideTimeOut = setTimeout(iName+".hideMenuHelper()", 500);
		}
	}
	function hideMenuHelper() {
		if(collapseTimeOut == 0) {
			collapseMenu();
		}
	}
	function expandMenu() {
		if(!menuVisible) {
			var xexpanded = false;
			var yexpanded = false;
			var curwidth = Number(menudivwrapper.style.width.replace(/px/g, ''));
			var curheight = Number(menudivwrapper.style.height.replace(/px/g, ''));
			if(curwidth < menuWidth) {
				var newwidth = curwidth + sizeIncrement;
				if(newwidth > menuWidth) {
					newwidth = menuWidth;
				}
				//alert(menudivwrapper.style.left);
				if(isRelative) {
				    if(expandDir == "down") {
				      menudivwrapper.style.top = firstMouseOverY + "px"; 
				      menudivwrapper.style.left = (firstMouseOverX-(newwidth/2)) + "px";
				      iframewrapper.style.top = firstMouseOverY + "px";
				      iframewrapper.style.left = (firstMouseOverX-(newwidth/2)) + "px";
				    } 
				} 
				if((firstMouseOverX != -1) || (!isRelative)) {
				    menudivwrapper.style.display = "block";
				    iframewrapper.style.display = "block"; 
				 } 
				menudivwrapper.style.width = newwidth + "px";
				iframewrapper.style.width = newwidth + "px";
			} else {
				xexpanded = true;
			}
			if(curheight < menuHeight) {
				var newheight = curheight + sizeIncrement;
				if(newheight > menuHeight) {
					newheight = menuHeight;
				}
				menudivwrapper.style.height = newheight + "px";
			   iframewrapper.style.height = newheight + "px";
			} else {
				yexpanded = true;
			}
			menuVisible = (xexpanded && yexpanded);
			if(!menuVisible) {
				expandTimeOut = setTimeout(iName+".expandMenu()", 100);
			} else {
				expandTimeOut = 0;
			}
		} 
	}
	function collapseMenu() {
		if(menuVisible) {
			var xexpanded = true;
			var yexpanded = true;
			var curwidth = Number(menudivwrapper.style.width.replace(/px/g, ''));
			var curheight = Number(menudivwrapper.style.height.replace(/px/g, ''));
			if(curwidth > 1) {
				var newwidth = curwidth - sizeIncrement;
				if(newwidth < 1) {
					newwidth = 1;
				}
				if(isRelative) {
				    if(expandDir == "down") {
				       menudivwrapper.style.left = (firstMouseOverX-(newwidth/2)) + "px";
				       iframewrapper.style.left = (firstMouseOverX-(newwidth/2)) + "px";
				     } 
				} 
				menudivwrapper.style.width = newwidth + "px";
				iframewrapper.style.width = newwidth + "px";
			} else {
				xexpanded = false;
			}
			if(curheight > 1) {
				var newheight = curheight - sizeIncrement;
				if(newheight < 1) {
					newheight = 1;
				}
				menudivwrapper.style.height = newheight + "px";
				iframewrapper.style.height = newheight + "px";
			} else {
				yexpanded = false;
			}
			menuVisible = (xexpanded || yexpanded);
			if(menuVisible) {
				collapseTimeOut = setTimeout(iName+".collapseMenu()", 100);
			} else {
			    menudivwrapper.style.display = "none";
			    iframewrapper.style.display = "none";
				collapseTimeOut = 0;
			}
		}
	}
}
