﻿/*
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"><span>Quick Login</span></a></li>');
	}
	else {
		$ul.append('<li><a id="mnuSecGroups" href="#"><img src="/qsPortal/Images/spacer.gif"><span>Security Groups</span></a></li>')
			.append('<li><a id="mnuAdmin" href="#"><img src="/qsPortal/Themes/Default/Images/PopupMenuAdmin.gif"><span>Admin</span></a></li>')
			.append('<li><a id="mnuProfile" href="#"><img src="/qsPortal/Themes/Default/Images/PopupMenuAdmin.gif"><span>Profile</span></a></li>')
			.append('<li><a id="mnuQuickLogout" href="#"><img src="/qsPortal/Themes/Default/Images/PopupMenuQuickLogout.gif"><span>Quick Logout</span></a></li>')
			.append('<li><a id="mnuInfo" href="#"><img src="/qsPortal/Themes/Default/Images/PopupMenuProperties.gif"><span>Page properties</span></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;
}

// dta v469 19.12.2011 Bugfix html5  ajout du document.documentElement.scrollxxx pour le calcul de la position du qsMenu
function qsGetMouseXY(e) {
	var posX; 
	var posY;


	if (!e)
		e = window.event || window.Event;

	if (typeof e.PageX != 'undefined') {

		posX = e.PageX + (window.scrollX || document.documentElement.scrollLeft || document.body.scrollLeft);
		posY = e.PageY + (window.scrollY || document.documentElement.scrollTop || document.body.scrollTop);
	}
	else {
		posX = e.clientX + (window.scrollX || document.documentElement.scrollLeft || document.body.scrollLeft);
		posY = e.clientY + (window.scrollY || document.documentElement.scrollTop || 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) {

	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 .syspopupMenuTitle').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);
			}

			//Check if title defined
			if (options.title) {
				$(this).find('span').html(options.title);
			}

			$(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;		
	
	}

}

