
var Popup = Class.create({

	initialize: function(file,w,h){
		var userAgent = navigator.userAgent.toLowerCase();
		Prototype.Browser.Version = (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1];
		this.body = $$('body')[0];
		this.html = $$('html')[0];
		this.w = w;
		this.h = h;
		this.loadingImageSrc = 'tools/popup/loading.gif';
		this.initOverlay();
		this.initLoading();
		this.initWindow();
		this.show(file);	
	},	

	show : function(el){
		this.clearElements();
// 		$('PP_HideSelect').show();
		$('PP_Overlay').show();
		this.showLoading();
		this.windowDimensions(this.w,this.h);
		this.setTitle();
		this.loadHTML(el);	
	},

	clearElements : function(){
		$("PP_Window").update("");
	},
	
	setTitle : function(){
		//var titleElement = new Element('div', {id : "PP_Title"}).update(' <a href="javascript: {}" id="PP_Close"><img src="tools/popup/close.gif" border="0" /> </a>');
		var titleElement = new Element('div', {id : "PP_Title"});
		$("PP_Window").appendChild(titleElement);
		//$('PP_Close').observe('click', function(){ this.hide(); }.bindAsEventListener(this) );
	},
	
	showLoading : function(){
		$('PP_Load').show();
	},
	
	hideLoading : function(){
		$('PP_Load').hide();
	},
	
	initOverlay : function(){
// 		var iframeElement = new Element('iframe', {id : 'PP_HideSelect', frameborder: 0, style : 'display: none'});
// 		this.body.appendChild(iframeElement);
		var overlayElement = new Element('div',{id : 'PP_Overlay', style : 'display: none;'});
		overlayElement.addClassName('PP_OverlayBG');
		//overlayElement.observe('click', function(){this.hide()}.bindAsEventListener(this));
		this.body.appendChild(overlayElement);
		
		if((Prototype.Browser.IE && Prototype.Browser.Version < 7)){
			var arrayPageSize = getPageSize();
			overlayElement.style.width = arrayPageSize[0] +"px";
			overlayElement.style.height = arrayPageSize[1] +"px";
		}
	},
	
	initLoading : function(){
		var loadingElement = new Element('div', {id : 'PP_Load', style : 'display: none;'});
		var loadingImage = new Element('img', {src : this.loadingImageSrc});
		loadingElement.appendChild(loadingImage);
		this.body.appendChild(loadingElement);	
	},
	
	initWindow : function(){
		var windowElement = new Element('div', {id : 'PP_Window', style : 'display: none;'});
		this.body.appendChild(windowElement);
	},
	
	hide : function(){
		if($(this.iframeId) != null){
			$(this.iframeId).remove();
		}
		this.hideLoading();
// 		$('PP_HideSelect').hide();
		$('PP_Overlay').hide();
		$('PP_Window').hide();
	},
	
	windowDimensions : function(width,height){
		this.width = width;
		this.height = height;
		$("PP_Window").setStyle(
			{
				marginLeft: '-' + parseInt((width / 2) + 16 ,10) + 'px', 
				width: width + 'px'
			}
		);
		if(!(Prototype.Browser.IE && Prototype.Browser.Version < 7)){
			$("PP_Window").setStyle({marginTop: '-' + parseInt((height / 2) + 20 ,10) + 'px'});
	  	}
		$("PP_Window").setStyle({width : width+"px", height: height+"px"});
	},
	
	loadHTML : function(url){
		this.iframeId = "PP_AjaxContent"+Math.round(Math.random()*1000);
		var ajaxContent = new Element('div', {name: this.iframeId, id : this.iframeId});
		ajaxContent.setStyle({width: (this.width)+'px', height: (this.height - 5)+'px', overflow: 'auto' });
		$("PP_Window").appendChild(ajaxContent);
		new Ajax.Request( 
			url, 
			{
				method : 'get',
				evalScripts: 'true', 
				onComplete: function(response){
					ajaxContent.innerHTML = response.responseText;
					$('PP_Window').show();
					this.hideLoading();
				}.bind(this)
			}
		);
	}
});

function getPageSize(){
	
	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;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	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;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

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

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