// JScript source code
function corpPanel(sTitleDiv, sMainDiv)
{
	var _self = this;
	var _TitleDiv = document.getElementById(sTitleDiv);
	var _MainDiv =  document.getElementById(sMainDiv);
	var _curHelp = new _mouseData(0,0);
	
	this.TitleDiv = _TitleDiv;
	this.MainDiv = _MainDiv;
	
	this.curHelp = _curHelp;
	
	this.Initialise = _Initialise;
	
	// Events
	this.checkMove = function() { _checkMove(_self); };
	this.setCursorDown = function() { _setCursorDown(_self); };
	this.setCursorUp = function() {_setCursorUp(_self); };
	this.setLeave = function() {_setLeave(_self); };
	this.dock = function() {_dock(_self); };
	
	this.Initialise();

	function _mouseData(x,y)
	{
		this.X = x;
		this.Y = y;
	}
	
	function _Initialise()
	{
		this.TitleDiv.onmouseout = this.setLeave;
		this.TitleDiv.onmousedown = this.setCursorDown;
		this.TitleDiv.onmouseup = this.setCursorUp;
		this.TitleDiv.onmousemove = this.checkMove;
		
		// Here we are just ascertaining the starting Position
		this.curHelp.X = this.MainDiv.offsetTop;
		this.curHelp.Y = this.MainDiv.offsetLeft;
		
		// If we are the first Panel, we need to initialise the zOrder Stack
		if(document.zOrder == undefined)
		{
			document.zOrder = 1;	
		}
		if(document.activePanel == undefined)
		{
			document.activePanel = null;
		}
	}

	function _checkMove(e)
	{
		// 1 or 2 are the Left and Right mouse button respectively	
		if(event.button > 0)
		{
		
			var x = (event.clientX + document.body.scrollLeft) - (e.curHelp.X);
			var y = (event.clientY + document.body.scrollTop) - (e.curHelp.Y);
				
			e.MainDiv.style.position = "absolute";
			e.MainDiv.style.left = x;
			e.MainDiv.style.top = y;
			if(document.activePanel != e)
			{
				e.MainDiv.style.zIndex = document.zOrder++;	
				document.activePanel = e;
			}				
			
			// DEBUG CODE
			//e.TitleDiv.innerHTML = e.TitleDiv.id + " - zOrder = " + e.MainDiv.style.zIndex;
			
			// This call may seem bizarre but it is to ensure that the window renderers smoothly
			document.selection.clear();
			
		}
		else
		{			
			// This line resets an event register that is generated the first time that the user leaves the toolbar. (This provides smooth windows like navigation)
			document.onmousemove = null;
						
			// These two lines are constantly updating the off set value when the user is over the toolbar, this provides a smoother movement
			e.curHelp.X = event.offsetX;
			e.curHelp.Y = event.offsetY;
			
			if(e.MainDiv.style.zIndex == 10)
			{
				e.MainDiv.style.zIndex = document.zOrder-1;
				document.onclick = function() { _setToBack(e); };	
			}
			
			//Ensure cursor is corrected
			e.setCursorUp();
			
			// DEBUG CODE
			//e.TitleDiv.innerHTML = e.TitleDiv.id + " - zOrder = " + e.MainDiv.style.zIndex;
			
			// Lastly, and quirkly we have to clear the selection object. This can cause a problem with the second visit to Drag (without and intervening srceen click)
			// can lead to the window being undragable.
			document.selection.clear();
						
		}
	}
	
	function _setToBack(e)
	{
		// When another window is clicked the zOrder of this window is decremented by one until is 0
		var iIndex = e.MainDiv.style.zIndex;
		//e.MainDiv.style.zIndex = (iIndex > 0) ? --iIndex : 0;	
		
		e.MainDiv.style.zIndex= ( iIndex > 0 ) ? iIndex-1 : 0;
			
		// DEBUG CODE
		//e.TitleDiv.innerHTML = e.TitleDiv.id + " - zOrder = " + e.MainDiv.style.zIndex;
		
		// We do not clear this down, as the next panel will do that.
		document.onclick = null;	
	}

	function _setCursorDown(e)
	{
		e.TitleDiv.style.cursor = "move";
	}

	function _setCursorUp(e)
	{
		e.TitleDiv.style.cursor = "default";
	}

	function _setLeave(e)
	{	
		//alert("test in setLeave");
		// Okay, Here is the deal, we still monitor the Mouse move event, and this event will be cleared when the User releases the mouse button
		document.onmousemove = function() { e.checkMove(e); };
	}

	function _dock(e)
	{
		e.MainDiv.style.position = "static";
	}
	
}