//Release Version 2.0 November 13 2006
//set up vars to detect browser and OS type since Mac IE and IE 5 PC are a pain in the rear
//Hate doing these but iframe support has a case of the funky cold madenas
var isMac = (navigator.userAgent.toLowerCase().indexOf('mac') != -1);
var isPC = (navigator.userAgent.toLowerCase().indexOf('windows') != -1);
var isIE = (navigator.userAgent.toLowerCase().indexOf('msie') != -1);
var isIE5 = (navigator.userAgent.toLowerCase().indexOf('msie 5.0') != -1);
var isPCIE50 = (isPC && isIE5);
var isFireFox = (navigator.userAgent.toLowerCase().indexOf('firefox') != -1);
var isSafari = (navigator.userAgent.toLowerCase().indexOf('safari') != -1);
var isMacIE = (isIE && isMac);
//alert('MacIE='+isMacIE + "\n isSafari=" +isSafari + "\n isFireFox=" +isFireFox);


if (document.layers) {
	//This is for older netscape browsers, we have to tell it to listen for mousedown events
	document.captureEvents(Event.MOUSEDOWN)
}
document.onmousedown = getMouseXYPos;


function getMouseXYPos(e)
{
	xPos = 0;
	yPos = 0;

	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		
		xPos = e.pageX - truebody().scrollLeft;
		yPos = e.pageY - truebody().scrollTop;
		
	}
	else if (e.clientX || e.clientY)
	{
		xPos = e.clientX;
		yPos = e.clientY;
		
		
	}
	// posx and posy contain the mouse position relative to the document
}

function truebody(){
	//handles Opera spoofing as IE as well a identifying pages that are labeled as STRICT in the DOCTYPE.  A;so sets the appropriate body object.
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat" && document.documentElement)? document.documentElement : document.body;
}

function getDocWidthHeight(){
	//Gets the entire document width and height
	//variables are global and available to the other functions
	//if IE (document.all?)
	docWidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset +window.innerWidth;
	docHeight=document.all? truebody().scrollHeight : document.body.offsetHeight;	

}

function getClientWidthHeight(){
	//Gets the width and height of the actual browser window inner space
	//variables are global and available to the other functions
	cltWidth= truebody().clientWidth;
	cltHeight= truebody().clientHeight;
	
	
}


function setContainerWidthHeight(cWidth, cHeight){
	//Call method to set docWidth and docHeight global vars as well as client width and hieght
	getClientWidthHeight();
	getDocWidthHeight();
	//if the box width is bigger than the doc width then make it smaller, etc, etc..
	conWidth = cWidth > cltWidth? cltWidth - 15: cWidth;
	conHeight = cHeight > cltHeight? cltHeight - 15: cHeight;
}
	
function createContainerHTML(cName, url){	
	//Creates all the necessary divs for the display box. 
	//This code will ultimately reside inside the div that is specified by the showContainer method(s).
	//first we check to see if the browser is safari since the opacity stuff does not work
	showOpacity = isSafari? "" : "filter:Alpha(opacity=70); opacity:0.70;";
	//Next lets see if the div exists on the page, if not lets add it to the DOM
	//Added November 13, 2006
	if (!document.getElementById(cName)) {
      var newNode = document.createElement("div");
      newNode.setAttribute("id", cName);
      document.body.appendChild(newNode);
    }
	//now we create the DHTML needed
	document.getElementById(cName).innerHTML = '<div id="'+ cName +'Main" style="top:-500px; background-color:#FFFFFF; position:absolute; z-index: 999;padding:0px;margin:0px; border:1px solid gray;"><div id="'+ cName +'close" style="background-color:#000000;padding: 2px 0px 2px 0px; position:absolute; width:100%"><a href="javascript:void(0);" onClick="hideContainer(\''+ cName +'\')" style="color:#ffffff; font-family:Arial; font-size:13px; font-weight:bold;">Close</a></div><iframe id="'+ cName +'Iframe" frameborder="0" name="'+ cName +'Iframe" longdesc="" style="padding:20px 0px 0px 0px; margin:0px;" src="'+ url +'" scrolling="yes">Loading page.  Please wait...</iframe></div><div id="'+ cName +'Shadow" style="top:-500px; position:absolute; background-color:#A2A2A2;' + showOpacity +' z-index: 998;"></div>';
}
	

function initContainer(containerName, containerStartURL, containerWidth, containerHeight){		
	createContainerHTML(containerName, containerStartURL);
	//Call setContainerWidthHeight to configure box size
	setContainerWidthHeight(containerWidth, containerHeight);
	//All the variables defined in this function are global to the rest of the functions
	//main div container that will hold the Iframe
	ContainerObj = document.getElementById(containerName+"Main");
	//div that holds the shadow box
	ContainerShadowObj = document.getElementById(containerName+"Shadow");
	//frame reference
	ContainerIFrameObj = document.getElementById(containerName+"Iframe");

	startWidth = 0;
	startHeight = 0;
	ContainerObj.style.width = 0;
	ContainerObj.style.height = 0;
	ContainerShadowObj.style.width = 0;
	ContainerShadowObj.style.height = 0;		
	ContainerIFrameObj.style.width = 0;
	ContainerIFrameObj.style.height = 0;
	//adding detect to makeURL unique and removed refresh of main frame source on hideContainer
	if (containerStartURL.indexOf("http://") == -1)
	{
	uidString = "ifuquid=" + new Date().getTime();

		if (containerStartURL.indexOf("?") > -1)
			delimAppend = "&";
		else
			delimAppend = "?";
	
	uidFinalString = delimAppend + uidString;
	}
	else{
		uidFinalString = "";
		}
	ContainerIFrameSrc = containerStartURL + uidFinalString;
	ContainerIFrameObj.src = ContainerIFrameSrc;
}

//original function removed with call to new function since JS does not support overloadding
//function showContainer(cName, startURL, cWidth, cHeight){		
//	showContainer(cName, startURL, cWidth, cHeight, true);	
//	alert("show container main");
//}

function showContainer(cName, startURL, cWidth, cHeight, useMouseCoord){
	initContainer(cName,startURL, cWidth, cHeight);
	ContainerObj.style.visibility = "visible";
	ContainerShadowObj.style.visibility = "visible";

	if (useMouseCoord == false)
	{	
		finalXPos = Math.round((cltWidth/2) - (conWidth/2));	
		finalYPos = Math.round((cltHeight/2) - (conHeight/2) + truebody().scrollTop);		
	}
	//else undefined or true
	else {
		var yPosCHieghtDiffDown = cltHeight - yPos;
		var yPosCHieghtDiffUp = cltHeight - yPosCHieghtDiffDown;
		var xPosCWidthDiffRight = cltWidth - xPos;
		var xPosCWidthDiffLeft = cltWidth - xPosCWidthDiffRight;
		finalYPos = conHeight > yPosCHieghtDiffDown?   truebody().scrollTop + yPosCHieghtDiffUp - (conHeight - yPosCHieghtDiffDown) : truebody().scrollTop + yPosCHieghtDiffUp;
		if ((conWidth > xPosCWidthDiffRight) && (conWidth < xPosCWidthDiffLeft)) 
			{finalXPos = xPos - conWidth;}
		else if ((conWidth > xPosCWidthDiffRight) && (conWidth > xPosCWidthDiffLeft))
			{finalXPos = (docWidth/2) - (conWidth/2);}
		else
			{finalXPos = xPos;}	
	}
	
	ContainerObj.style.top = (finalYPos - 10) + "px" ;
	ContainerShadowObj.style.top = finalYPos + "px" ;
		
	ContainerObj.style.left = finalXPos + "px" ;	
	ContainerShadowObj.style.left = (finalXPos + 10 ) + "px";		
	
	ContainerObj.style.width = conWidth + "px";
	ContainerShadowObj.style.width = conWidth + "px";				
	expandInterval = setInterval("expandContainer('"+ cName + "')", 10);
}


function hideContainer(containerName)
{	//this function uses the document.getElementById function instead of the objects defined
	//since it can be called remotely from another page that resides within
	document.getElementById(containerName+"Main").style.width = 0;
	document.getElementById(containerName+"Main").style.height = 0;
	document.getElementById(containerName+"Shadow").style.width = 0;
	document.getElementById(containerName+"Shadow").style.height = 0;		
	document.getElementById(containerName+"Iframe").style.width = 0;
	document.getElementById(containerName+"Iframe").style.height = 0;	
	document.getElementById(containerName+"Main").style.visibility = "hidden";
	document.getElementById(containerName+"Shadow").style.visibility = "hidden";
	document.getElementById(containerName+"Iframe").style.visibility = "hidden";
	//Removed April 13, 2006, causing problems with AJAX parent refresh
	//document.getElementById(containerName+"Iframe").src = ContainerIFrameSrc;
}

//function that expands	divs that hold iframe	
function expandContainer(containerName)
{	
	var expandIntRate = conHeight/5;
	if (startHeight < conHeight){
		//alert( startHeight+ " " + conHeight);
		startHeight = startHeight + expandIntRate;			
		ContainerObj.style.height = startHeight  + "px";		
		ContainerShadowObj.style.height = startHeight + "px";		
	}					
	else {
		clearInterval(expandInterval);		
		//ContainerIFrameObj.src = ContainerIFrameSrc;
		ContainerIFrameObj.style.width = conWidth + "px";
		ContainerIFrameObj.style.height = (conHeight - 20) + "px";	
		ContainerIFrameObj.style.visibility = "visible";
		ContainerObj.style.height = conHeight  + "px";		
		ContainerShadowObj.style.height = conHeight + "px";
	}
}
