// JavaScript Document
function geTableCNTR_FilePath(){
	var script = document.getElementsByTagName("SCRIPT");
	var myUrl = script[script.length - 1].src.replace(/\\/g,"/");
	var myPath = (myUrl.lastIndexOf("/") < 0) ? "." : myUrl.substring(0,myUrl.lastIndexOf("/"));
	return myPath;
}
var tableCNTR_FilePath = geTableCNTR_FilePath();

function tableCNTR(parameter){
	//过滤参数,获得所需值,并定义为全局变量
	var el			= filterPara("el",null);								//获取将要装入容器对象
	var color		= filterPara("color","orange");							//获取容器的颜色
	var title		= filterPara("title","");								//获取容器的标题
	var div			= filterPara("div",null);								//获取是否需要透明层
	var divColor	= filterDiv("color","#cccccc");							//获取透明层颜色
	var divAlpaha	= filterDiv("alpaha","50");								//获取透明层的透明度
	var onclick		= filterPara("onclick","");
	var move		= filterPara("move","no");

	setupTable();
	
	//***************************************************************/
	//过滤参数函数
	//filterStr:正则模式的关键字 		defaultVal:找不到匹配的默认值
	//***************************************************************/
	function filterPara(filterStr,defaultVal){
		var reExec = new RegExp("\\b" + filterStr + "\\b[^;]*","i");
		var reRepl = new RegExp(filterStr + ":","gi");
		var paraName = reExec.exec(parameter);
		paraName = (paraName != null) ? paraName.toString().replace(reRepl,"") : defaultVal ;
		return paraName;
	}
	
	//***************************************************************/
	//过滤透明层的参数
	//filterType:过滤类型 		defaultVal:找不到匹配的默认值
	//***************************************************************/
	function filterDiv(filterType,defaultVal){
		var filterResult;
		if(div == null)
			filterResult = null;
		else{
			if(filterType == "color"){
				var filterColor = div.match(/#[0-9a-fA-F]{3,6}/g);
				filterResult = (filterColor != null) ? filterColor : defaultVal ;
			}else if(filterType == "alpaha"){
				var filterAlpaha = div.match(/\b\d{1,3}\b/g);
				filterResult = (filterAlpaha != null) ? filterAlpaha : defaultVal ;
			}
		}
		return filterResult;
	}
	
	//***************************************************************/
	//初始化容器表格
	//***************************************************************/
	function setupTable(){
		handleSelect("none");	//隐藏所有下拉列表框
		if(div != null){tableCNTRAlpahaDiv(divColor,divAlpaha);}
		var tableCNTR = document.getElementById(el);//获取作用层的ID
		if(!tableCNTR){return false;}
		tableCNTR.style.position = "absolute";
		tableCNTR.style.overflow = "hidden";
		setTopTableCNTR(tableCNTR);		//置顶容器
		tableCNTR.style.display = "";

		//要在容器对象不隐藏的情况下才能获取,否则均为0
		var width = tableCNTR.offsetWidth;		//获取容器对象的宽度
		var height = tableCNTR.offsetHeight;	//获取容器对象的高度
		
		var tableCNTR_Head = document.getElementById("tableCNTR_Head_" + el);
		if(tableCNTR_Head){height -= 21;}
		var tableCNTR_Bottom = document.getElementById("tableCNTR_Bottom_" + el);
		if(tableCNTR_Bottom){height -= 8;}
		
		var newLayer = new countCenter(width,height);
		tableCNTR.style.left = newLayer.left + "px";
		tableCNTR.style.top = newLayer.top + "px";

		
		var tableCNTR_Head = document.getElementById("tableCNTR_Head_" + el);
		if(tableCNTR_Head){tableCNTR.removeChild(tableCNTR_Head);}
		var createHeah = document.createElement("div");
		createHeah.id = "tableCNTR_Head_" + el;
		createHeah.innerHTML = tableHtmlHead(width,height);
		tableCNTR.insertBefore(createHeah,tableCNTR.childNodes[0]);
		
		var tableCNTR_Bottom = document.getElementById("tableCNTR_Bottom_" + el);
		if(tableCNTR_Bottom){tableCNTR.removeChild(tableCNTR_Bottom);}
		var createBottom = document.createElement("div");
		createBottom.id = "tableCNTR_Bottom_" + el;
		createBottom.innerHTML = tableHtmlBottom();
		tableCNTR.appendChild(createBottom);
	}
	
	//***************************************************************/
	//计算容器居中
	//theW:容器对象的宽度		theH:容器对象的高度
	//***************************************************************/
	function countCenter(theW,theH){
		var pageW = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;	//页面的宽度
		var pageH = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;	//页面的高度
		var scrollH = (document.documentElement) ? document.documentElement.scrollTop : document.body.scrollTop;	//下拉滚动条的高度
		this.left = (pageW - theW) / 2;
		this.top = (pageH - theH) / 2 + scrollH;
	}
	
	//***************************************************************/
	//创建半透明层
	//Color:半透明层的颜色		Opacity:不透明程度，0%(完全透明)-100%(不透明)
	//***************************************************************/
	function tableCNTRAlpahaDiv(color,opacity){
		var chkAlpahaDiv = document.getElementById("tableCNTRAlpahaDiv");
		if(chkAlpahaDiv){document.body.removeChild(chkAlpahaDiv);}	//如果已经创建半透明层,则删之
		var ie0pacity = String(opacity);
		var ffOpacity = String(opacity / 100);
		var pageW = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;	//页面的宽度
		var pageH = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;	//页面的高度
		pageH = (document.body.scrollHeight <= pageH) ? pageH : document.body.scrollHeight;
		var alpahaDiv = document.createElement("div");
		alpahaDiv.id = "tableCNTRAlpahaDiv";
		alpahaDiv.style.position = "absolute";
		alpahaDiv.style.left = "0px";
		alpahaDiv.style.top = "0px";
		alpahaDiv.style.background = color;
		if(document.all){
			alpahaDiv.style.filter = "alpha(opacity=" + String(opacity) + ")";
		}else{
			alpahaDiv.style.opacity = String(opacity / 100);
		}
		alpahaDiv.style.zIndex = "109";
		alpahaDiv.style.width = pageW + "px";
		alpahaDiv.style.height = pageH + "px";
		document.body.appendChild(alpahaDiv);
	}
	
	//***************************************************************/
	//容器外框头部
	//width:容器对象的宽度		height:容器对象的高度
	//***************************************************************/
	function tableHtmlHead(width,height){
		height += 500;
		var moveFuc = (move == "yes") ? "cursor:move;\" onmousedown=\"moveTableCNTR(event,this);\"" : ";\"" ;
		var tableStr = "";
		tableStr += "<div style=\"height:22px;background:url(" + tableCNTR_FilePath + "/" + color + "/table01.gif) left repeat-y;" + moveFuc + ">";
		tableStr += "<div style=\"height:100%;background:url(" + tableCNTR_FilePath + "/" + color + "/table03.gif) right repeat-y;margin:0 0 0 5px;\">";
		tableStr += "<div style=\"height:100%;background:url(" + tableCNTR_FilePath + "/" + color + "/table02.gif);margin:0 5px 0 0;\">";
		tableStr += "    <samp style=\"float:left;margin-left:5px;line-height:22px;font-size:12px;font-weight:bolder;color:#fff;\">" + title + "</samp>";
		tableStr += "    <samp style=\"float:right;width:11px;height:12px;margin:5px 8px 0 0;overflow:hidden;background:url(" + tableCNTR_FilePath + "/" + color + "/table06.gif) no-repeat;cursor:pointer;\" onclick=\"delTableCNTR('" + el + "');" + onclick + "\"></samp>";
		tableStr += "</div>";
		tableStr += "</div>";
		tableStr += "</div>";
		tableStr += "<div style=\"position:absolute;width:" + (width - 1) + "px;height:" + height + "px;z-index:-110;background:url(" + tableCNTR_FilePath + "/" + color + "/table04.gif) left bottom repeat-y;\"></div>";
		tableStr += "<div style=\"position:absolute;width:" + (width - 1) + "px;height:" + height + "px;z-index:-110;background:url(" + tableCNTR_FilePath + "/" + color + "/table05.gif) right bottom repeat-y;margin-left:1px;background-color:#ffffff;\"></div>";
		return tableStr;
	}
	
	//***************************************************************/
	//容器外框尾部
	//***************************************************************/
	function tableHtmlBottom(){
		var moveFuc = (move == "yes") ? "cursor:move;\" onmousedown=\"moveTableCNTR(event,this);\"" : ";\"" ;
		var tableStr = "";
		tableStr += "<div style=\"height:7px;background:url(" + tableCNTR_FilePath + "/" + color + "/table07.gif) left repeat-y;overflow:hidden;" + moveFuc + ">";
		tableStr += "<div style=\"height:100%;background:url(" + tableCNTR_FilePath + "/" + color + "/table09.gif) right repeat-y;margin:0 0 0 5px;overflow:hidden;\">";
		tableStr += "<div style=\"height:100%;background:url(" + tableCNTR_FilePath + "/" + color + "/table08.gif);margin:0 5px 0 0;overflow:hidden;\"></div>";
		tableStr += "</div>";
		tableStr += "</div>	";
		return tableStr;
	}
	
	//***************************************************************/
	//处理下拉列表框
	//content:容器所要装载的内容
	//***************************************************************/
	function  handleSelect(state){
		var selects = document.getElementsByTagName("SELECT");
		if(!selects){return;}
		for(var i = 0; i < selects.length; i++)
			selects[i].style.display = state;
	}
}


function delTableCNTR(el){
	handleSelect("");	//显示所有下拉列表框
	var chkTableCNTR = document.getElementById(el);//获取用户登录层的ID
	if(chkTableCNTR)
		chkTableCNTR.style.display = "none";
	tableCNTRAlpahaDiv();
	
	//***************************************************************/
	//处理下拉列表框
	//content:容器所要装载的内容
	//***************************************************************/
	function  handleSelect(state){
		var selects = document.getElementsByTagName("SELECT");
		if(!selects){return;}
		for(var i = 0; i < selects.length; i++)
			selects[i].style.display = state;
	}
	
	function tableCNTRAlpahaDiv(){
		var chkAlpahaDiv = document.getElementById("tableCNTRAlpahaDiv");
		if(chkAlpahaDiv){document.body.removeChild(chkAlpahaDiv);}	//如果已经创建半透明层,则删之
	}
}


function moveTableCNTR(e,obj){
	var e = e ? e : event;
	if(document.all){	//(IE浏览器)判断鼠标是否按住左键
		if(e.button != 1){return false;}
	}else{				//(火狐浏览器)判断鼠标是否按住左键
		if(e.button != 0){return false;}
	}
	var nowX = e.clientX;
	var nowY = e.clientY;

	var moveDiv = obj.parentNode.parentNode;
	if(!moveDiv){return false;}
	
	setTopTableCNTR(moveDiv);
	var maxX = (document.body.scrollWidth - moveDiv.offsetWidth);	//容器对象的可移动范围(宽度)
	var pageH = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
	pageH = (document.body.scrollHeight <= pageH) ? pageH : document.body.scrollHeight;
	var maxY = (pageH - moveDiv.offsetHeight);	//容器对象的可移动范围(高度)

	//创建事件
	obj.onmouseup = function(){moveTableCNTR_Clear();}
	document.onmouseup = function(){moveTableCNTR_Clear();}
	document.onmousemove = function(e){
		var e = e ? e : event;
		var x = e.clientX;
		var y = e.clientY;

		var moveX = parseInt(moveDiv.style.left) + (x - nowX);	//容器对象的移动横坐标
		var moveY = parseInt(moveDiv.style.top) + (y - nowY);	//容器对象的移动纵坐标

		//计算是否超出可移动范围(宽度)
		if(moveX < 0){moveX = 0;}
		if(moveX > maxX){moveX = maxX;}
		//计算是否超出可移动范围(高度)
		if(moveY < 0){moveY = 0;}
		//if(moveY > maxY){moveY = maxY;}

		moveDiv.style.left = moveX + "px";
		moveDiv.style.top = moveY + "px";
		nowX = x;
		nowY = y;
	}

	//清除所创建的事件
	function moveTableCNTR_Clear(){
		document.onmousemove = null;
		obj.onmouseup = null;
		document.onmouseup = null;
	}
}

function setTopTableCNTR(setobj){
	var divs = document.getElementsByTagName("DIV");
	var zIndexMax = 0;
	var nowIndex;
	//搜索所有漂浮的层,并获取zIndex最大的值
	for(var i = 0; i < divs.length; i++){
		if(divs[i].style.position == "absolute" && divs[i].style.zIndex != "" && parseInt(divs[i].style.zIndex) >= 110){
			nowIndex = parseInt(divs[i].style.zIndex);
			if(nowIndex > zIndexMax)
				zIndexMax = nowIndex;
		}
	}
	setobj.style.zIndex = (zIndexMax == 0) ? 110 : zIndexMax + 1;
}
