/*
 * Copyright (c) 2008 FIZON GmbH
 * All rights reserved.
 *
 * $Id: ppopup.js 1739 2008-08-04 07:50:26Z as $
 */
PPopup = function(title, name, width, height, zIndex, position)
{
	var thisObj = this;
	this.contentUrl = false;
	this.titleUrl = false;


	this.construct = function(title, name, width, height, zIndex, position)
	{
		if (typeof PPopup.Objs == 'undefined') {
			PPopup.Objs = {};

			PPopup.WindowResize = function()
			{
				for (var name in PPopup.Objs)
					PPopup.Objs[name].position();
			}
			window.onresize = PPopup.WindowResize;

			/* PopUp Container */
			PPopup.PopupsDiv = document.createElement('div');
			PPopup.PopupsDiv.id = 'ppopups';


			document.getElementsByTagName('body')[0].appendChild(PPopup.PopupsDiv);
		}

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

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

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

		this.contentUrl = 'ppopup.php?popup='+this.name;

		/* POPUP */
		this.popupDiv = document.createElement('div');
		this.popupDiv.id = this.name;
		this.popupDiv.className = 'popup';
		this.popupDiv.style.display = 'none';
		if (width)
			this.popupDiv.style.width = width+'px';
		if (zIndex)
			this.popupDiv.style.zIndex = zIndex;
		PPopup.PopupsDiv.appendChild(this.popupDiv);

		/* HEADER */
		this.headerDiv = document.createElement('div');
		this.headerDiv.className = 'header';
		this.popupDiv.appendChild(this.headerDiv);

		/* TITLE */
		this.titleDiv = document.createElement('div');
		this.titleDiv.className = 'title';
		this.titleDiv.innerHTML = title;
		this.headerDiv.appendChild(this.titleDiv);

		/* closeopen */
		this.closeopenDiv = document.createElement('div');
		this.closeopenDiv.className = 'closeopen';
		var a = document.createElement('a');
		a.innerHTML = '-';
		a.href = '#';
		a.setAttribute('onclick', 'PPopup.Closeopen("'+this.name+'"); return false;');
		this.closeopenDiv.appendChild(a);
		this.headerDiv.appendChild(this.closeopenDiv);

		/* CONTENT */
		this.contentDiv = document.createElement('div');
		this.contentDiv.className = 'content';
		if (height)
			this.contentDiv.style.height = height+'px';
		this.popupDiv.appendChild(this.contentDiv);

		// alles mit clear-divs versehen.
		var clearDiv = document.createElement('div');
		clearDiv.className = 'clear';
		this.popupDiv.appendChild(clearDiv.cloneNode(true));
		this.headerDiv.appendChild(clearDiv.cloneNode(true));

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

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

		PDDController.Get().addDragObj(
            this.popupDiv, this.headerDiv);
	}
	

	/**
	 * title()
	 */
	this.title = function(title)
	{
		this.titleDiv.innerHTML = title;
	}


	/**
	 * content()
	 */
	this.content = function(content)
	{
		this.contentDiv.innerHTML = content;
	}


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

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

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

		return true;
	}


	/**
	 * getMousePosition
	 */
	function getMousePosition(e) {
		var x = 0;
		var y = 0;
		if (!e)
			var e = window.event;

		alert(window.event.x);

		if (e.pageX) {
			x = e.pageX;
			y = e.pageY;
		}
		else if (e.clientX || e.clientY) {
			x  = e.clientX + document.body.scrollLeft;
			x += document.documentElement.scrollLeft;
			y  = e.clientY + document.body.scrollTop;
			y += document.documentElement.scrollTop;
		}

		return [x, y]
	}

	/**
	 * position()
	 */
	this.position = function()
	{
		var x = 0;
		var y = 0;

		if (window.innerWidth) {
			var ww = window.innerWidth;
			var wh = window.innerHeight;
		}
		/* Internetexplorer !"§$%&/() */
		else {
			var ww = document.documentElement.clientWidth;
			var wh = document.documentElement.clientHeight;
		}

		x = (ww - thisObj.popupDiv.clientWidth) / 2;
		y = (wh - thisObj.popupDiv.clientHeight) / 2;

		thisObj.popupDiv.style.left = x+'px';
		thisObj.popupDiv.style.top  = y+'px';
	}


	/**
	 * 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.closeopenDiv.firstChild.innerHTML = '-';
			this.popupDiv.className = 'popup';
			this.setting('open', 1);
			break;

		case false:
			this.closeopenDiv.firstChild.innerHTML = '+';
			this.popupDiv.className = 'popup popup-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(functionHandler)
	{
		this.loadTitle();
		this.loadContent(functionHandler);
	}

	/**
	 * loadContent()
	 */
	this.loadContent = function(functionHandler)
	{
		if (! this.contentUrl)
			return;

		this.pajah.content.replaceInnerHTML(
			this.contentUrl, this.contentDiv, function() {
					thisObj.position();	
					functionHandler();
				}
			);
	}


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


	this.construct(title, name, width, height, zIndex, position);
}


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


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