var PopHtml = Class.create({
	
	initialize: function (name,target,params,onEvent,o) {
		this._name = name;
		this._target = target;
		this._params = params;
		this._onEvent = onEvent;
		this._content = this._bg = null;
		if(o){
			this._contentURL = o.contentURL;
			this._methodURL = o.methodURL;
		}
		
		this._targetHtmlStatic = target;
		
		this._bgColor = '#fff';
		this._bgAlpha = 50;
		
		if(o){
			this._bgColor = '#000';
			
			if(o.bgColor) this._bgColor = o.bgColor;
			if(o.bgAlpha) this._bgAlpha = o.bgAlpha;
			if(o.posy) this._posy = o.posy;
		}
		
		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._contentURL) {
			new Ajax.Request(
				this._contentURL, {
					method: this._methodURL,
					postBody:$H(this._ajaxParams).toQueryString(),
					parameters:"navid="+this._params.navid,
					onComplete: function(response) {
						this._content.innerHTML = response.responseText;
						this._setInterval();
					}.bind(this)
				}
			);
		}
		else {
			this._setInterval();
		}
	},
	
	_setInterval: 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:1004;position:absolute;top:0;left:0;background:'+this._bgColor+';filter:alpha(opacity='+this._bgAlpha+');-moz-opacity:0.'+this._bgAlpha+';opacity:0.'+this._bgAlpha+';"></div>' +
		'<div id="PopHtmlContent" style="z-index:1005;position:absolute;top:0;left:0;"></div>';	
		
		if (this.IE6) {
			str += '<iframe id="PopHtmlIframeIE6Hack" style="z-index:1003;position:absolute;top:0;left:0;filter:alpha(opacity=0);"></iframe>';	
		}
		
		this._target.innerHTML = str;
		this._target.setStyle({ display:'block' });
		this._bg = $('PopHtmlBg');
		if (this._contentURL) {
			this._content = $('PopHtmlContent');
			this._content.style.visibility = "hidden";
		}
		else this._content = $(this._name);
		
		
		this._content.setStyle({ visibility:'hidden', zIndex:1006 });
		Axis.each( this._setBgSize.bind(this,this._bg) );
		
		this._onWindowResize();
		
		if (this.IE6) {
			this._IframeIE6Hack = $('PopHtmlIframeIE6Hack');
			Axis.each( this._setBgSize.bind(this,this._IframeIE6Hack) );
		}
		
		if (!this._contentURL) 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) );
		if (this._contentURL) o.style.visibility = 'visible';
	},
	
	_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!=null && node.offsetParent!=null){
	      		node=node.offsetParent;
	      		offset+=node.offsetLeft;
	    	}
		}
  	offset = this._getCenter(o,k) - offset;
		o.style[k.pos] = offset + 'px';
	},
	
	close: function () {
		if (this._contentURL) {
			this._target.innerHTML = '';
		}
		this._target.setStyle({ display:'none' });
		this._content.setStyle({ visibility:'hidden' });
	}
	
});