// window control
function IsTopWindow(id)
{
	return IsTopObj(id) ;
}

function SetMovable(id)
{
	var obj = GetObjById(id) ;
	if (!obj)
		return false ;
	
	SetObjStyle(id, "left", String(GetObjStyle(id, "left"))) ;
	SetObjStyle(id, "top", String(GetObjStyle(id, "top"))) ;
	SetObjStyle(id, "position", "absolute") ;
	SetObjAttribute(id, "JS_MOVABLE", true) ;
	return true;
}

function CancelMovable(id)
{
	var obj = GetObjById(id) ;
	if (!obj)
		return false ;
	
	SetObjAttribute(id, "JS_MOVABLE", false) ;
	return true;
}

function IsMovable(id)
{
	var obj = GetObjById(id) ;
	if (!obj)
		return false ;
	
	if (GetObjAttribute(id, "JS_MOVABLE")==true)
		return true;
	
	// //DbgPrint("failed to get attribute: js_movable") ;
	return false ;
}

function SetForegroundWindow(id)
{
	var obj ;
	var MyIndex = 0 ; 
	var MyIndexCount = 0 ;
	var MaxIndex = 0 ;
	
	obj = GetObjById(id) ;
	
	// 找不到对象就退出:
	if (!obj)
	{
		//DbgPrint("SetForegroundWindow, Could not find object, input value: " + id) ;
		return false ;
	}
	
	// 不是顶上的也退出:
	if (!IsTopWindow(obj))
	{
		//DbgPrint("Not a top level window, exit") ;
		return false ;
	}
			
	if (GetObjStyle(obj, "position") != "absolute")
	{
		// 点空白处调用这个会提示. 注释掉
		// //DbgPrint("Window not absolute, always below those absolute windows") ;
		return false ;
	}
	
	// 在顶层窗口中找到当前的对象, 并确定MYINDEX, MYINDEXCOUNT和MaxIndex
	var topObjs = document.body.childNodes ;
	if (!topObjs)	// 任何特殊情况都要考虑
		return false ; 
		
	var counter = 0 ;
	
	if (counter == topObjs.length)
	{
			//DbgPrint("unable to set non-top window foreground") ;
			return false ;
	}
	
	// 找出MAXINDEX, MyIndexCount :
	for (counter = 0 ; counter < topObjs.length ; ++counter)
	{
		 // FF里会有这个不知道哪里来的东东. 必须无视它们				
		if (topObjs[counter].nodeName == "#text"|| topObjs[counter].nodeName == "#comment")
			continue ;
			
		// 不处理不显示的层(比如隐藏的菜单)
		if (GetObjStyle(topObjs[counter], "display") == "none")
			continue ;
			
		var tmpIndex = Number(GetObjStyle(topObjs[counter], "zIndex")) ;
		if (!tmpIndex)
		{
			tmpIndex = 0 ;
			topObjs[counter].style.zIndex = 0 ;	
		}	
		
		if (tmpIndex > MaxIndex)
		{
			MaxIndex = tmpIndex ;
		}
		
		if (tmpIndex == MyIndex)
		{
			++MyIndexCount ;
		}
	}
	--MyIndexCount ; // Exclude itself
	
	// //DbgPrint("SetForegroundWindow, MyIndexCount: " + MyIndexCount) ;
	
	// 找出有效元素中, ZINDEX大于MYINDEX的全都减1(不小于0), 并找出最大值
	if (MyIndexCount == 0)
	{
		for (counter = 0 ; counter < topObjs.length ; ++counter)
		{
		 	// FF里会有这个不知道哪里来的东东. 必须无视它们				
			if (!IsValidObj(topObjs[counter]))
				continue ;
				
			// 不处理不显示的层(比如隐藏的菜单)
			if (GetObjStyle(topObjs[counter], "display") == "none")
				continue ;
				
			var tmpIndex = Number(GetObjStyle(topObjs[counter], "zIndex")) ;
			if (!tmpIndex)
			{
				tmpIndex = 0 ;
				topObjs[counter].style.zIndex = 0 ;	
			}	
			
			if (tmpIndex > MyIndex)
			{
				tmpIndex = ((tmpIndex > 0) ? (tmpIndex-1) : 0) ;
				topObjs[counter].style.zIndex = tmpIndex;
			}
		}
	}
	
	MaxIndex =
		((MyIndexCount == 0) ? (MaxIndex) : (MaxIndex + 1));
	obj.style.zIndex = MaxIndex;
	
	return true ;
}

function GetWindowRect(id)
{
	return GetObjRect(id) ;
} 

function MoveWindow(id, x, y, width, height)
{
	var obj ;
	obj = GetObjById(id) 
		
	if (!obj)
	{
		//DbgPrint("MoveWindow: Unable to get object") ;
		return false ;
	}	
	
	var bNeedResize =  (width && height) ;
	if (IsTopWindow(obj))
	{
		obj.style.left = String(x) + "px" ;
		obj.style.top = String(y) + "px" ;
		if (bNeedResize)
		{
			ResizeWindow(obj, width, height) ;
		}
		return true ;
	}
	
	else if (GetObjStyle(obj, "position") == "ralative" ){
		SetObjStyle(obj, "left", String(GetPosPx(x))) ;
		SetObjStyle(obj, "top",  String(GetPosPx(y))) ;
		if (bNeedResize)
		{
			ResizeWindow(obj, width, height) ;
		}
		return true ;
	}
	return false ;
}

function ResizeWindow(id, newWidth, newHeigth)
{
	var obj ;
	obj = GetObjById(id) 
	if (!obj)
	{
		//DbgPrint("ResizeWindow: Unable to get object") ;
		return false ;
	}	
		
	if (!obj)
		return false ;
		
	SetObjStyle(obj, "width",  String(newWidth)) ;
	SetObjStyle(obj, "height", String(newHeigth)) ;
	return true ;
}

function ShowWindow(new_id, showCmd) 
{
	var obj = GetObjById(new_id)
	if (obj)
	{
		switch(showCmd)
		{
			case "SW_HIDE":
				SetObjStyle(obj, "display", "none") ;	
				break ;
			case "SW_SHOW":				
				SetObjStyle(obj, "display", "block") ;
				break ;
			case "SW_MIN":				
				// add min_tab 
				break ;
		}
		return true ;
	}
	return false ;
}

function GetTopParent(id)
{
	var obj = GetObjById(id) ;
	if (!obj)
		return false ;
	
	if (obj == document.body || obj == document.html || obj == document || obj==window)
	{
		// //DbgPrint("the object is document.body, no parent could be got") ;
		return false ;
	}
	
	while (!IsTopWindow(obj))
		obj = obj.parentNode ;
	
	return obj ;
}


/////////////////// 拖放的支持 /////////////////////
var __globle_window_control_captured = false ;
var __globle_window_control_captured_obj = null ;
var __globle_window_control_captured_last_mouse_pos = null ;
var __globle_window_control_capture_rgn = "__JS_Caputure_Rgn_Array" ;

var __globle_event_capturer_window_chain = new Array ;
function AddCaptureProc(_Func)
{
	for (var i = 0 ; i < __globle_event_capturer_window_chain.length ; ++i)
	{
		if (__globle_event_capturer_window_chain[i] == _Func)
			return true ; 
		}
	__globle_event_capturer_window_chain.push(_Func) ;
	return true ;
}

function RemoveCaptureProc(_Func)
{
	for (var i = 0 ; i < __globle_event_capturer_window_chain.length ; ++i)
	{
		if (__globle_event_capturer_window_chain[i] == _Func)
		{
			__globle_event_capturer_window_chain[i] = __globle_event_capturer_window_chain[__globle_event_capturer_window_chain.length-1] ; 
			__globle_event_capturer_window_chain.resize(__globle_event_capturer_window_chain.length-1) ;
			return true ; 
		}
	}
	return true ; 
}

function AddCaptureRgn(id, x, y, width, height)
{	
	var obj = GetObjById(id) ;
	if (!obj)
	{
//		//DbgPrint("AddCaptureRgn err: can't find object: >>" + id + "<<") ;
		return false ;
	}
	
	var DragRgn = GetObjAttribute(id, __globle_window_control_capture_rgn) ;
	if (!DragRgn || typeof(DragRgn) != "object") 
	{
		DragRgn = new Array() ;
	}
	
	var RgnObj = new Object ;
	RgnObj.x = GetPosPx(x) ; 
	RgnObj.y = GetPosPx(y) ; 
	RgnObj.width = GetPosPx(width) ;
	RgnObj.height = GetPosPx(height) ; 
	DragRgn.push(RgnObj) ;
	
	return SetObjAttribute(id, __globle_window_control_capture_rgn, DragRgn) ;
}

function RemoveAllCaptureRgn(id)
{	
	var obj = GetObjById(id) ;
	if (!obj)
	{
		//DbgPrint("RemoveCaptureRgn err: can't find object: >>" + id + "<<") ;
		return false ;
	}
		
	SetObjAttribute(id, __globle_window_control_capture_rgn, null) ;
	return true ;
}

function CallChainFunc(_Event)
{
	for (var i = 0 ; i < __globle_event_capturer_window_chain.length ; ++i)
	{
		__globle_event_capturer_window_chain[i](_Event) ;
	}
	return true ;
}

var __is_window_moving = 0 ;
function GlobleMouseEventProc(_Event) 
			// 注: 此函数 一律RETURN TRUE, 不然不能有其它的响应了. 
			// 这个级别是很高的,而且这个的返回没必要由调用者(程序员)检查
{
	var theEvent = GetEvent(_Event) ;
	var theEventSrc = GetEventSrc(theEvent) ;	
	
	///////////////////// MOUSE_DOWN ////////////////////
	if (theEvent.type == "mousedown")
	{
		var objTopWindow = GetTopParent(theEventSrc) ;
		if (HideMenu)
		{
			HideMenu(objTopWindow) ;
		}
		
		if (!objTopWindow)
		{ 
			CallChainFunc(_Event) ;
			return true ;
		}
		
		SetForegroundWindow(objTopWindow) ;
		
		if (!IsMovable(objTopWindow))		// 对于不可移动窗口. 我们不SETCAPTURE
		{
			CallChainFunc(_Event) ;
			return true ;
		}
	
		if (theEventSrc.nodeName != "INPUT" && theEventSrc.nodeName != "TEXTAREA")
		{
			SetObjAttribute(document.body, "onselectstart", function (){return false ;}) ;
		}
		var objOffset = GetMousePos(theEvent) ;		
		objOffset.x -= GetObjStyle(objTopWindow, "left") ;
		objOffset.y -= GetObjStyle(objTopWindow, "top") ;
		
		// 检查晃是在CAPTURE的范围内:
		var DragRgn = GetObjAttribute(objTopWindow, __globle_window_control_capture_rgn) ;
		if (!DragRgn)
		{
			// //DbgPrint("Not drag_drop setting") ;
		CallChainFunc(_Event) ;
			return true ;
		}
		
		var bNeedCapture = false ;
		for (var iRgnIndex = 0 ; iRgnIndex < DragRgn.length ; ++iRgnIndex)
		{
			
			if (objOffset.x >= DragRgn[iRgnIndex].x 
					&& objOffset.x < (DragRgn[iRgnIndex].x + DragRgn[iRgnIndex].width) 
					&& objOffset.y >= DragRgn[iRgnIndex].y 
					&& objOffset.y < (DragRgn[iRgnIndex].y + DragRgn[iRgnIndex].height))
			{
				bNeedCapture = true ; 
				break ; 
			}
		}
		
		if (bNeedCapture)
		{
			__globle_window_control_captured_obj = objTopWindow ;
			__globle_window_control_captured_last_mouse_pos = GetMousePos(theEvent) ;
			__globle_window_control_captured = true ;
		    CallChainFunc(_Event) ;
			return true ;
		}
		
		// CallChainFunc(_Event) ;
		return true;
	}	
	
	///////////////////// MOUSE_MOVE ////////////////////
	else if(theEvent.type == "mousemove")
	{	
		if (!__globle_window_control_captured)
		{
			CallChainFunc(_Event) ;
			return true ;
		}
			
		// 注意：这个下面是不可以使用GETTOPPARENT（THEEVENTSRC）的， 因为有可能MOUSE跑到了这个窗口的外面。 这样一来， 对象就不是要拖动的这个了。
		var objTopWindow = __globle_window_control_captured_obj ;
		var objOffset = GetMousePos(theEvent) ;
		
		var offsetOffsetX = objOffset.x - __globle_window_control_captured_last_mouse_pos.x ;
		var offsetOffsetY = objOffset.y - __globle_window_control_captured_last_mouse_pos.y ;
		
		var objOrgRect = new Object ;
        
        objOrgRect.x = objTopWindow.offsetLeft ;
        objOrgRect.y = objTopWindow.offsetTop ;
        var newX = (objOrgRect.x + offsetOffsetX) ;
        var newY = (objOrgRect.y + offsetOffsetY) ; 
		MoveWindow(objTopWindow, ((newX < 0) ? 0 : newX), ((newY < 0) ? 0 : newY)) ;
		
		__globle_window_control_captured_last_mouse_pos.x = ((newX <= 0) ? __globle_window_control_captured_last_mouse_pos
        .x - objOrgRect.x : objOffset.x);
		__globle_window_control_captured_last_mouse_pos.y = ((newY <= 0) ? __globle_window_control_captured_last_mouse_pos
        .y - objOrgRect.y : objOffset.y);
		CallChainFunc(_Event) ;
		return true ;
	}
	
	///////////////////// MOUSE_UP ////////////////////
	else if (theEvent.type == "mouseup")
	{	
		__globle_window_control_captured = false ;
		__globle_window_control_captured_obj = null ;
				
		SetObjAttribute(document.body, "onselectstart", function (){return true ;}) ;
		
		CallChainFunc(_Event) ;
		return true ;
	}
	else
	{
		return true ;	
	}

	return true ; // 永远走不到这一行
}
	
