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

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

		PToolbar.SingletonObj = this;

		this.html = {};
		this.html.out	= document.createElement('div');
		this.html.inset	= document.createElement('div');
		this.html.left	= document.createElement('div');
		this.html.right	= document.createElement('div');
		this.html.clear	= document.createElement('div');
		this.html.info  = document.createElement('ul');
		this.html.menu  = document.createElement('div');
		this.html.menuA = document.createElement('a');
		this.html.app  	= document.createElement('ul');

		this.html.out.id = 'ptoolbar';
		this.html.out.style.zIndex = 1000;
		this.html.out.style.display = 'none';

		this.html.menuA.innerHTML = 'Menü';
		this.html.menuA.href = '#';
		this.html.menuA.setAttribute('onclick', 'PMenu.Click(0); return false;');

		this.html.inset.className = 'in';
		this.html.left.className = 'left';
		this.html.right.className = 'right';
		this.html.clear.className = 'clear';
		this.html.menu.className = 'menu';
		this.html.app.className = 'app';

		this.html.out.appendChild(this.html.inset);
		this.html.inset.appendChild(this.html.left);
		this.html.inset.appendChild(this.html.right);
		this.html.inset.appendChild(this.html.clear);
		this.html.left.appendChild(this.html.menu);
		this.html.left.appendChild(this.html.app);
		this.html.menu.appendChild(this.html.menuA);
		this.html.right.appendChild(this.html.info);

		this.page = PPage.Get();
		this.user = PUser.Get();
		this.menu = PMenu.Get();

		this.infos = {};
		this.addInfo('user', '');
		this.addInfo('clock', '');

		this.apps = {};

		this.page.addObject(this.html.out);
	}


	/**
	 * addInfo()
	 */
	this.addInfo = function(name, content)
	{
		this.infos[name] = document.createElement('li');
		this.infos[name].className = name;
		this.infos[name].innerHTML = content;

		this.buildInfos();
		
		return this.infos[name];
	}


	/**
	 * removeInfo()
	 */
	this.removeInfo = function(name)
	{
		this.infos[name] = null;
		this.buildInfos();
		return true;
	}


	/**
	 * buildInfos()
	 */
	this.buildInfos = function()
	{
		this.html.info.innnerHTML = '';
		for (var name in this.infos)
			this.html.info.appendChild(this.infos[name]);
	}


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

		case false:
			this.html.out.style.display = 'none';
			break;

		default:
			return this.html.out.style.display == 'block';
		}

		return true;
	}


	/**
	 * reload()
	 */
	this.reload = function()
	{
		this.infos.user.innerHTML = this.user.values.benutzer;
		this.setClock();
	}


	/**
	 * setClock()
	 */
	this.setClock = function()
	{
		if (! this.display())
			return;

		this.infos.clock.innerHTML = date('d.m.y H:i:s');

		window.setTimeout('PToolbar.SetClock()', 1000);
	}


	/**
	 * registerApp()
	 */
	this.registerApp = function(app)
	{
		this.apps[app.id] = app;
		this.buildAppList();
	}


	/**
	 * unregisterApp()
	 */
	this.unregisterApp = function(app)
	{
		delete this.apps[app.id];
		this.buildAppList();
	}


	/**
	 * buildAppList()
	 */
	this.buildAppList = function()
	{
		this.html.app.innerHTML = '';

		for (var id in this.apps) {
			var app = this.apps[id];
			
			app.doom.toolbar = document.createElement('li');
			var a = document.createElement('a');
			app.doom.toolbar.appendChild(a);

			a.href = '#';
			a.innerHTML = this.apps[id].setTitle();
			a.setAttribute('onclick', 'PApp.Activate('+id+'); return false;');

			this.html.app.appendChild(app.doom.toolbar);
		}

		return true;
	}


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


	/**
	 * height()
	 *
     * @param	optional	integear	hoehe
	 *
	 * @return	integear	hoehe
	 */
	this.height = function(height)
	{
		if (typeof height != 'undefined') {
			// XXX: hoehe seten
		}
		
		return this.html.out.clientHeight;
	}


	this.construct();
}


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

PToolbar.SetClock = function() { PToolbar.Get().setClock(); }
