﻿/*******************************
* JS Window
* Copyright JAY 2007
* http://www.eternity3.com.cn
*******************************/
document.onmousemove = function(e){if(foucsWnd){foucsWnd.onmousemove(e);}}
document.onmouseup = function(e){if(foucsWnd){foucsWnd.onmouseup(e);}}

var foucsWnd;
var wndHandle = 0;

function getMousePos(e){
	var ev = e || window.event
	return{x:ev.clientX, y:ev.clientY};
}

function $(id){
	return document.getElementById(id);
}

function MoveWindow(wnd, left, top, width, height){
	var title, body, footer;
	title = $("windowTitle" + wnd.handle);
	body = $("windowBody" + wnd.handle);
	footer = $("windowFooter" + wnd.handle);
	wnd.style.left = left + "px";
	wnd.style.top = top + "px";
	wnd.style.width = width + "px";
	wnd.style.height = height + "px";
	body.style.height = height - title.clientHeight - footer.clientHeight + "px";
}

function CreateWindow(title, width, height, left, top){
	var div = document.createElement("DIV");
	wndHandle++;
	div.handle = wndHandle;
	div.isMax = false;
	div.isMin = false;
	div.id = "JSWindow" +  div.handle;
	div.className = "JSWindow";
	
	title = title || "&nbsp;";
	div.style.width = width || 400;
	div.style.height = height || 320;
	div.style.left = left || 10;	
	div.style.top = top || 10;
	div.style.zIndex=1000;

	var innerHTML;
	innerHTML = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
	innerHTML += "<tr>";
	innerHTML += "<td><table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
	innerHTML += "<tr>";
	innerHTML += "<td class=\"window_11\"></td>";
	innerHTML += "<td class=\"window_12\" id=\"windowTitle" + div.handle + "\">" + title + "</td>";
	innerHTML += "<td class=\"window_13\" id=\"windowMin" + div.handle + "\"></td>";
	innerHTML += "<td class=\"window_14\" style=\"display:none;\" id=\"windowMax" + div.handle + "\"></td>";
	innerHTML += "<td class=\"window_15\" id=\"windowClose" + div.handle + "\"></td>";
	innerHTML += "<td class=\"window_16\"></td>";
	innerHTML += "</tr>";
	innerHTML += "</table></td>";
	innerHTML += "</tr>";
	innerHTML += "<tr>";
	innerHTML += "<td><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" id=\"windowBody" + div.handle + "\">";
	innerHTML += "<tr>";
	innerHTML += "<td class=\"window_21\"></td>";
	innerHTML += "<td class=\"window_22\" id=\"windowContent" + div.handle + "\">&nbsp;</td>";
	innerHTML += "<td class=\"window_23\"></td>";
	innerHTML += "</tr>";
	innerHTML += "</table></td>";
	innerHTML += "</tr>";
	innerHTML += "<tr>";
	innerHTML += "<td><table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
	innerHTML += "<tr>";
	innerHTML += "<td class=\"window_31\"></td>";
	innerHTML += "<td class=\"window_32\" id=\"windowFooter" + div.handle + "\">&nbsp;</td>";
	innerHTML += "<td class=\"window_33\"></td>";
	innerHTML += "</tr>";
	innerHTML += "</table></td>";
	innerHTML += "</tr>";
	innerHTML += "</table>";
	div.innerHTML = innerHTML;

	div.focus = function(){
		if (foucsWnd){
			foucsWnd.style.zIndex = parseInt(foucsWnd.style.zIndex) - 1;
		}
		
		this.style.zIndex = 10;
		
		foucsWnd = this;
	}
	
	div.onmousedown = function(e){
		this.focus();
		this.mouseDown = true;
		lastpos = getMousePos(e);
	}
	
	div.onmouseup = function(){
		this.mouseDown = false;
	}
	
	var lastpos, pos, x, y, w, h, r, b;
	var resizeN, resizeS, resizeW, resizeE;
	var minW, minH;	
	minW = 200;
	minH = 100;	
	
	div.onmousemove = function(e){
		var newX, newY, newW, newH;
		pos = getMousePos(e);
		x = parseInt(this.style.left);
		y = parseInt(this.style.top);
		w = this.clientWidth;
		h = this.clientHeight;	
		r = x + w;
		b = y + h;
		
		if (!this.mouseDown){
			resizeN = Math.abs(pos.y - y) <= 3;
			resizeS = Math.abs(pos.y - y - h) <= 3;
			resizeW = Math.abs(pos.x - x) <= 3;
			resizeE = Math.abs(pos.x - x - w) <= 3;
		}
		
		if (resizeN || resizeS){this.style.cursor = "n-resize";}
		if (resizeW || resizeE){this.style.cursor = "w-resize";}
		if ((resizeN && resizeW) || (resizeS && resizeE)){this.style.cursor = "se-resize";}
		if ((resizeN && resizeE) || (resizeS && resizeW)){this.style.cursor = "ne-resize";}
		if (!(resizeN || resizeS || resizeW || resizeE)){this.style.cursor = "default";}
				
		if (this.mouseDown && !this.isMax){			
			newX = x;
			newY = y;
			newW = w;
			newH = h;			
			
			if (resizeN){
				if (b - pos.y > minH){
					newY = pos.y;
					newH = b - pos.y;
				}else{
					newY = b - minH;
					newH = minH;					
				}
			}
			if (resizeW){
				if (r - pos.x > minW){
					newX = pos.x;
					newW= r - pos.x;
				}else{
					newX = r - minW;
					newW = minW;
				}				
			}
			if (resizeS){
				if (pos.y - y > minH){
					newH = pos.y - y;
				}else{
					newH = minH;
				}
			}
			if (resizeE){
				if (pos.x - x > minW){
					newW = pos.x - x ;
				}else{
					newW = minW;
				}
			}
			
			if (!(resizeN || resizeS || resizeW || resizeE)){
				newX = x + pos.x - lastpos.x;
				newY = y + pos.y - lastpos.y;
			}
			lastpos = pos;
			MoveWindow(this, newX, newY, newW, newH);
		}
	}
	
	div.onmouseover = function(){
	}
	
	div.onmouseout = function(){
	}
	
	document.body.insertBefore(div, document.body.firstChild);
	MoveWindow(div, left, top, width, height);

	//窗口最小化
	$("windowMin" + div.handle).onclick = function(){
		var wnd = $("JSWindow" + div.handle);
		if (!wnd.isMin){
			$("windowBody" + div.handle).parentNode.parentNode.style.display = "none";
			wnd.isMin = true;
		}else{
			$("windowBody" + div.handle).parentNode.parentNode.style.display = "";
			wnd.isMin = false;
		}
	}
	
	// 窗口最大化
	$("windowMax" + div.handle).onclick = function(){
		var wnd = $("JSWindow" + div.handle);
		if (!wnd.isMax){
			wnd.saveX = wnd.style.left;
			wnd.saveY = wnd.style.top;
			wnd.saveW = wnd.style.width;
			wnd.saveH = wnd.style.height;
			MoveWindow(wnd, 0, 0, document.documentElement.clientWidth, document.documentElement.clientHeight);
			wnd.isMax = true;
		}else{
			MoveWindow(wnd, parseInt(wnd.saveX), parseInt(wnd.saveY), parseInt(wnd.saveW), parseInt(wnd.saveH));
			wnd.isMax = false;
		}			
	}

	// 窗口关闭
	$("windowClose" + div.handle).onclick = function(){
		document.body.removeChild($("JSWindow" + div.handle));
	}	
	
	return div;
}
