// JavaScript Document
function popup()
{
	this.frame = document.createElement('div');
	this.frame.id = 'popup';
	this.frame.style.visibility = 'hidden';
	this.frame.style.position = 'absolute';
	this.frame.style.zIndex = 15;
	
	this.x = 0;
	this.y = 0;
	
	this.displayed = false;
	this.target = false;
	
	if(window.XMLHttpRequest)
	{
		this.xmlhttp = new XMLHttpRequest();
	}
	else
	{
		this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	me = this;
	
	this.xmlhttp.onreadystatechange = function(){updatepopup();}
	
	this.display = function(target, object, vars)
	{
				
		if(this.displayed)
		{
			this.close();
		}
		else
		{	
			this.displayed = true;
			document.body.appendChild(this.frame);
		}
		
		this.target = target;
		
		this.refresh(object, vars);

	}
	
	this.refresh = function(object, vars)
	{
		this.xmlhttp.open("GET", '/popup.php?nocache=' + Math.random() + '&object=' + object + '&' + vars, true);
		this.xmlhttp.send();
	}
	
	this.show = function(html)
	{
		this.frame.innerHTML = html;
		this.move();
		this.frame.style.visibility = 'visible';
	}
	
	this.move = function()
	{
		if(this.displayed && this.target)
		{
			par = this.target;
			x = 0;
			y = 0;
			s = 0;
			
			while(par){
				x += parseInt(par.offsetLeft);
				y += parseInt(par.offsetTop);
				par = par.offsetParent;
			}
			
			if(x < 0)
			{
				x = 0;			
			}
			
			if(x < document.body.offsetWidth / 2)
			{
				x += this.target.offsetWidth + 5; 	
				this.frame.style.left = x + 'px';
				this.frame.style.right = '';
			}
			else
			{
				x = document.body.offsetWidth - x + 5;
				this.frame.style.right = x + 'px';
				this.frame.style.left = '';
			}
			
			y = y - (this.frame.offsetHeight / 2) + 20;

			s = document.body.scrollTop;

			if (s == 0)
			{
				if (window.pageYOffset)
					s = window.pageYOffset;
				else
					s = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
			}
			
			bottom = s + document.body.clientHeight;
			
			if(y + this.frame.offsetHeight > bottom)
			{
				y = bottom - this.frame.offsetHeight;
			}
			
			if(y < s)
			{
				y = s;
			}
			
			this.frame.style.top = y + 'px';
			
			this.frame.style.width = this.frame.children[1].offsetWidth + 10;
			this.x = x;
			this.y = y;
		}
	}
	
	this.close = function()
	{

		if(this.displayed)
		{
			this.frame.style.visibility = 'hidden';
			this.target = false;
		}
	}
	
	if(window.addEventListener)
	{
		window.addEventListener('resize', function(){ popup.move(); }, false);
	}
	else if(window.attachEvent)
	{
		window.attachEvent('onresize', function(){ popup.move(); });
	}

}

var popup = new popup();

function updatepopup()
{
	if(window.popup.xmlhttp.readyState == 4 && window.popup.xmlhttp.status == 200)
	{
		window.popup.show(window.popup.xmlhttp.responseText);
	}
}

