var popup = Class.create();
popup.prototype={
	initialize : function(id,w,h){
		this.el = $(id).cloneNode(true);
		$(id).style.display="none";
		this.el.style.display = "";
		this.el.style.visibility="visible";
		
		this.w = w;
		this.h = h;
		
		this.c = document.createElement("div");
		this.o = document.createElement("div");
		this.x = document.createElement("div");
		this.c.id="popupWin";
		this.o.id="popupOverlay";
		this.x.id="popupCancel";
		
		this.fadeColor = "#000000";
		this.popupColor = "#ffffff";
		this.opacity = 0.5;
		this.title="Event Horizon - Popup";
		
		var dim = this.getPageSize();
		var pScroll = this.getPageScroll();
		this.pageHeight = dim[3];
		this.pageWidth = dim[2];
	},
	open : function(){
		
		//set up container 'c'
		this.c.style.position = "absolute";
		this.c.style.zIndex = "200";
		this.c.style.width=this.w + "px";
		this.c.style.height=this.h + "px";
		this.c.style.overflow = "hidden";
		this.c.style.backgroundColor = this.popupColor;
		this.c.style.left=((this.pageWidth - this.w) / 2) + "px";
		this.c.style.top=((this.pageHeight - this.h) / 2) + "px";
		
		var heading = document.createElement("h1");
		heading.innerHTML = this.title;
		this.c.appendChild(heading);
		this.c.appendChild(this.el);
		
		//set up overlay 'o'
		this.o.style.position = "absolute";
		this.o.style.zIndex = "90";
		this.o.style.width="100%";
		this.o.style.height=this.pageHeight + "px";
		this.o.style.top ="0";
		this.o.style.left ="0";
		this.o.style.margin="0px 0px 0px 0px";
		this.o.style.backgroundColor = this.fadeColor;
		
		Try.these(
			this.o.style.opacity = this.opacity, //opacity for MOZ and Safari
			this.o.style.filter = 'alpha(opacity='+(this.opacity * 100)+')' //opacity for MSIE		  
		);
		
		//set up cancel button 'x'
		Position.clone(heading,this.x);
		this.x.style.position="absolute";
		this.x.style.float="right";
		this.x.style.width = "16px";
		this.x.style.height= "16px";
		this.x.style.left = (this.w - 21)+"px";
		this.x.style.top = "3px";
		this.x.style.cursor = "pointer";
		var p = this;
		this.c.appendChild(this.x);
		
		//add items to the DOM Tree
		this.o.style.display="none";
		this.c.style.display="none";
		document.body.insertBefore(this.o,document.body.firstChild);
		document.body.insertBefore(this.c,document.body.firstChild);
		new Effect.Appear(this.o,{duration:0.2,from:0,to:this.opacity,afterFinish:function(){
																				  new Effect.Appear($('popupWin'),{duration:0.3})
																				  }});
		
		document.body.style.overflow = "hidden";
	},
	destroy : function(){
		document.body.removeChild($('popupWin'));
		document.body.removeChild($('popupOverlay'));
		document.body.style.overflow = "visible";
		this.on_close();
	},
	on_close: function(){
		this.closed=true;	
	},
	getPageScroll : function(){

		var yScroll;
	
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}
	
		arrayPageScroll = new Array('',yScroll) 
		return arrayPageScroll;
	},
	//
	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	//
	getPageSize: function(){
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			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
			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 = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	}
}