﻿
/**
lightBox
如需使用，先initLightBox()进行初始化
开启方法：lightBox.show(title,fn) title：窗口显示的标题 fn，显示完成后执行的函数
关闭方法：lightBox.hidden(fn) fn:窗口关闭执行的函数
其他说明：
lightBox效果与数据毫不相关，数据放入show和hidden方法的fn参数内自行定义
同一页面同时只能有一个窗口
//并可以每次设置不同的主体内容
**/
function initLightBox(){
	window.lightBox = {
			bg:document.getElementById("lightBox_bg"),
			title:document.getElementById("lightBox_title"),
			cont:document.getElementById("lightBox"),
			mask:document.getElementById("lightBox_mask") || null,
			text:document.getElementById("lightBox_text"),
			model:{},
			currModel:"",
			fresh:false,
			_w:false,
			isIE6:(navigator.appVersion.indexOf('MSIE') != -1) && (parseFloat(navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE')+5,3))) <= 6 ? true : false,
			init:function(){
				if(this.isIE6){
					this.bg.innerHTML = "<iframe id='lightBox_mask'></iframe>";
					this.mask = document.getElementById("lightBox_mask");
				}
				this.bg.ondblclick = function(){lightBox.hidden()};
			},
			addModel:function(name,title,dom,showFn,closeFn){
				this.model[name] = {title:title,dom:dom,onShow:showFn,onClose:closeFn,o:null};
			},
			set:function(dom){
				this.text.innerHTML = "";
				if(typeof dom == "string" && dom != "undefined"){
					this.text.innerHTML = dom;
					return;
				}
				if(dom && dom.nodeType == 1){
					var temp = document.createElement("span");
						temp.innerHTML = dom.innerHTML;
						this.text.appendChild(temp);
						temp = null;
				}

			},
			show:function(model,o,fn){
				this.currModel = model;
				this.cont.style.display = "block";
				this.bg.style.display = "block";
				if(!this._w){
					this._w = this.cont.offsetWidth;
				}
				this.title.innerHTML = this.model[model]["title"] || "提示消息";
				this.set(this.model[model]["dom"]);
				this.doPos();
				if(o){
					this.model[model]["o"] = o;	
				}
				if(this.model[model]["onShow"]){
					this.model[model]["onShow"].call(this,o);
				}
				if(fn){
					fn.call(this);
				}
			},
			hidden:function(fn){
				this.cont.style.display = "none";
				this.bg.style.display = "none";
				if(fn){
					fn.call(this);
				}
				if(this.fresh){
					location.href = location.href.replace(/(.*)#.*/,"$1");
					this.fresh = false;
				}
				if(this.model[this.currModel]["onClose"]){
					this.model[this.currModel]["onClose"].call(this,this.model[this.currModel]["o"]);
				}
			},
			doPos:function(){
				var w = Math.max(document.documentElement.scrollWidth,document.documentElement.clientWidth)+"px";
				var h = Math.max(document.documentElement.scrollHeight,document.documentElement.clientHeight)+"px";
				this.bg.style.width = w;
				this.bg.style.height = h;
				this.doContPos();
			},
			doContPos:function(){
				var cont_w = (document.documentElement.clientWidth - this.cont.offsetWidth)/2;
				var cont_h = (document.documentElement.clientHeight - this.cont.offsetHeight)/2;
				if(this.isIE6){
					cont_h += document.documentElement.scrollTop;
				}
				this.cont.style.left = cont_w + "px";
				this.cont.style.top = cont_h + "px";
			},
			setWidth:function(w){
				var w1= w || this._w;
				this.cont.style.width = w1 + "px";
				if(w){
					this.doContPos();
				}
				
			}
	};
	lightBox.init();
}


