PopHtml = Class.create({
	
	initialize: function (name,target,contentURL,methodURL,ajaxParams,onEvent) {
		this._name = name;
		this._target = target;
		this._contentURL = contentURL;
		this._methodURL = methodURL;
		this._ajaxParams = ajaxParams;
		this._onEvent = onEvent;
		this._content = this._bg = null;
		this._doc = (!this.Safari) ?document.documentElement :document.body;
		this.IE6 = (navigator.appVersion.indexOf("MSIE 6")!=-1) ?true :false;
		this._intervalFixFirefox = null;
		Event.observe(window,'resize',this._onWindowResize.bind(this));		
		Event.observe(window,'scroll',this._onWindowResize.bind(this));
		this._writeBg();
		this._writeContent();
	},
	
	_onWindowResize: function () {
		this._startSetContentPosition(this._content);
		Axis.each( this._setBgSize.bind(this,this._bg) );
	},
	
	_getPageSize: function (k) {
		return (this._doc[k.scrollscale]<this._getWindowSize(k)) ?this._getWindowSize(k) :this._doc[k.scrollscale];
	},
	
	_getWindowSize: function (k) {
		return document.documentElement[k.clientscale];
	},
	
	_getCenter: function (o,k) {
		return this._doc[k.scrollpos] + (this._getWindowSize(k)/2)-(o[k.offsetscale]/2);		
	},
	
	_writeContent: function () {
		var newAJAX = new Ajax.Request(
			this._contentURL, {
				method: this._methodURL,
				postBody:$H(this._ajaxParams).toQueryString(),
				onComplete: function(response) {
					this._content.innerHTML = response.responseText;
					if (this._intervalFixFirefox==null) { this._intervalFixFirefox = new PeriodicalExecuter(this._setContent.bind(this),0); }
					else {
						this._intervalFixFirefox.callback = this._setContent.bind(this);
						this._intervalFixFirefox.registerCallback();
					}
				}.bind(this)
			}
		);
	},
	
	_writeBg: function () {
		
		var str =
		'<div id="PopHtmlBg" style="z-index:1000;position:absolute;top:0;left:0;background:#000;filter:alpha(opacity=50);-moz-opacity:0.50;opacity:0.50;"></div>' +
		'<div id="PopHtmlContent" style="z-index:1001;position:absolute;top:0;left:0;"></div>';			
		
		if (this.IE6) {
			str += '<iframe id="PopHtmlIframeIE6Hack" style="z-index:999;position:absolute;top:0;left:0;filter:alpha(opacity=0);"></iframe>';	
		}
		
		this._target.innerHTML = str;
		this._target.setStyle({ display:'block' });
		this._bg = $('PopHtmlBg');
		this._content = $('PopHtmlContent');
		this._content.setStyle({ visibility:'hidden' });
		Axis.each( this._setBgSize.bind(this,this._bg) );
		if (this.IE6) {
			this._IframeIE6Hack = $('PopHtmlIframeIE6Hack');
			Axis.each( this._setBgSize.bind(this,this._IframeIE6Hack) );
		}
		
	},
	
	_setBgSize: function (o,k) {
		o.style[k.scale] = this._getPageSize(k) + 'px';
	},
	
	_setContent: function () {
		this._intervalFixFirefox.stop();
		if (this._onEvent) { this._onEvent("onReady",this._name); } 
		this._startSetContentPosition(this._content);
		this._content.focus();
	},
	
	_startSetContentPosition: function (o) {
		Axis.each( this._setContentPosition.bind(this,o) );
		o.style.visibility = 'visible';
	},
	
	_setContentPosition: function (o,k) {
		o.style[k.pos] = this._getCenter(o,k) + 'px';	
	},
	
	close: function () {
		this._target.setStyle({ display:'none' });
	}
	
});