var msgAllowed = true;
var MessageBox = function(obj){
	myMsgBox = this;
	if (!msgAllowed){return;}
	//msgAllowed = false;
	this.ie = null;
	this.b = null;
	this.mask = obj.masked || "1";
	this.objBody = parent.document.getElementsByTagName("body")[0];
	this.show = obj.show || false;
	this.step = obj.step || 10;
	this.pause = obj.pause || 3000;
	this.beginOpac = obj.beginOpac || 85;
	this.endOpac = obj.endOpac || 85;
	this.msg = obj.msg || "";
	this.speed = obj.speed || 50;
	this.screenWidth = !document.all ? window.innerWidth : document.body.clientWidth;
	this.screenHeight = !document.all ? window.innerHeight : document.body.clientHeight;
	this.mb = {};
	this.top = obj.top || -1;
	this.left = obj.left || -1;
	this.fadeTimer = null;
	this.finish = obj.finish || null;
	this.init(obj);
	this.container = obj.container;
}

MessageBox.prototype = {
	init: function(obj){
		this.b = new Browsers();
		this.ie = "(ie|ie7)";
		var fader = this;
		this.mb = document.createElement("div");
		this.func = obj.onclick || "fader.destroy()";				
		this.mb.onclick = function (){eval(fader.func)};
		this.mb.id = obj.id || "msgBox";
		this.mb.style.position = obj.position || "absolute";
		this.mb.style.width = (obj.width) ? obj.width : "300px";
		this.mb.style.zIndex = obj.zIndex || 10000;
		this.mb.style.backgroundColor = obj.bgColor || "#F7F7F7";
		this.mb.style.color = obj.color || "#000";
		this.mb.style.padding = obj.padding || "10px";
		this.mb.style.border = obj.border || "1px";
		this.mb.style.borderColor = obj.borderColor || "#CCCCCC";
		this.mb.style.borderStyle = obj.borderStyle || "solid";
		this.mb.style.fontFamily = 	obj.fontFamily || "Trebuchet MS, Lucida Grande, Arial, Helvetica, sans-serif";
		this.mb.style.fontSize = obj.fontSize || "16px";
		this.mb.style.fontWeight = obj.weight || "bold";
		this.mb.icon = obj.icon || "";
		this.mb.msg = obj.msg || "";
		this.feedContent();
		this.mb.style.display = "block";
		this.mb.style.left = obj.left || (this.b.getInnerWidth()/2 - parseInt(this.mb.style.width)/2)+"px";
		this.mb.style.textAlign= obj.align || "justify";
		this.mb.style.margin = obj.margin || "10px";
		this.mb.className = obj.className || "";
		this.mb.style.textIndent = obj.textIndent || "";
		if (this.mask == "1")
			this.overlay = new Mask();
		if (typeof obj.container == "undefined"){
			this.objBody.appendChild(this.mb);
		}else{
			document.getElementById(obj.container).appendChild(this.mb);
			this.mb.style.top = "30px";
		}
		browser.addEvent(window, "resize", function(){fader.box_resize()});
		browser.addEvent(window, "scroll", function(){fader.box_scroll()});
		this.box_resize();
		this.fadein();
	},
	
	feedContent: function (){
		var close_btn = '';
		if (this.mb.icon){
			this.mb.style.background = this.mb.style.backgroundColor+" url("+this.mb.icon+") no-repeat 1em";
		}
		if (this.show){
			close_btn = '<div style="position:absolute; width:'+this.mb.style.width+'; cursor:default; text-align:right; top:-18px; font-family:Arial; font-size:10px; color:#FFF">close</div>';
		}
		this.mb.innerHTML = close_btn+this.mb.msg;
	},
	
	fadein: function (){
		var fader = this;
		if (this.beginOpac < this.endOpac){
			this.beginOpac += this.step;
			if (this.ie.match(this.b.type)) {
				this.mb.style.filter = "alpha(opacity="+this.beginOpac+")";
			}else{
				this.mb.style.opacity=this.beginOpac/100;
			}
			this.fadetimer = setTimeout(function(){fader.fadein()},this.speed);
		}else{
			clearTimeout(this.fadetimer);
			if (!this.show)
			setTimeout(function (){fader.fadeout()}, this.pause);
		}
	},

	fadeout: function () {
		var fader = this;
		if (this.endOpac > 0){
			this.endOpac -= this.step;
			if (this.ie.match(this.b.type)) {
				this.mb.style.filter = "alpha(opacity="+this.endOpac+")";
			}else{
				this.mb.style.opacity=this.endOpac/100;
			}
			this.fadetimer=setTimeout(function (){fader.fadeout()}, this.speed);
		}else {
			try{
				clearTimeout(this.fadetimer);
				fader.destroy();
			}catch(e){}
		}
	},

	box_scroll: function(){
		//this.mb.style.left = (browser.getInnerWidth()/2 - parseInt(this.mb.style.width)/2)+ browser.getScrollLeft()+"px";
		if (this.left > -1){
			this.mb.style.left = parseInt(this.left) + browser.getScrollLeft()+"px";
		}else{
			this.mb.style.left = (browser.getInnerWidth()/2 - parseInt(this.mb.style.width)/2)+ browser.getScrollLeft()+"px";
		}

		if (this.top > -1){
			this.mb.style.top = parseInt(this.top) + browser.getScrollTop()+ "px";
		}else{
			this.mb.style.top = browser.getInnerHeight()/2 + browser.getScrollTop()+ "px";
		}
	},

	box_resize: function(){
		//this.mb.style.left = (browser.getInnerWidth()/2 - parseInt(this.mb.style.width)/2)+ browser.getScrollLeft()+"px";
		if (this.left > -1){
			this.mb.style.left = parseInt(this.left) + browser.getScrollLeft()+"px";
		}else{
			this.mb.style.left = (browser.getInnerWidth()/2 - parseInt(this.mb.style.width)/2)+ browser.getScrollLeft()+"px";
		}
		if (this.top > -1){
			this.mb.style.top = parseInt(this.top) + browser.getScrollTop()+ "px";
		}else{
			this.mb.style.top = browser.getInnerHeight()/2 + browser.getScrollTop()+ "px";
		}
	},

	destroy: function(){
		fader = this;
		if (typeof this.container == "undefined"){
			this.objBody.removeChild(this.mb);
		}else{
			document.getElementById(this.container).removeChild(this.mb);
		}
		//delete this.mb;
		if (this.mask == "1"){
			this.overlay.destroy();
		}
		msgAllowed = true;
		browser.removeEvent(window, "resize", function(){fader.box_resize()});
		browser.removeEvent(window, "scroll", function(){fader.box_scroll()});

		if(typeof this.finish =='function'){
			this.finish();
		}
	}
}
var Browsers = function(){
	this.b = navigator.userAgent.toLowerCase();
	this.init();
}
Browsers.prototype = {
	init: function(){
		if (this.b.indexOf('opera')!=-1) this.type = 'opera';
		else if (this.b.indexOf('msie 7')!=-1) this.type = 'ie7';
		else if (this.b.indexOf('msie')!=-1) this.type = 'ie';
		else if (this.b.indexOf('safari')!=-1) this.type = 'safari';
		else if (this.b.indexOf('gecko')!=-1) this.type ='gecko';
	},

	getScrollLeft: function(){
		return Math.max(document.body.scrollLeft, document.documentElement.scrollLeft);
	},

	getScrollTop: function(){
		return Math.max(document.body.scrollTop, document.documentElement.scrollTop);
	},

	getInnerWidth: function(){
		var ie = "(ie|ie7)";
		if (ie.match(this.type)) this.innerWidth = document.documentElement.clientWidth;
		else this.innerWidth = document.documentElement.clientWidth;
		return this.innerWidth;		
	},

	getInnerHeight: function(){
		var ie = "(ie|ie7)";
		if (ie.match(this.type)) this.innerHeight = document.documentElement.clientHeight;
		else this.innerHeight = window.innerHeight;
		return this.innerHeight;		
	},
	
	getPageWidth: function(){
		var reg = "(ie|ie7|gecko)";
		if (reg.match(this.type)) this.width = Math.max(document.documentElement.clientWidth,document.body.scrollWidth);
		else this.width = document.body.scrollWidth;
		return this.width;
	},

	getPageHeight: function(){
		var reg = "(ie|ie7)";
		if (reg.match(this.type)){
			this.width = Math.max(document.documentElement.clientHeight,document.body.scrollHeight);
		}else if (this.type == "gecko"){
			this.width = Math.max(document.documentElement.clientHeight,document.body.scrollHeight) + this.getScrollTop();
		}else this.width = document.body.scrollHeight;
		return this.width;
	},

	addEvent: function( obj, type, fn ) { 
		if ( obj.attachEvent ) { 
			obj['e'+type+fn] = fn; 
			obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
			obj.attachEvent( 'on'+type, obj[type+fn] ); 
		} else 
			obj.addEventListener( type, fn, false ); 
	},

	removeEvent: function ( obj, type, fn, funcObj ) { 
		if ( obj.detachEvent ) { 
			obj.detachEvent( 'on'+type, obj[type+fn] ); 
			obj[type+fn] = null; 
		} else {
			obj.removeEventListener( type, fn, false ); 
		}
	}
}
var browser = new Browsers();

var Mask = function(obj){
	myMask = this;
	this.objBody = document.getElementsByTagName('body')[0];
	this.overlay = document.createElement("DIV");
	if (typeof obj == "undefined") obj = {};
	this.init(obj);
}
Mask.prototype = {
	init: function(obj){
		this.overlay.style.position = "absolute";
		this.overlay.style.backgroundColor = obj.color ||  "#000000";
		reg = "(ie|ie7)";
		if (reg.match(browser.type)) {
			this.overlay.style.filter = "alpha(opacity="+(obj.alpha||65) +")";
		}else{
			this.overlay.style.opacity= obj.alpha / 100 || 0.65;
		}

		this.objBody.appendChild(this.overlay);
		this.overlay.style.zIndex = obj.zIndex || 999;
		this.overlay.style.width = browser.getPageWidth()+"px";
		this.overlay.style.height = browser.getPageHeight()+"px";
		this.overlay.style.left = 0;
		this.overlay.style.top = 0;
		this.hideHighIndex();
		browser.addEvent(window, "resize", this.resize);
		browser.addEvent(window, "scroll", this.scroll);
		this.resize();
	},

	hideHighIndex: function(){
		if (browser.type == "ie"){
			var selects = document.getElementsByTagName("select");
			for (i = 0; i < selects.length; i++) {
				selects[i].style.visibility = "hidden";
			}
		}
	},

	showHighIndex: function(){
		if (browser.type == "ie"){
			var selects = document.getElementsByTagName("select");
			for (i = 0; i < selects.length; i++) {
				selects[i].style.visibility = "visible";
			}
		}
	},
	
	resize: function(){
		myMask.overlay.style.width = browser.getPageWidth()+"px";
		myMask.overlay.style.height = browser.getPageHeight()+"px";	
	},

	scroll: function(){
		if (browser.type == "gecko"){
			myMask.overlay.style.width = browser.getPageWidth() + browser.getScrollLeft()+"px";
			myMask.overlay.style.height = browser.getPageHeight() + 100 +"px";
		}
	},
	
	destroy: function(){
		m = this;
		reg = "(ie|ie7|opera)";
		this.objBody.removeChild(this.overlay);
		browser.removeEvent(window, "resize", this.resize);
		this.showHighIndex();
	}
}