/**
 * LICENSE
 *
 * This source file is subject to the EWS Software License that is bundled
 * with this package in the file LICENSE.txt.
 * If you did not receive a copy of the license please send us an email
 * so we can send you a copy immediately.
 * Permission to copy and distribute verbatim copies of this source
 * are not granted.
 *
 * @copyright 	Copyright (c) 2008-2009 entrance web studio
 * @license	EWS Software License
 */

/**
 * CWorkplaceShell
 *
 * @package				workplace
 * @copyright 		Christoph Lukas Lindtner, entrance web studio
 *
 * @dependencies	Prototype 1.6, jQuery
 */
function CWorkplaceShell ()
{
	/* ------------------------------------------------------------------------------------------------------------ *
	 *							 																				     																										*
	 * Class constants																							     																						*
	 *																											     																										*
	 * ------------------------------------------------------------------------------------------------------------ */
	 
	/**
	 * Displays a progress cursor.
	 *
	 * @const CURSOR_PROGRESS
	 */
	this.CURSOR_PROGRESS = "progress";
	
	
	/**
	 * Displays a applicationWaiting cursor.
	 *
	 * @const CURSOR_WAIT
	 */
	this.CURSOR_WAIT = "wait";
	
	
	/* ------------------------------------------------------------------------------------------------------------ *
	 *																											     																										*
	 * Class variables																						  	     																					*
	 *																											     																										*
	 * ------------------------------------------------------------------------------------------------------------ */
	 
	/**
	 * A pointer onto this object
	 *
	 * @var CView self
	 */
	var self = this;
	
	
	/**
	 * A div container blocking user input.
	 *
	 * @var HTMLDivObject overlay
	 */
	var overlay = null;
	
	
	/* ------------------------------------------------------------------------------------------------------------ *
	 *																											     																										*
	 * Class properties																								     																					*
	 *																											     																										*
	 * ------------------------------------------------------------------------------------------------------------ */


	/**
	 * The main content pane element.
	 *
	 * @var HTMLDivElement pane
	 */
	this.pane = null;
	
	 
	/* ------------------------------------------------------------------------------------------------------------ *
	 *																											     																										*
	 * Class methods																							  	     																					*
	 *																											     																										*
	 * ------------------------------------------------------------------------------------------------------------ */
	
	/**
	 * Pseudo constructor
	 *
	 * @return void
	 */
	this.init = function () {
		// create overlay
	
		
		// create progress bar
		createProgressBar ();
	}
	
	/**
	 * Loads new content into the workplace pane. The second parameter can be an object defining various parameters which will be added to
	 * the url. The third optional parameter can be a custom callback function.
	 *
	 * @param String url
	 * @param Object params
	 * @param Function callback
	 * @return void
	 */
	this.load = function ( url , params , callback ) {
		if ( ! self.pane )
			throw new Error ( "No workplace pane found to load content" );
		
		if ( callback instanceof Function )
			externalWorkplaceCallback = callback;
		
		// if no params are given create an empty object
		if ( ! params )
			params = new Object ();
		
		// starts cleaning process
		//gc.clean ();
		
		self.wait ();
		jQuery ( self.pane ).load ( url , params , internalWorkplaceCallback );
	}
	
	
	/**
	 * Handles the different application states.
	 *
	 * @param Boolean progress
	 * @param String cursorType
	 * @return void
	 */
	this.wait = function ( cursor ) {
		if ( ! cursor )
			cursor = self.CURSOR_WAIT;
		
		// if application is in progress state an overlay is not needed
		if ( cursor == self.CURSOR_PROGRESS )
			document.body.style.cursor = self.CURSOR_PROGRESS;
		
		// otherwise append an overlay to prevent user interaction
		else {
			if ( ! overlay )
				createOverlay ();
					
			overlay.show ();
			
			// change cursor
			document.body.style.cursor = self.CURSOR_WAIT;
		}
	}
	
	
	/**
	 * If the application is in wating mode resume processing.
	 *
	 * @return void
	 */
	this.resume = function () {
		if ( ! overlay )
			createOverlay ();
				
		overlay.hide ();
		document.body.style.cursor = "default";
	}
	
	
	/**
	 * @private
	 */
	
	/**
	 * Creates the overlying div to prevent user input if workplace is in applicationWaiting state.
	 *
	 * @return HTMLDivElement
	 */
	function createOverlay () {
		// create a div layer and append it to the body on the top of the application.
		overlay = createElement ( "div" , { "class" : "ui-workplace-overlay" } );
		
		// hide overlay on startup
		overlay.hide ();
		
		// attach and return
		document.body.appendChild ( overlay );
		return overlay;
	}
	
	
	/* ------------------------------------------------------------------------------------------------------------ *
	 *							 																				     																										*
	 * Callbacks																										     																						*
	 *																											     																										*
	 * ------------------------------------------------------------------------------------------------------------ */
	
	/**
	 * Default callback function invoking if new content is loaded into the workplace pane.
	 *
	 * If an external callback is defined it will be applied even though no connection to the remote file can be established.
	 *
	 * @param Sring responseText
	 * @param String textStatus
	 * @param XMLHttpRequest request
	 * @return void
	 */
	function internalWorkplaceCallback ( responseText , textStatus , request ) {
		self.resume ();
		
		if ( textStatus != "success" )
			jAlert ( "Connection lost. Please try again!" );
	}
}
