
/*
 * LayerPopup library utilities
 */
var LPlib = new Object();
LPlib.jsPath		= '/js/layer_popup';
LPlib.agent_name	= navigator.userAgent.toLowerCase();
LPlib.is_ie			= ((LPlib.agent_name.indexOf("msie") != -1) && (LPlib.agent_name.indexOf("opera") == -1));
LPlib.is_ie7		= (LPlib.is_ie && (parseFloat(navigator.appVersion.split('MSIE')[1])>=7));
LPlib.is_ie7_2		= (LPlib.is_ie7 && (document.all[0].nodeType==8));
LPlib.is_gecko		= navigator.product;
LPlib.is_strict		= (document.compatMode == 'CSS1Compat');
LPlib.z_index		= 1000;
LPlib.css_files		= [];
LPlib.objects		= [];
LPlib.body			= function() { return ( ((!LPlib.is_ie) || (LPlib.is_ie && LPlib.is_strict)) ? self.document.documentElement : self.document.body ) };
LPlib.bodySize		= function() { return { width:LPlib.body().scrollWidth, height:LPlib.body().scrollHeight }; };
LPlib.screenSize	= function() { return { width:screen.availWidth, height:screen.availHeight }; }; 
LPlib.browserSize	= function() { return { 
							width:(LPlib.is_ie ? LPlib.body().clientWidth : window.innerWidth), 
							height:(LPlib.is_ie ? LPlib.body().clientHeight : window.innerHeight) 
						};
					};

// API function
LPlib.getCurrentObject	= function() {
	for (var i=LPlib.objects.length;i>0;i--) {
		if (LPlib.objects[i-1].isOpen) return LPlib.objects[i-1];
	}
	return null;
}
LPlib.close				= function() {
	var obj = LPlib.getCurrentObject();
	if (obj) obj.close();
}
LPlib.closeLoadingMask	= function() {
	var obj = LPlib.getCurrentObject();
	if (obj) obj.closeLoadingMask();
}
LPlib.showLoadingMask	= function() {
	var obj = LPlib.getCurrentObject();
	if (obj) obj.showLoadingMask();
}
LPlib.setSize			= function(width, height) {
	var obj = LPlib.getCurrentObject();
	if (obj) obj.setSize(width, height);
}
LPlib.setCloseFunction	= function(func) {
	var obj = LPlib.getCurrentObject();
	if ((obj)&&(func)&&(typeof(func)=='function')) {
		obj.closeFunction = func;
	}else{
		obj.closeFunction = null;
	}
}

// Get background Size
LPlib.getBgSize			= function() {
	if(!LPlib.bodySize().width)	LPlib.bodySize().width=LPlib.body().scrollWidth;
	if(!LPlib.bodySize().height)	LPlib.bodySize().height=LPlib.body().scrollHeight;
	var width = ((LPlib.body().clientWidth>LPlib.bodySize().width) ? LPlib.body().clientWidth : LPlib.bodySize().width);
	var height = ((LPlib.body().clientHeight>LPlib.bodySize().height) ? LPlib.body().clientHeight : LPlib.bodySize().height);
	return { width:width, height:height }; 
};

// Hide/Show trouble elements
LPlib.hideObjs = [];
LPlib.toggleTroubleElements = function (on) {
	if (on) {
		for (var i=0; i<LPlib.hideObjs.length; i++){
			LPlib.hideObjs[i].style.visibility = 'visible';
		}
		LPlib.hideObjs = [];
	}else{
		LPlib.hideObjs = [];
		var objs = document.getElementsByTagName('select');
		for (var i=0; i<objs.length; i++){
			if (objs[i].style.visibility != 'hidden') { objs[i].style.visibility = 'hidden'; LPlib.hideObjs.push(objs[i]); }
		}
		var objs = document.getElementsByTagName('object');
		for (var i=0; i<objs.length; i++){
			if (objs[i].style.visibility != 'hidden') { objs[i].style.visibility = 'hidden'; LPlib.hideObjs.push(objs[i]); }
		}
		var objs = document.getElementsByTagName('embed');
		for (var i=0; i<objs.length; i++){
			if (objs[i].style.visibility != 'hidden') { objs[i].style.visibility = 'hidden'; LPlib.hideObjs.push(objs[i]); }
		}
	}
};

// Protect mouse dragging
LPlib.removeSelection = function (obj) {
	if (obj) {
		obj.style.cursor	= 'default';
		obj.ondblclick		= LPlib.clearSelection;
		obj.onmousedown		= LPlib.clearSelection;
		obj.oncontextmenu	= LPlib.clearSelection;
		obj.onselectstart	= LPlib.clearSelection;
	}
};

// Add event callback
LPlib.addEvent = function (obj, evt, func, capture) {
	if (!obj) return;
	if (obj.attachEvent) { obj.attachEvent(evt, func); }
	else if (obj.addEventListener) {
		if (!capture) capture=false;
		evt = evt.replace(/^on/i,'');
		obj.addEventListener(evt, func, capture);
	}else{ obj[evt] = fnc; }
	return;
};

// Remove event callback
LPlib.removeEvent = function (obj, evt, func, capture) {
	if (!obj) return;
	if (obj.attachEvent) { obj.detachEvent(evt, func); }
	if (obj.addEventListener) {
		if (!capture) capture=false;
		evt = evt.replace(/^on/i,'');
		obj.removeEventListener(evt, func, capture);
	}else{ obj[evt] = null; }
	return;
};

// Protect mouse dragging callback
LPlib.clearSelection = function (e) {
	try {
		var obj = ( (window.event) ? window.event.srcElement : e.target );
		if ((obj.type=='text')||(obj.type=='textarea')||(obj.type=='file')||(obj.type=='password')) return true;
		if(document.selection && document.selection.empty){
			document.selection.empty() ;
		} else if(window.getSelection) {
			var sel=window.getSelection();
			if(sel && sel.removeAllRanges) sel.removeAllRanges() ;
			if(sel && sel.collapse) sel.collapse() ;
		}
	}catch(e){}
	if (LPlib.is_gecko) {
		var objInputs = document.getElementsByTagName('input');
		for (var i=0; i<objInputs.length; i++) {
			if ((objInputs[i].type=='text')||(objInputs[i].type=='textarea')||(objInputs[i].type=='file')||(objInputs[i].type=='password')) 
				objInputs[i].blur();
		}
	}
	return false;
};

// [private] Get current applyed style
LPlib.getStyle = function (obj, style) {
	var value = '';
	if (obj) {
		if (document.defaultView)
			value = document.defaultView.getComputedStyle(obj,'').getPropertyValue(style);
		else if (obj.currentStyle) {
			style = style.replace(/\-(\w)/g, function (strMatch,p1){return p1.toUpperCase();});
			value = obj.currentStyle[style];
		}
	}
	return value;
};



/*
 * LayerPopup main object
 */
var LayerPopup = 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;

	// [public] Open dialog
    this.open = function (url, width, height, 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;
		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.toString() + 'px';
		this.dlg.style.height		= this.height.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	= '0px';
			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';
			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';
	}
	
	// [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';
    }

	// [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';
    }

	// [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.style.overflow		= 'hidden';

		this.loading				= this.document.createElement('DIV');
		this.loading.setAttribute('id', this.id + '_loading');
		this.loading.style.overflow	= 'hidden';
		this.loading.className		= 'LayerPopupLoadingMask';
		LPlib.removeSelection(this.loading);
		this.loading.innerHTML		= '<table><tr><td><img src="'+ LPlib.jsPath + '/loading2.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 );
		
		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 = 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() + ')';
		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 );
	
}


/*
 * LayerMask main object
 */
var LayerAlert = function (id) {

	this.dlg			= null;				// Dialog element
	this.dlgBg			= null;				// Background overlay element
	this.document		= self.document;
	this.id				= id;
	this.buttons		= ['img_css btn_ok'];
	this.buttonsClose	= [true];
	this.selectButton	= null;
	this.width			= 500;
	this.height			= 210;
	this.isOpen			= false;
	this.fade			= { speed:20, step:0.15, run:false };
	this.closeFunction	= null;
	this.init			= false;
	this.create			= false;

	// [public] Open dialog
    this.open = function (msg, buttons, closeFunction, buttonsClose, 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)(msg, buttons, closeFunction, buttonsClose, autoClose, fadeSpeed)}).bind(this), 50 );
    			return;
    		}
    		this.init = true;
    	}

		this.isOpen				= true;
		this.fade.opacity		= parseFloat(LPlib.getStyle(this.dlgBg,'opacity'));
		this.fade.speed			= ( (!fadeSpeed && (fadeSpeed!==0)) ? this.fade.speed	 : parseInt(fadeSpeed) );
		this.fade.arguments		= {msg:msg, autoClose:autoClose}; 
		this.buttons			= buttons;
		if ((!buttonsClose) || (buttons.length!=buttonsClose.length)) {
			this.buttonsClose = [];
			for (var i=0; i<buttons.length; i++) this.buttonsClose.push(false);
		}else{
			this.buttonsClose = buttonsClose;
		}
		if ((closeFunction)&&(typeof(closeFunction)=='function')) {
			this.closeFunction = closeFunction;
		}else{
			this.closeFunction = null;
		}
		
		this.toggleBackground(true);
		if (this.fade.speed == 0) this.show(msg);
	}

	// [public] Resize for opened dialog
	this.setSize = function () {
		this.dlg.style.width		= this.width.toString() + 'px';
		this.dlg.style.height		= this.height.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	= '0px';
			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';
			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';
			}
		}
	}
	
	// [public] Close for opened dialog
    this.close = function() {
    	if (!this.isOpen) return;
		if (this.dlg) this.dlg.style.visibility	= 'hidden';
		this.toggleBackground(false);
    }

	// [public] Change Message
	this.changeMsg = function (msg, buttons, closeFunction, buttonsClose) {
		this.dlg.style.visibility	= 'hidden';
		this.buttons		= buttons;
		if ((!buttonsClose) || (buttons.length!=buttonsClose.length)) {
			this.buttonsClose = [];
			for (var i=0; i<buttons.length; i++) this.buttonsClose.push(false);
		}else{
			this.buttonsClose = buttonsClose;
		}
		if ((closeFunction)&&(typeof(closeFunction)=='function')) {
			this.closeFunction = closeFunction;
		}else{
			this.closeFunction = null;
		}
		this.show(msg);
	}
	
	// [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.style.overflow		= 'hidden';
		
		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();
		}
	}

	// [private] Display on
	this.show = function (msg) {
		this.setSize();
		var html = '';
		html	+= '<div class="box_bg">';
		html	+= '<div class="text">' + msg.toString() + '</div>';
		if (this.buttons.length == 1)
			html	+= '<div class="clearfix ctrl_button01">';
		else
			html	+= '<div class="clearfix ctrl_button">';
		for (var i=0; i<this.buttons.length; i++) {
			html	+= '<a href="#" onclick="LPlib.getCurrentObject().selectButton='+ (i+1).toString() +';';
			if (!this.buttonsClose[i]) {
				html+= 'LPlib.close();';
			}else{
				html+= 'LPlib.getCurrentObject().closeFunction('+ (i+1).toString() +');';
			}
			html	+= 'return false;" class="'+ this.buttons[i].toString() +'">ok</a>';
		}
		html	+= '</div>';
		html	+= '</div>';
		this.dlg.innerHTML = html;
		this.dlg.style.visibility	= 'visible';
	}

	// [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() + ')';
		LPlib.toggleTroubleElements(true);
		this.dlgBg.onclick = null;
		this.isOpen	= false;
		if ((this.closeFunction)&&(typeof(this.closeFunction)=='function')) {
			if ( !this.buttonsClose[this.selectButton-1] )
				this.closeFunction(this.selectButton);
		}
	}
	
    // [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.msg);
	}
		
	// run this object
	LPlib.addEvent( window, 'onload', this.createObject.bind(this), false );
	
}


/*
 * LayerMask main object
 */
var LayerMask = function (id, css) {

	this.dlg			= null;				// Dialog element
	this.dlgBg			= null;				// Background overlay element
	this.document		= self.document;
	this.id				= id;
	this.width			= 0;
	this.height			= 0;
	this.autoClose		= true;
	this.isOpen			= false;
	this.fade			= { speed:20, step:0.15, run:false };
	this.closeFunction	= null;
	this.init			= false;
	this.create			= false;

	// [public] Open dialog
    this.open = function (msg, img, width, height, 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)(msg, img, width, height, autoClose, fadeSpeed)}).bind(this), 50 );
    			return;
    		}
    		this.init = true;
    	}

		this.isOpen				= true;
		this.autoClose			= autoClose;
		this.fade.opacity		= parseFloat(LPlib.getStyle(this.dlgBg,'opacity'));
		this.fade.speed			= ( (!fadeSpeed && (fadeSpeed!==0)) ? this.fade.speed	 : parseInt(fadeSpeed) );
		this.fade.arguments		= {msg:msg, img:img, width:width, height:height, autoClose:autoClose}; 
		
		this.toggleBackground(true);
		if (this.fade.speed == 0) this.show(msg, img, width, height);
	}

	// [public] Resize for opened dialog
	this.setSize = function (width, height) {
		this.width		= parseInt(width);
		this.height		= parseInt(height);

		this.dlg.style.width		= width.toString() + 'px';
		this.dlg.style.height		= height.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	= '0px';
			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('left');
				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';
			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';
			}
		}
	}
	
	// [public] Close for opened dialog
    this.close = function() {
    	if (!this.isOpen) return;
		if ((this.closeFunction)&&(typeof(this.closeFunction)=='function')) {
			if (!this.closeFunction()) return;
		}
		if (this.dlg) this.dlg.style.visibility	= 'hidden';
		this.toggleBackground(false);
    }

	// [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.style.overflow		= 'hidden';
		this.dlg.className			= 'LayerPopupLayerMask';
		
		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 (msg, img, width, height) {
		this.dlg.style.visibility	= 'hidden';
		this.setSize(width, height);
		var html = '';

		html += '<div class="LayerPopupMask_ImgMsg">';
		if (this.autoClose) {
			if (img) html += '<img src="'+ img +'" title="'+ msg +'" width="640" height="480" onclick="LPlib.close();" style="cursor:pointer;" />';
		}else{
			if (img) html += '<img src="'+ img +'" title="'+ msg +'" width="640" height="480"/>';
		}
		if (msg) html += msg;
		html += '</div>';
		html += '<div class="LayerPopupMask_Close"><img src="'+ LPlib.jsPath + '/closelabel.gif" title="•Â‚¶‚é" onclick="LPlib.close();" /></div>';
		
		this.dlg.innerHTML = html;
		this.dlg.style.visibility	= 'visible';
	}

	// [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() + ')';
		LPlib.toggleTroubleElements(true);
		this.dlgBg.onclick = null;
		this.isOpen	= false;
	}
	
    // [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.msg, this.fade.arguments.img, 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 layerAlert	= new LayerAlert('LayerAlert');
var layerPopup	= new LayerPopup('LayerPopup');
var layerMask	= new LayerMask('LayerMask');


