/**
 * popup
 * Javascript Interface
 *
 *
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2007-08-31
 * @package popup
 */

/**
 * All Popup objects.
 * POPUP_WINDOWS[ popupId ] = HTMLnodeObject
 *
 */
var POPUP_WINDOWS = new Array();

/**
 * (constructor)
 * create a new Popup object.
 *
 * @param string pTitel
 * @param string pSrc
 * @param int pWidth
 * @param int pHeight
 *
 */
function Popup ( pTitel, pSrc, pWidth, pHeight, pTop, pLeft )
{
	this.popupId = "popup_" + Math.random();
	this.opacityValue = 30;
	this.lablebarHeight = 21;
	this.disableCurtain = false;
	this.fixed = false;
	this.zIndex = 100000;

	this.titel = pTitel;
	this.src = pSrc;
	this.width = pWidth;
	this.height = pHeight;
	this.top = pTop ? pTop : "auto";
	this.left = pLeft ? pLeft : "auto";

	this.ns6 = document.getElementById && !document.all;
	this.curtain = false;
	this.popup = false;
	this.mousePressed = false;
	this.detachedWindow = false;


	/**
	 * Close this popup window.
	 *
	 */
	this.close = function () {
		// close detached window if exist
		if( this.detachedWindow ){
			this.detachedWindow.close();
			return
		}
		// return if no popup exist
		if( !this.popup )
 			return;

 		// remove popup from document tree
		document.getElementsByTagName("body")[0].removeChild( this.popup );
		this.popup = false;

		// close curtain if no longer requested by an other popup
		for(key in POPUP_WINDOWS){
			if( !POPUP_WINDOWS[ key ].popup && this.curtain && !POPUP_WINDOWS[ key ].disableCurtain  ){
				document.getElementsByTagName("body")[0].removeChild( this.curtain );
				this.curtain = false;
				break;
			}
		}
	}


	/**
	 * Detach this popup window. Open a new Browser window to display src.
	 *
	 */
	this.detach = function () {
		this.close();
		this.detachedWindow = window.open( this.src, this.popupId, "width=" + this.width + ",height=" + this.height + ",left=10,top=10,resizable=no");
		this.detachedWindow.focus();
	}


	/**
	 * Set focus to this popup window
	 *
	 */
	this.focus = function () {
		// focus on detached window
		if( this.detachedWindow ){
			this.detachedWindow.focus();
			return
		}
		if( !this.popup || this.popup.firstChild.className )
			return;

		this.popup.firstChild.className = 'popup_has_focus';
		this.popup.style.zIndex = this.zIndex + 10;

		if( this.popup.firstChild.nextSibling.nextSibling )
			this.popup.removeChild( this.popup.firstChild.nextSibling.nextSibling );


		// remove focus from all other windows
		for(key in POPUP_WINDOWS){
			if( key != this.popupId && POPUP_WINDOWS[ key ].popup ){
				POPUP_WINDOWS[ key ].popup.style.zIndex = this.zIndex + 1;
				POPUP_WINDOWS[ key ].popup.firstChild.removeAttribute( "class" );

				if( !POPUP_WINDOWS[ key ].popup.firstChild.nextSibling.nextSibling ){
					tmp = document.createElement( "div" );
					onclickatt = document.createAttribute( "onclick" );
					onclickatt.nodeValue = "POPUP_WINDOWS['" + POPUP_WINDOWS[ key ].popupId + "'].focus();";
					tmp.setAttributeNode( onclickatt );
					//
					POPUP_WINDOWS[ key ].popup.appendChild( tmp );
					//
					tmp.style.position = "fixed";
					tmp.style.zIndex = POPUP_WINDOWS[ key ].zIndex + 2;
					tmp.style.top = POPUP_WINDOWS[ key ].top;
					tmp.style.left = POPUP_WINDOWS[ key ].left;
					tmp.style.width = POPUP_WINDOWS[ key ].width + "px";
					tmp.style.height = (POPUP_WINDOWS[ key ].height + POPUP_WINDOWS[ key ].lablebarHeight) + "px";
					tmp.style.backgroundColor = "black";
					// set opacity
					percentage = 80 - this.opacityValue;
				   	filterValue = "Alpha(opacity=" + percentage + ")"; // (IE-)Filter value
				   	opacityValue = "" + percentage/100; // CSS opacity
				   	count = (tmp.length) ? tmp.length : 1;
				   	for(i=0; i<count; i++) {
					    objStyle = (tmp.length) ? tmp[i].style : tmp.style;
					    objStyle.filter=filterValue; // "Alpha" filter (IE)
					    objStyle.MozOpacity=opacityValue; // "-moz-opacity" (Mozilla)
					    objStyle.KhtmlOpacity=opacityValue; // "-khtml-opacity" (Konqueror/Safari)
					    objStyle.opacity=opacityValue; // "opacity" (CSS-3-Browser)
					}

				}
			}
		}
	}



	/**
	 * Set popup window titel. Rewrite titel if called after a popup is displayed.
	 *
	 */
	this.get_frame  = function () {
		if( this.popup )
			return this.popup.firstChild.nextSibling.nextSibling.nextSibling.firstChild.firstChild;
		else
			return false;
	}


	/**
	 * Open current popup window and display as <div>-container. Drop curtain if requested.
	 *
	 */
 	this.display = function () {
 		if( this.popup ){
 			this.focus();
 			return;
 		}

		// create a curtain if no one exist
		if( !this.disableCurtain && !this.curtain ) {
			// create curtain
			curtain = document.createElement( "div" );
			id = document.createAttribute( "id" );
			id.nodeValue = "popup__curtain";
			curtain.setAttributeNode( id );
			document.getElementsByTagName("body")[0].appendChild( curtain );

			this.curtain =  document.getElementById("popup__curtain");

			// set some style css for curtain
			this.curtain.style.position = "fixed";
			this.curtain.style.zIndex = this.zIndex;
			this.curtain.style.top = 0;
			this.curtain.style.left = 0;
			this.curtain.style.backgroundColor = "black";
			this.curtain.style.width = (this.ns6?window.innerWidth:document.body.clientWidth) + "px";
			this.curtain.style.height = (this.ns6?window.innerHeight:document.body.clientHeight) + "px";

			// set opacity
			percentage = 100 - this.opacityValue;
		   	filterValue = "Alpha(opacity=" + percentage + ")"; // (IE-)Filter value
		   	opacityValue = "" + percentage/100; // CSS opacity
		   	count = (this.curtain.length) ? this.curtain.length : 1;
		   	for(i=0; i<count; i++) {
			    objStyle = (this.curtain.length) ? this.curtain[i].style : this.curtain.style;
			    objStyle.filter=filterValue; // "Alpha" filter (IE)
			    objStyle.MozOpacity=opacityValue; // "-moz-opacity" (Mozilla)
			    objStyle.KhtmlOpacity=opacityValue; // "-khtml-opacity" (Konqueror/Safari)
			    objStyle.opacity=opacityValue; // "opacity" (CSS-3-Browser)
			}
		}

		// create new popup HTML DIV
		popupcontainer = document.createElement( "div" );
		id = document.createAttribute( "id" );
		id.nodeValue = this.popupId;
		popupcontainer.setAttributeNode( id );
		document.getElementsByTagName("body")[0].appendChild( popupcontainer );

		// get popup and fill it with code
		this.popup = document.getElementById( this.popupId );
		this.popup.className = 'popup';
		this.popup.style.position = "fixed";
		this.popup.style.width = this.width + "px";
		this.popup.style.height = (this.height + this.lablebarHeight) + "px";
		this.popup.style.zIndex = this.zIndex + 1;

		this.popup.innerHTML = 	'<span class="header_before"></span><span class="header_after"></span><div class="head" onmousemove="POPUP_WINDOWS[ \'' + this.popupId + '\' ].move(event);" onmousedown="if( !POPUP_WINDOWS[ \'' + this.popupId + '\' ].fixed ){ this.style.cursor = \'move\'; POPUP_WINDOWS[ \'' + this.popupId + '\' ].mousePressed = true; }" onmouseup="this.style.cursor = \'auto\'; POPUP_WINDOWS[ \'' + this.popupId + '\' ].mousePressed = false;" onmouseout="this.style.cursor = \'auto\'; POPUP_WINDOWS[ \'' + this.popupId + '\' ].mousePressed = false;">' +
		'<h2>' + this.titel + '</h2>' +
		'<a href="javascript:POPUP_WINDOWS[ \'' + this.popupId + '\' ].close();" title="Fenster schließen." class="close"><span>close</span></a>' +
		'</div>' +
		'<div class="iframe0">' +
		'<div class="iframe1">' +
		'<iframe src="' + this.src + '" width="100%" height="' + this.height + 'px"></iframe>' +
		'</div></div>' +
		'<span class="status_before"></span><span class="status_after"></span>' +
		'<div class="status"></div>';

		// set some style css to popup container
		this.set_top();
		this.set_left();
		this.popup.style.display = "block";

		// add popup to POPUP_WINDOWS and focus
		POPUP_WINDOWS[ this.popupId ] = this;
		this.focus();
	}


	/**
	 * (private method)
	 * Move this popup window by pressing and moving the mouse. Called from popup windows own action listener.
	 *
	 * @param Event event
	 */
	this.move = function ( event ) {
		if( !this.mousePressed ){
			this.move_prevY = false;
		 	this.move_prevX = false;
			return;
		}

		// browserweiche
		if ( document.getElementById && !document.all ){
			// ns6
	  		posX = event.clientX + window.pageXOffset;
	  		posY = event.clientY + window.pageYOffset;
		}
		else{
	  		posX = window.event.clientX + document.documentElement.scrollLeft;
	  		posY = window.event.clientY + document.documentElement.scrollTop;
		}

		if( this.move_prevX ){
			this.set_top( this.top + ( posY - this.move_prevY ) );
			this.set_left( this.left + ( posX - this.move_prevX ) );
		}

		 this.move_prevY = posY;
		 this.move_prevX = posX;
	}


	/**
	 * Move this popup window to pLeft position.
	 *
	 * @param int pLeft
	 */
	this.set_left = function ( pLeft ) {
		if( pLeft )
			this.left = pLeft;
		if( this.left == "auto" )
			this.left = Math.ceil( ( (this.ns6?window.innerWidth:document.body.clientWidth) - this.width) / 2 );

		// browserweiche
		if( this.ns6 ) this.popup.style.left = this.left + "px";
		else this.popup.style.pixelLeft = this.left + "px";
	}


	/**
	 * Set popup window titel. Rewrite titel if called after a popup is displayed.
	 *
	 * @param int pLeft
	 */
	this.set_titel  = function ( pTitel ) {
		this.titel = pTitel;

		if( this.popup )
			this.popup.firstChild.firstChild.innerHTML = this.titel;
	}


	/**
	 * Move this popup window to pTop position.
	 *
	 * @param int pTop
	 */
	this.set_top  = function ( pTop ) {
		if( pTop )
			this.top = pTop;
		if( this.top == "auto" )
			this.top = Math.ceil( ( (this.ns6?window.innerHeight:document.body.clientHeight) - (this.height + this.lablebarHeight )) / 2 );

		// browserweiche
		if( this.ns6 ) this.popup.style.top = this.top + "px";
		else this.popup.style.pixelTop = this.top + "px";
	}

} // End of Popup
