/*
 * Copyright (c) 2008 FIZON GmbH
 * All rights reserved.
 *
 * $Id: pdesktop.js 1739 2008-08-04 07:50:26Z as $
 */

PDesktop = function()
{
	/**
	 * construct()
	 */
	this.construct = function()
	{
		if (typeof PDesktop.SingletonObj != 'undefined')
			return;

		PDesktop.SingletonObj = this;

		this.doom = {};
		this.doom.desktop = document.createElement('div');
		this.doom.desktop.id = 'pdesktop';
		this.doom.desktop.style.display = 'none';
		this.doom.desktop.style.position = 'fixed';
		this.doom.desktop.style.left = '0';
		this.doom.desktop.style.top = '0';
		this.doom.desktop.style.width = '100%';
		this.doom.desktop.style.zIndex = '100';

		this.page = PPage.Get();
		this.toolbar = PToolbar.Get();

		this.page.addObject(this.doom.desktop);

		this.apps = {};
	}


	/**
	 * display()
	 */
	this.display = function(action)
	{
		switch (action) {
		case true:
			this.doom.desktop.style.display = 'block';
			this.reload();
			break;

		case false:
			this.doom.desktop.style.display = 'none';
			break;

		default:
			return this.doom.desktop.style.display == 'block';
		}

		return true;
	}


	/**
	 * reload()
	 */
	this.reload = function()
	{
		this.doom.desktop.style.height
			= (window.innerHeight - this.toolbar.height())+'px';
	}


	/**
	 * width()
	 */
	this.width = function()
	{
		return this.doom.desktop.clientWidth;
	}


	/**
	 * height()
	 */
	this.height = function()
	{
		return this.doom.desktop.clientHeight;
	}


	/**
	 * addObject()
	 */
	this.addObject = function(obj)
	{
		return this.doom.desktop.appendChild(obj) ? true : false;
	}


	/**
	 * removeObject()
	 */
	this.removeObject = function(obj)
	{
		return this.doom.desktop.removeChild(obj) ? true : false;
	}


	/**
	 * registerApp()
	 */
	this.registerApp = function(app)
	{
		this.apps[app.id] = app;
		return this.addObject(app.doom.app) ? true : false;
	}


	/**
	 * unregisterApp()
	 */
	this.unregisterApp = function(app)
	{
		delete this.apps[app.id];
		return this.removeObject(app.doom.app) ? true : false;
	}


	/**
	 * activateApp()
	 */
	this.activateApp = function(app)
	{
		for (var id in this.apps) {
			this.apps[id].doom.app.className = 'papp';
		}

		app.doom.app.className = 'papp active'
	}


	this.construct();
}


/**
 * PDesktop.Get()
 */
PDesktop.Get = function()
{
	if (typeof PDesktop.SingletonObj == 'undefined')
		new PDesktop;
	return PDesktop.SingletonObj;
}
