// JavaScript Document

/**
 * <p>Title: alert js</p>
 * <p>Description: 弹出层通用JS</p>
 * <p>Copyright: Copyright(c)2009</p>
 * <p>Company: 360quan</p>
 *
 * @author: Phoebus
 * @version: 1.0.14
 * @date: 2009-6-1
 * @comment:
 */

ph$.alet = {
	/**
	 * 构造弹出层
	 * @param object
	 */
	build : function(options) {
		// include css files
		ph$.alet.include_css("http://image.360quan.cn/css/alert/alert_page.css");
		var el = this;
		// if parame is obj, site config value
		if (typeof(options) == "object") {
			el.cfg = {
				id : options.id,			//弹出层id
				title : options.title,		//顶部标题
				clos : options.clos,		//右上角关闭开关的文本
				head : options.head,		//第一行大标题名，也可以作为提示
				img : options.img,			//图标
				cont : options.cont, 		//弹出框内容
				btn : options.btn,			//确认按钮，为数组，最高二维
				width : options.width,		//弹出层宽度
				visiby : options.visiby,	//是否隐藏，默认为显示
				shadow : options.shadow		//阴影效果
			};
		} else {
			// if parame is string, parame is the alert message
			el.cfg = {
				id : "p_alert",
				title : "提示",
				clos : "关闭",
				head : options,
				btn : "确定",
				visiby : "visible",
				shadow : 2
			};
		};
		var neo = {
			root : "",
			shadow : "",
			title : "",
			clos : "",
			head : "",
			img : "",
			cont : "",
			btn_box : "",
			btn : ""
		};
		// the confit id is not null
		if (el.cfg.id) {
			// if the alert exist
			if (document.getElementById(el.cfg.id)) {
				var tarcont = [];
				tarcont.par = document.getElementById(el.cfg.id);
				tarcont.list = tarcont.par.getElementsByTagName("div");
				for (var i=0; i<tarcont.list.length; i++) {
					if (tarcont.list[i].className == "layer_head") {
						tarcont.list = tarcont.list[i];
						break;
					}
				}
				// change the alert message
				tarcont.list.innerHTML = el.cfg.head;
				// show the alert layer
				ph$.alet.show(tarcont.par);
				return false;
			}
		}
		//create alert main layer
		neo.root = document.createElement("div");
		// set styles
		neo.root.className = "alert_layer";
		// set the width
		if (el.cfg.width) {
			neo.root.style.width = el.cfg.width + "px";
			neo.root.style.marginLeft = (-1 * (el.cfg.width)/2) + "px";
		} else {
			neo.root.style.width = "300px";
			neo.root.style.marginLeft = (-1 * (300)/2) + "px";
		}
		// set top is IE6
		if (window.XMLHttpRequest) {
			neo.root.style.top = document.documentElement.clientHeight > 300 ? (document.documentElement.clientHeight - 200) / 2 + "px" : "150px";
		}
		neo.root.style.zIndex = 2000;
		// show or hidden
		if (el.cfg.visiby) {
			neo.root.style.visibility = el.cfg.visiby;
		} else {
			neo.root.style.visibility = "visible";
		}
		// is id, insert it
		if (el.cfg.id) {
			neo.root.setAttribute("id", el.cfg.id);
		}
		// insert obj
		document.body.appendChild(neo.root);
		// is true, create layer title
		if (el.cfg.title) {
			neo.title = document.createElement("div");
			neo.title.className = "layer_title";
			//is true, create button of close
			if (el.cfg.clos) {
				neo.clos = document.createElement("a");
				neo.clos.href = "#";
				neo.clos.className = "title_btn";
				neo.clos.innerHTML = el.cfg.clos;
				// the close btn's click event
			    neo.clos.onclick  = function() {
					ph$.alet.hidden(neo.root, neo.shadow);
					return false;
				};
				//append button of close
				neo.title.appendChild(neo.clos);
			}
			// set the object title, and inset them
			neo.title.appendChild(document.createTextNode(el.cfg.title));
			// insert obj
			neo.root.appendChild(neo.title);
		}
		//is true, create layer for head
		if (el.cfg.head) {
			neo.head = document.createElement("div");
			neo.head.className = "layer_head";
			//is true, create icon
			if (el.cfg.img) {
				neo.img = document.createElement("img");
				neo.img.src = el.cfg.img;
				//append icon
				neo.head.appendChild(neo.img);
			}
			// insert text
			neo.head.innerHTML += el.cfg.head;
			// insert obj
			neo.root.appendChild(neo.head);
		}
		//is true, create layer of content
		if (el.cfg.cont) {
			neo.cont = document.createElement("div");
			neo.cont.className = "layer_cont";
			// target is obj? and insert it
			if (typeof(el.cfg.cont) == "object") {
				if (el.cfg.cont.style.display = "none") {
					el.cfg.cont.style.display = "";
				}
				// insert object
				neo.cont.appendChild(el.cfg.cont);
			} else {
				// is id? set it object
				var target = document.getElementById(el.cfg.cont);
				if (target) {
					if (target.style.display = "none") {
						target.style.display = "";
					}
					neo.cont.appendChild(target);
				} else {
					// just text, set them
					neo.cont.innerHTML = el.cfg.cont;
				}
			};
			// insert obj
			neo.root.appendChild(neo.cont);
		}
		// set the button
		if (el.cfg.btn) {
			// is true, create layer of content
			neo.btn_box = document.createElement("div");
			neo.btn_box.className = "layer_btn";
			// is object
			if (ph$.alet.isArray(el.cfg.btn)) {
				for (var i=0; i<el.cfg.btn.length; i++) {
					neo.btn = document.createElement("input");
					neo.btn.type = "button";
					// btn text is null? set the [ok] text of default
					neo.btn.value = el.cfg.btn[i][0] ? el.cfg.btn[i][0] : "\u786e\u5b9a";
					neo.btn.className = "btn_input";
					// is not disable
					if (neo.btn.className != "btn_disable") {
						// act style when the mouseover
						neo.btn.onmouseover = function() {
							this.className = 'btn_over';
						};
						// default style
						neo.btn.onmouseout = function() {
							this.className = 'btn_input';
						};
					}
					// just has the event
					if (el.cfg.btn[i][1]) {
						neo.btn.eval = el.cfg.btn[i][1];
						// button click event
						neo.btn.onclick = function() {
							// the param was a function
							if (this.eval instanceof Function) {
								this.eval();
							} else if (this.eval.indexOf("(") < 0) {
								// spell and eval the function
								eval(this.eval + "()");
							} else {
								// eval the function
								eval(this.eval);
							}
							ph$.alet.hidden(neo.root, neo.shadow);
							return false;
						};
					}
					// the event just null
					if (!el.cfg.btn[i][1]) {
						// the click event is the hidden the layer
						neo.btn.onclick = function() {
							ph$.alet.hidden(neo.root, neo.shadow);
							return false;
						};
					}
					// insert obj
					neo.btn_box.appendChild(neo.btn);
				}
			} else {
				// just pass a text
				// create a new element of input
				neo.btn = document.createElement("input");
				// set styles
				neo.btn.type = "button";
				// the param was the value text
				neo.btn.value = el.cfg.btn;
				neo.btn.className = "btn_input";
				// two styles
				if (neo.btn.className != "btn_disable") {
					neo.btn.onmouseover = function() {
						this.className = 'btn_over';
					};
					neo.btn.onmouseout = function() {
						this.className = 'btn_input';
					};
				}
				// click event
				neo.btn.onclick = function() {
					ph$.alet.hidden(neo.root, neo.shadow);
					return false;
				}
				// insert obj
				neo.btn_box.appendChild(neo.btn);
			}
			// insert obj
			neo.root.appendChild(neo.btn_box);
		}
		
		// shadow layer
		if (el.cfg.shadow || el.cfg.visiby == "visible") {
			// not null, show it
			if (document.getElementById("alert_shadow")) {
				document.getElementById("alert_shadow").style.visibility = "visible";
			} else if (el.cfg.shadow > 0) {
				// create a new element
				neo.shadow = document.createElement("div");
				// set id, only one
				neo.shadow.id = "alert_shadow";
				// set alpha
				neo.shadow.style.filter = "alpha(opacity=50)";
				neo.shadow.style.position = "absolute";
				neo.shadow.style.opacity = 0.5;
				neo.shadow.style.zIndex = 1999;
				// shadow style
				if (el.cfg.shadow == 1) {
					// style 1, follow shadow
					neo.shadow.style.width = neo.root.offsetWidth + "px";
					neo.shadow.style.height = neo.root.offsetHeight + "px";
					neo.shadow.style.top = (neo.root.offsetTop + 2) + "px";
					neo.shadow.style.left = (neo.root.offsetLeft + 2) + "px";
					neo.shadow.style.backgroundColor = "#000";
				} else if (el.cfg.shadow > 1) {
					// style 2&3, all screen
					neo.shadow.style.top = 0;
					neo.shadow.style.left = 0;
					neo.shadow.style.width = document.body.clientWidth + "px";
					neo.shadow.style.height = document.documentElement.clientHeight < document.body.clientHeight ?  document.body.clientHeight + "px" : document.documentElement.clientHeight + "px"
					if (el.cfg.shadow == 2) {
						// style 2 colour is the black 
						neo.shadow.style.backgroundColor = "#000";
					} else if (el.cfg.shadow == 3) {
						// style 2 colour is the white 
						neo.shadow.style.backgroundColor = "#FFF";
					}
				}
				// insert the shadow
				document.body.appendChild(neo.shadow);
			}
		};
		return false;
	},
	/**
	 * 隐藏弹出层
	 * @param object, 要隐藏的目标弹出层
	 * @param object/string, 背景层对象/是否保留背景层不隐藏
	 */
	hidden : function(obj, obj2) {
		if (typeof(obj) != "object") {
			obj = document.getElementById(obj);
		}
		obj.style.visibility = "hidden";
		if (obj2 == null || obj2 == "") {
			obj2 = document.getElementById("alert_shadow");
		}
		if (obj2 != "keep") {
			if (!obj2) {
				obj2 = document.getElementById("alert_shadow");
			}
			obj2.style.visibility = "hidden";
		}
	},
	/**
	 * 移除弹出层
	 * @param object, 要移除的目标弹出层
	 * @param object/string, 背景层对象/是否保留背景层不隐藏
	 */
	remove : function(obj, obj2) {
		if (typeof(obj) != "object") {
			obj = document.getElementById(obj);
		}
		obj.parentNode.removeChild(obj);
		if (obj2 != "keep") {
			if (!obj2) {
				obj2 = document.getElementById("alert_shadow");
			}
			obj2.style.visibility = "hidden";
		}
	},
	/**
	 * 显示弹出层
	 * @param object, 要显示的目标弹出层
	 * @param object/string, 背景层对象/是否强制背景层不显示
	 */
	show : function(obj, obj2) {
		if (typeof(obj) != "object") {
			obj = document.getElementById(obj);
		}
		obj.style.visibility = "visible";
		if (obj2 != "blank") {
			if (!obj2) {
				obj2 = document.getElementById("alert_shadow");
			}
			obj2.style.visibility = "visible";
		}
	},
	/**
	 * 嵌入js
	 * @param file, 目标js文件的完整路径
	 */
	include_js : function(file) {
		var html_doc = document.getElementsByTagName('head')[0];
		js = document.createElement('script');
		js.setAttribute('type', 'text/javascript');
		js.setAttribute('src', file);
		html_doc.appendChild(js);
	 
		js.onreadystatechange = function () {
			if (js.readyState == 'complete') {
				//alert('JS onreadystate fired');
			}
		}
	 
		js.onload = function () {
			//alert('JS onload fired');
		}
		return false;
	},
	/**
	 * 嵌入css
	 * @param file, 目标css文件的完整路径
	 */
	include_css : function(css_file) {
		var html_doc = document.getElementsByTagName('head')[0];
		css = document.createElement('link');
		css.setAttribute('rel', 'stylesheet');
		css.setAttribute('type', 'text/css');
		css.setAttribute('href', css_file);
		html_doc.appendChild(css);
	 
		// alert state change
		css.onreadystatechange = function () {
			if (css.readyState == 'complete') {
				//alert('CSS onreadystatechange fired');
			}
		}
		css.onload = function () {
			//alert('CSS onload fired');
		}
		return false;
	},
	/**
	 * 检测目标是否是数据
	 * @param tar
	 * @return bool
	 */
	isArray : function(obj) {
		return Object.prototype.toString.call(obj) === '[object Array]';
	},
	/**
	 * 复制文本到粘贴板
	 * @param string/
	 * @return bool
	 */
	copyToClipboard : function(txt) {
		if(window.clipboardData) {
			window.clipboardData.clearData();
			window.clipboardData.setData("Text", txt);
		} else if(navigator.userAgent.indexOf("Opera") != -1) {
			window.location = txt;
		} else if (window.netscape) {
			try {
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			} catch (e) {
				alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");
				return false;
			}
			var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
			if (!clip)
				return;
			var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
			if (!trans)
				return;
			trans.addDataFlavor('text/unicode');
			var str = new Object();
			var len = new Object();
			var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
			var copytext = txt;
			str.data = copytext;
			trans.setTransferData("text/unicode",str,copytext.length*2);
			var clipid = Components.interfaces.nsIClipboard;
			if (!clip)
				return false;
			clip.setData(trans,null,clipid.kGlobalClipboard);
		}
		//alert("Copy Succeed!")
	}
};

/**
 * 重新封装成简单方式调用
 * @param 弹出信息
 */
var p_alert = function(msg) {
	p_alert.o = document.getElementById("p_alert") ? document.getElementById("p_alert") : null;
	if (p_alert.o) {
//		ph$.alet.show(p_alert.o);
		ph$.alet.build(msg);
	} else {
		p_alert.b = new ph$.alet.build(msg);
	}
}

