var eventType;     							//事件种类, "move"、"resize" 
var oX, oY, oLeft, oTop, oWidth, oHeight; 	//存储原始移动前的位置 
var divClone, oDiv;

function setDiv() 
{ 
	var newLeft,newTop,newWidth,newHeight; 
	
	var div = document.createElement("div"); 
	//div.id = "panelDiv"; 
	div.style.position = "absolute"; 
	div.style.backgroundColor = "#66ffff" 
	div.style.padding = "2px 5px 5px 2px"; 
	div.style.overflow = "hidden"; 
	div.style.zIndex = 1; 

	div.style.width = "100px"; 
	div.style.height = "100px"; 
	//div.style.left = "10px"; 
	//div.style.top = "10px"; 
	div.offsetLeft = "10px"; 
	div.offsetTop = "10px"; 
	div.style.filter = "Alpha(opacity=80)"; 
	
	div = setChild(div); 

	document.body.appendChild(div); 
} 

function setChild(div) 
{ 
   //底色 
   //var cDiv = document.createElement; 
   //var backDiv = document.createElement("div"); 
   //backDiv.style.cssText = "left: 0px; top: 0px; width: 100%; height: 100%; background-color: #F5F5F5;"; 
   //div.appendChild(backDiv); 
   
   //标题 
   //var topDiv = document.createElement("div"); 
   //topDiv.style.cssText = "left: 2px; top: 2px; width: 100%; height: 30px; position: absolute; background-color: #78ABFF; vertical-align: middle; z-index: 5"; 

   //topDiv.style.cursor = "move"; 
   //topDiv.setAttribute("onmousedown", "setMove(this)"); 

   //topDiv.innerHTML = "<span style='top: 5px; left:5px; font-size: 20px; font-weight: bold; color: #102548; position: relative;' onselectstart='return false'>标题栏</span>"; 
   //div.appendChild(topDiv); 
   
   //内容 
	var contentDiv = document.createElement("div"); 
	contentDiv.style.cssText = "left: 0px; top: 0px; width: 100%; position: absolute; overflow: hidden; "; 
	contentDiv.style.height = (parseInt(div.style.height)) + "px"; 
	contentDiv.innerHTML = "<input type=text name=x value=0 size=4><br><input type=text name=y value=0 size=4><br><input type=text name=w value=100 size=4><br><input type=text name=h value=100 size=4>"; 
	contentDiv.style.cursor = "move"; 
	contentDiv.onmousedown = function() {setMove(this)};
	div.appendChild(contentDiv); 
   
	//关闭按钮 
	var closeDiv = document.createElement("div"); 
	closeDiv.style.cssText = "right: 5px; top : 5px; width: 14px; height: 14px; position: absolute; text-align: center; cursor: pointer; z-index: 10"; 
	closeDiv.innerHTML = "<div style='font-size: 12px; font-weight: bold; color: #0E377A;'>×</div>"; 
	closeDiv.onclick = function() {eCloseDiv(this)}; 
	div.appendChild(closeDiv); 
   
	//调整大小 
	var reDiv = document.createElement("div"); 
	reDiv.style.cssText = "right: 0px; bottom: 0px; width: 5px; height: 5px; position: absolute;"; 
	
	reDiv.style.cursor = "se-resize"; 
	reDiv.onmousedown = function() {setResize(this)};
	
	div.appendChild(reDiv); 
	
	return div; 
} 

function setMove(obj) 
{ 
	if (event.button == 1) 
	{ 
		oDiv = obj.parentNode; 
		divClone = document.createElement("div"); 
		divClone.style.cssText = "position: absolute; border:1px dotted #aaaaaa;"; 
		divClone.style.width = oDiv.offsetWidth;
		divClone.style.height = oDiv.offsetHeight;
		divClone.style.left = oDiv.style.left;
		divClone.style.top = oDiv.style.top;
		divClone.onmousemove = function() {startMove(this)};
		divClone.onmouseup = function() {endMove(this)};
		oX = parseInt(event.clientX); 
		oY = parseInt(event.clientY); 
		oLeft = parseInt(divClone.style.left); 
		oTop = parseInt(divClone.style.top); 
		document.body.appendChild(divClone); 
		divClone.setCapture(); 
		eventType = "move"; 
	} 
} 

//拖移 
function startMove(obj) 
{ 
   if (eventType == "move" && event.button == 1) 
   { 
	   var moveDiv = obj; 
	   moveDiv.style.left = (oLeft + event.clientX - oX) + "px"; 
	   moveDiv.style.top = (oTop + event.clientY - oY) + "px"; 
   } 
} 

//拖移结束调用动画 
function endMove(obj) 
{ 
	if (eventType == "move") 
	{ 
		eventType = ""; 

		oDiv.style.left = divClone.style.left; 
		oDiv.style.top = divClone.style.top; 
		
		try
		{	
			divClone.parentNode.removeChild(divClone); 
			divClone == null; 
		}
		catch(e){}
	}
} 

//clone调整大小的节点 
function setResize(obj) 
{ 
	if (event.button == 1) 
	{ 
		oDiv = obj.parentNode; 
		divClone = oDiv.cloneNode(true); 
		divClone.style.filter = "Alpha(opacity=50)"; 
		divClone.getElementsByTagName("div")[1].onmousemove = function() {startResize(this)};
		divClone.getElementsByTagName("div")[1].onmouseup = function() {endResize()};
		oX = parseInt(event.clientX); 
		oY = parseInt(event.clientY); 
		oWidth = parseInt(divClone.style.width); 
		oHeight = parseInt(divClone.style.height); 
		document.body.appendChild(divClone); 
		divClone.getElementsByTagName("div")[1].setCapture(); 
		eventType = "resize"; 
	} 
} 

//拖动调整大小 
function startResize(obj) 
{ 
   if (eventType == "resize" && event.button == 1) 
   { 
   var nX = event.clientX; 
   var nY = event.clientY; 
   if (nX > oX - oWidth && nY > oY - oHeight + 40) 
   { 
     var resizeDiv = obj.parentNode; 
     resizeDiv.style.width = (oWidth + event.clientX - oX) + "px"; 
     resizeDiv.style.height = (oHeight + event.clientY - oY) + "px"; 
     resizeDiv.getElementsByTagName("div")[0].style.height = (parseInt(resizeDiv.style.height)) + "px"; 
   } 
   } 
} 

//调整大小结束 
function endResize() 
{ 
	if (eventType == "resize") 
	{ 
		divClone.getElementsByTagName("div")[2].releaseCapture(); 
		oDiv.style.width = divClone.style.width; 
		oDiv.style.height = divClone.style.height; 
		oDiv.getElementsByTagName("div")[0].style.height = (parseInt(oDiv.style.height)) + "px"; 
		
		//更新大小
		var content = oDiv.getElementsByTagName("div")[0];

		content.getElementsByTagName("input")[2].value = oDiv.offsetWidth;
		content.getElementsByTagName("input")[3].value = oDiv.offsetHeight;

		try
		{	
			divClone.parentNode.removeChild(divClone); 
			divClone == null; 
		}
		catch(e){}
		
		eventType = ""; 
	} 
} 


//关闭DIV 
function eCloseDiv(btn) 
{ 
	var obj = btn.parentNode;
	
	obj.parentNode.removeChild(obj); 
} 
