﻿// 滤镜效果
function LayerFilter() {

	var layer = new Array();
	var objDrag = new Array();
	var objDragId = new Array();
	
	var objX = 0;
	var objY = 0;

	// 记录body的相关初始
	var eventMouseMove = document.body.onmousemove;
	var eventMouseUp = document.body.onmouseup;
	this.eventBody = function() {
		document.body.onmousemove = eventMouseMove;
		document.body.onmouseup = eventMouseUp;
	};
	
	// 初始化层
	this.initLayer = function() {
		var argArray = arguments;
		
		// 定义层需要的初始字串变量
		var objHtml = "";
		for(var i=0; i<argArray.length; i++) {
			// 如果已存在该层则直接跳过
			if( $(argArray[i])!=null ) return false;
			
			// 求出其相应的对象
			/*
			switch(argArray[i]) {
				case "popupArea" :					
					objHtml = popupArea.initialize(); 
					break;
				case "popupJob" : 
					objHtml = popupJob.initialize();
					break;
				//case "popupCalling" : objHtml = this.initCalling(); break;
				default : return false;
			}
			*/
			objHtml = eval(argArray[i] + ".initialize()")
			objDragId[i] = argArray[i];
			layer[i] = document.createElement("div");
			layer[i].id = argArray[i];
			layer[i].name = argArray[i];
			layer[i].style.visibility = "hidden";
			layer[i].style.zIndex = "999";
			layer[i].innerHTML += objHtml;
			document.body.appendChild(layer[i]);
			
			var styleWidth = layer[i].firstChild.clientWidth + "px";
			var styleHeight = layer[i].firstChild.clientHeight + "px";
			var layerId = argArray[i];
			
			layer[i] = this.setLayerStyle(layer[i], layerId, "0px", "0px", styleWidth, "0px", "absolute", "0px solid #C0D4DB", "default", "#FFF");
			if( isIE() ){
				layer[i].innerHTML += '<iframe src="about:blank" style="position:absolute;z-index:-1;left:0;top:0;width:' + layer[i].scrollWidth.toString() + 'px;height:' + layer[i].scrollHeight.toString() + 'px;"></iframe>';
			}
			objDrag[i] = layer[i].firstChild.firstChild;
			objDrag[i].style.cursor = "move";
			
			
			objDrag[i].onmousedown = function(event) {
				mouseDragDown(event, this.parentNode.parentNode);
			};
			objDrag[i].onselectstart = function() { return false; };
			
			if( isIE() ) {
				layer[i].firstChild.onresize = function() { checkAndResetLayerTop(this.parentNode); };
			}
			else {
				layer[i].firstChild.onclick = function() { checkAndResetLayerTop(this.parentNode); };
			}
		}
	};
	
	// 隐藏其它层
	this.hideOtherLayer = function(objId) {
		for(var i=0; i<objDragId.length; i++) {
			if( objDragId[i] != objId && $(objDragId[i]) != null ) {
				this.hideLayer(objDragId[i]);
			}
		}
	};
	
	// 隐藏层
	this.hideLayer = function() {
		var argArray = arguments;
		for(var i=0; i<argArray.length; i++) {
			if( $(argArray[i])!=null ) {
				$(argArray[i]).style.visibility = "hidden";
				this.hideElementAll(argArray[i]);
			}
		}
	};
	
	// 显示层
	this.displayLayer = function(objId) {
		if( $(objId)!=null ) {
			var obj = $(objId);
			this.hideOtherLayer(objId);
			if( obj.style.visibility == "hidden" ) {
				this.setLayerPosition(obj);
				this.showElementAll(objId);
				obj.style.visibility = "visible";
			}
			
			document.body.onmousemove = function(event) { mouseDrag(event, obj); };
			document.body.onmouseup = function(event) { objX=objY=0; };
		}
	};
	
	// 当前页面尺寸
	var bodyScrollWidth = 0;
	var bodyScrollHeight = 0;	
	function getBodySize() {
		bodyScrollWidth = document.body.scrollWidth;
		bodyScrollHeight = document.body.scrollHeight;
	};
		
	// 设置对象属性
	this.setLayerStyle = function(obj, id, top, left, width, height, position, border, cursor, background) {
		obj.id = id?id:null;
		obj.style.top = top?top:"0px";
		obj.style.left = left?left:"0px";
		obj.style.width = width?width:"0px";
		obj.style.height = height?height:"0px";
		obj.style.position = position?position:"static";
		obj.style.border = border?border:"1px #000 solid";
		obj.style.cursor = cursor?cursor:"default";
		obj.style.background = background?background:"";
		
		return obj;
	};
	
	// 建立渐变层
	this.buildLayer = function() {
		getBodySize();
		var obj = document.createElement("div");
		obj.id = "globalLayer";
		obj.style.display = "none";
		obj.style.zIndex = "98";
		obj = this.setLayerStyle(obj, "globalLayer", "0px", "0px", bodyScrollWidth+"px", isIE()?bodyScrollHeight+"px":bodyScrollHeight+16+"px", "absolute", "#333 0px solid", "default", "darkgray");
		obj.onselectstart = function() { return false; }
		if( isIE() )
			obj.style.filter = "alpha(opacity=30)";
		else
			obj.style.opacity = 30/100;
		document.body.appendChild(obj);
	};
	
	// 显示渐变层并隐藏元素
	this.hide = function() {		
		this.synSizeByBody("globalLayer");
		$("globalLayer").style.display = "block";
		if( isIE() ) {
			this.hideElementAll();
		}
	};
	
	// 根据页面调整元素尺寸
	this.synSizeByBody = function() {
		getBodySize();
		var argArray = arguments;
		for(var i=0; i<argArray.length; i++) {
			if( $(argArray[i]) != null ) {
				$(argArray[i]).style.width = (bodyScrollWidth) + "px";
				$(argArray[i]).style.height = (bodyScrollHeight) + "px";
			}
		}
	};
	
	// 隐藏所有元素
	this.hideElementAll = function() {
		this.hideElement("select");
		this.hideElement("object");
		this.hideElement("iframe");
	};
	
	// 显示所有元素
	this.showElementAll = function() {
		this.showElement("select");
		this.showElement("object");
		this.showElement("iframe");
	};
	
	// 根据标签名称隐藏元素
	this.hideElement = function(tag) {
		return
		try {
			var obj = document.getElementsByTagName(tag);

			for(var i=0; i<obj.length; i++) {
				obj[i].style.visibility = "hidden";
			}
		}catch(e) {
			alert(e.message);
		}
	};
	
	// 根据标签名称显示元素
	this.showElement = function(tag) {
		return
		try {
			var obj = document.getElementsByTagName(tag);
				
			for(var i=0; i<obj.length; i++) {
				obj[i].style.visibility = "visible";
			}
		}catch(e) {
			alert(e.message);
		}
	};
	
	// 关闭层
	this.cancel = function() {
		$("globalLayer").style.display = "none";
		if( isIE() ) {
			this.showElementAll();
		}
		this.eventBody();
	};
	
	// 将层的位置定位在body可见区域中央
	this.setLayerPosition = function(obj) {
		obj.style.display = "block";
		var styleWidth = obj.style.width.substring(0, obj.style.width.length-2);
		var clientHeight = obj.firstChild.clientHeight;
		var objLeft = parseInt(document.body.scrollLeft + (document.body.clientWidth - styleWidth)/2) + "px";
		var relTop = (document.documentElement.clientHeight-clientHeight)/2 > 0 ? (document.documentElement.clientHeight-clientHeight)/2 : 0;
		var objTop = parseInt(document.documentElement.scrollTop + relTop) + "px";
		obj.style.top = objTop;
		obj.style.left = objLeft;
		checkAndResetLayerTop(obj);
	};
	
	// 重新定位层位置
	function checkAndResetLayerTop(obj) {
		var clientHeight = obj.firstChild.clientHeight;
		var styleTop = parseInt(obj.style.top.substring(0, obj.style.top.length-2));
		if( clientHeight+styleTop>bodyScrollHeight ) {
			obj.style.top = (bodyScrollHeight - clientHeight) + "px";
		}
	};
	
	// 鼠标拖动
	function mouseDrag(event, obj) {
		if( objX!=0 && objY!=0 ) {
			if( event==null ) {// IE
				event = window.event;
			}
			if( event.button==1 || event.button==0 ) {
				var objWidth = obj.firstChild.clientWidth;
				var objHeight = obj.firstChild.clientHeight;
				getBodySize();
				var leftPo = event.clientX - objX;
				if( leftPo < 0 ) leftPo = 0;
				if( leftPo > bodyScrollWidth - objWidth ) leftPo = bodyScrollWidth - objWidth;
				
				var topPo = event.clientY - objY;
				if( topPo < 0 ) topPo = 0;
				if( topPo > bodyScrollHeight - objHeight ) topPo = bodyScrollHeight - objHeight;
				
				obj.style.left = leftPo + "px";
				obj.style.top = topPo + "px";
			}
		}
	}
	
	function mouseDragDown(event, obj) {
		var objLeft = obj.style.left;
		var objTop = obj.style.top;
		objLeft = objLeft.replace(/p|x/g,"");
		objTop = objTop.replace(/p|x/g,"");
		if( event==null ) {// IE
			event = window.event;
		}
		var clientX = String(event.clientX).replace(/p|x/g,"");
		var clientY = String(event.clientY).replace(/p|x/g,"");
		objX = clientX - objLeft;
		objY = clientY - objTop;
	}
}