//<script>
// Javascript PopupWindow object 
// displays a popup window showing specified text 

// Usage:
// var myPopup = new PopupWindow();
// mypopup.text = "Hello world";
// mypopup.show(100, 100);

// Note: some versions of Netscape 4 will crash
// if the document from which this object is created
// does not have at least one element that has a style
// setting.
// To prevent this, add something like:
// <DIV ID="dummy" NAME="dummy" STYLE="color: yellow;"></DIV>
// to the document body.

// TODO: For Netscape, reposition popup if it would appear 
// below or to right of window edge


// PopupWindow show method
// displays popup
function PopupWindow_show(x, y)
{
	var popupHTML = "";
	if (is_nav4up)
	{
		
		delete this.popupLayer;
		this.popupLayer = new Layer(this.width);
		
		this.popupLayer.visibility = "hide";
		
		// Build HTML for document in popup layer
		// Use a DIV with inline style tags for borders
		popupHTML = "<HTML><HEAD><\/HEAD><BODY>"; 

		popupHTML += 
			"<DIV ID=popupInnerDiv NAME=popupInnerDiv STYLE=\"" +
			"padding: " + this.padding  + "px;" +
			"border-style: " + this.borderStyle + "; " +
			"border-width: " + this.borderWidth + "px; " +
			"border-color: " + this.borderColor + "; " + 
			"font-family: " + this.fontFamily + "; " + 
			"font-size: " + this.fontSize + "pt; " + 
			"color: " + this.color + "; " + 
			"\">";
			
		//insert the text and close the tags
		popupHTML += this.text;
		popupHTML += "<\/DIV>";
		popupHTML += "<\/BODY><\/HTML>"

		//set properties
		this.popupLayer.pageX = x;
		this.popupLayer.pageY = y + this.popupGap;
		this.popupLayer.bgColor = this.backgroundColor;
		this.popupLayer.zIndex = 0;
		this.popupLayer.document.open("text/html");
		this.popupLayer.document.clear();
		this.popupLayer.document.write( popupHTML );
		this.popupLayer.document.close();
		
		this.popupLayer.visibility = "show";
	
		
	}
	if (is_ie4up)
	{	

		//Set style for inner table cell 
		var styleText = "";
		styleText = 
			"border-style: " + this.borderStyle + "; " +
			"border-color: " + this.borderColor + "; " + 
			"border-width: " + this.borderWidth + "px; " +
			"font-family: " + this.fontFamily + "; " + 
			"font-size: " + this.fontSize + "pt; " + 
			"padding: " + this.padding + "; " + 
			"color: " + this.color + "; " +  
			"background-color: " + this.backgroundColor + "; ";
						
		//wrap the whole thing in a table or IE4 will display the box
		//as wide as this.width, no matter how little text is contained within
		popupHTML = "<TABLE cellpadding=0 cellspacing=0 border=0  style=\"border-top:none; border-bottom:none\">" + 
		"<TR><TD ID=popupCell CLASS=popupCell STYLE=\"" + styleText + "\">"
		+ this.text
		+ "<\/TD><\/TR><\/TABLE>" ;

		this.popupLayer.innerHTML = popupHTML;
		
				
		// Reposition popups that would appear below or to
		// the right of the browser window
		var popupHeight;
		var popupWidth;
		var availBottom;
		var availRight;
		var availTop;
		var availLeft;
		var popupX;
		var popupY;
				
		//popupY = y + this.popupGap;
		popupY = y + this.popupGap;
		popupX = x;

		popupHeight = document.all("popupCell").offsetHeight;
		popupWidth = document.all("popupCell").offsetWidth;
		
		// Workaround for incorrect popupWidth for first time popup
		// is displayed
		if (popupWidth >  this.width) popupWidth = this.width;
		
		if (popupHeight > 0	&& popupWidth > 0) 
		{
			availBottom = document.body.scrollTop + document.body.clientHeight - y - this.popupGap ;
			availRight =  document.body.scrollLeft + document.body.clientWidth - x;
			availTop = y - document.body.scrollTop - this.popupGap;
			availLeft = x - document.body.scrollLeft;  

	
			if (availBottom < popupHeight)
			{
				if (availTop > popupHeight) popupY = document.body.scrollTop + availTop - popupHeight;			
			}

			if (availRight < popupWidth)
			{
				if (document.body.clientWidth > popupWidth) popupX = document.body.scrollLeft + document.body.clientWidth - popupWidth;
			}
			
			
		}		
		
		// set style information for containing DIV
		with (this.popupLayer.style)
		{
			visibility = "hidden";	
			pixelTop = popupY;
			pixelLeft = popupX;
			pixelWidth = this.width;
			visibility = "visible";
		}

	}
	this.visible = true;
	return true;
}

//Hide popup
function PopupWindow_hide()
{

	if (is_nav4up) this.popupLayer.visibility = "hide";
	if (is_ie4up && is_win32) this.popupLayer.style.visibility = "hidden";
	this.visible = false;		
	return true;
}



function PopupWindow_overlaps(element)
{
	this.debugInfo = "in function";
	if(window.is_ie4up && window.is_win32)
	{

		var el = element;
		var pop = document.all("popupCell");
		
		var elTop = element.offsetTop;
		var elLeft = element.offsetLeft;
		
		//get element's position relative to body
		while (el != document.body)
		{
			el = el.offsetParent;
			elTop += el.offsetTop;
			elLeft += el.offsetLeft;
		}
		
		var elBottom = elTop + element.offsetWidth;
		var elRight = elLeft + element.offsetHeight; 
		
		

		var popTop = pop.offsetTop;
		var popLeft = pop.offsetLeft;
		//get element's position relative to body

		while (pop != document.body)
		{
			pop = pop.offsetParent;
			popTop += pop.offsetTop;
			popLeft += pop.offsetLeft;
		}

		
		var popBottom = popTop + pop.offsetWidth;
		var popRight = popLeft + pop.offsetHeight; 

		
		var msg = "";
		msg+="\n elTop=" + elTop;
		msg+="\n elLeft=" + elLeft;
		msg+="\n elBottom=" + elBottom;
		msg+="\n elRight=" + elRight;
		msg+="\n popTop=" + popTop;
		msg+="\n popLeft=" + popLeft;
		msg+="\n popBottom=" + popBottom;
		msg+="\n popRight=" + popRight;

		this.debugInfo = msg;
		
		
		if (//top left corner of element is within popup
			(elTop > popTop && elTop < popBottom  
			&& elLeft > popLeft && elLeft < popRight )
			|| //top right corner of element is within popup
			(elTop > popTop && elTop < popBottom  
			&& elRight > popLeft && elRight < popRight )
			|| //bottom left corner of element is within popup
			(elBottom > popTop && elBottom < popBottom  
			&& elLeft > popLeft && elLeft < popRight )
			|| //bottom right corner of element is within popup
			(elBottom > popTop && elBottom < popBottom  
			&& elRight > popLeft && elRight < popRight )

			)
		{
				return true;
		}
	
	}
	return false;
}




//PopupWindow constructor
function PopupWindow()
{

	if ((is_nav4up) || (is_ie4up || is_win32))
	{	
		//set property defaults
		this.width = 200;
		this.color = "#000000";
		this.backgroundColor = "#FFFFCC";
		this.text = "";
		this.fontFamily = "sans-serif";
		this.fontSize = 10;
		this.borderStyle = "solid";
		this.borderWidth = 1;
		this.borderColor = "black";
		this.padding = 1;
		this.popupGap = 17;
		this.visible = false;
		this.debugInfo = "";

		// Create a layer in Netscape or a new DIV element in IE
		// to act as a popup container and store a reference to it
		if (is_nav4up) 
		{
			this.popupLayer = new Layer(this.width);
			this.popupLayer.visibility = "hide";
		}
		if (is_ie4up || is_win32) 
		{
			// Insert DIV element as the last element in the body
			document.body.insertAdjacentHTML("BeforeEnd","<DIV ID=\"iePopupLayer\" STYLE=\"position: absolute; visibility: hidden\"></DIV>");
			this.popupLayer = document.all.iePopupLayer;
			this.popupLayer.style.visibility = "hidden";
		}

		//add member functions to this object 
		PopupWindow.prototype.show = PopupWindow_show;
		PopupWindow.prototype.hide = PopupWindow_hide;
		PopupWindow.prototype.overlaps = PopupWindow_overlaps;
	}
}

