	/************************************************************************************************************	
	(C) www.dhtmlgoodies.com, October 2005
	
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Updated:	
		March, 11th, 2006 - Fixed positioning of tooltip when displayed near the right edge of the browser.
		April, 6th 2006, Using iframe in IE in order to make the tooltip cover select boxes.
		
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
	
	************************************************************************************************************/
	
	/** Essentialy modified by Pavel Vlasov **/
	
var hwdfContextMenu_minWidth=20;
var hwdfContextMenu_maxWidth=400;

var hwdfContextMenuHideDelay=700;

var hwdfContextMenu = false;
var hwdfContextMenuShadow = false;
var hwdfContextMenu_shadowSize = 2;
var hwdfContextMenu_iframe = false;
var hwdfContextMenu_is_msie = navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('opera')==-1 && document.all;
var hwdfContextMenuHideTask;
	
function showContextMenu(e, menuUrl) {
	cancelHideContextMenu();
	
	var clientX = e.clientX;
	var clientY = e.clientY;
	
	var bodyWidth;
	var bodyHeight;

	if (window.innerWidth) {
		bodyWidth = window.innerWidth - 10;
		bodyHeight = window.innerHeight - 10;
	} else {
		bodyWidth = Math.max(document.body.clientWidth,document.documentElement.clientWidth) - 10;
		bodyHeight = Math.max(document.body.clientHeight,document.documentElement.clientHeight) - 10;
	}				
	
	if(!hwdfContextMenu){
		hwdfContextMenu = document.createElement('DIV');
		hwdfContextMenu.className = 'hwdfContextMenu';
		hwdfContextMenu.onmouseout = hideContextMenu;
		hwdfContextMenu.onmouseover = cancelHideContextMenu;
		hwdfContextMenuShadow = document.createElement('DIV');
		hwdfContextMenuShadow.className = 'hwdfContextMenuShadow';
		
		document.body.appendChild(hwdfContextMenu);
		document.body.appendChild(hwdfContextMenuShadow);	
		
		if(hwdfContextMenu_is_msie){
			hwdfContextMenu_iframe = document.createElement('IFRAME');
			hwdfContextMenu_iframe.frameborder='5';
			hwdfContextMenu_iframe.style.backgroundColor='#FFFFFF';
			hwdfContextMenu_iframe.src = '#'; 	
			hwdfContextMenu_iframe.style.zIndex = 100;
			hwdfContextMenu_iframe.style.position = 'absolute';
			document.body.appendChild(hwdfContextMenu_iframe);
		}
		
	}
	
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0; 
	var leftPos = clientX + 10;
	var topPos = clientY + 10 + st;
	
	hwdfContextMenu.style.width = null;	// Reset style width if it's set 		
	hwdfContextMenu.innerHTML = "Loading ...";
	
	hwdfContextMenuShadow.style.width = hwdfContextMenu.offsetWidth + 'px';
	hwdfContextMenuShadow.style.height = hwdfContextMenu.offsetHeight + 'px';					
			
	hwdfContextMenu.style.left = leftPos + 'px';
	hwdfContextMenu.style.top = topPos + 'px';
	
	hwdfContextMenuShadow.style.left =  leftPos + hwdfContextMenu_shadowSize + 'px';
	hwdfContextMenuShadow.style.top = topPos + hwdfContextMenu_shadowSize + 'px';

	if(hwdfContextMenu_is_msie){
		hwdfContextMenu_iframe.style.left = hwdfContextMenu.style.left;
		hwdfContextMenu_iframe.style.top = hwdfContextMenu.style.top;
		hwdfContextMenu_iframe.style.width = hwdfContextMenu.offsetWidth + 'px';
		hwdfContextMenu_iframe.style.height = hwdfContextMenu.offsetHeight + 'px';
	
	}					
	
	hwdfContextMenu.style.display='block';
	hwdfContextMenuShadow.style.display='block';
	if(hwdfContextMenu_is_msie)hwdfContextMenu_iframe.style.display='block';
	
	var sk = new sack();		
	sk.requestFile = menuUrl;	// Specifying which file to get
	sk.onCompletion = function() { 
		hwdfContextMenu.innerHTML = this.response;	
		ajax_parseJs(hwdfContextMenu)
		
		if(hwdfContextMenu.offsetWidth>hwdfContextMenu_maxWidth){	/* Exceeding max width of tooltip ? */
			hwdfContextMenu.style.width = hwdfContextMenu_maxWidth + 'px';
		}
		
		var tooltipWidth = hwdfContextMenu.offsetWidth;		
		if(tooltipWidth<hwdfContextMenu_minWidth) tooltipWidth = hwdfContextMenu_minWidth;		
		
		var tooltipHeight = hwdfContextMenu.offsetHeight;		
		
		hwdfContextMenu.style.width = tooltipWidth + 'px';
		hwdfContextMenuShadow.style.width = hwdfContextMenu.offsetWidth + 'px';
		hwdfContextMenuShadow.style.height = hwdfContextMenu.offsetHeight + 'px';
		
		// Positioning algorithm if tooltip is bottom/right is not visible
		// a) Try to position below, move to the left
		// b) Try to position on the right, move up
		// c) Try to position above, affinity to the right
		// d) Try to position on the left, affinity to the bottom
		if((leftPos + tooltipWidth)>bodyWidth) { // Doesn't fit to the right
			if((topPos + tooltipHeight)>bodyHeight + st) { // Doesn't fit to the bottom as well (A)
				if (clientX > tooltipWidth) { // Can move to the left
					leftPos = clientX-tooltipWidth-10;
					topPos = Math.max(10, bodyHeight - tooltipHeight) + st;
					hwdfContextMenu.style.left = leftPos + 'px';
					hwdfContextMenuShadow.style.left = (leftPos + hwdfContextMenu_shadowSize) + 'px';								
					hwdfContextMenu.style.top = topPos + 'px';
					hwdfContextMenuShadow.style.top = (topPos + hwdfContextMenu_shadowSize) + 'px';
				} else if (clientY > tooltipHeight) { // Can move to the top.
					leftPos = Math.max(10, bodyWidth - tooltipWidth);
					topPos = clientY - tooltipHeight - 10 + st;				
					hwdfContextMenu.style.left = leftPos + 'px';
					hwdfContextMenuShadow.style.left = (leftPos + hwdfContextMenu_shadowSize) + 'px';								
					hwdfContextMenu.style.top = topPos + 'px';
					hwdfContextMenuShadow.style.top = (topPos + hwdfContextMenu_shadowSize) + 'px';
				}
			} else { // (B)
				leftPos = Math.max(10, bodyWidth - tooltipWidth);
				hwdfContextMenu.style.left = leftPos + 'px';
				hwdfContextMenuShadow.style.left = (leftPos + hwdfContextMenu_shadowSize) + 'px';								
			}
		} else if((topPos + tooltipHeight)>bodyHeight + st) { // Fits to the left, but not to the bottom (C)
			topPos = Math.max(10, bodyHeight - tooltipHeight) + st;
			hwdfContextMenu.style.top = topPos + 'px';
			hwdfContextMenuShadow.style.top = (topPos + hwdfContextMenu_shadowSize) + 'px';
		}
		
		if(hwdfContextMenu_is_msie){
			hwdfContextMenu_iframe.style.left = hwdfContextMenu.style.left;
			hwdfContextMenu_iframe.style.top = hwdfContextMenu.style.top;
			hwdfContextMenu_iframe.style.width = hwdfContextMenu.offsetWidth + 'px';
			hwdfContextMenu_iframe.style.height = hwdfContextMenu.offsetHeight + 'px';
		
		}					
	};	
	sk.runAJAX();		// Execute AJAX function	
}
	
function cancelHideContextMenu() {
	if (hwdfContextMenuHideTask) {
		window.clearTimeout(hwdfContextMenuHideTask);
		hwdfContextMenuHideTask=null;
	}
}	
	
function hideContextMenu() {	
    cancelHideContextMenu();
	hwdfContextMenuHideTask = window.setTimeout(function() {
		if (hwdfContextMenu) {
			hwdfContextMenu.style.display='none';
			hwdfContextMenuShadow.style.display='none';		
			if(hwdfContextMenu_is_msie) {
				hwdfContextMenu_iframe.style.display='none';		
			}
		}	
	},
	hwdfContextMenuHideDelay);	
}	
