function showElementById(inDocument, inId)
{
	inDocument.getElementById(inId).style.display = '';
};


function hideElementById(inDocument, inId)
{
	inDocument.getElementById(inId).style.display = 'none';
};


function getElementPosition(inElement)
{
	var theLeft = theTop = 0;
	if (inElement.offsetParent)
	{
		theLeft = inElement.offsetLeft;
		theTop = inElement.offsetTop;
		while (inElement = inElement.offsetParent)
		{
			theLeft += inElement.offsetLeft;
			theTop += inElement.offsetTop;
		}
	}
	return [theLeft, theTop];
};


function getWindowDimensions(inWindow)
{
	if (typeof(inWindow.innerWidth) == 'number')
	{
		return [inWindow.innerWidth, inWindow.innerHeight];
	}
	else if (inWindow.document.documentElement && (inWindow.document.documentElement.clientWidth || inWindow.document.documentElement.clientHeight))
	{
		return [inWindow.document.documentElement.clientWidth, inWindow.document.documentElement.clientHeight];
	}
	else if (inWindow.document.body && (inWindow.document.body.clientWidth || inWindow.document.body.clientHeight))
	{
		return [inWindow.document.body.clientWidth, inWindow.document.body.clientHeight];
	}
	return [0, 0];
};


function getWindowScrollOffset(inWindow)
{
	if (typeof(inWindow.pageXOffset) == 'number')
	{
		return[inWindow.pageXOffset, inWindow.pageYOffset];
	}
	else if (typeof(inWindow.document.documentElement.scrollLeft) == 'number')
	{
		return [inWindow.document.body.scrollLeft, inWindow.document.body.scrollTop];
	}
	else if (typeof(inWindow.document.body.scrollLeft) == 'number')
	{
		return [inWindow.document.body.scrollLeft, inWindow.document.body.scrollTop];
	}
	return [0, 0];
};


function repositionAndAvoidClipping(inTargetPosition, inContainerWindow, inContainedElement)
{
	var thePadding = 20;
	var theTargetPadding = 15;
	var theContainerWindowDimensions = getWindowDimensions(inContainerWindow);
	var theContainerWindowScrollOffset = getWindowScrollOffset(inContainerWindow);
	var theWindowDimensions = getWindowDimensions(inContainedElement.contentWindow);

	var x = inTargetPosition[0] - theContainerWindowScrollOffset[0];
	var y = inTargetPosition[1] - theContainerWindowScrollOffset[1];

	if (x > theContainerWindowDimensions[0] - theWindowDimensions[0] - thePadding)
		x = theContainerWindowDimensions[0] - theWindowDimensions[0] - thePadding;
	if (x < 0)
		x =0;

	if (y > theContainerWindowDimensions[1] - theWindowDimensions[1] - thePadding)
		y = theContainerWindowDimensions[1] - theWindowDimensions[1] - thePadding;
	if (y < 0)
		y =0;

	
	var offset = inTargetPosition[0] - theContainerWindowScrollOffset[0] - x;
	if (offset <= 0 && theTargetPadding + offset > 0)
		x = x + theTargetPadding + offset
	if (offset > 0 && offset < theTargetPadding + theWindowDimensions[0])
		if(offset < theWindowDimensions[0]/2 +1)
			x = x + theTargetPadding + offset;
		else
			x = x - theTargetPadding - (theWindowDimensions[0] - offset);

	inContainedElement.style.left = x + theContainerWindowScrollOffset[0];
	inContainedElement.style.top = y + theContainerWindowScrollOffset[1];
};


function fitFrameToContent(inFrame, inPaddingX, inPaddingY)
{
	if(typeof(inPaddingX) != 'number')
		inPaddingX = 0;
	if(typeof(inPaddingY) != 'number')
		inPaddingY = 0;

	var theFirefoxVersion=navigator.userAgent.substring(navigator.userAgent.indexOf("Firefox")).split("/")[1];
	var theFirefoxPadding = parseFloat(theFirefoxVersion) >= 0.1 ? 16 : 0; //need extra height in px to add to iframe in FireFox 1.0+
	var theIEPadding = 20;

	//must set initial values, must be > 0 so IE does not to ignore the frame
	inFrame.style.height=1;
	inFrame.style.width=1;

	if(inFrame.contentDocument && typeof(inFrame.contentWindow.innerHeight) == 'number' && typeof(inFrame.contentWindow.scrollMaxX) == 'number')
	{
		inFrame.style.height = inFrame.contentWindow.innerHeight + inFrame.contentWindow.scrollMaxY  + inPaddingY + theFirefoxPadding;
		inFrame.style.width = inFrame.contentWindow.innerWidth + inFrame.contentWindow.scrollMaxX + inPaddingX;
	}
	else if (inFrame.contentWindow.document && inFrame.contentWindow.document.body && inFrame.contentWindow.document.body.scrollHeight)
	{
		inFrame.style.height = inFrame.contentWindow.document.body.scrollHeight + inPaddingY + theIEPadding ;
		inFrame.style.width = inFrame.contentWindow.document.body.scrollWidth + inPaddingX + theIEPadding;
	}

};


function showGlossaryPopUp(inTargetLocationElementId, inFrameElementId, inURL, inTitle, inTitleElementId)
{
	var itsTarget = document.getElementById(inTargetLocationElementId);
	var itsFrame = document.getElementById(inFrameElementId);
	var itsFrameElementId = inFrameElementId;
	var itsURL = inURL;
	var itsTitleElementId = inTitleElementId ? inTitleElementId : 'title';
	var itsTitle = inTitle;
	var itsWindow = window;

	/*	Unfortunately this does not work on IE. Hacked up the dom by adding this object and made the call the onload form the glossary body.
		theFrame.onload =  function()
		{
			showElementById(document,itsFrameElementId);
			if(itsFrame.contentDocument && itsFrame.contentDocument.getElementById(itsTitleElementId))
				itsFrame.contentDocument.getElementById(itsTitleElementId).innerHTML = itsTitle;
			else if (itsFrame.contentWindow.document)
				itsFrame.contentWindow.document.getElementById(itsTitleElementId).innerHTML = itsTitle;
			fitFrameToContent(itsFrame);
			repositionAndAvoidClipping(getElementPosition(itsTarget), window, itsFrame);
		};
	*/

	window.glossaryData = this;
	this.onloadHandler = function ()
	{
		//must show iframe first; firefox fails when display is none, first set the size to zero
		//hack: set the size to 1 and position outside viewport
		itsFrame.style.height=1;
		itsFrame.style.width=1;
		itsFrame.style.left = -500;
		itsFrame.style.top = -500;
		showElementById(document,itsFrameElementId);
		if(itsFrame.contentDocument && itsFrame.contentDocument.getElementById(itsTitleElementId))
			itsFrame.contentDocument.getElementById(itsTitleElementId).innerHTML = itsTitle;
		else if (itsFrame.contentWindow.document)
			itsFrame.contentWindow.document.getElementById(itsTitleElementId).innerHTML = itsTitle;
		fitFrameToContent(itsFrame);
		repositionAndAvoidClipping(getElementPosition(itsTarget), window, itsFrame);
	};
	itsFrame.src = inURL;

	this.getFrameElementId = function()
	{
		return itsFrameElementId;
	};

};




