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

PApp = function(app)
{
	/**
	 * construct()
	 */
	this.construct = function(app)
	{
		this.id = PApp.Objs.length;
		PApp.Objs.push(this);

		this.app = app;
		this.doom = {menu:{}, content:{}};
		this.title = app.title;
		this.user = PUser.Get();
		this.menu = PMenu.Get();
		this.loaded = false;
		this.opened = false;
		this.autostart = false;
	}


	/**
	 * load()
	 */
	this.load = function()
	{
		// wenn der Benutzer nicht angemeldet ist
		// wird auch keine Programm inizialisiert
		if (! this.user.logedin)
			return false;

		// es darf nicht wiederhollt geladen werden
		if (this.loaded)
			return true;


		this.doom.app 				= document.createElement('div');
		this.doom.menu.outside		= document.createElement('div');
		this.doom.menu.inside		= document.createElement('div');
		this.doom.menu.left			= document.createElement('div');
		this.doom.menu.right		= document.createElement('div');
		this.doom.menu.clear		= document.createElement('div');
		this.doom.menu.list			= document.createElement('ul');
		this.doom.menu.title		= document.createElement('li');
		this.doom.menu.options		= document.createElement('ul');
		this.doom.content.outside	= document.createElement('div');
		this.doom.content.inside	= document.createElement('div');
		this.doom.resizeCorner		= document.createElement('div');

		this.doom.app.id = 'papp_'+this.id;
		this.doom.app.style.position = 'absolute';
		this.doom.app.className				= 'papp';
		this.doom.menu.outside.className	= 'menu-out';
		this.doom.menu.inside.className		= 'menu-in';
		this.doom.menu.left.className		= 'left';
		this.doom.menu.right.className		= 'right';
		this.doom.menu.clear.className		= 'clear';
		this.doom.content.outside.className	= 'content-out';
		this.doom.content.inside.className	= 'content-in';


		this.doom.resizeCorner.style.position = 'absolute';
		this.doom.resizeCorner.style.backgroundColor = '#999';
		this.doom.resizeCorner.style.width = '15px';
		this.doom.resizeCorner.style.height = '15px';

		this.doom.menu.outside.setAttribute('onclick', 'PApp.Activate('+this.id+');');

		this.doom.app.appendChild(this.doom.menu.outside);
		this.doom.app.appendChild(this.doom.content.outside);
		this.doom.menu.outside.appendChild(this.doom.menu.inside);
		this.doom.menu.inside.appendChild(this.doom.menu.left);
		this.doom.menu.inside.appendChild(this.doom.menu.right);
		this.doom.menu.inside.appendChild(this.doom.menu.clear);
		this.doom.menu.left.appendChild(this.doom.menu.list);
		this.doom.menu.list.appendChild(this.doom.menu.title);
		this.doom.menu.right.appendChild(this.doom.menu.options);
		this.doom.content.outside.appendChild(this.doom.content.inside);
		this.doom.app.appendChild(this.doom.resizeCorner);

		this.addWinOption('-', 'PApp.Minimize('+this.id+')');
		this.addWinOption('+', 'PApp.Close('+this.id+')');
	
		this.setTitle(this.title);

		this.desktop = PDesktop.Get();
		this.toolbar = PToolbar.Get();

		// Programm ins Menü einhaengen
		this.menuEntry = this.menu.addApp(this.title, "PApp.Open("+this.id+")");

		this.app.__load();
		this.loaded = true;

		PDDController.Get().addObj(
			this.doom.app, this.doom.menu.outside, this.doom.resizeCorner, false, PApp.Resize);


		return true;
	}


	/**
	 * unload()
	 */
	this.unload = function()
	{
		// ein nicht geladenes Programm kann 
		// nicht noch mal ungeladen werden.
		if (! this.loaded)
			return false;

		// wenn das Programm offen ist
		// schliessen wir es vorher.
		if (this.opened)
			this.close();

		// Programm aus dem Menü aushanegen
		this.menuEntry.destroy();

		// Programm selber entladen
		this.app.__unload();

		this.loaded = false;

		return true;
	}


	/**
	 * open()
	 */
	this.open = function()
	{
		if (! this.loaded) {
			if (! this.load())
				return false;
		}

		if (this.opened)
			return true;

		this.app.__open();
		this.desktop.registerApp(this);
		this.toolbar.registerApp(this);
		this.opened = true;
		this.doom.app.style.width = this.desktop.doom.desktop.clientWidth+'px';
		this.doom.app.style.height = (this.desktop.doom.desktop.clientHeight-25)+'px';
		this.resize();
		this.activate();

		return true;
	}


	/**
	 * close()
	 */
	this.close = function()
	{
		if (! this.opened)
			return true;

		this.deactivate();
		this.desktop.unregisterApp(this);
		this.toolbar.unregisterApp(this);
		this.app.__close();
		this.doom.content.inside.innerHTML = '';
		this.opened = false;

		var todelete = null;
		for (var i = 0; i < PApp.ActiveHistory.length; i++)
			if (PApp.ActiveHistory[i] == this.id)
				todelete = i;
		if (todelete != null)
			PApp.ActiveHistory.splice(todelete, 1);

		if (PApp.ActiveHistory.length > 0)
			PApp.Activate(PApp.ActiveHistory[0]);
	}


	/**
	 * activate()
	 */
	this.activate = function()
	{
		this.desktop.activateApp(this);
		this.toolbar.activateApp(this);
		this.resize();
		
		var todelete = null;
		for (var i = 0; i < PApp.ActiveHistory.length; i++)  
			if (PApp.ActiveHistory[i] == this.id) 
				todelete = i;
		if (todelete != null)
			PApp.ActiveHistory.splice(todelete, 1)

		PApp.ActiveHistory.unshift(this.id);

		for (var i = 0; i < PApp.ActiveHistory.length; i++) {
			var app = PApp.Get(PApp.ActiveHistory[i]);
			
			app.doom.app.style.zIndex = PApp.ActiveHistory.length - i + 500;
			app.doom.content.inside.style.overflow = (app.id == this.id)
				? 'scroll' : 'hidden';
		}
	}


	/**
	 * resize()
	 */
	this.resize = function()
	{
		this.doom.content.inside.style.width = (this.doom.app.clientWidth - this.doom.menu.outside.clientWidth - 2)+'px'; 
		this.doom.content.inside.style.height = (this.doom.app.clientHeight - this.doom.menu.outside.clientHeight - 3)+'px'; 

		this.doom.resizeCorner.style.left = (this.doom.app.clientWidth - 15)+'px';
		this.doom.resizeCorner.style.top = (this.doom.app.clientHeight - 15)+'px';
	}

	
	/**
	 * deactivate()
	 */
	this.deactivate = function()
	{

	}


	/**
	 * minimize()
	 */
	this.minimize = function()
	{

	}


	/**
	 * setTitle()
	 */
	this.setTitle = function()
	{
		if (this.setTitle.arguments.length == 0)
			return this.doom.menu.title.innerHTML;
	
		this.doom.menu.title.innerHTML = this.setTitle.arguments[0];
		return true;
	}


	/**
	 * addWinOption()
	 */
	this.addWinOption = function(title, onclick)
	{
		var option = document.createElement('li');
		var a = document.createElement('a');
		option.appendChild(a);
		this.doom.menu.options.appendChild(option);

		a.innerHTML = title;
		a.href = '#';
		a.setAttribute('onclick', onclick+'; return false;');

		return option;
	}


	/**
	 * removeWinOption()
	 */
	this.removeWinOption = function(option)
	{
		return this.doom.menu.options.removeChild(option) ? true : false;
	}


	/**
	 * addObject()
	 */
	this.addObject = function(obj)
	{
		this.doom.content.inside.appendChild(obj);
		return true;
	}


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


	/**
	 * addWindow()
	 */
	this.addWindow = function(win)
	{
		this.addObject(win.html.window);
	}


	this.construct(app);
}


PApp.Objs = [];
PApp.ActiveHistory = [];


/**
 * PApp.Get()
 */
PApp.Get = function(id)
{
	return PApp.Objs[id];
}


/**
 * PApp.Load()
 */
PApp.Load = function(id)
{
	if (PApp.Load.arguments.length == 0) {
		for (var i = 0; i < PApp.Objs.length; i++)
			PApp.Objs[i].load();
	}
	else {
		PApp.Get(id).load();
	}
}


/**
 * PApp.Unload()
 */
PApp.Unload = function(id)
{
	if (PApp.Unload.arguments.length == 0) {
		for (var i = 0; i < PApp.Objs.length; i++)
			PApp.Objs[i].unload();
	}
	else {
		PApp.Get(id).unload();
	}
}


/**
 * PApp.Open()
 */
PApp.Open = function(id)
{
	return PApp.Get(id).open();
}


/**
 * PApp.Close()
 */
PApp.Close = function(id)
{
	return PApp.Get(id).close();
}


/**
 * PApp.Activate()
 */
PApp.Activate = function(id)
{
	return PApp.Get(id).activate();
}


/**
 * PApp.Minimize
 */
PApp.Minimize = function(id)
{
	if (PApp.Minimize.arguments.length == 0) {
		for (var i = 0; i < PApp.Objs.length; i++)
			PApp.Objs[i].minimize();
	}
	else {
		PApp.Get(id).minimize();
	}

}


/**
 * PApp.Autostart()
 */
PApp.Autostart = function()
{
	for (var i = 0; i < PApp.Objs.length; i++)
		if (PApp.Objs[i].autostart)
			PApp.Objs[i].open();
}


PApp.Resize = function()
{
	PApp.Get(PDDController.SingletonObj.ddID.replace('papp_', '')).resize();
}
