// CustomPopupScript.js 
 var IE = true;
 var oldWindowWidth = 0;
 var oldWindowHeight = 0;
 var oldID = '0';
 
 if (self.innerWidth)// Mozilla Firefox - inner dimensions
	{ oldWindowWidth = self.innerWidth;
	  oldWindowHeight = self.innerHeight;
		IE = false;}

 function reloadWindow() {
	if (oldWindowWidth != self.innerWidth || oldWindowHeight != self.innerHeight) location.reload(); }
		
 if (!IE) onresize = reloadWindow;		 


var cursorX=0;//global because Firefox needs this
var cursorY=0;
// window.status='cursorX=' + cursorX + '  cursorY=' + cursorY;//remove this - used only for debug

window.onload = fonload;
function fonload(){document.onmousemove = fonmousemove;}
function fonmousemove(ev){
  var e = ev || window.event;//ev for Firefox and window.event for IE
	cursorX=e.clientX;
	cursorY=e.clientY;
//	window.status='cursorX=' + cursorX + '  cursorY=' + cursorY;//remove this - used only for debug
}

function makeCustomPopup(content,id,width,horizontal,vertical,relative,method,selection,shadowR,shadowB,subX,subY,debug,marginX,marginY){

//	   alert(  ' id=' + id  + '\nwidth=' + width + '\nhorizontal=' + horizontal + ' vertical=' + vertical + ' relative=' + relative + '\nshadowR=' + shadowR + '\nshadowB=' + shadowB + '\nsubX=' + subX + '\nsubY=' + subY +  '\nmethod=' + method); 

	if (oldID == id) {
		var element;
		if (document.getElementById) element = document.getElementById(id);
		else if (document.all) element = document.all[id];	
		else return;
		if (element.style.display == 'block') {
			closeCustomPopup(id,'y');
			return;
		}
	}

	if (oldID != '0' && oldID != id) closeCustomPopup(oldID,'y');
	oldID = id;
	
	
	horizontal = horizontal.charAt(0).toLowerCase();
	vertical = vertical.charAt(0).toLowerCase();
	relative = relative.charAt(0).toLowerCase();
	if (method == null || method.charAt(0).toLowerCase() != 'o') method = false;
	else method = true; 
	if (shadowR == null) shadowR=0;
	if (shadowB == null) shadowB=0;
  if (subX == null) subX=0;
  if (subY == null) subY=0;
	if (width == null) width = '0';
  if (width == '') width = '0';	
	var setWidth=width;//used for debug
	if (debug == null) debug = false; 
	else debug = true;
	var newX=0;//coordinates relative to top left inside corner of page
	var newY=0;
  var element;

// these 2 not needed
	var screenWidth=640;
	var screenHeight=480;
	if (window.screen.availWidth || window.screen.availHeight)// Firefox and IE
	{ screenWidth=window.screen.availWidth;
	  screenHeight=window.screen.availHeight;
	}
// these 2 not needed
	var windowX=0;
	var windowY=0;
	if (self.screenLeft || self.screenTop)// IE position of top left inner window in relation to screen
	{ windowX=self.screenLeft;//position of inner window
	  windowY=self.screenTop;	}
  else	if (self.screenX || self.screenY)// Firefox position of top left outer window in relation to screen
	{ windowX=self.screenX;//position of outer window
	  windowY=self.screenY;	}

		
	var scrollX=0;//amounts window has scrolled
	var scrollY=0;
	if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft)) 
	{ scrollX = document.documentElement.scrollLeft; 
	  scrollY = document.documentElement.scrollTop;} 
	else if (document.body && (document.body.scrollTop || document.body.scrollLeft)) 
	{ scrollX = document.body.scrollLeft;
	  scrollY = document.body.scrollTop;}

  cursorX =	cursorX + scrollX;//add in scroll	
	cursorY =	cursorY + scrollY;
  
	var windowWidth=640;
	var windowHeight=480;
	if (self.innerWidth)// Mozilla Firefox - inner dimensions
	{ windowWidth = self.innerWidth - 24;// subtract 24 for width of scroll bar 
	  windowHeight = self.innerHeight - 24;}
	else if (document.documentElement && document.documentElement.clientWidth) 
	{ windowWidth = document.documentElement.clientWidth; 
		windowHeight = document.documentElement.clientHeight;}
	else if (document.body && document.body.clientWidth) //IE inner window dimensions
	{ windowWidth = document.body.clientWidth;
	  windowHeight = document.body.clientHeight;}
	
	if (width.indexOf('%') > 0) {
		var w = width.substring(0,width.length - 1);
		width = (windowWidth - shadowR) * w / 100;	
	}
	width = 1 * width;//string to number
	if (IE) var boxwidth = width; else var boxwidth = width - subX;
	var boxheight// calculated below
	boxwidth=width;
	
//	   alert( '\nwidth=' + width + ' boxwidth=' + boxwidth); 	
	
	
// the following creates a box containing the contents and retrieves the height and possibly the width to use in calculating the shadow and displaying the box in the right place
	var cid=id;
	var element,height;
	if (document.getElementById) element = document.getElementById(cid);
	else if (document.all) element = document.all[cid];	
	else return;
	if (IE) {element.style.height = 0 + 'px';element.style.width = 0 + 'px';}
	if (boxwidth > 0) element.style.width= boxwidth + 'px';
	if (content != '') {
		element.innerHTML='';
		element.innerHTML=content;}	
	element.style.display = 'block';
	height=element.offsetHeight;
   width=element.offsetWidth;
	element.style.display = 'none';
	if (IE) {boxwidth = width; boxheight = height;} 
	else {boxwidth = width - subX; boxheight = height - subY;}

//	   alert( '\nwidth=' + width + ' boxwidth=' + boxwidth); 	

	
	if (relative == 'c')// relative to cursor
	{
		if (horizontal == 'r') newX = cursorX + 4;//right
		else if (horizontal == 'c') newX = cursorX - (width + shadowR) / 2;//centre
		else newX = cursorX - width - 0 - shadowR;//left
		
		if (vertical == 'b') newY = cursorY + 8;//bottom
		else if (vertical == 'm') newY = cursorY - (height + shadowB) / 2;//middle
		else newY = cursorY - height - 4 - shadowB;//top
	}
	else  //relative to window'
	{
		if (horizontal == 'r') newX = windowWidth - width - shadowR + scrollX;
		else if (horizontal == 'c') newX = (windowWidth - width - shadowR) / 2 + scrollX;
		else newX = scrollX;
		
		if (vertical == 'b') newY = windowHeight - height - shadowB + scrollY;
		else if (vertical == 'm') newY = (windowHeight - height - 0 - shadowB) /2 + scrollY;
		else newY = scrollY;
	}

	if (newX > windowWidth - width - shadowR + scrollX) newX = windowWidth - width - shadowR + scrollX;
	if (newY > windowHeight - height - shadowB + scrollY) newY = windowHeight - height - shadowB + scrollY;

	if (newX < scrollX) newX = scrollX;
	if (newY < scrollY) newY = scrollY;
	
// Now test for box over cursor

	if (method && (newX - 4) < cursorX && (newX + width + 4 + shadowR) > cursorX && (newY - 4) < cursorY && (newY + height + 4 + shadowB) > cursorY)
	{
		var left = cursorX - 12 - shadowR - scrollX - width;
		var right = windowWidth - cursorX - 12 - shadowR + scrollX - width;
		var top = cursorY - 12 - shadowB - scrollY - height;
		var bottom = windowHeight - cursorY - 12 - shadowB + scrollY - height; 

		var position = 'left';
		var maximum = left;
		if (right > maximum) {position = 'right'; maximum = right;}
		if (top > maximum) {position = 'top'; maximum = top;}
		if (bottom > maximum) {position = 'bottom'; maximum = bottom;}
		if (maximum < 0) position = 'bottom';
		if (position == 'left') newX = cursorX - width - 12 - shadowR;
		else if (position == 'right') newX = cursorX + 4;	
		else if (position == 'top') newY = cursorY - height - 12 - shadowB;
		else newY = cursorY + 4;
	}


	if (shadowR != 0 || shadowB != 0)
	{
	cid=id + 'Shadow';

	if (document.getElementById) element = document.getElementById(cid);
	else if (document.all) element = document.all[cid];	
	else return;
	element.style.left=newX + shadowR + 'px';
	element.style.top=newY + shadowB + 'px';
	element.style.width=boxwidth+'px';
	element.style.height=boxheight+'px';
	element.style.display = 'block';
  }
 
	cid=id;
	if (document.getElementById) element = document.getElementById(cid);
	else if (document.all) element = document.all[cid];	
	else return;
	element.style.left=newX+'px';
	element.style.top=newY+'px';
	element.style.width=boxwidth+'px';
	element.style.height=boxheight+'px';
	if (content != '') {
		element.innerHTML='';
		element.innerHTML=content;}	
	element.style.display = 'block';
	
	if (debug) {
  var	totalWidth=width + shadowR;
	var totalHeight=height + shadowB;
	var totalWidthM=totalWidth + marginX;
	var totalHeightM=totalHeight + marginY;
	window.status='Width=' + totalWidth + '  Height=' + totalHeight + '  Width inc margins=' + totalWidthM + '  Height inc margins=' + totalHeightM;
		}
	
	if (IE && selection != null && selection.charAt(0).toLowerCase() == 'y')	{	
		var elements,node,thiswidth,thisheight,x,y,obj;
		
		if (document.getElementsByTagName) elements = document.getElementsByTagName('select');
		else return;
		for (var i=0; i < elements.length; i++)	{
			node = elements.item(i);
	    node.style.visibility='visible';
      thiswidth=node.offsetWidth;
			thisheight=node.offsetHeight;			
			x = 0; y = 0; obj = elements.item(i);
			if (obj.offsetParent) {
				while (obj.offsetParent) {
					x += obj.offsetLeft
					y += obj.offsetTop
					obj = obj.offsetParent;	}}
			if (x + thiswidth > newX && y + thisheight > newY && x < newX + width + shadowR && y < newY + height + shadowB) {
				node.style.visibility='hidden';}
		}
	}

//	   alert( '\nscreenWidth=' + screenWidth + ' screenHeight=' + screenHeight + '\nwindowWidth=' + windowWidth + ' windowHeight=' + windowHeight + '\nwindowX=' + windowX + ' windowY=' + windowY + '\ncursorX=' + cursorX + ' cursorY=' + cursorY  + '\nwidth=' + width + ' height=' + height + '\nnewX=' + newX + ' newY=' + newY + '\nhorizontal=' + horizontal + ' vertical=' + vertical + ' relative=' + relative +  '\nscrollX=' + scrollX  + ' scrollY=' + scrollY  + '\nleft=' + left  + ' right=' + right  + '\ntop=' + top  + ' bottom=' + bottom + '\nposition=' + position  + ' maximum=' + maximum + '\nid=' + id + '\nshadowR=' + shadowR + '\nshadowB=' + shadowB + '\nsubX=' + subX + '\nsubY=' + subY +  '\nmethod=' + method + '\nboxwidth=' + boxwidth + ' boxheight=' + boxheight + '\nsetWidth=' + setWidth); 
}

function closeCustomPopup(id,selection) 
{
	var element;
	var cid=id;
	if (document.getElementById) element = document.getElementById(cid);
	else if (document.all) element = document.all[cid];	
	else return;
	element.style.display = 'none';
	
	var cid=id + 'Shadow';
	if (document.getElementById) element = document.getElementById(cid);
	else if (document.all) element = document.all[cid];	
	else return;
	if (element != null) element.style.display = 'none';
	
	if (IE && selection != null && selection.charAt(0).toLowerCase() == 'y')
	{
		var elements;
		if (document.getElementsByTagName) elements = document.getElementsByTagName('select');
		else return;
		for (var i=0; i < elements.length; i++)
		{
			var node = elements.item(i);
			node.style.visibility = 'visible';
		}
	}
}

