/*--------------------------------------------------------------
 Correct implementation of Popups:
 To correctly implement these methods, you must do the following.
	1.  If you need to figure out the size needed based on the textwidth
		or height, place the following DIV declaration somewhere in the HTML
		of the page.
		
		<div id="divLengthTest" style='left=5px; top=5px; color:#ff0000; 
		visibility:hidden; position:absolute; borderWidth:0px; padding:0px; z-index:100'></div>

		Then call getTextHeight() and pass the content to measure, the fixed width
		you want for the popup menu, and the id of the DIV above.
		
	2.  To use a popup call the openPopup method passing the 
		object to which the popup will be relative, the DIV to display
		as the content of the popup, the width of the popup and the height.
		Either of the last two params could be coming from the getTextHeight() or
		getTextWidth() methods.
		
	3.  Finding the mouse on the screen can be done by adding an image that is
		one pixel in size to receive the click event.  The click event can then 
		get the	current mouse position and set the global X and Y.
		This is only necessary if you need to click a hyperlink and want a popup
		but also want to execute code in the HREF property.
		
		To do this, add the following code:
		<img src="images/pixel.gif" id="imgPixel" onclick="setMousePos()">
		
		Then when you call the HREF of a link, use something to this effect:
		href='javascript:document.all.imgPixel.click();openPopup(document.all.img1, divRT, 250, getTextHeight(divRTInner.innerHTML, 250, document.all.divLengthTest));'

   4.   Also make sure to add the following script tag. This allows us to show popups in Firefox
        and other non-IE browsers.
        <script src="common/jquery-1.3.min.js"></script>


---------------------------------------------------------------*/
// Create the popup object. For predictable results, always create
// a global popup object.
if (isInternetExplorer())
{
    var oPopup = window.createPopup();
}
var lefter = 0;
var topper = 0;
var baseText = null;
var popupDiv = null;
var lastRelObj = null;

//this method sets the mouse position on the screen
function setMousePos(Event)
{				
    if (isInternetExplorer())
    {
	    lefter = event.screenX + 15;
	    topper = event.screenY + 5;
	}
	else
	{
	    lefter = Event.clientX + 15;
	    topper = Event.clientY + 5;	
	}
}
		
function openPopup(relObject, divDisplay, intWidth, intHeight)
{
    if (isInternetExplorer())
    {
	    // The popup object exposes the document object and its
	    // properties.
	    var oPopBody = oPopup.document.body;
    	
    	popupDiv = oPopup;
    	
	    // The following HTML that populates the popup object with a string.
	    oPopBody.innerHTML = divDisplay.innerHTML;
    	
	    // Parameters of the show method are in the following order: x-coordinate,
	    // y-coordinate, width, height, and the element to which the x,y 
	    // coordinates are relative. Note that this popup object is displayed
	    // relative to the body of the document.
	    oPopup.show(lefter, topper, intWidth, intHeight, relObject);	    
    }
    else
    {
        //if clicking on same element as last time
        if (lastRelObj == relObject)
        {
            //if popup is shown.
            if (popupDiv != null)
            {
                //hide it.
                hidePopup();
            }
            else
            {
                //otherwise show it.
                lastRelObj = relObject;
                divDisplay.setAttribute('class', 'popup');
                showPopup(relObject, divDisplay, intWidth, intHeight);
            }
        }
        else
        {
            //if not the same object as last time, show the div
            lastRelObj = relObject;
            divDisplay.setAttribute('class', 'popup');
            showPopup(relObject, divDisplay, intWidth, intHeight);
        }   
    }
}
		
//this method determines the height needed given a constrained width
function getTextHeight(pstrHTML, pintWidth, divToUse)
{

	divToUse.innerHTML = pstrHTML;
	divToUse.style.width = pintWidth + 'px';
	//return divToUse.clientHeight;  //ie only
	return divToUse.offsetHeight;	
}
//this method determines the width needed given a constrained height
function getTextWidth(pstrHTML, pintHeight, divToUse)
{
	divToUse.innerHTML = pstrHTML;
	divToUse.style.height = pintHeight + 'px';
	//return divToUse.clientWidth;	  //ie only	
	return divToUse.offsetWidth;
}

//this method shows a popup based on the DIV tag rather than the IE specific popup box.
function showPopup(relObject, divDisplay, width, height)
{
    //set the global object
    popupDiv = divDisplay;       
    
    var top = $(relObject).offset().top + $(relObject).height() + 4;
    var left = $(relObject).offset().left + 40;
    
    divDisplay.style.top = top + "px";
    divDisplay.style.left = left + "px";
    divDisplay.style.width = width + "px";
    divDisplay.style.height = height + "px";
    
    divDisplay.style.visibility = "visible";    
    divDisplay.style.display = '';
    
}

function hidePopup()
{
    if (isInternetExplorer())
    {
        oPopup.hide()
    }
    else
    {    
        if (popupDiv != null)
        {
            popupDiv.style.visibility = "hidden";
            popupDiv = null;
        }
    }
}	
