﻿/*
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').bind('contextmenu', '"return qsOnContextMenu(e);"');
	//$('#MainColLeft').attr('oncontextmenu', "return qsOnContextMenu(e)");

	//Link the click event on all editPopup class
	LinpPopupMenu();
}

//Link the click event on all editPopup class
function LinpPopupMenu(){
	//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) {

	//Prepare the default admin menu
	var $menu = $('#sysadminMenu');
	$menu.find('div').remove();
	$menu.find('ul').remove();

	$ul = $menu.append('<div id="mnuTitle" class="syspopupMenuTitle">QuickMenu</div>')
	.append('<ul></ul>').find('ul');
	if (gobjSessionVar.lngMemberID == 0) {
		$ul.append('<li><a id="mnuQuickLogin" href="#"><img src="/qsPortal/Themes/Default/Images/PopupMenuQuickLogin.gif" alt="Pop up Menu Quick Login">Quick Login</a></li>');
	}
	else {
		$ul.append('<li><a id="mnuSecGroups" href="#"><img src="/qsPortal/Images/spacer.gif" alt="">' + jsTrad('Security Groups') + '</a></li>')
			.append('<li><a id="mnuAdmin" href="#"><img src="/qsPortal/Themes/Default/Images/PopupMenuAdmin.gif" alt="Pop up Menu Admin">' + jsTrad("Admin") + '</a></li>')
			.append('<li><a id="mnuProfile" href="#"><img src="/qsPortal/Themes/Default/Images/PopupMenuAdmin.gif" alt="Pop up Menu Admin">' + jsTrad("Profile") + '</a></li>')
			.append('<li><a id="mnuQuickLogout" href="#"><img src="/qsPortal/Themes/Default/Images/PopupMenuQuickLogout.gif" alt="Pop Up Menu Quick Logout">Quick Logout</a></li>')
			.append('<li><a id="mnuInfo" href="#"><img src="/qsPortal/Themes/Default/Images/PopupMenuProperties.gif" alt="Pop Up Menu Properties">' + jsTrad("Page properties") + '</a></li>');
	}

	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);
			}				
		}		
	});

	/* DTA v451 4.2.2010 */
	if (typeof(gobjClipboard) != 'undefined') {
		//if (gobjClipboard.Length() < 1) {
		//	$('#' + containerId + 'D ul #mnuClipboardPaste').parent().hide();
		//	$('#' + containerId + 'D ul #mnuClipboardClear').parent().hide();
		//}
		$('#' + containerId + 'D ul #mnuClipboardPaste span').html(String(gobjClipboard.Length()));
	}

	/* DTA v435 23.12.2008 remonte le menu si il atteind le bas de la page. */
	var scrollTop = (window.pageYOffset || document.documentElement.scrollTop ||  document.body.scrollTop);
	var windowHeight = (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight);
	var menuHeight = $('#' + containerId + 'D').height();
	var border = 5;
	
	if (posY > (windowHeight - menuHeight - border) + scrollTop)
		posY = (windowHeight - menuHeight - border) + scrollTop;

	$('#' + 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;	
}

function qsPopupMenu(containerId, mnuId, posX, posY) {

	var properties = eval('mnu' + mnuId);
	if (typeof (properties) == 'undefined')
		return;
	
	$('#' + containerId + 'D *').remove();
	$('#' + containerId + '> *').clone().appendTo('#' + containerId + 'D');

	var options = properties['mnuTitle'];
	if (options && options.title) {
		$('#' + containerId + 'D #mnuTitle').html(options.title);
	}

	$('#' + containerId + 'D a').each(function() {
		//if defined in the option, add it to sysmenuEditD
		var options = properties[$(this).attr('id')];

		if (options) {

			if (options.url) {
				$(this).attr('href', options.url);
			}

			//Check if javascript function defined
			if (options.click) {
				$(this).click(options.click);
			}
			$(this).parent('li').show();
		}
		else {
			if ($(this).parent().attr('id') != 'mnuBottom') {
				$(this).parent('li').hide();
			}
			else {
				$(this).attr('href', 'javascript:return false;');
				$(this).addClass('disable');
				
			}
		}

	});




	/* DTA v435 23.12.2008 remonte le menu si il atteind le bas de la page. */
	var scrollTop = (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop);
	var windowHeight = (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight);
	var menuHeight = $('#' + containerId + 'D').height();
	var border = 5;

	if (posY > (windowHeight - menuHeight - border) + scrollTop)
		posY = (windowHeight - menuHeight - border) + scrollTop;

	$('#' + 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;	
}


function clsClipboard(pstrNodes) {
	this.mblnCut = true;

	this.maobjNodeInfo = [];

	if (pstrNodes != '') {
		astr = pstrNodes.split(',');

		for (i = 0; i < astr.length; i++) {
			astrNodeInfo = astr[i].split(';');
			this.maobjNodeInfo[this.maobjNodeInfo.length] = { node: astrNodeInfo[0], dph: astrNodeInfo[1], dphfk: astrNodeInfo[2], cut: astrNodeInfo[3] };
		}
	}

	this.AddCopy = function(plngNode) {
		blnExist = false;
		for (i = 0; i < this.maobjNodeInfo.length; i++) {
			if (this.maobjNodeInfo[i].node == plngNode) {
				blnExist = true;
				break;
			}
		}
		if (!blnExist) {
			this.maobjNodeInfo[this.maobjNodeInfo.length] = { node: plngNode, dph: '', dphfk: -1, cut: false };
			this.UpdateClipboard();
		}
	}

	this.Cut = function(plngNode, pstrDPH, plngDPHFK) {
		this.mblnCut = true;
		this.maobjNodeInfo = [];
		if (typeof (pstrDPH) == 'undefined')
			pstrDPH = '';
		if (typeof (plngDPHFK) == 'undefined')
			plngDPHFK = -1;
		this.maobjNodeInfo[0] = { node: plngNode, dph: pstrDPH, dphfk: plngDPHFK, cut: true };
		this.UpdateClipboard();
		$("#syseditMenuD").hide();
	}
	
	this.Copy = function(plngNode) {
		this.mblnCut = false;
		this.maobjNodeInfo = [];
		this.maobjNodeInfo[0] = { node: plngNode, dph: '', dphfk: -1 , cut : false };
		this.UpdateClipboard();
		$("#syseditMenuD").hide();
	}
	
	this.Length = function() {
		return this.maobjNodeInfo.length;
	}

	this.UpdateClipboard = function() {

		strNodeInfo = '';
		for (i = 0; i < this.maobjNodeInfo.length; i++) {
			if (i > 0) {
				strNodeInfo += ',';
			}
			strNodeInfo += this.maobjNodeInfo[i].node + ';' + this.maobjNodeInfo[i].dph + ';' + this.maobjNodeInfo[i].dphfk;
			if (this.maobjNodeInfo[i].cut) {
				strNodeInfo += ';true';
			}
			else {
				strNodeInfo += ';false';
			}
		}

		var objJSon = this.AjaxProcess('action=UpdateClipboard&Nodes=' + strNodeInfo);
	}

	this.ClearClipboard = function() {
		var objJSon = this.AjaxProcess('action=ClearClipboard');
		this.maobjNodeInfo = [];
	}

	this.AjaxProcess = function(pstrParams) {
		
		var strData = "Ajax=Clipboard&" + pstrParams;
		var objJSon = {};

		$.ajax({
			type: 'POST',
			cache: false,
			async: true,
			dataType: 'json',
			url: '/qsPortal/Ajax/Get.asp',
			data: strData,
			beforeSend: function() {
				//						strTempHtml = $(gobjTagMgr.getContentObject()).html();
				//						$(gobjTagMgr.getContentObject()).html(gstrHtmlWait);
			},
			success: function(req) {
				objJSon = req;
			},
			error: function(req, err) {
				//						alert('Error!');
				objJSon = { error: true, content: req.responseText };
			}
		});

		return objJSon;		
	
	}

}

