
/*
 * LayerWin main object
 */
var LayerWin = function (id) {

	this.dlg			= null;				// Dialog element
	this.dlgBg			= null;				// Background overlay element
	this.ifm			= null;				// Iframe element
	this.loading		= null;				// Loading Mask
	this.document		= self.document;
	this.id				= id;
	this.width			= 0;
	this.height			= 0;
	this.isOpen			= false;
	this.fade			= { speed:30, step:0.2, run:false };
	this.closeFunction	= null;
	this.init			= false;
	this.create			= false;

	this.frameBorder	= 1;
	this.closeBtn		= null;
	this.closeBtnSrc	= '/rebirth/img/wizmans/btnClose.gif';
	this.closeBtnSize	= { width:27, height:27 };

	// [public] Open dialog
    this.open = function (url, width, height, closeBtn, autoClose, fadeSpeed) {
    	if (!this.init) {
    		if ( (!this.create) || (LPlib.getStyle(this.dlgBg,'background-color')=='')
    			|| (LPlib.getStyle(this.dlgBg,'background-color') == 'transparent') || (parseFloat(LPlib.getStyle(this.dlgBg,'opacity')) == 1) ) {
    			window.setTimeout( (function(){this.open.bind(this)(url, width, height, autoClose, fadeSpeed)}).bind(this), 50 );
    			return;
    		}
    		this.init = true;
    	}
		this.isOpen				= true;
		if (closeBtn) this.closeBtn.style.display = 'block';
		this.fade.opacity		= parseFloat(LPlib.getStyle(this.dlgBg,'opacity'));
		this.fade.speed			= ( (!fadeSpeed && (fadeSpeed!==0)) ? this.fade.speed	 : parseInt(fadeSpeed) );
		this.fade.arguments		= {url:url, width:width, height:height, autoClose:autoClose};
		this.toggleBackground(true);
		if (this.fade.speed == 0) this.show(url, width, height);
	}

	// [public] Resize for opened dialog
	this.setSize = function (width, height) {
		this.width		= parseInt(width);
		this.height		= parseInt(height);

		this.dlg.style.width		= (this.width+this.frameBorder*2).toString() + 'px';
		this.dlg.style.height		= (this.height+this.frameBorder*2).toString() + 'px';

		if ((LPlib.browserSize().width < this.width) || (LPlib.browserSize().height < this.height)) {
			this.dlg.style.position		= 'absolute';
			this.dlg.style.marginLeft	= '0px';
			this.dlg.style.marginTop	= '5px';
			this.dlg.style.height		= (this.height+5).toString() + 'px';
			if (LPlib.browserSize().width < this.width) {
				this.dlg.style.left	= '0px';
			}else{
				this.dlg.style.left	= ((LPlib.browserSize().width-this.width)/2).toString() + 'px';
			}
			if (LPlib.is_ie && (!LPlib.is_ie7_2)) this.dlg.style.removeExpression('top');
			if (LPlib.browserSize().height < this.height) {
				this.dlg.style.top	= '0px';
			}else{
				this.dlg.style.top	= ((LPlib.browserSize().height-this.height)/2).toString() + 'px';
			}
			window.scrollTo(0,0);
		}else{
			this.dlg.style.position		= (LPlib.is_ie && !LPlib.is_ie7_2) ? 'absolute' : 'fixed';
			this.dlg.style.left			= '50%';
			this.dlg.style.marginLeft	= (0-(this.width/2)).toString() + 'px';
			this.dlg.style.marginTop	= '0px';
			if (LPlib.is_ie && (!LPlib.is_ie7_2)) {
				this.dlg.style.setExpression('top', 'LPlib.body().scrollTop + Math.round((LPlib.body().clientHeight-this.style.pixelHeight)/2)' );
			}else{
				this.dlg.style.top			= '50%';
				this.dlg.style.marginTop	= (0-(this.height/2)).toString() + 'px';
			}
		}

		this.loading.style.width	= this.width.toString() + 'px';
		this.loading.style.height	= this.height.toString() + 'px';
		this.ifm.style.width		= this.width.toString() + 'px';
		this.ifm.style.height		= this.height.toString() + 'px';

		this.loading.style.left		= this.frameBorder.toString() + 'px';
		this.loading.style.top		= this.frameBorder.toString() + 'px';

		this.closeBtn.style.left	= (this.width - this.closeBtnSize.width + this.frameBorder).toString() + 'px';
		this.closeBtn.style.top		= this.frameBorder.toString() + 'px';
	}

	// [public] Close for opened dialog
    this.close = function() {
    	if (!this.isOpen) return;
		if (this.ifm) this.ifm.style.visibility	= 'hidden';
		if (this.loading) this.loading.style.display = 'block';
		if (this.dlg) this.dlg.style.visibility	= 'hidden';
		this.closeURL();
		this.toggleBackground(false);
    }

	// [public] Show loading mask
    this.showLoadingMask = function() {
    	if (!this.isOpen) return;
		//if (this.ifm) this.ifm.style.visibility	= 'hidden';
		if (this.loading) this.loading.style.display = 'block';
		if (this.ifm) this.ifm.style.visibility	= 'visible';
    }

	// [public] Close loading mask
    this.closeLoadingMask = function() {
    	if (!this.isOpen) return;
		if (this.loading) this.loading.style.display = 'none';
		if (this.ifm) this.ifm.style.visibility	= 'visible';
    }

	// [public] Adjust Scrolling
    this.adjustScrolling = function() {
		var id	= this.ifm.getAttribute('id');
		var win = ((LPlib.is_ie) ? this.document.getElementById(id).contentWindow : window.frames[id]);
		if (win.LPlib) {
			if (win.LPlib.bodySize().height > this.height) {
				this.setSize(this.width+20, this.height);
				this.closeBtn.style.left	= (this.width - this.closeBtnSize.width + this.frameBorder -20).toString() + 'px';
				this.ifm.setAttribute('scrolling','auto');
			}else{
				this.ifm.setAttribute('scrolling','no');
				this.closeBtn.style.left	= (this.width - this.closeBtnSize.width + this.frameBorder).toString() + 'px';
			}
		}
	}

	// [private] Create DOM element on window.onload
	this.createObject = function () {

		LPlib.objects.push(this);

		this.dlgBg					= this.document.createElement('DIV');
		this.dlgBg.setAttribute('id', this.id + '_bg');
		this.dlgBg.style.visibility	= 'hidden';
		this.dlgBg.style.position	= (LPlib.is_ie && !LPlib.is_ie7_2) ? 'absolute' : 'fixed';
		this.dlgBg.style.zIndex		= LPlib.z_index++;
		this.dlgBg.style.left		= '0px';
		this.dlgBg.style.top		= '0px';
		this.dlgBg.className		= 'LayerPopupBackground';
		this.document.body.appendChild( this.dlgBg );

		this.dlg					= this.document.createElement('DIV');
		this.dlg.setAttribute('id', this.id);
		this.dlg.style.visibility	= 'hidden';
		this.dlg.style.position		= (LPlib.is_ie && !LPlib.is_ie7_2) ? 'absolute' : 'fixed';
		this.dlg.style.zIndex		= LPlib.z_index++;
		this.dlg.style.left			= '0px';
		this.dlg.style.top			= '0px';
		this.dlg.className			= 'LayerPopupWindow';
		this.dlg.style.overflow		= 'hidden';

		this.loading				= this.document.createElement('DIV');
		this.loading.setAttribute('id', this.id + '_loading');
		this.loading.style.position	= 'absolute';
		this.loading.style.overflow	= 'hidden';
		this.loading.className		= 'LayerPopupLoadingMask';
		LPlib.removeSelection(this.loading);
		this.loading.innerHTML		= '<table><tr><td><img src="'+ LPlib.jsPath + '/loading_b.gif" /></td></tr></table>'
		this.dlg.appendChild( this.loading );

		this.ifm					= this.document.createElement('IFRAME');
		this.ifm.setAttribute('id', this.id + '_iframe');
		this.ifm.setAttribute('name', this.id + '_iframe');
		this.ifm.setAttribute('src', 'about:blank');
		this.ifm.setAttribute('frameBorder','0');
		this.ifm.setAttribute('marginwidth','0');
		this.ifm.setAttribute('marginheight','0');
		this.ifm.setAttribute('scrolling','auto');
		this.ifm.setAttribute('allowtransparent','true');
		this.ifm.style.visibility	= 'hidden';
		//this.ifm.style.border		= 'none';
		this.ifm.onbeforeunload		= null;
		this.dlg.appendChild( this.ifm );

		this.closeBtn				= this.document.createElement('IMG');
		this.closeBtn.setAttribute('src', this.closeBtnSrc );
		this.closeBtn.setAttribute('alt', '•Â‚¶‚é' );
		this.closeBtn.style.position	= 'absolute';
		this.closeBtn.style.display	= 'none';
		this.closeBtn.style.width	= this.closeBtnSize.width + 'px';
		this.closeBtn.style.height	= this.closeBtnSize.height + 'px';
		this.closeBtn.style.cursor	= 'pointer';
		LPlib.addEvent( this.closeBtn, 'onclick', this.close.bind(this), false );
		this.dlg.appendChild( this.closeBtn );

		LPlib.addEvent( window, 'onresize', this.onResize.bind(this), false );
		this.document.body.appendChild( this.dlg );
		this.create 				= true;
	}

	// [private] Browser Resize
	this.onResize = function () {
		if (this.isOpen) {
			this.setSize(this.width, this.height);
		}
	}

	// [private] Display on
	this.show = function (url, width, height) {
		this.setSize(width, height);
		this.loadURL(url);
		this.dlg.style.visibility			= 'visible';
	}

	// [private] Load document for iframe
    this.loadURL = function(url) {
		var id	= this.ifm.getAttribute('id');
		var win = ((LPlib.is_ie) ? this.document.getElementById(id).contentWindow : window.frames[id]);
		win.location.replace(url);
		//this.ifm.setAttribute('src', url);
    }

	// [private] Close document for iframe
    this.closeURL = function() {
    	try {
			var id		= this.ifm.getAttribute('id');
    		var oDoc	= this.ifm.contentWindow || this.ifm.contentDocument || window.frames[id];
    		if (oDoc.document) oDoc = oDoc.document;
			oDoc.body.innerHTML = '';
		}catch(e){}
    }

	// [private] Toggle background overlay
    this.toggleBackground = function (on) {
		this.dlgBg.style.width	= '100%';
		if (LPlib.is_ie && (!LPlib.is_ie7_2)) {
			this.dlgBg.style.setExpression('height', 'LPlib.getBgSize().height' );
		}else{
			this.dlgBg.style.height	= '100%';
		}
		if (on) {
			this.fadeInBackground();
		}else{
			this.fadeOutBackground();
		}
    }

	// [private] Fade out effect of background overlay
    this.fadeOutBackground = function () {
		if (this.fade.speed > 0) {
			if (!this.fade.run) {
				this.fade.run	= true;
				this.fade.target = 0;
			}
			var opacity	= parseFloat(LPlib.getStyle(this.dlgBg,'opacity'));
			if (opacity > this.fade.target) {
				var step = ((opacity-this.fade.target)<this.fade.step) ? (opacity-this.fade.target) : this.fade.step;
				this.dlgBg.style.opacity	= (opacity - step).toString();
				this.dlgBg.style.filter		= 'alpha(Opacity=' + ((opacity-step)*100).toString() + ')';
				this.fade.handle = window.setTimeout( this.fadeOutBackground.bind(this), this.fade.speed/2 );
				return;
			}
			window.clearTimeout(this.fade.handle);
			this.fade.run	= false;
		}
		this.dlgBg.style.visibility	= 'hidden';
		this.dlgBg.style.opacity	= this.fade.opacity.toString();
		this.dlgBg.style.filter		= 'alpha(Opacity=' + (this.fade.opacity*100).toString() + ')';
		this.dlgBg.style.height		= '10px';
		this.dlg.style.height		= '10px';
		LPlib.toggleTroubleElements(true);
		this.dlgBg.onclick = null;
		this.isOpen	= false;
		if ((this.closeFunction)&&(typeof(this.closeFunction)=='function')) {
			this.closeFunction();
		}
	}

    // [private] Fade in effect of background overlay
	this.fadeInBackground = function () {
		if (this.fade.speed > 0) {
			if (!this.fade.run) {
				LPlib.toggleTroubleElements(false);
				this.fade.run		= true;
				this.fade.target	= this.fade.opacity;
				this.dlgBg.style.visibility	= 'visible';
				this.dlgBg.style.opacity	= '0';
				this.dlgBg.style.filter		= 'alpha(Opacity=0)';
				this.fade.handle = window.setTimeout( this.fadeInBackground.bind(this), this.fade.speed );
				return;
			}
			var opacity	= parseFloat(LPlib.getStyle(this.dlgBg,'opacity'));
			if (opacity < this.fade.target) {
				var step = ((this.fade.target-opacity)<this.fade.step) ? (this.fade.target-opacity) : this.fade.step;
				this.dlgBg.style.opacity	= (opacity + step).toString();
				this.dlgBg.style.filter		= 'alpha(Opacity=' + ((opacity+step)*100).toString() + ')';
				this.fade.handle = window.setTimeout( this.fadeInBackground.bind(this), this.fade.speed );
				return;
			}
			window.clearTimeout(this.fade.handle);
			this.fade.run	= false;
		}else{
			LPlib.toggleTroubleElements(false);
			this.dlgBg.style.visibility	= 'visible';
		}
		this.dlgBg.onclick = ( this.fade.arguments.autoClose ? this.close.bind(this) : null );
		this.show(this.fade.arguments.url, this.fade.arguments.width, this.fade.arguments.height);
	}

	// run this object
	LPlib.addEvent( window, 'onload', this.createObject.bind(this), false );

}

// Replace 'this' prototype  for functions
Function.prototype.bind = function( thisObj ) {
	var method = this;
	return function() {
		method.apply(thisObj, arguments);
	}
}

var layerWin	= new LayerWin('LayerWin');


