function getMousePosition(e){
	return [e?e.clientX : window.event.x,e?e.clientY : window.event.y];
}
var windowResizable = [];
function onWindowMove(obj,x,y){
	for(var i=0;i<windowResizable.length;i++){
		windowResizable[i].resize([x,y],windowResizable[i].windowArrayPosition);
	}
}

function onWindowUp(obj,x,y){
	for(var i=0;i<windowResizable.length;i++){
		windowResizable[i].hasSelected = false;
	}
}

function onWindowDown(obj,x,y){
	for(var i=0;i<windowResizable.length;i++){
		if(windowResizable[i] == obj){
			obj.hasSelected = true;
			obj.windowArrayPosition = obj.getArrayPosition([x,y]);	
		}
	}
}
function registerResizableWindow(prop){
	prop = getCommonProperty(prop,{
      id:"",
	  borderSize:4,
	  minHeight:50,
	  minWidth:50,
	  maxWidth:500,
	  maxHeight:500
   	});
	var obj = document.getElementById(prop.id); 
	if(obj){
		var r = getRealClientBound(obj);
		obj.bound = {left:r.left,right:r.right,top:r.top,bottom:r.bottom};
		obj.borderSize = prop.borderSize ;
		obj.windowMinHeight = prop.minHeight;
		obj.windowMinWidth = prop.minWidth;
		obj.windowMaxHeight = prop.maxHeight;
		obj.windowMaxWidth = prop.maxWidth;
		obj.hasSelected = false;
		obj.registerClass = "resizablewindow";
		obj.windowArrayPosition = [0,0,0,0];
		obj.onmousedown = function(e){
			obj.hasSelected = true;
			obj.windowArrayPosition = obj.getArrayPosition(e?[e.clientX,clientY] : [event.x,event.y]);
		}
		obj.resizeToUp = function(xy){
			var height = obj.bound.bottom - obj.bound.top + (obj.bound.top-xy[1]);
			if(height > obj.windowMinHeight && height < obj.windowMaxHeight){
				obj.style.height = height + "px";
				obj.style.top = obj.bound.top - (obj.bound.top-xy[1]) + "px";
			}
			else if(height >= obj.windowMaxHeight){
				obj.style.height = obj.windowMaxHeight + "px";
				obj.style.top = obj.bound.top - (obj.bound.top-xy[1] - (height-obj.windowMaxHeight)) + "px"; 
			}
			else{
				obj.style.height = obj.windowMinHeight + "px";
				obj.style.top = obj.bound.top - (obj.bound.top-xy[1] - (height-obj.windowMinHeight)) + "px"; 
			}
			obj.bound.top = parseInt(obj.style.top);
		}
		obj.resizeToLeft = function(xy){
			var width = obj.bound.right - obj.bound.left + (obj.bound.left-xy[0]);
			if(width > obj.windowMinWidth && width < obj.windowMaxWidth){
				obj.style.width = width + "px";
				obj.style.left = obj.bound.left - (obj.bound.left-xy[0]) + "px";
			}
			else if(width >= obj.windowMaxWidth){
		
				obj.style.width = obj.windowMaxWidth + "px";
				obj.style.left = obj.bound.left - (obj.bound.left-xy[0] - (width-obj.windowMaxWidth)) + "px";			
			}
			else{
				obj.style.width = obj.windowMinWidth + "px";
				obj.style.left = obj.bound.left - (obj.bound.left-xy[0] - (width-obj.windowMinWidth)) + "px"; 
			}
			obj.bound.left = parseInt(obj.style.left);
		}
		obj.resizeToDown = function(xy){
			var height = obj.bound.bottom - obj.bound.top + (xy[1]-obj.bound.bottom);
			if(height > obj.windowMinHeight && height < obj.windowMaxHeight){
				obj.style.height = height + "px";
			}
			else if(height >= obj.windowMaxHeight){
				obj.style.height = obj.windowMaxHeight + "px";
			}
			else{
				obj.style.height = obj.windowMinHeight + "px";
			}
			obj.bound.bottom = xy[1] ;
		}
		obj.resizeToRight = function(xy){
			var width = obj.bound.right - obj.bound.left + (xy[0]-obj.bound.right);
			if(width > obj.windowMinWidth && width < obj.windowMaxWidth){
				obj.style.width = width + "px";
			}
			else if(width >= obj.windowMaxWidth){
				obj.style.width = obj.windowMaxWidth + "px";		
			}
			else{
				obj.style.width = obj.windowMinWidth + "px";
			}
			obj.bound.right = xy[0] ;
		}
		obj.getArrayPosition = function(xy){
			var arr = [0,0,0,0];
			if(xy[0]<obj.bound.left + obj.borderSize ){
				arr[3] = 1;
			}
			if(xy[1]<obj.bound.top + obj.borderSize){
				arr[0] = 1;
			}
			if(xy[0]>obj.bound.right - obj.borderSize){
				arr[1] = 1;
			}
			if(xy[1]>obj.bound.bottom - obj.borderSize){
				arr[2] = 1;
			}
			if((arr[0] && arr[3]) || (arr[2] && arr[1])){
				obj.style.cursor = "nw-resize";
			} 
			return arr;
		}
		obj.changeIcon = function(arr){
			if((arr[0] && arr[3]) || (arr[2] && arr[1])){
				obj.style.cursor = "nw-resize";
			} 
			else if((arr[0] && arr[1]) || (arr[2] && arr[3])){
				obj.style.cursor = "ne-resize";
			}
			else if(arr[0] || arr[2]){
				obj.style.cursor = "n-resize";					 
			}
			else if(arr[1] || arr[3]){
				obj.style.cursor = "w-resize";				
			}
			else{
				obj.style.cursor = "default";
			}
		}
		obj.resize = function(xy,arr){
			obj.changeIcon(obj.getArrayPosition(xy));
			if(obj.hasSelected){
				if((arr[0] && arr[3]) || (arr[2] && arr[1])){
					obj.style.cursor = "nw-resize";
					if(arr[2]){
						obj.resizeToDown(xy);
						obj.resizeToRight(xy);
					}
					else{
						obj.resizeToUp(xy);
						obj.resizeToLeft(xy);
					}
				} 
				else if((arr[0] && arr[1]) || (arr[2] && arr[3])){
					obj.style.cursor = "ne-resize";
					if(arr[0]){
						obj.resizeToUp(xy);
						obj.resizeToRight(xy);
								
					}
					else{
						obj.resizeToDown(xy);
						obj.resizeToLeft(xy);
					}
				}
				else if(arr[0] || arr[2]){
					obj.style.cursor = "n-resize";
					if(arr[2]){
						obj.resizeToDown(xy);
					}
					else{
						obj.resizeToUp(xy);
					}
				}
				
				else if(arr[1] || arr[3]){
					obj.style.cursor = "w-resize";
					if(arr[1]){
						obj.resizeToRight(xy);
					}
					else{
						obj.resizeToLeft(xy);
					}
				}
			}
		}
		windowResizable[windowResizable.length] = obj;
	}
	return obj;
}

