﻿/*
QS4 popup menu handling functions
*/

$(document).ready(function(){InitPopupMenu();});

function InitPopupMenu() {
	//Should Fix the flickering in IE
	if (jQuery.browser.msie) {
		try {document.execCommand("BackgroundImageCache", false, true);
		} catch(err) {
		}
	}
	//The code below does not work with jQuery 1.1+
	//The onContextMenu is set in the incHtmlBody
	//Attach the onmenucontext handler
	//$('#MainRowTop').attr('onContextMenu', '"return qsOnContextMenu(e);"');
	//$('#MainColLeft').attr('oncontextmenu', "return qsOnContextMenu(e)");

	//Link the click event on all editPopup class
	$('.editPopup').hover(
		function(e){
			qsShowWithDelay($(this), $('#syseditMenuD'), e, 10);
		}
		, function(){
			qsHideWithDelay($('#syseditMenuD'));
		}
	);
}

var objTimeOut = null;

function qsOnContextMenu(e) {
	var pos = qsGetMouseXY(e);		
	qsPopupMenu('sysadminMenu', 'Admin', pos[0], pos[1]);
	qsHideWithDelay($('#sysadminMenuD'), 1500);
	try{
		if (e) {e.cancelBubble = true;}
	}
	catch(err){
		if (e) {e.preventDefault();e.stopPropagation();}
	}
	return false;
}
	
function qsGetMouseXY(e) {
	var posX; 
	var posY;


	if (!e)
		e = window.event || window.Event;

	if (typeof e.PageX != 'undefined') {
		
		posX = e.PageX;
		posY = e.PageY;
	}
	else {
		posX = e.clientX + document.body.scrollLeft;
		posY = e.clientY + document.body.scrollTop;
	}
	return [posX, posY];
}

function qsShowWithDelay(el, target, e, delay) {
	
	if (objTimeOut != null) {
		window.clearTimeout(objTimeOut);
		objTimeOut = null;
	} 
	if (!$(target).style) {
		var pos = qsGetMouseXY(e);		
		objTimeOut = window.setTimeout(function(){qsPopupMenu('syseditMenu', $(el).attr('id'), pos[0], pos[1]);}, delay || 500);
	}
}

function qsHideWithDelay(el, delay) {
	if (objTimeOut != null) {
		window.clearTimeout(objTimeOut);
		objTimeOut = null;
	} 
	objTimeOut = window.setTimeout(function(){$(el).hide()}, delay || 500);
	
}

function qsPopupMenu(containerId, mnuId, posX, posY) {
		
	//First clear any <li> from syseditMenuD
	$('#' + containerId + 'D li').remove();
	//Clear Menu title
//	$('#' + containerId + 'D #mnuTitle').remove();
	
	//Add the title
	if ($('#' + containerId + ' li#mnuTitle')) {
		var item = $('#' + containerId + ' li#mnuTitle').clone();
		$('#' + containerId + 'D ul').append(item);
		var options = eval('mnu'+mnuId)['mnuTitle'];
		if (options && options.title) {
			$(item).html(options.title);
		}
		$(item).show();
	}

	$('#' + containerId + ' li').find('a').each(function(){
		//if defined in the option, add it to sysmenuEditD		
		var options = eval('mnu'+mnuId)[$(this).attr('id')];
		if (options) {
			var item = $(this).parent('li').clone();
			$('#' + containerId + 'D ul').append(item);
			$(item).show();
			item = $(item).find('a');
			
			//Check if href defined
			if (options.url) {
				$(item).attr('href', options.url);
			}				
			//Check if javascript function defined
			if (options.click) {
				$(item).click(options.click);
			}				
		}		
	});

	$('#' + containerId + 'D').css({"position": "absolute", "left": posX + "px", "top": posY + "px"});		
	//$('#' + containerId + 'D').css({"position": "absolute"});		
	$('#' + containerId + 'D').css({"width": "9em", "background": "#ffffff"});
	//$('#' + containerId + 'D').show();
	$('#' + containerId + 'D').fadeIn();
	$('#' + containerId + 'D').hover(function(){if (objTimeOut){window.clearTimeout(objTimeOut);objTimeOut=null}}, function() {qsHideWithDelay($('#' + containerId + 'D'))});	
	return false;	
}
