var popUpExists = 0;

function createPopUpAtLocation(e, height, width, content, className)
{
	killExistingPopUp();
	var pos = getMousePosition(e);
	x = pos[0]; y = pos[1];
	
	pop = createNewChildLayer();
	pop.innerHTML = pop.innerHTML + content;

	if (className != '') { setClassName("childLayer", className) };
	positionDiv("childLayer", x, y);
	
	if ((height!=0) ||(width!=0)){ sizeDiv("childLayer", height, width); }
	popUpExists = 1;
}

function createPopUpSummary(x,y,title,content)
{
	killExistingPopUp();
	pop = createNewChildLayer();
	
	//Add div for title parameter, if present
	if (title!='') {
	var titleDiv = document.createElement("div");
	titleDiv.setAttribute("ID","summaryTitle");
	titleDiv.setAttribute("class","popUpSummaryTitle");
	titleDiv.innerHTML = title;
	pop.appendChild(titleDiv);
	}
	
	pop.innerHTML = pop.innerHTML + "<div class=\"popUpSummaryContents\">" + content + "</div>";
	pop.innerHTML = pop.innerHTML + "<div style=\"padding-top:4px;padding-right:10px;\" align=\"right\"><a href=\"javascript:killExistingPopUp();\">Close Window</a>";
	positionDiv("childLayer",x,y);
	sizeDiv("childLayer",300,300);
	setClassName("childLayer","popUpSummary");
	popUpExists = 1;
}
function createLoginPopUp()
{
	killExistingPopUp();
	pop = createNewChildLayer();
	
	positionDiv("childLayer", 300,200);
	sizeDiv("childLayer",120,250);
	pop.innerHTML = pop.innerHTML + loginHTML;
	pop.innerHTML = pop.innerHTML + "<div align=\"right\"><a href=\"javascript:killExistingPopUp();\">Close Window</a></div>";
	setClassName("childLayer","login_popup");
	popUpExists = 1;
}
function createPopUpMenu(direction, parentElement, height, width, className, contentSource)
{
	killExistingPopUp();
	var origin = getItemPosition(parentElement);
	var originX = origin[0]; var originY = origin[1];
	var dim = getItemDimensions(parentElement);
	var dimHeight = dim[0]; var dimWidth = dim[1];
	
	pop = createNewChildLayer();

	if (className != '') { setClassName("childLayer", className) };
	
	//Different positioning based on where the directions say to put the menu.
	
	if (direction == "right")
	{
		var newX = originX + dimWidth;	
		positionDiv("childLayer", newX, originY);
	}
	else if (direction == "left")
	{
		var newX = originX-width;
		if (newX<0) { newX = 0; }
		positionDiv("childLayer",newX, originY);
	}
	else if (direction == "top")
	{
		var newY = originY-height;
		if (newY<0) { newY = 0; }
		positionDiv("childLayer",originX,newY);
	}
	else if (direction == "bottom")
	{
		var newY = originY+dimHeight;
		positionDiv("childLayer",originX,newY);
	}
	
	sizeDiv("childLayer", height, width);
	popUpExists = 1;
	
	for(x=0;x<contentSource.length;x++)
	{
		var newMenuItem = "<div class=\"popUpMenuItem\"><a href=\"" + contentSource[x][1] + "\">" + contentSource[x][0] + "</a></div>";
		pop.innerHTML =  pop.innerHTML + newMenuItem;
	}
	bringToFront("childLayer");
}

function createNewChildLayer()
{
	var parentLayer = returnObjRef("theBody");
	var childLayer = document.createElement("div");
	childLayer.setAttribute("id","childLayer");      
	parentLayer.appendChild(childLayer);
	return childLayer;
}

function killExistingPopUp()
{
	if (popUpExists == 1) {
	a=returnObjRef("theBody");
	d=returnObjRef("childLayer");
	a.removeChild(d); 
	popUpExists = 0;
	}
}

function getMousePosition(e)
{
	var tempX = 0;
	var tempY = 0;
	
	if (document.all) {
		tempX = e.clientX + document.body.scrollLeft;
		tempY = e.clientY + document.body.scrollTop;
	} else {
		tempX = e.clientX;
		tempY = e.clientY;
	}
	var pos = new Array(tempX, tempY);
	return pos;
}
	
function positionDiv(div, x, y)
{
	d = returnObjRef(div);
	d.style.position = "absolute";
	d.style.top = y + "px";
	d.style.left = x + "px";
}

function bringToFront(div)
{
	d=returnObjRef(div);
	d.style.zindex="10";
}

function sizeDiv(div, h, w)
{
	d = returnObjRef(div);
	if (w!=0) {d.style.width = w+"px"; }
	if (h!=0) {d.style.height = h+"px";}
}

function getItemPosition(item)
{
	i = returnObjRef(item);
	var left =i.offsetLeft;
	var top =i.offsetTop;
	while ((i=i.offsetParent) != null) { 
		left += i.offsetLeft; 
		top += i.offsetTop; 
	}
	
	var pos = new Array(left,top);
	return pos;
}	

function getItemDimensions(item)
{
	i = returnObjRef(item);
	var dim = new Array(i.offsetHeight, i.offsetWidth);
	return dim;
}