function PopUp () {
	this.isIE6				= false;
	this.isMacFF			= false;
	this.loadImg			= new Image ();
	this.scrollPos		= 0;	

	this.loadImg.src	= '/v2/img/pu_load.gif';

	var ua = navigator.userAgent.toLowerCase ();
	if (ua.indexOf ('mac') != -1 && ua.indexOf ('firefox') != -1) this.isMacFF = true;

	if (typeof document.body.style.maxHeight === 'undefined') this.isIE6 = true;

	window.popup = this;
}

PopUp.prototype.init = function (s) {
	this.x					= s.x ? s.x:-1;
	this.y					= s.y ? s.y:-1;
	this.width			= s.w ? s.w:640;
	this.height			= s.h ? s.h:480;
	this.url				= s.u ? s.u:'';
	this.onShow			= s.show ? s.show:null;
	this.onHide			= s.hide ? s.hide:null;
}

PopUp.prototype.show = function (s) {
	if (!s) return;
	this.init (s)

	if (this.onShow) this.onShow ();
	this.showOverlay ();

	var eBody		= document.getElementsByTagName ('body')[0];
	var eWin		= document.createElement ('div');
	eWin.id			= 'PU_window';
	eBody.appendChild (eWin);

	var eIframe	= document.getElementById ('PU_iframeContent');
	if (eIframe) eWin.removeChild (eIframe);
	eIframe										= document.createElement ('iframe');
	eIframe.id								= 'PU_iframeContent';
	eIframe.frameBorder				= '0';
	eIframe.allowTransparency	= 'yes';
	eIframe.style.width				= this.width + 'px';
	eIframe.style.height			= this.height + 'px';
	eIframe.onload						= this.showIframe ();
	eIframe.src								= this.url;
	eIframe.scrolling					= 'no';
	eWin.appendChild (eIframe);

	this.position ();
}

PopUp.prototype.hide = function () {
	var eBody		= document.getElementsByTagName ('body')[0];
	var eWin		= document.getElementById ('PU_window');
	var eIframe	= document.getElementById ('PU_iframeContent');

	if (eIframe) eIframe.src = 'about:blank';
	if (eWin) eBody.removeChild (eWin);
	if (window.popup) window.popup.hideOverlay ();

	if (window.popup.onHide) window.popup.onHide ();
}

PopUp.prototype.showOverlay = function () {
	var eHtml = document.getElementsByTagName ('html')[0];
	var eBody	= document.getElementsByTagName ('body')[0];

	// Disable scrolling //
	if (document.documentElement) this.scrollPos = document.documentElement.scrollTop; else if (document.body) this.scrollPos = document.body.scrollTop;
	eHtml.style.width			= '100%';
	eHtml.style.height		= '100%';
	eHtml.style.overflow	= 'hidden';
	eBody.style.width			= '100%';
	eBody.style.height		= '100%';
	if (document.documentElement) document.documentElement.scrollTop = this.scrollPos; else if (document.body) document.body.scrollTop = this.scrollPos;

	if (this.isIE6) {
		if (document.getElementById ('PU_hideSelect') === null) { // Iframe to hide select elements in IE6
			var eIframe	= document.createElement ('iframe');
			eIframe.id = 'PU_hideSelect';
			eBody.appendChild (eIframe);
		}
	}

	eOverlay	= document.getElementById ('PU_overlay');
	eLoad			= document.getElementById ('PU_load');

	if (eOverlay === null) {
		var eOverlay = document.createElement ('div');

		eOverlay.id							= 'PU_overlay';
		eOverlay.oncontextmenu	= function () { return false; };
		eOverlay.onclick				= function () { window.popup.hide (); };
		eBody.appendChild (eOverlay);

		if (this.isMacFF) eOverlay.className = 'PU_overlayMacFF'; else eOverlay.className = 'PU_overlayBG';
	}

	if (eLoad === null) {
		var eLoad	= document.createElement ('div');
		var eImg	= document.createElement ('img');

		eLoad.id	= 'PU_load';
		eImg.src	= this.loadImg.src;

		eLoad.appendChild (eImg);
		eBody.appendChild (eLoad);
	}

	eOverlay.style.display	= 'block';
	eLoad.style.display			= 'block';
	eLoad.focus ();
}

PopUp.prototype.hideOverlay = function () {
	var eHtml			= document.getElementsByTagName ('html')[0];
	var eBody			= document.getElementsByTagName ('body')[0];
	var eIframe		= document.getElementById ('PU_hideSelect');
	var eOverlay	= document.getElementById ('PU_overlay');
	var eLoad			= document.getElementById ('PU_load');

	if (eLoad)		eBody.removeChild (eLoad);
	if (eOverlay)	eBody.removeChild (eOverlay);
	if (eIframe)	eBody.removeChild (eIframe);

	eHtml.style.width			= 'auto';
	eHtml.style.height		= 'auto';
	eHtml.style.overflow	= '';
	eBody.style.width			= 'auto';
	eBody.style.height		= 'auto';
	if (document.documentElement) document.documentElement.scrollTop = this.scrollPos; else if (document.body) document.body.scrollTop = this.scrollPos;
}


PopUp.prototype.showIframe = function () {
	var eBody	= document.getElementsByTagName ('body')[0];
	var eLoad	= document.getElementById ('PU_load');
	var eWin	= document.getElementById ('PU_window');
	eBody.removeChild (eLoad);
	eWin.style.display = 'block';
}

PopUp.prototype.position = function () {
	var eWin		= document.getElementById ('PU_window');
	eWin.style.width	= this.width + 'px';
	eWin.style.height	= this.height + 'px';

	if (this.x >= 0) {
		eWin.style.left	= this.x + 'px';
	} else {
		var left = parseInt (eWin.offsetWidth / 2);
		eWin.style.marginLeft	= '-' + left + 'px';
	}

	if (this.y >= 0) {
		eWin.style.top	= this.y + 'px';
	} else if (!this.isIE6) {
		var top = parseInt (eWin.offsetHeight / 2);
		eWin.style.marginTop	= '-' + top + 'px';
	}
}

PopUp.prototype.resize = function (w, h) {
	if (w == this.width && h == this.height) return;
	if (w > 0) this.width = w;
	if (h > 0) this.height = h;

	var eIframe	= document.getElementById ('PU_iframeContent');
	eIframe.style.width		= this.width + 'px';
	eIframe.style.height	= this.height + 'px';
	this.position ();
}

