/// Popup Window
/// Michael De Geest
/// RAMIT vzw
/// 27/10/2004

/*******************************************
 * HANDLE WINDOW EVENTS
 ******************************************/

window.onresize = handleWindowsResize;



/*******************************************
 * FIELDS
 ******************************************/

//global variables

var m_Element;
var m_WindowWidth, m_WindowHeight;
var m_PopupWin;
var m_CurrentX, m_CurrentY, m_CurrentWidth, m_CurrentHeight;
	
/*******************************************
 * CONSTRUCTOR
 ******************************************/

function PopupWin(width, height, xCoord, yCoord, headColor, bodyColor, borderColor, id, title)
{
	//setting up PopupWin variables
	this.el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? getLyrRef(id,document): null;
	if (!this.el) return null;
	this.doc = (document.layers)? this.el.document: this.el;
	this.css = (this.el.style)? this.el.style: this.el;
	
	var px = (document.layers)? "": "px";
			
	this.xCoord = xCoord;
	if(xCoord) this.css.left = this.xCoord+px;
	this.yCoord = yCoord;
	if(yCoord) this.css.top = this.yCoord+px;
	
	
	this.width = width;
	this.height = height;
	
	this.tableOuterStart = '<div id="popuptable" style="width:100%"><table cellspacing="0" cellpadding="2" border="0" bgcolor="#' + borderColor + '"><tr><td>';
	this.tableOuterEnd = '</td></tr></table>';
	this.header = "<div style='width:100%;padding:0px;font-family:Verdana;font-size:11px;font-weight:bold;color:#000000;background-color:#" + headColor + "'><table border=0 width=100%><tr><td>"+ title +"</td><td align=right><a href='javascript:handleHideWindow();'><span style='color:#333333;text-decoration:none;font-size:11;font-weight:bold'>X</span></a></td></div>";
	this.tableInnerStart = '<table width="100%" cellspacing="7" cellpadding="1" border="0" bgcolor="#' + bodyColor + '"><tr><td><div style="font-family:verdana; font-size:11;">';
	this.tableInnerEnd = '</div></td></tr></table></div>';
	
	this.obj = id + "PopupWin";
	eval(this.obj + "=this");
	
	//setting up PopupWin functions
	this.setContent = setContent;
	this.showWindow = showWindow;
	this.hideWindow = hideWindow;
	this.setCoords = setCoords;
	

}


/*******************************************
 * SPECIFIC PopupWin FUNCTIONS
 *****************************************/

function setContent(content)
{
	newcontent = "";
	contentWithoutTag = new Array();
	contentWithoutTag = content.split(">");
	if(contentWithoutTag.length > 1)
	{
		contentWithoutTag2 = contentWithoutTag[1].split("<");
		m_LineLength = 	contentWithoutTag2[0].length;
	}
	aantalEraf = 1;
	
	myArray = new Array();
	myArray = content.split(" ");
	/*
	while(m_LineLength > 30)
	{

		for(i=0; i<myArray.length-aantalEraf; i++)
		{
			
			newcontent += myArray[i];
			if(i!= myArray.length-aantalEraf-1) newcontent += " ";
		}
				
		contentWithoutTag = new Array();
		contentWithoutTag = content.split(">");
		contentWithoutTag2 = contentWithoutTag[1].split("<");
		m_LineLength = 	contentWithoutTag2[0].length;
		
		newcontent += "<br>";
		
		for(i=myArray.length-aantalEraf; i<myArray.length; i++)
		{
			
			newcontent += myArray[i];
			if(i!= myArray.length-1) newcontent += " ";
		}
		
		aantalEraf++;
		//contentTemp = content.substr(0,9*(content.length/10));
		//m_LineLength = contentTemp.Length;
		//contentTemp += '<BR>';
		//contentTemp += content.substr(9*(content.length/10));
		//content = contentTemp;
	}
	*/
	
	this.doc.innerHTML = this.tableOuterStart + this.header + this.tableInnerStart + content + this.tableInnerEnd + this.tableOuterEnd;
}

function showWindow()
{
	this.css.visibility = "visible";
}

function hideWindow()
{
	this.css.visibility = "hidden";
}

function setCoords(xCoord, yCoord, hScroll, vScroll)
{
	var x, y, xRechts, yOnder, updatedWindowWidth, updatedWindowHeight, xLim, yLim;

	//document.getElementById("popuptable").firstChild.offsetWidth = 100;
	//alert(m_PopupWin.width);

	//breedte van het popupvenster instellen
	m_PopupWin.width = document.getElementById("popuptable").firstChild.offsetWidth;
	//hoogte van het popupvenster instellen	
	m_PopupWin.height = document.getElementById("popuptable").firstChild.offsetHeight;
			
	//zorg ervoor dat de breedte van het popupvenster de breedte van het hoofdvenster
	//niet overstijgt.
		
	if (navigator.appName == "Microsoft Internet Explorer")	x = xCoord + hScroll;
	else x = xCoord; //bij netscape bvb zit de scrollmarge reeds berekend in de xCoord !!!!

	while(x + m_PopupWin.width > m_WindowWidth + hScroll - 50)
	{
		x = x - 2;
	}
	x=x-10;
	if (x < hScroll) x = hScroll;
	
	//zorg ervoor dat de hoogte van het popupvenster de hoogte van het hoofdvenster
	//niet overstijgt.
	if(navigator.appName =="Microsoft Internet Explorer") y = yCoord + vScroll;
	else y = yCoord //bij netscape bvb zit de scrollmarge reeds berekend in de yCoord !!!!
	
	while(y + m_PopupWin.height > m_WindowHeight + vScroll)
	{
		y = y - 2;
	}
	y=y-10;
	if (y < vScroll) y = vScroll;
	
	//stel de nieuwe coordinaten van het popupvenster in.
	if (x!=null) this.xCoord=x;
	if (y!=null) this.yCoord=y;	
	


	//verplaats het popupvenster.
	if (this.css.moveTo)
	{	
		this.css.moveTo(Math.round(this.xCoord),Math.round(this.yCoord)); 
	}
	else
	{ 
		this.css.left=Math.round(this.xCoord)+"px"; 
		this.css.top=Math.round(this.yCoord)+"px"; 
	}
	
	m_CurrentX = this.xCoord;
	m_CurrentY = this.yCoord;
	m_CurrentWidth = m_PopupWin.width;
	m_CurrentHeight = m_PopupWin.height
	
	if (navigator.appName != "Microsoft Internet Explorer") {
		window.captureEvents(Event.MOUSEMOVE);	
		window.onmousemove = handleWindowsOnMouseMove;
	} else 
		document.onmousemove = handleWindowsOnMouseMove;
}

/*******************************************
 * HELP FUNCTIONS
 ******************************************/	

function getMouseX(e)
{
	return (e.pageX)? e.pageX: e.clientX;
}

function getMouseY(e)
{
	return (e.pageY)? e.pageY: e.clientY;
}

function getWinWidth()
{
	var winWidth = 0;

	if (navigator.appName == "Microsoft Internet Explorer" && document.body && document.body.clientWidth)
	{
		winWidth = document.body.clientWidth;
	}
	else if (window.innerWidth) winWidth = window.innerWidth-5;
	else winWidth = 0;

	return winWidth;
}
function getWinHeight()
{
	var winHeight = 0;
	
	if (navigator.appName == "Microsoft Internet Explorer" && document.body && document.body.clientHeight) 
	{
		winHeight = document.body.clientHeight;
	}
	else if (window.innerHeight) winHeight = window.innerHeight-5;
	else winHeight = 0;

	return winHeight;
}	

function getScrollY()
{
	var scroll_y = 0;
	if (document.documentElement && document.documentElement.scrollTop)
		scroll_y = document.documentElement.scrollTop;
	else if (document.body && document.body.scrollTop) 
		scroll_y = document.body.scrollTop; 
	else if (window.pageYOffset)
		scroll_y = window.pageYOffset;
	else if (window.scrollY)
		scroll_y = window.scrollY;
	return scroll_y;
}
function getScrollX()
{
	var scroll_x = 0;
	if (document.documentElement && document.documentElement.scrollLeft)
		scroll_x = document.documentElement.scrollLeft;
	else if (document.body && document.body.scrollLeft) 
		scroll_x = document.body.scrollLeft; 
	else if (window.pageXOffset)
		scroll_x = window.pageXOffset;
	else if (window.scrollX)
		scroll_x = window.scrollX;
	return scroll_x;
}

/*******************************************
 * PopupWin Handlers
 ******************************************/

function handlePopupWin(e, name, title, content, headColor, bodyColor, borderColor)
{	
	handleWindowsResize();
	
	if(!headColor) headColor = "99CCFF";
	if(!bodyColor) bodyColor = "6FB7FF";
	if(!borderColor) borderColor = "0000FF";
	
	m_PopupWin = new PopupWin(20, 20, 10, 10, headColor, bodyColor, borderColor, name, title);
	if (!m_PopupWin) return;
	
	m_PopupWin.setContent(content);
	
	hScroll = getScrollX();
	vScroll = getScrollY();
	
	e = (window.event)? window.event: e;
	x = getMouseX(e);
	y = getMouseY(e);
		
	//setTimeout(m_PopupWin.obj+".setCoords("+x+", "+y+", "+hScroll+", "+vScroll+")",1000);	
	m_PopupWin.setCoords(x,y,hScroll,vScroll);
	//m_WinShow = setTimeout(m_PopupWin.obj+".showWindow()",200);
	m_PopupWin.showWindow();
	
	//setTimeout(m_PopupWin.obj+".hideWindow()",3000);
}
		 
function handleHideWindow()
{
	//m_WinHide = setTimeout(m_PopupWin.obj+".hideWindow()",200);
	m_PopupWin.hideWindow();
}


/*******************************************
 * Window Handlers
 ******************************************/	

function handleWindowsResize()
{
	m_WindowWidth = getWinWidth();
	m_WindowHeight = getWinHeight();
}

function handleWindowsOnMouseMove(e)
{
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		if(event.clientX < m_CurrentX || event.clientX > m_CurrentX+m_CurrentWidth) m_PopupWin.hideWindow();
		if(event.clientY < m_CurrentY || event.clientY > m_CurrentY+m_CurrentHeight) m_PopupWin.hideWindow();
	}
	else
	{
		if(e.PageX < m_CurrentX || e.PageX> m_CurrentX+m_CurrentWidth) m_PopupWin.hideWindow();
		if(e.PageY < m_CurrentY || e.PageY> m_CurrentY+m_CurrentHeight) m_PopupWin.hideWindow();
	}
}