/**
 * Popup Window support.
 *
 * @author tam
 */ 

function PopupWindow()
{
}

// vars for moving of windows
PopupWindow.lastEventX = 0;
PopupWindow.lastEventY = 0;
PopupWindow.currentMovingWindow = null;

PopupWindow.log = LogFactory.getLog("Popup");

/**
 * Open a popup window (positioned next to the element which fired the command).
 */
PopupWindow.open = function(_evt, _windowId, _refElement, _top, _left)
{
	var e = _evt;
	if(window.event)
	{
		e = event;
	}
	var popup = document.getElementById(_windowId);
	
	if(!popup)
	{
		return false;
	}
	
	// only calculate position if _refElement and e is provided
	if(_refElement && e != null)
	{
		PopupWindow.log.info("refElement: offsetLeft = " + _refElement.offsetLeft + "; offsetTop = " + _refElement.offsetTop);
		PopupWindow.log.info("event: layerX = " + e.layerX + "; layerY = " + e.layerY);
		//_refElement.parentNode.appendChild(popup);
		if(e.layerY)
		{
			popup.style.top = (e.layerY + 10) + "px";
		}
		else if(e.clientY)
		{
			popup.style.top = (e.clientY - 100 + 10) + "px";
		}
	}
	else if(_top && _left)
	{
		popup.style.left = _left + "px";
		popup.style.top = _top + "px";
	}
	popup.style.display = "";
	return false;
}

/**
 * Close a popup window.
 */
PopupWindow.close = function(_windowId)
{
	var popup = document.getElementById(_windowId);
	if(popup)
	{
		popup.style.display = "none";
	}
}

PopupWindow.positionRightOf = function(_windowId, _refWindowId)
{
	var popup = document.getElementById(_windowId);
	var refwin = document.getElementById(_refWindowId);
	// only continue when both windows exist
	if(popup && refwin)
	{
		var cleft = refwin.offsetLeft;
		var ctop = refwin.offsetTop;
		var cwidth = refwin.clientWidth;
		popup.style.left = (cleft + cwidth + 1) + "px";
		popup.style.top = ctop + "px";
	}
}

/**
 * Event handler: Start moving the window
 */
PopupWindow.startMove = function(evt, _windowId)
{
	PopupWindow.currentMovingWindow = document.getElementById(_windowId);
	if(navigator.userAgent.indexOf("Gecko") > -1)
	{
		window.captureEvents(Event.MOUSEMOVE);
		window.captureEvents(Event.MOUSEUP);
		window.onmousemove = PopupWindow.doMove;
		window.onmouseup = PopupWindow.stopMove;
		PopupWindow.lastEventX = evt.layerX;
		PopupWindow.lastEventY = evt.layerY;
		//evt.stopPropagation();
	}
	else if(navigator.userAgent.indexOf("MSIE") > -1)
	{
		document.onmousemove = PopupWindow.doMove;
		document.onmouseup = PopupWindow.stopMove;
		//window.attachEvent("onmousemove", moveControl);
		//window.attachEvent("onmouseup", stopMoveControl);
		PopupWindow.lastEventX = window.event.offsetX;
		PopupWindow.lastEventY = window.event.offsetY;
	}
	// silently ignore unsupported browsers.
	// alert("Moving is not supported for UserAgent: " + navigator.userAgent);
	return false; // prevent event bubbling (i.e. text selection)
}

/**
 * Event handler: Stop moving the window
 */
PopupWindow.stopMove = function(evt)
{
	PopupWindow.currentMovingWindow = null;
	if(navigator.userAgent.indexOf("Gecko") > -1)
	{
		window.onmousemove = null;
		window.onmouseup = null;
		window.releaseEvents(Event.MOUSEMOVE);
		window.releaseEvents(Event.MOUSEUP);
	}
	else if(navigator.userAgent.indexOf("MSIE") > -1)
	{
		document.onmousemove = null;
		document.onmouseup = null;
		/*
		window.detachEvent("onmousemove");
		window.detachEvent("onmouseup");*/
	}
	return false;
}

PopupWindow.doMove = function(evt)
{
	if(navigator.userAgent.indexOf("MSIE") > -1)
	{
		evt = window.event;
	}
	/*
	var diffX = evt.clientX - PopupWindow.lastEventX;
	var dixxY = evt.clientY - PopupWindow.lastEventY;
	PopupWindow.lastEventX = evt.clientX;
	PopupWindow.lastEventY = evt.clientY;
	*/
	
	var elem = PopupWindow.currentMovingWindow;
	
	var elemLeft = evt.clientX + elem.offsetLeft - PopupWindow.lastEventX;
	var elemTop = evt.clientY + elem.offsetTop - PopupWindow.lastEventY;
	elem.style.left = elemLeft + "px";
	elem.style.top = elemTop + "px";
	window.status = ""
	return false;
}