/*
 * favore::dhtmlDesktop 1.0
 *
 * Copyright (c) 2007 Evgenij Terehov (www.favore.net)
 * Licensed under the GPL licenses.
 *
 */

function TWindow(pWindowId)
{
	// window jQuery-Object
	var window = $("div#"+pWindowId);
	// window ID
	var windowId = pWindowId;
	
	
	// ---------------------
	// basic attributes
	
	// desktop position (from the left top corner)
	var topPx;
	var leftPx;
	var widthPx;
	var heightPx;
	
	// smalles size
	var minWidthPx;
	var minHeightPx;
	var minContentWidthPx;
	var minContentHeightPx;
	var heightDifference;
	var widthDifference;
	
	// scrollbar width
	var scrollbarWidth = 18;
	

	// window click position
	var clickWindowX;	
	var clickWindowY;
	
	// desktop click position
	var clickDesktopX;
	var clickDesktopY;
		
	// window click size 
	var clickWidthPx;
	var clickHeightPx;
		
	// window click position
	var clickWindowTopPx;
	var clickWindowLeftPx;
	
	
	// window status (normal, max, hidden)
	var windowStatus = 'normal';
	
	// index of the active window
	var zIndex = 0;
	
	
	// "construct": creates a new window
	__init();
	
	
	//--------------------------------------	
	//(private) "construct": creates a new window
	function __init()
	{

		// initial size is the minimal size
		minWidthPx  = window.width();
		minHeightPx = window.height();
		
		// create the scrollbars
		__createsScrollbar();
		
		// content area
		minContentWidthPx  = window.find("div.jScrollPaneContainer").width();
		minContentHeightPx = window.find("div.jScrollPaneContainer").height();
		widthDifference =  minContentWidthPx - minWidthPx;
		heightDifference =  minContentHeightPx - minHeightPx;
		

		// saves the coordinates of the window
		__setWindowCoordinates();	
		
		// completes the window (events,...)
		__completesWindow();

	}
	
	//--------------------------------------
	

	// (private) saves the coordinates of the window
	function __setWindowCoordinates()
	{
		// only, when not maximized
		if (windowStatus != "max")
		{	
			topPx 		= window[0].offsetTop;
			leftPx 		= window[0].offsetLeft;
			widthPx		= window[0].offsetWidth;
			heightPx	= window[0].offsetHeight;
		}
	}
	
	
	// (private) registers the events 
	function __completesWindow()
	{

		// add main event (activation)
		window.mousedown( function(e) { desktop.setObjectActive(this.id); e.stopPropagation();} );

		// adds resize-divs
		__createResize();


		// adds events to the window title
		window.children(".title").mousedown( function() { desktop.startAction('moveObject',window.get(0).id); } );
		window.children(".title").mouseup( function() { desktop.stopAction(); } );

		// adds events to the buttons
		window.find(".close").click( function() { desktop.startAction('closeWindow',window.get(0).id); return false;} );
	}
	
	// (private) creates or reinit the scrollbar 
	function __createsScrollbar()
	{
		window.find('.content').jScrollPane({
			scrollbarWidth:scrollbarWidth, 
			scrollbarMargin:0,
			showArrows: true
			});
	}
	
	// (private) makes the window resizable
	function __createResize()
	{

		// North
		window.append('<div class="resize_N" alt="resize" style="position:absolute;left:0px;top:-1px;width:100%;height:3px;cursor:n-resize;z-index:5;line-height:1px;" onmousedown="desktop.startAction(\'resizeWindowN\',\''+window.get(0).id+'\');" onmouseup="desktop.stopAction();" />&#160;</div>');
		// South
		window.append('<div class="resize_S" alt="resize" style="position:absolute;left:0px;bottom:-1px;width:100%;height:3px;cursor:n-resize;z-index:5;line-height:1px;" onmousedown="desktop.startAction(\'resizeWindowS\',\''+window.get(0).id+'\');" onmouseup="desktop.stopAction();" />&#160;</div>');
		// West
		window.append('<div class="resize_W" alt="resize" style="position:absolute;left:-1px;top:0px;height:100%;width:3px;cursor:e-resize;z-index:5;line-height:1px;" onmousedown="desktop.startAction(\'resizeWindowW\',\''+window.get(0).id+'\');" onmouseup="desktop.stopAction();" />&#160;</div>');
		// East
		window.append('<div class="resize_E" alt="resize" style="position:absolute;right:-1px;top:0px;height:100%;width:3px;cursor:e-resize;z-index:5;line-height:1px;" onmousedown="desktop.startAction(\'resizeWindowE\',\''+window.get(0).id+'\');" onmouseup="desktop.stopAction();" />&#160;</div>');
		// North West
		window.append('<div class="resize_NW" alt="resize" style="position:absolute;top:-2px;left:-2px;width:15px;height:10px;cursor:nw-resize;z-index:5;line-height:1px;" onmousedown="desktop.startAction(\'resizeWindowNW\',\''+window.get(0).id+'\');" onmouseup="desktop.stopAction();" />&#160;</div>');
		// North East
		window.append('<div class="resize_NE" alt="resize" style="position:absolute;top:-2px;right:-2px;width:15px;height:10px;cursor:ne-resize;z-index:5;line-height:1px;" onmousedown="desktop.startAction(\'resizeWindowNE\',\''+window.get(0).id+'\');" onmouseup="desktop.stopAction();" />&#160;</div>');
		// South West
		window.append('<div class="resize_SW" alt="resize" style="position:absolute;left:-2px;bottom:-2px;width:15px;height:10px;cursor:ne-resize;z-index:5;line-height:1px;"  onmousedown="desktop.startAction(\'resizeWindowSW\',\''+window.get(0).id+'\');" onmouseup="desktop.stopAction();" />&#160;</div>');
		// South East
		window.append('<div class="resize_SE" alt="" style="position:absolute;right:-2px;bottom:-2px;width:15px;height:10px;cursor:nw-resize;z-index:5;line-height:1px;"  onmousedown="desktop.startAction(\'resizeWindowSE\',\''+window.get(0).id+'\');" onmouseup="desktop.stopAction();" />&#160;</div>');

	}
	
	// (public) returns the current z-index
	this.getZindex = function()
	{
		return zIndex;	
	}
	
	// (public) sets a new z-index
	this.setZindex = function(pZIndex)
	{
		zIndex = pZIndex;
		window.css("z-index", zIndex);		
	}
	
	//------ window actions ----------------
	
	// (public) saves the click positions
	this.setClickPosition = function(pMouseX, pMouseY)
	{
		// window click position
		clickWindowX = pMouseX - leftPx;
		clickWindowY = pMouseY - topPx;
		
		// desktop click position
		clickDesktopX = pMouseX;
		clickDesktopY = pMouseY;
		
		// current size of the window
		clickWidthPx  = widthPx;
		clickHeightPx = heightPx;
		
		// current window position
		clickWindowTopPx  = topPx;
		clickWindowLeftPx = leftPx;
		
	}
	// (public) follows the mouse
	this.onMouseMove = function(pMouseX, pMouseY)
	{
		// only if the window is not maximised
		if(windowStatus == 'normal')
		{
			__setPositionX((pMouseX-clickWindowX));
			__setPositionY((pMouseY-clickWindowY));	  
		}
	}
	
	// (public) changes the size to the west
	this.onMouseResizeE = function(pMouseX)
	{ 
		// window
		__setWidth((clickWidthPx+(pMouseX-clickDesktopX)));
	
	}
		
	// (public) changes the size to the north
	this.onMouseResizeS = function(pMouseY)
	{
		// window
		__setHeight((clickHeightPx+(pMouseY-clickDesktopY)));

	}
	
	// (public) changes the size to the east
	this.onMouseResizeW = function(pMouseX)
	{ 
		// hold the min size
		if(minWidthPx<=(clickWidthPx-(pMouseX-clickDesktopX)))
		{
			// change the width of the window
			__setWidth(clickWidthPx-(pMouseX-clickDesktopX));
			// change the position of the window
	  		__setPositionX(clickWindowLeftPx+(pMouseX-clickDesktopX)); 
			
			
		}
	}
		
	// (public) changes the size to the south
	this.onMouseResizeN = function(pMouseY)
	{
		// hold the min size
		if(minHeightPx<=(clickHeightPx-(pMouseY-clickDesktopY)))
		{
			// change the height of the window
			__setHeight(clickHeightPx-(pMouseY-clickDesktopY));
			// change the position of the window
	  		__setPositionY(clickWindowTopPx+(pMouseY-clickDesktopY));
			
		}
	}
//------  changes the window size ---------------- 
	
	// (public) width
	this.setWidth = function(newWidthPx)
	{
		__setWidth(newWidthPx); 	
	}
	
	// (private) width
	function __setWidth(newWidthPx)
	{
		// hold the min size
		if(newWidthPx>=minWidthPx)
		{
			var newWidthEm = newWidthPx /10;
			window.css('width',newWidthEm+"em");
			
			var newContentWidthEm = newWidthEm + widthDifference/10;
			// adjust the content size
			window.find("div.jScrollPaneContainer").css('width',newContentWidthEm+"em");			
			
			// reinit the scrollbars
			__createsScrollbar();
			
			// saves the new position
			__setWindowCoordinates();
		}

	}
	
	
	// (public) height
	this.setHeight = function(newHeightPx)
	{
		__setHeight(newHeightPx); 	
	}
	
	// (private) height
	function __setHeight(newHeightPx)
	{
		// hold the min size
		if(newHeightPx>=minHeightPx)
		{
			var newHeightEm = newHeightPx/10;
			window.css('height',newHeightEm+"em");
			
			var newContentHeightEm = newHeightEm + heightDifference/10;
			// adjust the content size
			window.find("div.jScrollPaneContainer").css('height',newContentHeightEm+"em");
			
			
			// adjust border etc.
			// boder
			window.find("img.border_e").css('height',newHeightEm+"em");
			window.find("img.border_w").css('height',newHeightEm+"em");
			window.find("img.border_s").css('top',newHeightEm+"em");
			window.find("img.corner_sw").css('top',newHeightEm+"em");
			window.find("img.corner_se").css('top',newHeightEm+"em");
			// resizer
			window.find("img.resize_e").css('height',newHeightEm+"em");
			window.find("img.resize_w").css('height',newHeightEm+"em");
			window.find("img.resize_S").css('top',newHeightEm+"em");
			window.find("img.resize_sw").css('top',newHeightEm+"em");
			window.find("img.resize_se").css('top',newHeightEm+"em");
			
			
			// reinit the scrollbars
			__createsScrollbar();
			
			// saves the new position
			__setWindowCoordinates();
		}
	}

	
	
//------ de/activate the possibility to resize ----------------
		
	// (private) deactivate all
	function __deaktivateResize()
	{
		__deaktivateResizeN();
		__deaktivateResizeS();
		__deaktivateResizeW();
		__deaktivateResizeE();
		__deaktivateResizeNW();
		__deaktivateResizeNE();
		__deaktivateResizeSW();
		__deaktivateResizeSE();
		
	}
	
	// (private) activate all
	function __aktivateResize()
	{
		__aktivateResizeN();
		__aktivateResizeS();
		__aktivateResizeW();
		__aktivateResizeE();
		__aktivateResizeNW();
		__aktivateResizeNE();
		__aktivateResizeSW();
		__aktivateResizeSE();
		
	}
			
		
	// (private) deactivate north
	function __deaktivateResizeN()
	{
		window.children('.resize_N').hide();
	}
	// (private) activate north
	function __aktivateResizeN()
	{
		window.children('.resize_N').show();
	}
	
	// (private) deactivate south
	function __deaktivateResizeS()
	{
		window.children('.resize_S').hide();
	}
	// (private) activate south
	function __aktivateResizeS()
	{
		window.children('.resize_S').show();
	}
	
	// (private) deactivate east
	function __deaktivateResizeE()
	{
		window.children('.resize_E').hide();
	}
	// (private) activate east
	function __aktivateResizeE()
	{
		window.children('.resize_E').show();
	}
	
	// (private) deactivate west
	function __deaktivateResizeW()
	{
		window.children('.resize_W').hide();
	}
	// (private) activate west
	function __aktivateResizeW()
	{
		window.children('.resize_W').show();
	}
	
	// (private) deactivate north west
	function __deaktivateResizeNW()
	{
		window.children('.resize_NW').hide();
	}
	// (private) activate north west
	function __aktivateResizeNW()
	{
		window.children('.resize_NW').show();
	}
	
	// (private) deactivate north east
	function __deaktivateResizeNE()
	{
		window.children('.resize_NE').hide();
	}
	// (private) activate north east
	function __aktivateResizeNE()
	{
		window.children('.resize_NE').show();
	}
	
	// (private) deactivate south west
	function __deaktivateResizeSW()
	{
		window.children('.resize_SW').hide();
	}
	// (private) activate south west
	function __aktivateResizeSW()
	{
		window.children('.resize_SW').show();
	}
	
	// (private) deactivate south east
	function __deaktivateResizeSE()
	{
		window.children('.resize_SE').hide();
	}
	// (private) activate south east
	function __aktivateResizeSE()
	{
		window.children('.resize_SE').show();
	}

//------ position the window ----------------
	
	// (public) x position
	this.setPositionX = function(positionX)
	{
		__setPositionX(positionX); 	
	}
	
	// (private) x position
	function __setPositionX(positionX)
	{
		window.css('left',positionX);
		// saves the new position
		__setWindowCoordinates();
	}
	
	// (public) y position
	this.setPositionY = function(positionY)
	{
		__setPositionY(positionY);
	}
	
	// (private) y position
	function __setPositionY(positionY)
	{
		window.css('top',positionY);
		// saves the new position
		__setWindowCoordinates();	
	}

	
//------ close the window----------------
	
	// (public) close
	this.close = function()
	{
		__close(); 	
	}
	
	// (private) close
	function __close()
	{
				window.hide();
	}	
			
	
}