var PopHtml = Class.create({
	
	initialize: function (name,target,params,onEvent) {
		this._name = name;
		this._target = target;
		this._onEvent = onEvent;
		this._content = this._bg = null;
		this.IE6 = (navigator.appVersion.indexOf("MSIE 6")!=-1) ?true :false;
		this.Safari = (navigator.appVersion.indexOf("Safari")!=-1) ?true :false;		
		this._doc = (!this.Safari) ?document.documentElement :document.body; 	
		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 () {
		if (this._intervalFixFirefox==null) { this._intervalFixFirefox = new PeriodicalExecuter(this._setContent.bind(this),0); }
		else {
			this._intervalFixFirefox.callback = this._setContent.bind(this);
			this._intervalFixFirefox.registerCallback();
		}
	},
	
	_writeBg: function () {
		
		var str =
		'<div id="PopHtmlBg" style="z-index:1000;position:absolute;top:0;left:0;background:#fff;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 src="javascript:false;" 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 = $(this._name);
		this._content.setStyle({ visibility:'hidden', zIndex:1002 });
		Axis.each( this._setBgSize.bind(this,this._bg) );
		this._onWindowResize();
		if (this.IE6) {
			this._IframeIE6Hack = $('PopHtmlIframeIE6Hack');
			Axis.each( this._setBgSize.bind(this,this._IframeIE6Hack) );
		}
		
		this._content.style.visibility = 'visible';
		
	},
	
	_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) );
	},
	
	_setContentPosition: function (o,k) {
		//o.style[k.pos] = this._getCenter(o,k) + 'px';	
		var node = o;
		var offset = 0;
		if (k.pos=="left")
			while (node.offsetParent){
	      node=node.offsetParent;
	      offset+=node.offsetLeft;    
	    }
  	offset = this._getCenter(o,k) - offset;
		o.style[k.pos] = offset + 'px';
	},
	
	close: function () {
		this._target.setStyle({ display:'none' });
		this._content.setStyle({ visibility:'hidden' });
	}
	
});