/***************************************************************************************************
File:common.js
Owner: gcmd.nasa.gov; tcherry
Description: A collection of javascript methods to manage inline popups on both the caller page and the popup page
Known Users:
	recordfeatureguide.html
	site1-8.html
Dates:
	2008/01/11 - created by tcherry
***************************************************************************************************/


/*************************************** popup window code  ***************************************/

/** ui event to ask parent to close the popup */
function doClose()
{
	if (parent && parent.callback_closeHelp)
	{
		parent.callback_closeHelp("instrumentSupplemental");
	}
}

/**
* UI event to change the parent to a new url
* @param atag pointer to an html "a" element
* @return false, don't process any more requests
*/
function doGo(atag)
{
	if (atag && atag.href && 0<atag.href.length && parent)
	{
		parent.document.location.href = atag.href;
	}
	return false;
}

/*************************************** parent window code ***************************************/

/**
* public method to close an existing popup and open an new one
* @param url page to open
* @param width number of pixels wide of the popup
* @param height number of pixels high of the popup
* @param owner optional pointer to html element that user clicked to open popup
* @return false html event state; don't continue processing event
*/
function exit(url, width, height, owner){return help(url, width, height, owner);}

/**
* public method to open an new one
* @param url page to open
* @param width number of pixels wide of the popup
* @param height number of pixels high of the popup
* @param owner optional pointer to html element that user clicked to open popup
* @return false html event state; don't continue processing event
*/
function help(url, width, height, owner)
{
	var popupWin = getInfoWindow();
	popupWin.src = url;

	var left = (-1*Math.floor(width/2));
	var top = (-1*Math.floor(height/2));

	/* dynamic styles */
	popupWin.style.width = popupWin.width = width;
	popupWin.style.height = popupWin.height = height;
	popupWin.style.marginLeft = left + "px";
	popupWin.style.marginTop = top + "px";

	if (document.all){width += 25;}//make room for scroll bar in IE
	popupWin.style.width = width + "px";
	popupWin.style.height = height + "px";
	popupWin.style.display = "block";

	if (owner!=null)
	{
		var pos = findPosition(owner).top - top;
		popupWin.style.top = pos + "px";
	}
	else
	{
		popupWin.style.top = "50%";
	}
	
	return false;
}

/**
* public method called by an inline popup when the user has requested that the popup is to be closed
* @param id string id for popup
*/
function callback_closeHelp(id)
{
	try
	{
		var popupWin = getInfoWindow();
		if (id=="instrumentSupplemental" && popupWin!=null)
		{
			popupWin.style.height = popupWin.style.width = 0;
			popupWin.src = "about:blank";
			popupWin.style.display = "none";
		}
	}
	catch (e){handleSupCommError();}
}

/**
* protected method to find and return the info inline popup frame
* @return pointer to inline popup iframe
*/
function getInfoWindow()
{
	//<iframe class="window" style="display:none;" id="info" src="about:blank"></iframe>
	
	var info = document.getElementById("info");
	if (info==null)
	{//first time here
		info = document.createElement("iframe")
		info.className = "window";
		info.style.display = "none";
		info.id = "info";
		info.src = "about:blank";
		info.style.position = "absolute";
		info.style.top = "50%";
		info.style.left = "50%";
		info.style.backgroundColor = "white";
		info.style.border = "4px black solid";
		document.getElementsByTagName("body")[0].appendChild(info);
	}
	return info;
}

/**
* protected method to find the position of an object within the rendered DOM
* @param obj a rendered html element
* @return object containing values left and top
*/
function findPosition(obj)
{
	var curleft = curtop = 0;
	try
	{
		if (obj.offsetParent!=null)
		{
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			while (obj = obj.offsetParent)
			{
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		}
	}
	catch (e){handleSupCommError(e);}
	return {left: curleft, top: curtop};
}

/**
* public method to hide all div tags set to the buttonRow class
* @param className name of the css class to search for
*/
function showOnInline(className)
{
	try
	{
		if (className==null || className.trim().length<=0){className = "buttonRow";}
		if (parent!=window)
		{//not an inline popup
			var divs = document.getElementsByTagName("div");
			for (var y=0; y<divs.length; ++y)
			{
				var div = divs[y];
				if (0<=div.className.indexOf(className)){div.style.display="block";}
			}
		}
	}
	catch (e){handleSupCommError(e);}
}

/**
* handle an error by printing it out as an alert
* @param error javascript error object
*/
function handleSupCommError(error){alert(e.name + ": " + e.message)}