var Probox = Class.create({

	initialize: function(selector,close_text,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.close_text = close_text;
		this.initOverlay();
		this.initLoading();
		this.initWindow();
		this.show($(selector));
	},	

	show : function(content){
		this.clearElements();
// 		$('PB_HideSelect').show();
		$('PB_Overlay').show();
		this.showLoading();
		this.windowDimensions(this.w,this.h);
		this.setTitle();
		this.loadHTML(content.innerHTML);
	},

	clearElements : function(){
		$("PB_Window").update("");
	},
	
	setTitle : function(){
		var titleElement = new Element('div', {id : "PB_Title"}).update(' <a href="javascript: {}" id="PB_Close">'+this.close_text+'</a>');
		$("PB_Window").appendChild(titleElement);
		$('PB_Close').observe('click', function(){ this.hide(); }.bindAsEventListener(this) );
	},
	
	showLoading : function(){
		$('PB_Load').show();
	},
	
	hideLoading : function(){
		$('PB_Load').hide();
	},
	
	initOverlay : function(){
// 		var iframeElement = new Element('iframe', {id : 'PB_HideSelect', frameborder: 0, style : 'display: none'});
// 		this.body.appendChild(iframeElement);
		var overlayElement = new Element('div',{id : 'PB_Overlay', style : 'display: none;'});
		overlayElement.addClassName('PB_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 : 'PB_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 : 'PB_Window', style : 'display: none;'});
		this.body.appendChild(windowElement);
	},
	
	hide : function(){
		if($(this.iframeId) != null){
			$(this.iframeId).remove();
		}
		this.hideLoading();
// 		$('PB_HideSelect').hide();
		$('PB_Overlay').hide();
		$('PB_Window').hide();
	},
	
	windowDimensions : function(width,height){
		this.width = width;
		this.height = height;
		$("PB_Window").setStyle(
			{
				marginLeft: '-' + parseInt((width / 2) + 16 ,10) + 'px', 
				width: width + 'px'
			}
		);
		if(!(Prototype.Browser.IE && Prototype.Browser.Version < 7)){
			$("PB_Window").setStyle({marginTop: '-' + parseInt((height / 2) + 20 ,10) + 'px'});
	  	}
		$("PB_Window").setStyle({width : width+"px", height: height+"px"});
	},
	
	loadHTML : function(content){
		this.iframeId = "PB_AjaxContent"+Math.round(Math.random()*1000);
		var settings = {'name': this.iframeId, 'id': this.iframeId, 'class':'PB_Content'};
		var ajaxContent = new Element('div', settings);
		ajaxContent.setStyle({width: (this.width)+'px', height: (this.height - 25)+'px' });
		$("PB_Window").appendChild(ajaxContent);
		ajaxContent.innerHTML = content;
		$('PB_Window').show();
		this.hideLoading();
	}
});

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