// JavaScript Document
/* 
 * "div_"+uitl_int span元素ID
 * showId+"_title" td ID，功能设置窗口标题
 * "div_"+uitl_int+"_content" tr ID,显示内容的容器
 * showId div元素ID,存放显示的内容;
 * "div_"+uitl_int+"_button" div元素ID ,存放窗口中关闭，最大化，最小化按钮
 * "div_"+uitl_int+"_img" img元素ID,最大化按钮的图标，改变图标可以使用他的属性来完成
 * 注:最小化的功能完成，需要在当前页面中body标签下加入<div id="tray" style="position:absolute;bottom:0px;width:100%;height:25px;z-index:1;"></div>
 *
 * 设置菜单 格式 [菜单标题]|[菜单动作]$.... 
 * 函数实现 setMenu([菜单标题]|[菜单动作]$....) 如果标题没有任务动作，将菜单动作设为0，如果为分符线，可设为1;
 */
//----------------------------------------------------------------------------------------------------------
var uitl_int = 0;
var prevElement = null;
var uitlfocus = 0;
//初始化
function init()
{
	document.body.insertAdjacentHTML("afterBegin","<div id=\"tray\" style=\"position:absolute;bottom:0px;width:100%;height:25px;z-index:1;direction:rtl;\"></div>");
	document.body.scroll="no";
}
//打开窗口
function openWindow(object,winId,title,arg)
{
	if(document.getElementById(winId)==null)
		setWindow(object,winId,title,arg);
	else
	{
		Focus(getDiv(winId));
		setTitle(winId,title);
		setContent(winId,object);
	}
	uitlfocus = 1;
}
//设置窗口
function setWindow(object,winId,title,arg)//界面的内容(object||string),窗口ID，窗口标题,参数
{
	//参数设置
	var element = null;
	var showId = "unknown_"+uitl_int;
	var width = 300;
	var height = 400;
	var left = -1;
	var top = -1;
	//判断参数
	if(title == "")
		title = "末命名-"+uitl_int;
	if(winId != undefined && winId != "")
		showId = winId;
	if(arg != undefined && arg != "")
	{	
		var tmparr = arg.split(",");
		for(j=0;j<tmparr.length;j++)
		{
			eval(tmparr[j])	;
		}
	}
	if(top == -1)
		top = (document.body.clientHeight-height)/2;
	if(left == -1)
		left = (document.body.clientWidth-width)/2;
	//创建层
	element = document.createElement("span");
	element.id = "div_"+uitl_int;
	element.style.position = "absolute";
	element.style.left = left;
	element.style.top = top;
	element.style.width = width;
	element.style.height = height;
	element.style.zIndex = uitl_int;
	element.onclick = new Function("Focus(this)");
	element.onmousemove = new Function("calcResize(this)");
	element.onmousedown = new Function("setWinSize(this)");
	element.oncontextmenu = new Function("return _menu(this)");
	//设置最大化时变化
	element.setAttribute("tempWidth","0px");//最大化前宽度
	element.setAttribute("tempHeight","0px");//最大化前高度
	element.setAttribute("tempTop","0px");//最大化前top位置
	element.setAttribute("tempLeft","0px");//最大化前left位置
	element.setAttribute("isMax","0");//是否最大化,1-是 0-否
	element.setAttribute("isMin","0");//是否最大化,1-是 0-否
	element.setAttribute("winId",showId);//窗口ID
	//创建表格
	var tmpTable = document.createElement("table");
	tmpTable.style.width="100%";
	tmpTable.style.height="100%";
	//tmpTable.border="2";
	tmpTable.className = "windowTable";
	tmpTable.cellPadding="0";
	tmpTable.cellSpacing="0";
	//插入一行
	tmpTable.insertRow();
	//插入单元格
	tmpTable.rows[0].insertCell();
	//设置单元格属性
	tmpTable.rows[0].cells[0].style.height="21";
	tmpTable.rows[0].cells[0].style.border="#319AFF"; 
	//在单元格里创建表格
	var temp = document.createElement("table");
	temp.style.width="100%";
	temp.style.height="100%";
	temp.border="0";
	temp.cellPadding="0";
	temp.cellSpacing="0";
	//标题显示，关闭，最大化最小显示
	temp.insertRow();
	temp.rows[0].insertCell();
	temp.rows[0].cells[0].className = "windowTitle";
	temp.rows[0].cells[0].noWrap = true;
	temp.rows[0].cells[0].title = title;
	temp.rows[0].cells[0].id = showId+"_title";
	temp.rows[0].cells[0].innerHTML = "<strong>"+title+"</strong>";
	temp.rows[0].cells[0].ondblclick= new Function("maxImize(document.all.div_"+uitl_int+")");
	temp.rows[0].cells[0].onmousedown = new Function("MDown(div_"+uitl_int+")");
	temp.rows[0].cells[0].onselectstart=new Function("event.returnValue=false");
	temp.rows[0].insertCell();
	var tmpHtml = "";
	tmpHtml += "<div id='div_"+uitl_int+"_button'>";
	tmpHtml += "<button class=\"windowsbutton\" onclick=\"minImaize(document.all.div_"+uitl_int+");this.blur()\">";
	tmpHtml += "<img src='images/WinFormIco/minimize.gif' width='13' height='11' alt='最小化' />";
	tmpHtml += "</button><button class=\"windowsbutton\" onClick=\"maxImize(document.all.div_"+uitl_int+");this.blur()\">";
	tmpHtml += "<img src='images/WinFormIco/miximize.gif' width='13' height='11' alt='最大化' id='div_"+uitl_int+"_img'/></button>";
	tmpHtml += "<button class=\"windowsbutton\" onclick=\"closeWindow(document.all.div_"+uitl_int+");this.blur()\">";
	tmpHtml += "<img src='images/WinFormIco/Close.gif' width='13' height='11' alt='Close' /></button>";
	tmpHtml += "</div>";
	temp.rows[0].cells[1].innerHTML = tmpHtml;
	tmpHtml = "";
	tmpTable.rows[0].cells[0].appendChild(temp);
	
	//插入一行 主内容显示
	tmpTable.insertRow();
	//插入单元格
	tmpTable.rows[1].id = "div_"+uitl_int+"_content";
	tmpTable.rows[1].insertCell();
	//设置单元格属性
	tmpTable.rows[1].cells[0].className = "windowBackground";//背景色
	//内容显示在Div中----------------------------
	var contentDiv = document.createElement("div");
	contentDiv.style.width = "100%";
	contentDiv.style.height = "100%";
	contentDiv.id = showId;
	contentDiv.style.overflow = "auto";
	contentDiv.setAttribute("divId","div_"+uitl_int);
	//内容显示Div结束------------------------------
	tmpTable.rows[1].cells[0].appendChild(contentDiv);
	element.appendChild(tmpTable);
	document.body.appendChild(element);
	setTitleColor(element);
	//设置内容
	setContent(contentDiv,object);
	uitl_int++;
}
//设置窗口中的内容
function setContent(winId,object)
{
	if(winId == "")
		alert("末知窗口ID");	
	else {
		if(typeof winId != 'object')
			winId = document.getElementById(winId);
		
		winId.innerHTML = "";
		if(typeof object == 'object')
			winId.appendChild(object);
		else if(typeof object == 'string')
			winId.innerHTML = object;
		else
			alert("object必须是一个对象，或字符串");
	}
}
//设置标题
function setTitle(winId,title)
{
	winId = document.getElementById(winId+"_title");
	winId.innerHTML = "<strong>"+title+"</strong>";
}
//获取焦点
function Focus(current)
{
	if(uitlfocus==0){
		uitl_int++;
		current.style.zIndex = uitl_int;
		setTitleColor(current);
		
		if(current.isMin == "1")
			minImaize(current)
	}
	uitlfocus = 0;
}
//关闭窗口
function closeWindow(current)
{
	document.body.removeChild(current);
}
//最大化窗口
function maxImize(current)
{		
	if(current.isMin == "0")
	{
		if(current.isMax=="0")
		{
			document.all(current.id+"_img").src = "images/WinFormIco/RestoreWindow.gif"; //最大化前先改图标
			
			current.setAttribute("tempWidth",current.style.width);
			current.setAttribute("tempHeight",current.style.height);
			current.setAttribute("tempTop",current.style.top);
			current.setAttribute("tempLeft",current.style.left);
			current.setAttribute("isMax","1");
			
			current.style.left = "0";
			current.style.top = "0";
			current.style.width = document.body.clientWidth;
			current.style.height = document.body.clientHeight;
		} else {
			document.all(current.id+"_img").src = "images/WinFormIco/miximize.gif";
			
			current.style.top = current.tempTop;
			current.style.left = current.tempLeft;
			current.style.width = current.tempWidth;
			current.style.height = current.tempHeight;
			current.setAttribute("isMax","0");
		}
	}
}
//最小化窗口document.createElement("div")
function minImaize(current)
{
	closeMenu();
	if(document.getElementById("tray")==null)
	{
		alert("缺少document.all.tray对象.\n实现<div id=\"tray\" style=\"position:absolute;bottom:0px;width:100%;height:25px;z-index:1;\"></div>");
		return;
	}
	
	var tmpId = current.id;
	
	if(current.isMin == "0"){//最小化
		if(current.isMax == "0"){
			current.tempWidth = current.style.width
			current.tempHeight = current.style.height
		}
		
		var tmpTitle = document.all(current.winId+"_title").title;//窗口的标题
		if(tmpTitle.length > 5)//如果标题长度大于5，只获取前5个字节
			document.all(current.winId+"_title").innerHTML = tmpTitle.substring(0,5)+"...";	
		
		current.style.removeAttribute("position");
		current.style.direction = "ltr";
		current.style.width = "10%";
		current.style.height = "10";
		current.isMin = "1";
		tray.appendChild(current);
		document.all(tmpId+"_content").style.display = "none"; //隐藏容器
		document.all(tmpId+"_button").style.display = "none";//隐藏关闭,最大化,最小化按钮
	} else {//还原
		closeMenu();
		var tmpTitle = document.all(current.winId+"_title").title;//窗口的标题
		document.all(current.winId+"_title").innerHTML = tmpTitle
		
		current.style.position = "absolute";
		if(current.isMax != "1"){
			current.style.width = current.tempWidth;
			current.style.height = current.tempHeight;
		} else {
			current.style.width = document.body.clientWidth;
			current.style.height = document.body.clientHeight;
		}
		current.isMin = "0";
		document.body.appendChild(current);
		document.all(tmpId+"_content").style.display = "";
		document.all(tmpId+"_button").style.display = "";
	}
}
//改变标题栏颜色
function setTitleColor(current)
{
	if(prevElement != null)
		prevElement.className = "windowTitleLFBackground";
	current.className = "windowTitleBackground";
	prevElement = current;
}
//通过winId获取div
function getDiv(winId)
{
	return document.getElementById(document.getElementById(winId).divId);
}
//---------------------桌面--------------------------------------------------------------------------------
var icotop = 0;
var icoleft = 0;
function setDesktopIco(img,title,active){
	var ico = document.createElement("div");
	ico.style.position = "absolute";
	ico.style.left = icoleft;
	ico.style.top = icotop;
	ico.style.width = "60";
	ico.style.height = "60";
	ico.setAttribute("isMax","0");
	ico.setAttribute("isMin","0");
	ico.onmousedown = new Function("MDown(this)");
	ico.ondblclick = new Function(active);
	
	var temp = document.createElement("table");
	temp.style.width="100%";
	temp.style.height="60";
	temp.border="0";
	temp.cellPadding="0";
	temp.cellSpacing="0";
	temp.insertRow();
	temp.rows[0].insertCell();
	temp.rows[0].cells[0].align="center";
	temp.rows[0].cells[0].innerHTML = "<img src=\""+img+"\" width=\"32\" height=\"32\" />";
	temp.insertRow();
	temp.rows[1].insertCell();
	temp.rows[1].cells[0].align="center";
	temp.rows[1].cells[0].innerHTML = "<span>"+title+"</span>";
	temp.rows[1].cells[0].className = "desktopFontColor";
	
	ico.appendChild(temp);
	document.body.appendChild(ico);
	if(icotop > document.body.clientHeight - 100)
	{
		icoleft += 65;
		icotop = 0;
	} else 
		icotop += 65;
}
//----------------------------------------------------------------------------------------------------------------------------
//移动改变大小
var dragEffect;
var dragging = false;
var currentObj=null;
var pX,pY,dragging;
document.onmouseup=MUp
document.onmousemove=MMove

function MDown(Object){
	if(Object.isMax == "0" && Object.isMin == "0"){
		currentObj=Object
		currentObj.setCapture()
		pX=event.x-currentObj.style.pixelLeft;
		pY=event.y-currentObj.style.pixelTop;
	}
}

function MMove(){
	
	 if(dragging){//设置窗口大小	
		 if(dragEffect!=undefined)
			dragEffect();
	 } else if(currentObj!=null){ //移动窗口
		currentObj.style.left=event.x-pX;
		 var tmpTop = event.y-pY
		 if(parseInt(tmpTop) >= 0)
		 {
			currentObj.style.top=tmpTop;
		 } else {
			currentObj.style.top=0; 
		 }
	 }
}

function MUp(){
	if(dragging){
		dragEffect = null;
		dragging = false;	
	} else if(currentObj!=null){
		 currentObj.releaseCapture();
		 currentObj = null;
	}
}
function setWinSize(w)
{
	if(w.isMax == "0" && w.isMin == "0"){
		var offsetX = event.x - parseInt(w.currentStyle.left);
		var offsetY = event.y - parseInt(w.currentStyle.top);
		
		var width = parseInt(w.currentStyle.width);
		var resizeX = offsetX > width - 1;
		var height = parseInt(w.currentStyle.height);
		var resizeY = offsetY > height - 1;
		
		if (resizeX || resizeY)
		{
		  dragging = true;
		  var offsetX = event.x - width;
		  var offsetY = event.y - height;
		  dragEffect = function()
		  {
			if (resizeX)
			  w.style.pixelWidth = event.x - offsetX;
			if (resizeY)
			  w.style.pixelHeight = event.y - offsetY;
		  }
		}
	}
}
function calcResize(w)
{
  if(w.isMax == "0" && w.isMin == "0"){
	  var offsetX = event.x - parseInt(w.currentStyle.left);
	  var offsetY = event.y - parseInt(w.currentStyle.top);
	  var width = parseInt(w.currentStyle.width);
	  var resizeX = offsetX > width - 1;
	  var height = parseInt(w.currentStyle.height);
	  var resizeY = offsetY > height - 1;
	  w.style.cursor = (resizeX||resizeY)?(resizeY?"S":"")+(resizeX&&(offsetY>25)?"E":"")+"-resize":"default";
  }
}
//--------------------------------------------------------------------------------------------------------------------------------
function _menu(current)
{
	if(current.tagName == "SPAN")
	{
		if(current.isMin != undefined && current.isMin == "1")
		{
			setTitleColor(current);
			//设置菜单 格式
			setMenu("&nbsp;<img src='images/WinFormIco/RestoreWindow.gif' width='9' height='9'>&nbsp;&nbsp;还原|Focus(document.all."+current.id+")$&nbsp;<img src='images/WinFormIco/mnuOS.gif' width='8' height='9'>&nbsp;&nbsp;设置|System_body_property()$---------------|1$&nbsp;<img src='images/WinFormIco/Close.gif' width='8' height='9'>&nbsp;&nbsp;关闭|tray.removeChild(document.all."+current.id+")");
		}
	}
	return false;
}
function setMenu(arg)
{
	closeMenu();
	var showMsg,action;
	var menu = document.createElement("div");
	menu.id = "menu";
	menu.className = "menu";
	
	var menuList = document.createElement("table");
	menuList.width="100%";
	menuList.height="100%";
	menuList.border="0";
	menuList.cellSpacing="0";
	menuList.cellPadding="0";
	
	var arr = arg.split("$");
	
	for(i=0;i<arr.length;i++){
		
		var tmparr = arr[i].split("|");
		showMsg = tmparr[0];
		action = tmparr[1];
		
		menuList.insertRow();
		menuList.rows[i].insertCell();
		menuList.rows[i].cells[0].noWrap = true;
		menuList.rows[i].cells[0].className="menuMouseout";
		if(action != "1")
		{
			menuList.rows[i].cells[0].style.height="20px";
			menuList.rows[i].cells[0].onmouseover = new Function("this.className='menuMouseover';");
			menuList.rows[i].cells[0].onmouseout = new Function("this.className='menuMouseout';");
		}
		if(action != "0")
			menuList.rows[i].cells[0].onclick = new Function(action);
		menuList.rows[i].cells[0].innerHTML = showMsg;
	}
	
	menu.appendChild(menuList);
	document.body.appendChild(menu);
	
	var rightedge=document.body.clientWidth-event.clientX
	var bottomedge=document.body.clientHeight-event.clientY
	
	if (rightedge<menu.offsetWidth)
		menu.style.left=document.body.scrollLeft+event.clientX-menu.offsetWidth
	else
		menu.style.left=document.body.scrollLeft+event.clientX
		
	if (bottomedge>menu.offsetHeight)
		menu.style.top=document.body.scrollTop+event.clientY
	else
		menu.style.top=document.body.scrollTop+event.clientY-menu.offsetHeight
}
function closeMenu(){
	if(document.getElementById("menu")!=null)
		document.body.removeChild(document.getElementById("menu"));
}
//--------------------------------调色板------------------------------------------------------------------------------------------
function SystemColorPanel(arg)
{
	try{
		var c = new Color()
		openWindow(c.CreatePanel(c),'system_color_Panel','调色板','width=292,height=242')
		//实现面板中确定按钮功能
		color_JS_Ok.onclick = function(){
			if(color_JS_SelColor.value!="#NaNNaNNaN"){
				try{
					if(typeof arg == "object"){
						var arr = arg;
						for(var i=0;i<arr.length;i++)
						{
							if(arr[i].indexOf("?") != -1)
							{
								var tmp = arr[i].replace(/\?/g,color_JS_SelColor.value);
								eval(tmp);
							} else {
								eval(arr[i] +"='"+ color_JS_SelColor.value+"'");
							}
						}
					} else if(typeof arg == "string") {
						if(arg.indexOf("?") != -1)
						{
							var tmp = arg.replace(/\?/g,color_JS_SelColor.value)
							eval(tmp);
						} else {
							eval(arg +"='"+ color_JS_SelColor.value+"'");
						}
					}
				} catch(err){
					alert("父窗口已关闭，非法操作！");	
				}
				closeWindow(getDiv("system_color_Panel"));
			} else {
				alert("请选择基色.");	
			}
		}
		//实现面板中取消按钮功能
		color_JS_cancel.onclick = function(){
			closeWindow(getDiv("system_color_Panel"));	
		}
	} catch(err){
		alert("Color类未引进!");	
	}
}
//--------------------------------------------------------------------------------------------------------------------------------
//document.oncontextmenu = new Function("return false"); //禁止右键
//document.onclick = closeMenu;
//---Map--------------------------------------------------------------------------------------------------------------------------
function struct(key, value){

  this.key = key;
  this.value = value;

}

function put(key, value){
  
  for (var i = 0; i < this.map.length; i++)
  {
    if ( this.map[i].key === key )
    {
      this.map[i].value = value;
      return;
    }
  }
  
  this.map[this.map.length] = new struct(key, value);

}

function get(key)
{
  for (var i = 0; i < this.map.length; i++)
  {
    if ( this.map[i].key === key )
    {
      return this.map[i].value;
    }
  }
  
  return null;
}

function removeKey(key)
{
  var v;
  for (var i = 0; i < this.map.length; i++)
  {
    v = this.map.pop();
    if ( v.key === key )
      continue;
      
    this.map.unshift(v);
  }
}

function size(){
  return this.map.length;
}

function isEmpty(){
  return this.map.length <= 0;
}

function classMap() {

  this.map = new Array();

  this.get = get;
  this.put = put;
  this.removeKey = removeKey;
  this.size = size;
  this.isEmpty = isEmpty;
}
//--List------------------------------------
function ArrayList()
{
	this.list = new Array();
	this.add = add;
	this.getValue = getValue;
	this.getSize = getSize;
	this.remove = remove;
}
function getValue(index)
{
	return this.list[index];	
}
function add(value)
{
	this.list[this.list.length] = value;	
}
function getSize()
{
	return this.list.length;	
}
function remove(index)
{

}
//--domdocment------------------------------
function getPartList(xmldoc,rot)//xml文档，rot指定根 返回数组
{
	var list = new ArrayList();
	var root = xmldoc.getElementsByTagName(rot).item(0);
	nodes(root,list);
	return list;
}
function getList(xmldoc)//xmldoc文档 返回数组
{
	var list = new ArrayList();
	var root = xmldoc.documentElement;
	nodes(root,list);
	return list;
}
function nodes(node,list)
{
	try{
		var map = new classMap();
		for(var i=0;i<node.childNodes.length;i++)
		{
			var tmpNode = node.childNodes.item(i);
			if(tmpNode.firstChild.hasChildNodes)
			{
				nodes(tmpNode,list);
			} else {
				map.put(tmpNode.nodeName,tmpNode.text);
			}
		}
		if(map.size()>0)
		{
			list.add(map);
		}
		//if(map.size()>0)
		map = null;
	} catch(e){
		alert(e);	
	}
}
//------Ctrl+Enter------------------------------
function presskey(active)
{
	if(event.ctrlKey && window.event.keyCode==13){
		eval(active);	
	}
}
//----字符转意------------------------------------
function Replace(str)
{
	str = str.replace(/\</g,"&lt;");
	str = str.replace(/\>/g,"&gt;");
	str = str.replace(/\ /g,"&nbsp;");
	str = str.replace(/[\r]/g,"<br>&nbsp;");
	str = str.replace(/[\n]/g,"<br>&nbsp;");
	return str;
}
//----------图标----------------------------------------
