/* ----------------------------------------------------------------------
	Methods to get position of mouse pointer 
	and show/hide a layer inside other of the VIE Portal software desktops
	at the position of mouse pointer
	Reference: http://acko.net/blog/mouse-handling-and-absolute-positions-in-javascript
-----------------------------------------------------------------------	*/

function __getAbsoluteSizeById(id)
{
	var obj = document.getElementById(id);
	if(obj)
		return __getAbsoluteSize(obj);
	else
		return { width:0, height:0 };
}

function __getAbsoluteSize(obj)
{
	var size = { width:0, height:0 };
	if(obj)
	{
		if(obj.style.width)
		{
			size = { width:obj.style.width, height:obj.style.height };
			size.width = size.width.replace('px','').replace('pt','');
			size.height = size.height.replace('px','').replace('pt','');
		}
		else
			size = { width:obj.width, height:obj.height };
	}
	return size;
}

function __getAbsolutePositionById(id)
{
	var obj = document.getElementById(id);
	if(obj)
		return __getAbsolutePosition(obj);
	else
		return { x:-1, y:-1 };
}

function __getAbsolutePosition(obj)
{
	if(!obj)
		return { x:-1, y:-1 };
  var pos  = { x:obj.offsetLeft, y:obj.offsetTop };
  if(obj.offsetParent)
  {
    var tmpObj = __getAbsolutePosition(obj.offsetParent);
    pos.x += tmpObj.x;
    pos.y += tmpObj.y;
  }
  return pos;
}

function ___getRelativeCoordinates(event, obj)
{
  var x = 0, y = 0;

	// IE
  if (!window.opera && typeof event.offsetX != 'undefined')
  {
    var pos = __getAbsolutePosition(obj);
    x = event.offsetX + pos.x;
    y = event.offsetY + pos.y;
  }
  else
  {
    x = event.pageX;
    y = event.pageY;
  }
  
  // subtract distance to middle
  return { x: x, y: y };
}

// show a layer (by Id) at the current position of mouse pointer
function ___showInside()
{
	var parentObj = arguments[0]||null;
	var id = arguments[1]||'';
	if(parentObj==null || id=='') return;
	var offsetX = arguments[2]||0;
	var offsetY = arguments[3]||0;
	var e = arguments[4]||window.event;
	var obj = document.getElementById(id);
	if(!obj) return;
	var pos = ___getRelativeCoordinates(e, parentObj);
	obj.style.left = pos.x + offsetX + 'px';
	obj.style.top = pos.y + offsetY + 'px';
}

/* ----------------------------------------------------------------------
	Methods to show/hide a layer in the VIE Portal
-----------------------------------------------------------------------	*/

function __showLayer(parent, id, position, offsetLeft, offsetTop){
	var nTop = 0, nLeft = 0;
	var objParent = parent;	
	while (objParent.tagName!="BODY")
	{
		nTop = nTop + objParent.offsetTop;
		objParent = objParent.offsetParent;
	}
	objParent = parent;
	while (objParent.tagName!="BODY")
	{
		nLeft = nLeft + objParent.offsetLeft;
		objParent = objParent.offsetParent;
	}
	
	var nWidth = 0, nHeight = 0;
	switch(position)
	{
		case "left":
			nWidth = 0 - objParent.offsetWidth;
			break;

		case "right":
			nWidth = objParent.offsetWidth;
			break;
		break;

		case "top":
			nHeight = 0 - objParent.offsetHeight;
		break;

		case "under":
			nHeight = objParent.offsetHeight;
			break;

		default:
			break;
	}
	var obj = document.getElementById(id);
	if(!obj)
		return;
		
	obj.style.top = nTop + parent.offsetTop + offsetTop + nHeight;
	obj.style.left = nLeft + offsetLeft + nWidth;
	obj.style.visibility = "visible";
}

// hide a layer by Id on a webpage
function __hideLayer(id){
	var obj = document.getElementById(id);
	if(!obj)
		return;
	obj.style.visibility = "hidden";
}

// show an inline element in the HTML document (by Id)
function __show(){
	var id = arguments[0]||'';
	if(id=='') return;
	var objElement = document.getElementById(id);
	if(!objElement) return;
	if(!objElement.style) return;
	var left = arguments[1]||-1;
	var top = arguments[2]||-1;
	var offsetLeft = arguments[3]||0;
	var offsetTop = arguments[4]||0;
	objElement.style.display = "";
	objElement.style.visibility = "visible";
	if(left>=0) objElement.style.left = left + offsetLeft;
	if(top>=0) objElement.style.top = top + offsetTop;
}

// hide an inline element in the HTML document (by Id)
function __hide(id){
	var objElement = document.getElementById(id);
	if(!objElement) return;
	if(!objElement.style) return;
	objElement.style.display = "none";
	objElement.style.visibility = "hidden";
}

// show an element itself
function __showMe(obj){
	obj.style.visibility = "visible";
}

// hide an element itself
function __hideMe(obj){
	obj.style.visibility = "hidden";
}

// set CSS class for a layer by Id on a webpage
function __setLayerClass(id, className){
	var obj = document.getElementById(id);
	if(!obj)
		return;
	obj.className = className;
}

// set CSS class for a object on a webpage
function __setCssClass(obj, className) {
	if(!obj)
		return;
	obj.className = className;
}

/* ----------------------------------------------------------------------
	Methods to show actions menu and VIE Portal elements
-----------------------------------------------------------------------	*/

// show action menu for a portlet
function _showActionMenu(parent, id) {
	__showLayer(parent, id, "none", 0, 0);
}

// hide (minimize) a portlet in a desktop of ViePortal
function _hide(id){
	_hideViePortalElement(id);
	_hideViePortalElement(id + "_M");
	_showViePortalElement(id + "_R");
}

// show (restore) a minimized portlet in a desktop of ViePortal
function _show(id){
	_hideViePortalElement(id + "_R");
	_showViePortalElement(id);
	_showViePortalElement(id + "_M");
}

// temporary close a portlet in a desktop of ViePortal
function _close(id){
	_hideViePortalElement(id);
}

// show an element on ViePortal desktop (inline on webpage)
function _showViePortalElement(id){
	__show(id);
}

// hide an element on ViePortal desktop (inline on webpage)
function _hideViePortalElement(id){
	__hide(id);
}

