/*
 * Copyright (c) 2008 FIZON GmbH
 * All rights reserved.
 *
 * $Id: pwindow.js 1739 2008-08-04 07:50:26Z as $
 */
PWindow = function(title, name)
{
	this.contentUrl = false;
	this.titleUrl = false;


	this.constructor = function(title, name)
	{
		if (typeof PWindow.Objs == 'undefined')
			PWindow.Objs = {};

		/* ID */
		this.id = PWindow.Objs.length;

		/* NAME */
		if (typeof name == 'undefined')
			name = 'pwindow_'+this.id;
		this.name = name;

		/* Object speichern */
		PWindow.Objs[this.name] = this;

		this.contentUrl = 'pwindow.php?window='+this.name;
		this.html = {};

		/* WINDOW */
		this.html.window = document.createElement('div');
		this.html.head = document.createElement('div');
		this.html.content = document.createElement('div');
		this.html.left = document.createElement('div');
		this.html.right = document.createElement('div');
		this.html.clear = document.createElement('div');
		this.html.title = document.createElement('div');
		this.html.closeopen = document.createElement('div');
		this.html.closeopenA = document.createElement('a');

		this.html.window.className = 'window';
		this.html.head.className = 'head';
		this.html.content.className = 'content';
		this.html.left.className = 'left';
		this.html.right.className = 'right';
		this.html.clear.className = 'clear';
		this.html.title.className = 'title';
		this.html.closeopen.className = 'closeopen';

		this.html.window.appendChild(this.html.head);
		this.html.window.appendChild(this.html.content);
		this.html.head.appendChild(this.html.left);
		this.html.head.appendChild(this.html.right);
		this.html.head.appendChild(this.html.clear);
		this.html.left.appendChild(this.html.title);
		this.html.right.appendChild(this.html.closeopen);
		this.html.closeopen.appendChild(this.html.closeopenA);

		this.html.window.id = this.name;
		this.html.window.style.display = 'none';
		this.html.head.setAttribute('onclick', 'PWindow.Closeopen("'+this.name+'"); return false;');
		this.html.title.innerHTML = title;
		this.html.closeopenA.innerHTML = '-';
		this.html.closeopenA.href = '#';

		/* SETTINGS */
		this.open(this.open());

		/* Asyncrones nachladen.. */
		this.pajah = {title: new PaJaH, content: new PaJaH}
	}


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

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

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

		return true;
	}


	/**
	 * changeDisplay()
	 */
	this.changeDisplay = function()
	{
		this.display(! this.display());
	}


	/**
	 * setting()
	 */
	this.setting = function(setting, value)
	{
		var cname = this.name+'_setting_'+setting;

		// Lesen
		if (typeof value == 'undefined') {
			var cvalue = PCookie.Get(cname);
			if (! cvalue) {
				cvalue = -1;
				this.setting(setting, cvalue);
			}
			return cvalue;
		}

		// Speichern
		else {
			return PCookie.Set(cname, value);
		}
	}


	/**
	 * open()
	 */
	this.open = function(action)
	{
		switch (action) {
		case true:
			this.html.closeopenA.innerHTML = '-';
			this.html.window.className = 'window';
			this.setting('open', 1);
			break;

		case false:
			this.html.closeopenA.innerHTML = '+';
			this.html.window.className = 'window window-closed';
			this.setting('open', 0);
			break;

		default:
			var v = this.setting('open');
			if (v == -1) {
				v = 1;
				this.setting('open', v);
			}
			return (v == 1);
		}

		return true;
	}


	/**
	 * closeopen()
	 */
	this.closeopen = function(closeopen)
	{
		this.open(! this.open());
	}


	/**
	 * load()
	 */
	this.load = function()
	{
		this.loadTitle();
		this.loadContent();
	}

	/**
	 * loadContent()
	 */
	this.loadContent = function()
	{
		if (this.contentUrl)
			this.pajah.content.replaceInnerHTML(this.contentUrl, this.html.content);
	}


	/**
	 * loadTitle()
	 */
	this.loadTitle = function()
	{
		if (this.titleUrl)
			this.pajah.title.replaceInnerHTML(this.titleUrl, this.html.title);
	}


	this.constructor(title, name);
}


/**
 * PWindow.GetWindowByName()
 */
PWindow.GetWindowByName = function(name)
{
	if (typeof PWindow.Objs[name] == 'undefined')
		return false;
	return PWindow.Objs[name];
}


/**
 * PWindow.Closeopen()
 */
PWindow.Closeopen = function(name)
{
	return PWindow.GetWindowByName(name).closeopen();
}


/**
 * PWindow.ChangeDisplay()
 */
PWindow.ChangeDisplay = function(name)
{
	return PWindow.GetWindowByName(name).changeDisplay();
}
