/**
 *	Popup Class
 *	-----------
 *
 *	Developed By John Arnfield, Clever Clover
 *	04 October 2007
 *
*/

function Popup(handle,visDS) {
	//Initialize variables
	this.lastMX = 0;
	this.lastMY = 0;
	this.transMX = 0;
	this.transMY = 0;
	this.mouseDown = false;
	this.opacity = 25;
	this.shown = false;
	this.visDisp = visDS || "block";
	
	//Attatch this
	this.handle = $(handle);
	this.handle.style.zIndex = 1001;
	this.handle.hide();
	
	//Create the screener
	var screener = this.createScreener();
	document.body.appendChild(screener);
	screener.hide();
	
	//Save references
	this.screener = screener;
	
	//Handle mouse moves
	var objCpy = this;
	addEvent(document,		'mousemove',	function(e){objCpy.mouseHandler(e);});
	addEvent(document,		'mouseup',		function(){objCpy.mouseDown=false;});
	addEvent(this.screener,	'click',		function(){objCpy.hide();});
	
	//Set the original opacities
	this.refreshOpacities();
}

Popup.prototype.refreshScreener = function()
{
	//Get the page size
	var pDims = this.getPageSize();
	this.screener.style.height = pDims[1]+'px';
}

Popup.prototype.createScreener = function()
{
	var screener = document.createElement('div');
	screener.style.width = '100%';
	screener.style.height = '100%';
	screener.style.backgroundColor = '#fff';
	screener.style.position = 'absolute';
	screener.style.cursor = 'pointer';
	screener.style.left = 0;
	screener.style.top = 0;
	screener.style.zIndex = 1000;
	
	return $(screener);
}

Popup.prototype.refreshOpacities = function()
{
	this.screener.style.opacity = this.opacity*0.03;
	this.screener.style.filter = 'alpha(opacity='+(this.opacity*3)+')';
	this.handle.style.opacity = this.opacity*0.1;
	this.handle.style.filter = 'alpha(opacity='+(this.opacity*10)+')';
}

Popup.prototype.show = function()
{
	this.handle.show();
	if (!this.shown) {
		this.handle.style.position = 'absolute';
		this.handle.style.top = ((this.getPageSize()[3]-this.handle.offsetHeight)/2)+'px';
		this.handle.style.left = ((this.getPageSize()[2]-this.handle.offsetWidth)/2)+'px';
	}
	this.refreshScreener();
	this.screener.show();
	
	this.shown = true;
}

Popup.prototype.hide = function(force)
{
	if (force) {
		this.screener.hide();
		this.handle.hide();
		this.refreshOpacities();
	} else {
		var objRef = this;
		setTimeout(function(){objRef.decreaseOpacity(objRef.hide)},30);
	}
}

Popup.prototype.decreaseOpacity = function(callback)
{
	this.opacity--;
	this.refreshOpacities();
	
	if (this.opacity < 0) {
		this.opacity = 10;
		this.hide(true);
	}
	else {
		this.hide();
	}
}

Popup.prototype.resetOpacity = function()
{
	this.opacity = 10;
	this.refreshOpacities();
}

Popup.prototype.attatchDragHandle = function(element)
{
	//Get the element if it's an id
	element = $(element);
	
	//If this is invalid
	if (!element.tagName) return false;
	
	element.style.cursor = 'move';
	var objCpy = this;
	
	//Attatch the event
	addEvent(element,'mousedown',function(){objCpy.mouseDown=true;});
}

Popup.prototype.attatchCloseHandle = function(element)
{
	//Get the element if it's an id
	element = $(element);
	
	//If this is invalid
	if (!element.tagName) return false;
	
	element.style.cursor = 'pointer';
	var objCpy = this;
	
	//Attatch the event
	addEvent(element,'click',function(){objCpy.hide()});
}

Popup.prototype.attatchOpenHandle = function(element)
{
	//Get the element if it's an id
	element = $(element);
	
	//If this is invalid
	if (!element.tagName) return false;
	
	element.style.cursor = 'pointer';
	var objCpy = this;
	
	//Attatch the event
	addEvent(element,'click',function(){objCpy.show()});
}



Popup.prototype.getMousePos = function(e)
{
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	
	return [posx,posy];
}

Popup.prototype.mouseHandler = function(e)
{
	var mouseCoords = this.getMousePos(e);
	
	
	this.transMX = mouseCoords[0] - this.lastMX;
	this.transMY = mouseCoords[1] - this.lastMY;
	
	this.lastMX = mouseCoords[0];
	this.lastMY = mouseCoords[1];
	
	if (this.mouseDown) {
		this.handle.style.left = (this.handle.offsetLeft+this.transMX)+'px';
		this.handle.style.top = (this.handle.offsetTop+this.transMY)+'px';
	}
}

Popup.prototype.getPageSize = function()
{
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}