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

// saves the desktop-object		
var desktop; 


function loadDesktop(pConsoleId)
{
	desktop = new TDesktop(pConsoleId);
	// catch some events
	//stop the current action
	document.onmouseup = desktop.stopAction;
	   
	return desktop;
}

function TDesktop(pConsoleId)
{

	// current mouse position
	var mouseX 			= 0;
	var mouseY 			= 0;
	
	// div-ID of the log console
	var consoleDivId 	= pConsoleId;
			

	// array with all window-objects
	var windows 		= new Array();
	// array with all icon-objects
	var icons 			= new Array();
	
	// div Id of the current object
	var currentObjectId;
		
	// active action
	// possible: 
	// moveObject, resizeWindowW, resizeWindowN, resizeWindowNW,  
	// resizeWindowE, resizeWindowS, resizeWindowSE, resizeWindowSW, resizeWindowNE
	var action 			= "";


	// index of the first window
	var minZIndex 	= 10;
	var biggestZIndex 	= 10;
	// biggest possible index
	var reservedZIndex 	= 100;
	
	
	// construct: creates the desktop
	__init();
	
	//(private) "construct": 
	function __init()
	{
		// Log
		__newLogEntry('creating desktop ...');
		__newLogEntry('Console-Div-ID: ' + consoleDivId);
		
		// needed to calculate the avaliable browser size
		$('body').get(0).innerHTML += '<div style="position:absolute;bottom:0;right:0;height:1px;width:1px;line-height:0;" id="desktop_size"></div>';


		// search for icons and register them
		$("a.icon").each(
			function(){
				__registerIcon(this.id);
			}
		);
		
		// search for windows and register them
		/*$("div.window").each(
			function(){
				__registerWindow(this.id);
			}
		);*/
		
	}
	
	// ============= Log-Console (start) =======================

	// (private) outputs the log messages
	function __newLogEntry(pMessage)
	{
		var date = new Date();
		$("#"+consoleDivId)[0].innerHTML +=  "("+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds() +')' + pMessage + '<br />'; 
	}

	// ============= Log-Console (end)   =======================


// =============== basic actions (start)   =================
		
		
	//-------------executes the movement action:------------------------
  		
	// (public) gets the mouse position and executes the movement
	$().mousemove(function(e)
	{

	
 	    mouseX =e.pageX;
		mouseY = e.pageY;

	    // which action should be executed?
	    switch(action)
	    {

	    	case "moveObject":
				__moveObject(currentObjectId);
	    		break;
	    	case "resizeWindowW":
				__resizeWindowW(currentObjectId);
	    		break;	
	    	case "resizeWindowN":
				__resizeWindowN(currentObjectId);
	    		break;	
	    	case "resizeWindowNW":
				__resizeWindowNW(currentObjectId);
	    		break;		
	    	case "resizeWindowE":
				__resizeWindowE(currentObjectId);
	    		break;	
	    	case "resizeWindowS":
				__resizeWindowS(currentObjectId);
	    		break;	
			case "resizeWindowSE":
				__resizeWindowSE(currentObjectId);
	    		break;	
	    	case "resizeWindowSW":
				__resizeWindowSW(currentObjectId);
	    		break;	
	    	case "resizeWindowNE":
				__resizeWindowNE(currentObjectId);
	    		break;		
			case "moveIcon":
				__moveIcon(currentObjectId);
	    		break;    				    			    				
	    	default:
	    	// no action
	    		break;
	    }
		
		return true;
	});
	
		
	//-----------------register the object------------------------
  		
	// (public) register the window and create the window object
	this.registerWindow = function(pCurrentObjectId)
	{
	  // private
	  return __registerWindow(pCurrentObjectId);
	}
	
	// (private) register the window and create the window object
	function __registerWindow(pCurrentObjectId)
	{
		// if not yet done -> create new object
		if(typeof windows[pCurrentObjectId] != 'object')
		{
			windows[pCurrentObjectId]  = new TWindow(pCurrentObjectId);
			// place in the foreground
			__setZIndex(pCurrentObjectId);

			// Log
			__newLogEntry('window "' + pCurrentObjectId + '" registered');
		}
		return windows[pCurrentObjectId];
	}
	
	
	// (public) register the icon and create the icon object
	this.registerIcon = function(pCurrentObjectId)
	{
	  // private
	  __registerIcon(pCurrentObjectId);
	}
	
	// (private) register the icon and create the icon object
	function __registerIcon(pCurrentObjectId)
	{

		// if not yet done -> create new object
		if(typeof icons[pCurrentObjectId] != 'object')
		{
			icons[pCurrentObjectId]  = new TIcon(pCurrentObjectId);

			// Log
			__newLogEntry('icon "' + pCurrentObjectId + '" registered');
		}
	}	
	
	
	//----------------------starts an action--------------------------

	// (public) starts the action
	this.startAction = function(pDoAction, pCurrentObjectId)
	{
	  // private
	  __startAction(pDoAction, pCurrentObjectId);
	}
	
	// (private) stops the current action
	function __startAction(pDoAction, pCurrentObjectId)
	{
		/*
	 	* Don't Select the Text when window moving in IE!
	 	*/
		if($.browser.msie)
		{
			document.onselectstart = __noSelection;
		}
		else
		{
			$(document.body).mousedown( function(e) {return false;});
		}
		
		// window action or a movement action
	    switch(pDoAction)
	    {
	        case "maxMinWindow":
	
				//activates the window
				__setObjectActive(pCurrentObjectId);
	    		
	    		// log
	    		__newLogEntry('max/min window: ' + pCurrentObjectId);
	    		    			    		
	    		// maximize the window
	    		windows[pCurrentObjectId].maxMinWindow();
	    		
	    		break; 	 
				
	    	case "closeWindow":
	
				//activates the window
				__setObjectActive(pCurrentObjectId);
				
				// closes the window 
				windows[pCurrentObjectId].close();
				
				// destroys the object
				delete(windows[pCurrentObjectId]);
	    		
	    		// log
	    		__newLogEntry('close window: ' + pCurrentObjectId);
	    		
	    		break; 	 
				 			    		
			// mouse-action: nothing to do, just activate the object and start the action
	    	default:
			
				// log
				__newLogEntry('starting mouse action: ' + pDoAction);
				    		
				// execute the action
				action = pDoAction;
				
				// moving a window
				if(pDoAction != "moveIcon")
				{
					// activates the object
					__setObjectActive(pCurrentObjectId);
					// sets the window click position
					windows[pCurrentObjectId].setClickPosition(mouseX, mouseY);
				}
				// moving an icon
				else
				{
					// sets the window click position
					icons[pCurrentObjectId].setClickPosition(mouseX, mouseY);
				}
					    		
				__newLogEntry('click position saved: (x:'+mouseX+' y:'+mouseY+')');
		
	    		break;
	    }
	}
	

	//----------------------stopps an action--------------------------

	// (public) stopps the current action
	this.stopAction = function(e)
	{
	  // private
	  __stopAction();
	}
	
	// (private) stopps the current action
	function __stopAction()
	{
		/*
	 	*  Select the Text wagain in IE!
	 	*/
		if($.browser.msie)
		{
			document.onselectstart = __doSelection;
		}
		else
		{
			$(document.body).mousedown( function(e) {return true;});
		}
	    // active?
	    if(action)
	    {						
			// kog
			__newLogEntry('stopped "'+action+'" action');
			
			// no active action
			action = '';
			
		}
	
	}
	
	// (private) no selection during an animation in IE
	function __noSelection()
	{
		return false;
	}
	// (private) no selection during an animation in IE
	function __doSelection()
	{
		return true;
	}
		
 	
// ================ basic actions  (end)   ==================

// ================ object actions  (start)   ===================


	//----------------------activate the object--------------------------

	// (public) set object active
	this.setObjectActive = function(pCurrentObjectId)
	{
	  // private
	  __setObjectActive(pCurrentObjectId);
	}
	
	// (private) set window active
	function __setObjectActive(pCurrentObjectId)
	{
		// not yet activated
		if(currentObjectId!=pCurrentObjectId)
		{
			currentObjectId = pCurrentObjectId;
			
			// not registered? -> register
	        __registerWindow(pCurrentObjectId);
			
			
			// log
			__newLogEntry('window ' + pCurrentObjectId + ' is active');
		}
		
		// move the window to the foreground
		__setZIndex(pCurrentObjectId);
	}
	
	// (public) set icon active
	this.setIconActive = function(pCurrentObjectId)
	{
	  // private
	  __setIconActive(pCurrentObjectId);
	}
	
	// (private) set icon active
	function __setIconActive(pCurrentObjectId)
	{
		// not yet activated
		if(currentObjectId!=pCurrentObjectId)
		{
			currentObjectId = pCurrentObjectId;
			// log
			__newLogEntry('icon ' + pCurrentObjectId + ' is active');
		}
		

	}
	
	// (private) calculates the new z-index
	function __setZIndex(pCurrentObjectId)
	{
		// limit?
		if(biggestZIndex >= reservedZIndex)
		{// limits achieved --> reset
			biggestZIndex = __resetZIndex();
			 			
		}
		
		// arise the biggest z-index
		biggestZIndex++;
		
		// sets the new z-index of the window
		windows[pCurrentObjectId].setZindex(biggestZIndex);
		
		// log
		__newLogEntry('zIndex of "'+pCurrentObjectId+ '" is: '+biggestZIndex );
		
	
	}

	// (public) sets the biggest known z-index
	this.setBiggestZIndex = function(zIndex)
	{
		if(zIndex >= minZIndex)
		{
			biggestZIndex = zIndex;
		}
		else
		{
			biggestZIndex = minZIndex;
		}
	}	
	
	// (public) restes the z-indexes without changing the order
	this.resetZIndex = function()
	{
		__resetZIndex();
	}
	
	// (private) restes the z-indexes without changing the order
	function __resetZIndex()
	{
		// finds the smalles z-index
			var smallestZIndex = biggestZIndex;
			
			// iterates all the windows and looks for the smallest z-index
			for (thisWindowId in windows)
			{
				if(parseInt(windows[thisWindowId].getZindex()) < smallestZIndex)
				{
					smallestZIndex = parseInt(windows[thisWindowId].getZindex());
				}
			}
			
			// subtract the smallest z-index of all the windows ==> same order, but smaller z-index
			for (thisWindowId in windows)
			{
				windows[thisWindowId].setZindex((parseInt(windows[thisWindowId].getZindex()) - smallestZIndex), true);
			}
			
			// finds the new biggest
			biggestZIndex = 0;
			
			// iterates all the windows and looks for teh biggest z-index
			for (thisWindowId in windows)
			{
				if(parseInt(windows[thisWindowId].getZindex()) > biggestZIndex)
				{
					biggestZIndex = parseInt(windows[thisWindowId].getZindex());
				}	
			}

			// log
			__newLogEntry('The zIndexes were reseted. -'+ smallestZIndex +' new biggest: '+ biggestZIndex);
			
			// retuns the new biggest
			return biggestZIndex;
	}


	// (public) returns window with this id
	this.getWindow = function(id)
	{
		return windows[id];
	}

	//-------------- mouse event -------------------------------------------
	
	// (private) positions the object with the mouse
	function __moveObject(pCurrentObjectId)
	{ 
		// new window position
		windows[pCurrentObjectId].onMouseMove(mouseX,mouseY);
	}

	// (private) changes the width of the window
	function __resizeWindowW(pCurrentObjectId)
	{
		windows[pCurrentObjectId].onMouseResizeW(mouseX);
	}
	
	// (private) changes the height of the window
	function __resizeWindowN(pCurrentObjectId)
	{
		windows[pCurrentObjectId].onMouseResizeN(mouseY);
	}
	
	// (private) changes the width and the height of the window
	function __resizeWindowNW(pCurrentObjectId)
	{
		windows[pCurrentObjectId].onMouseResizeN(mouseY);
		windows[pCurrentObjectId].onMouseResizeW(mouseX);
	}
	

	// (private) Fchanges the width of the window
	function __resizeWindowE(pCurrentObjectId)
	{
		windows[pCurrentObjectId].onMouseResizeE(mouseX);
	}
	
	// (private) changes the width and the height of the window
	function __resizeWindowNE(pCurrentObjectId)
	{
		windows[pCurrentObjectId].onMouseResizeN(mouseY);
		windows[pCurrentObjectId].onMouseResizeE(mouseX);
	}
	
	// (private) changes the height of the window
	function __resizeWindowS(pCurrentObjectId)
	{
		windows[pCurrentObjectId].onMouseResizeS(mouseY);
	}
	
	// (private) changes the width and the height of the window
	function __resizeWindowSE(pCurrentObjectId)
	{
		windows[pCurrentObjectId].onMouseResizeS(mouseY);
		windows[pCurrentObjectId].onMouseResizeE(mouseX);
	}	
	
	// (private) changes the width and the height of the window
	function __resizeWindowSW(pCurrentObjectId)
	{
		windows[pCurrentObjectId].onMouseResizeS(mouseY);
		windows[pCurrentObjectId].onMouseResizeW(mouseX);
	}	
	
	// (private) positions the object with the mouse
	function __moveIcon(pCurrentObjectId)
	{ 
		// new window position
		icons[pCurrentObjectId].onMouseMove(mouseX,mouseY);
	}

// ================ object actions  (end)   ===================

// ================= basic infos  (start)   ===================

	
	// (public) returns the current width of the desktop
	this.getDesktopWidth = function()
	{
		return $('div#desktop_size').get(0).offsetLeft;
	}
	
	// (public) returns the current height of the desktop
	this.getDesktopHeight = function()
	{
		return $('div#desktop_size').get(0).offsetTop;
	}

// ================= basic infos  (end)   ====================

}

	
