/**
	pics.window.js
	Desc:	Framework für DHTML-Fenster

	basiert auf pics.base
	
	Author: Christian Grösswang
	
	20061227,gc - v0.01		Prototype + Test
	20070801,gc - v1.00		first release
	20071105,gc - v1.01		Close macht jetzt auch destroy
	20080410,gc - v1.02		Erweiterung um Move, sowie Option HideOnly bei Close
	20080810,gc	- v1.03		BUG: zerstörte Fenster werden jetzt auch aus Liste entfernt
**/




/*
	Funktionsübersicht 
	
	Window-Klasse + -Framework:
		pics.Windows.Window 		Das Fenster-Objekt
		pics.Windows.addWindow		neues Fenster erstellen
		pics.Windows.getWindow		bestehendes Fenster zurückgeben
		pics.Windows.showWindow		Fenster anzeigen
		pics.Windows.hideWindow		Fenster ausblenden
		pics.Windows.closeWindow	Fenster schliessen
		pics.Windows.msgbox			Meldung anzeigen ( mit unterschiedlichen Buttons )
		pics.Windows.infobox		Infobox anzeigen ( ohne Titelliste, nur für Textausgabe )
		pics.Windows.inputbox		Eingabefenster anzeigen ( 1 oder mehrere Eingabefelder + OK/Abbrechen oder Weiter/zurück )
*/

// Konstanten für die Klasse
	if (typeof C_PICSWIN_ImagePath == 'undefined') C_PICSWIN_ImagePath = "images/";

// Constructor, falls das PICS-Framework nicht geladen ist
	if (typeof pics == 'undefined') pics = {};

/* 
 *	====================================================================================
 *	===== FRAMEWORK: pics.Windows													====
 *	====================================================================================
 */

	// Constructor
	if (typeof pics.Windows == 'undefined') pics.Windows = {};

/* ========================= Basis-Funktionen ========================= */
	
	pics.Windows.list=new Array();		// Liste mit den verwalteten Fenstern
	pics.Windows.active="";				// Name des aktiven Fensters
	pics.Windows.dragElement=null;		// Element für Drag & Drop
	pics.Windows.maxLevel=100;			// aktuell vorderstes Element

/* ========================= globle Hilfsfunktionen für Windows ========================= */

	/**
	 * pics.Windows.closeWindow
	 *		Fenster anhand des Namens bzw der ID schliessen 
	 *		@param pWindowName 		Name des Fensters
	 *		@param pID 				ID des Fensters in der Liste
	 */
	pics.Windows.closeWindow = function(pWindowName, pID)
	{
		if (typeof pWindowName == 'undefined') pWindowName='';
		if (pWindowName == '') pWindowName=pics.Windows.list[pID];
		
		// Namen des Fensters holen
		pics.Windows.list[pWindowName].hide();
		if (pics.Windows.list[pWindowName].destroyOnClose) 
		{
			pics.Windows.list[pWindowName].destroy();
			// und auch aus der Liste entfernen
			delete(pics.Windows.list[pWindowName]);
		}
	};

	/**
	 * pics.Windows.addWindow
	 *	Neues Fenster in der Auflistung erstellen
	 */
	pics.Windows.addWindow = function(pWindowName)
	{
	
		if (typeof pWindowName == 'undefined') 
		{
			var jetzt=new Date();
			pWindowName='win_'+jetzt.getTime();
		}
	
		// Fenster erstellen
		oWin=new pics.Windows.Window();
		// an die Liste aufnehmen
		pics.Windows.list[pWindowName]=oWin;
		pics.Windows.list[oWin.id]=pWindowName;
		return oWin;
	};

	/**
	 * pics.Windows.getWindow
	 *	passendes Fenster-Obejekt zurückgeben
	 */
	pics.Windows.getWindow = function(pWindowName)
	{
		return pics.Windows.list[pWindowName];
	};

	/**
	 * pics.Windows.hideWindow
	 *	Fenster ausblenden
	 *		@param pWindowName 		Name des Fensters
	 *		@param pID 				ID des Fensters in der Liste
	 */
	pics.Windows.hideWindow = function(pWindowName, pID)
	{
		if (typeof pWindowName == 'undefined') pWindowName='';
		if (pWindowName == '') pWindowName=pics.Windows.list[pID];
		// Fenster erstellen
		pics.Windows.list[pWindowName].hide();
	};

	/**
	 * pics.Windows.showWindow
	 *	Fenster einblenden
	 *		@param pWindowName 		Name des Fensters
	 *		@param pID 				ID des Fensters in der Liste
	 */
	pics.Windows.showWindow = function(pWindowName, pID)
	{
		if (typeof pWindowName == 'undefined') pWindowName='';
		if (pWindowName == '') pWindowName=pics.Windows.list[pID];
		// Fenster erstellen
		pics.Windows.list[pWindowName].show();
	};

	/**
	 * pics.Windows.isActive
	 *	Ist das das aktive Fenster ?
	 */
	pics.Windows.isActive = function(pWindowName)
	{
		if (pics.Windows.active==pWindowName) return true;
		return false;
	};


	/**
	 * pics.Windows.activate
	 *	das entsprechende Fenster aktivieren, ein eventuell vorher aktives deaktiviern
	 */
	pics.Windows.activate = function(pWindowName)
	{
		// letztes aktives deaktivieren
		if (pics.Windows.active!="") pics.Windows.hideWindow(pics.Windows.active);
		pics.Windows.active=pWindowName;
		pics.Windows.showWindow(pWindowName);
		return false;
	};


	/**
	 * pics.Windows.infoBox
	 *	Neue InfoBox in der Auflistung erstellen
	 */
	pics.Windows.infoBox = function(pWindowName)
	{
	
		if (typeof pWindowName == 'undefined') 
		{
			var jetzt=new Date();
			pWindowName='win_'+jetzt.getTime();
		}
	
		// Fenster erstellen
		oWin=new pics.Windows.Window();
		// dann noch formatieren
		oWin.showTitleBar=false;
		oWin.classWindow='pics_InfoBoxWindow';
		oWin.classContent='pics_InfoBoxContent';
		oWin.classButtons='pics_InfoBoxButtonContainer';

		// Stil Content zuweisen
		oWin.content.setAttribute("class",oWin.classContent);
		if (pics.isIE) oWin.content.className=oWin.classContent;
		
		// Stil Buttons zuweisen
		oWin.buttons.setAttribute("class",oWin.classButtons);
		if (pics.isIE) oWin.buttons.className=oWin.classButtons;
		
		// an die Liste aufnehmen
		pics.Windows.list[pWindowName]=oWin;
		pics.Windows.list[oWin.id]=pWindowName;
		return oWin;
	};
	
/* ========================= Hilfsfunktionen für Windows Drag&Drop ========================= */

	/**
	 * pics.Windows.dragStart
	 *	Drag beginnen
	 */
	pics.Windows.dragStart = function(pEvent)
	{
		// Erlaubt das Fenster Drag & Drop ?
		if (!this.parentObject.dragEnable) return false;
		
	
		// Positionen ermitteln
   		mouseX = (pEvent) ? pEvent.pageX : event.clientX + document.body.scrollLeft;
		mouseY = (pEvent) ? pEvent.pageY : event.clientY + document.body.scrollTop;
		
		winX = this.parentObject.window.offsetLeft;
		winY = this.parentObject.window.offsetTop;
		
		// alert("CLICK: "+aktivX+'/'+aktivY+' : '+this.tagName);
		// this.parentObject.setContent(mouseX+'/'+mouseY +' === '+winX+'/'+winY+' zIndex:'+this.parentObject.window.style.zIndex);
		
		// aktuelle Position merken
		this.parentObject.dragX=mouseX - winX;
		this.parentObject.dragY=mouseY - winY;

		pics.Windows.dragElement = this.parentObject;

		// jetzt noch in den Vordergrund: Quick und Dirty-Implementierung
		if (this.parentObject.window.style.zIndex!=pics.Windows.maxLevel)
		{
			pics.Windows.maxLevel++;
			this.parentObject.window.style.zIndex=pics.Windows.maxLevel;
		}
		
		// Eventhandler auf MOVE
		document.onmousemove = pics.Windows.dragMove;
		document.onmouseup = pics.Windows.dragStop;
	}	// pics.Windows.dragStart
	
	/**
	 * pics.Windows.dragMove
	 *	Drag beginnen
	 */
	pics.Windows.dragMove = function(pEvent)
	{
   		mouseX = (pEvent) ? pEvent.pageX : event.clientX + document.body.scrollLeft;
		mouseY = (pEvent) ? pEvent.pageY : event.clientY + document.body.scrollTop;
		if(pics.Windows.dragElement!=null) 
		{
			pics.Windows.dragElement.window.style.left = (mouseX - pics.Windows.dragElement.dragX) + "px";
			pics.Windows.dragElement.window.style.top = (mouseY - pics.Windows.dragElement.dragY) + "px";
		}
	}	// pics.Windows.dragMove

	/**
	 * pics.Windows.dragStop
	 *	Drag beenden
	 */
	pics.Windows.dragStop = function(pEvent)
	{
		pics.Windows.dragElement = null;
	}	// pics.Windows.dragStop
		
/* 
 *	====================================================================================
 *	===== OBJEKT: pics.Windows.Window 												====
 *	====================================================================================
 */
	
	/**
	 * pics.Windows.Windows
	 *	Konstruktor für ein neues Windows-Objekt
	 */
	pics.Windows.Window = function() 
	{
	
		// eindeutige ID zuordnen (wir nehmen da die aktuelle Zeit )
		var jetzt=new Date();
		this.id=jetzt.getTime();
	
		// Verweis auf das Fenster selbst ( äusserer DIV )
		this.window=null;
		pics.Windows.maxLevel++;
		this.level=pics.Windows.maxLevel;
		this.destroyOnClose=true;
		
		// Verweis auf Modal-Layer
		this.windowmodal=null;
		
		// proportionen des Fensters
		this.width=0;
		this.height=0;

		// Eigenschaften für das Aussehen: Defaultwerte vorbelegen
		this.showTitleBar=true;
		this.showCloseButton=true;
		this.classTitleBar='pics_Titlebar';
		this.classWindow='pics_Window';
		this.classContent='pics_Content';
		this.classButtons='pics_Buttons';
		this.classIFrame='pics_IFrame';
		this.classModal='pics_Modal';
	
		// Eigenschaften für Drag & Drop
		this.dragX=0;
		this.dragY=0;
		this.dragEnable=true;
		
	
		// Objekt für Titelleiste erstellen
		this.titleBar = document.createElement("div");
			this.titleBar.setAttribute("class",this.classTitleBar);
			if (pics.isIE) this.titleBar.className=this.classTitleBar;
		
		// Objekt für Content erstellen
		this.content = document.createElement("div");
			this.content.setAttribute("class",this.classContent);
			if (pics.isIE) this.content.className=this.classContent;
	
		// Objekt für Buttons erstellen
		this.buttons = document.createElement("div");
			this.buttons.setAttribute("class",this.classButtons);
			if (pics.isIE) this.buttons.className=this.classButtons;
	
		// Objektverweise platzieren
		this.titleBar.parentObject=this;
		
		// Timer für das Fenster merken
		this.closeTimer=null;
	
	} // pics.Windows.Window
	
	/**
	 * pics.Windows.Window.prototype.create
	 *	das eigentliche Fenster erzeugen
	 */
	pics.Windows.Window.prototype.create = function(pX, pY, pWidth, pHeight, pContent, pTitle, pModal)
	{
		// undefinierte Parameter vorbelegen
		if (typeof pX == 'undefined') pX=0;
		if (typeof pY == 'undefined') pY=0;
		if (typeof pWidth == 'undefined') pWidth=200;
		if (typeof pHeight == 'undefined') pHeight=0;
		if (typeof pContent == 'undefined') pContent='';
		if (typeof pTitle == 'undefined') pTitle='';
		if (typeof pModal == 'undefined') pModal=false;
	
		// Proportionen
		this.width=pWidth;
		this.height=pHeight;
		
		// Neues Objekt erstellen
		var oDiv = document.createElement("div");
	
		// bei Modalem Fenster alles um 1000 nach oben
		if (pModal == true) this.level+=1000;
		
		// x/y an die Scroll-Position anpassen
		pX=pX+pics.getPageScrollX();
		pY=pY+pics.getPageScrollY();
		
		// Attribute setzen
		oDiv.setAttribute("id","pics_Window_"+this.id);	
		// für Firefox müssen wir das style-Attribute setzen, der IE versteht das nicht
	    var lStyle="position: absolute; left: "+pX+"px; top: "+pY+"px; width: "+(pWidth+4)+"px; z-index:"+this.level+";";
		if (pHeight!=0) lStyle+=" height: "+(pHeight+20)+"px;";
		oDiv.setAttribute("style",lStyle);
		oDiv.setAttribute("class",this.classWindow);

	    var lStyle="position: relative; width: "+pWidth+"px; ";
		if (pHeight!=0) lStyle+=" height: "+pHeight+"px;";
		this.content.setAttribute("style",lStyle);

	    var lStyle="position: relative; width:100%; ";
		this.buttons.setAttribute("style",lStyle);


		// für den IE müssen wir das über das DOM machen
		if (pics.isIE)
		{
			oDiv.style.position="absolute";
			oDiv.style.left=pX;
			oDiv.style.top=pY;
			oDiv.style.width=pWidth;
			if (pHeight!=0) oDiv.style.height=(pHeight+20);
			oDiv.style.zIndex=this.level;
			oDiv.className=this.classWindow;
		}
		// einfärben
		// oDiv.style.backgroundColor="#FFFF00";

		// 2. Variante mit DIVs über DOM
		if (this.showTitleBar)
		{
			this.titleBar.innerHTML=this.createTitleBar(pTitle);
			oDiv.appendChild(this.titleBar);
		}
		this.content.innerHTML=pContent;
		oDiv.appendChild(this.content);
		oDiv.appendChild(this.buttons);
		
		// und jetzt im Document einfügen
		if (pModal == true)
		{
			lWidth=document.body.scrollWidth;
			lHeight=document.body.scrollHeight;
			if (lHeight<document.body.offsetHeight)
			{
				lWidth=document.body.offsetWidth;
				lHeight=document.body.offsetHeight;
			}
			// Falls nur ganz wenig Inhalt in der Seite
			lHeight=Math.max(lHeight,screen.height);
			// wenn Modal, dann den Layer in einen Overlay-Layer einbetten
			var oModal = document.createElement("div");
			oModal.setAttribute("class",this.classModal);
		    var lStyle="width: "+(2*lWidth)+"px; height: "+(2*lHeight)+"px;";
			oModal.setAttribute("style",lStyle);
			if (pics.isIE)
			{
				oModal.className=this.classModal;
				oModal.style.width=2*lWidth;
				oModal.style.height=2*lHeight;
			}
			document.body.appendChild(oModal);
			this.windowmodal=oModal;
			// und jetzt den oDiv einbetten und tauschen
			// oDiv=oModal;
		}
		document.body.appendChild(oDiv);
		this.window=oDiv;
		
		// zur Sicherheit müssen wir uns selber noch dem Titlebar zuweisen
		// this.titleBar.parentObject=this;
		
		// 20070817,gc - nicht bei modalen Dialogen!
		// Event-Handler
		if (!pModal) this.titleBar.onmousedown = pics.Windows.dragStart;
		
		// Demo-Allert
	//	oDiv.onclick = Function("alert('clicked');");
	};
	
	/**
	 * pics.Windows.Window.prototype.destroy
	 *	das Fenster zerstören
	 */
	pics.Windows.Window.prototype.destroy = function()
	{
		if (this.window) document.body.removeChild(this.window);
		if (this.windowmodal) document.body.removeChild(this.windowmodal);
	} // window.destroy
	
	/**
	 * pics.Windows.Window.prototype.setContent
	 *	neuen Content einfügen
	 */
	pics.Windows.Window.prototype.setContent = function(pContent)
	{
		// zur sicherheit den Stil auf Content setzen 
		this.content.setAttribute("class",this.classContent);
		if (pics.isIE) this.content.className=this.classContent;
	
		this.content.innerHTML=pContent;
	};
	
	/**
	 * pics.Windows.Window.prototype.addButton
	 *	Schaltfläche anfügen
	 */
	pics.Windows.Window.prototype.addButton = function(pButtonText, pButtonCode)
	{
		if (typeof pButtonText == 'undefined') pButtonText="OK";
		if (typeof pButtonCode == 'undefined') pButtonCode="pics.Windows.closeWindow('','"+this.id+"')";

		//lHtml='<div align="center"><br><a onclick="'+pButtonCode+'" class="pics_InfoBoxButton">&nbsp;'+pButtonText+'&nbsp;</a></div>\n';
		lHtml='&nbsp;&nbsp;<a onclick="'+pButtonCode+'" class="pics_InfoBoxButton">&nbsp;'+pButtonText+'&nbsp;</a>&nbsp;&nbsp;';
		this.buttons.innerHTML=this.buttons.innerHTML+lHtml;
	};

	/**
	 * pics.Windows.Window.prototype.setIFrame
	 *	IFrame als Content einfügen
	 */
	pics.Windows.Window.prototype.setIFrame = function(pUrl, pScrolling)
	{
		if (typeof pScrolling == 'undefined') pScrolling="Auto";
	
		// zur sicherheit den Stil auf IFrame setzen (ohne Padding)
		this.content.setAttribute("class",this.classIFrame);
		if (pics.isIE) this.content.className=this.classIFrame;
	
		this.content.innerHTML='<iframe src="'+pUrl+'" width="'+this.width+'" height="'+this.height+'" frameborder="0" scrolling="'+pScrolling+'"></iframe>';
	}
	
	/**
	 * pics.Windows.Window.prototype.createTitleBar
	 *	HTML für Titelleiste erstellen
	 */
	pics.Windows.Window.prototype.createTitleBar = function(pTitel)
	{
		lHtml='<table width="100%" cellspacing="0" cellpadding="0" border="0"><tr>';
		lHtml+='<td class="pics_TitlebarText">'+pTitel+'</td>\n';
		lHtml+='<td width="20" align="right"><a onClick="pics.Windows.closeWindow(\'\',\''+this.id+'\')"><img src="'+C_PICSWIN_ImagePath+'close.gif" width="16" height="16" alt="Fenster schliessen" border="0"></a></td>\n';
		lHtml+='</tr></table>\n';
	
		return lHtml;
	}	// pics.Windows.Window.prototype.createTitleBar
	
	/**
	 * pics.Windows.Window.prototype.hide
	 *	Fenster ausblenden
	 */
	pics.Windows.Window.prototype.hide = function()
	{
		// wenn es einen Timer gibt, dann löschen
		if (this.closeTimer!=null)
		{
			window.clearTimeout(this.closeTimer);
			this.closeTimer=null;
		}
		
		// zur sicherheit den Stil auf Content seten 
		this.window.style.visibility='hidden';
		if (this.windowmodal) this.windowmodal.style.visibility='hidden';
	}	// pics.Windows.Window.prototype.hide
	
	/**
	 * pics.Windows.Window.prototype.show
	 *	Fenster einblenden
	 */
	pics.Windows.Window.prototype.show = function()
	{
		// zur sicherheit den Stil auf Content seten 
		this.window.style.visibility='';
		if (this.windowmodal) this.windowmodal.style.visibility='';
	} // pics.Windows.Window.prototype.show

	/**
	 * pics.Windows.Window.prototype.move(pX, pY)
	 *	Fenster verschieben
	 *		@param pX 		X-Position
	 *		@param pY 		Y-Position
	 */
	pics.Windows.Window.prototype.move = function(pX, pY)
	{
		if (typeof pX == 'undefined') pX=-9999;
		if (typeof pY == 'undefined') pY=-9999;

		if (pX>-9999) this.window.style.left=pX;
		if (pY>-9999) this.window.style.top=pY;
	}	// pics.Windows.Window.prototype.move
	
	
/* ========================= Windows : Drag&Drop ========================= */
	

	/**
	 * pics.Windows.Window.prototype.mouseMove
	 *	Maus-Bewegung verarbeiten
	 */
	pics.Windows.Window.prototype.mouseMove = function(event, win, target)
	{
		this.setContent("HALLO");
	}
	
	
		
	/**
	 * pics.Windows.Window.prototype.debug
	 *	Debug-Information ausgeben
	 */
	pics.Windows.Window.prototype.debug = function(pContent)
	{
		alert("DEBUG:"+pContent);
	}
	
	/**
	 * pics.Windows.alert
	 *	Eigener alert auf InfoBox-Basis
	 */
	pics.Windows.alert = function(pMessage,pTitle) {
		if (typeof pTitle == 'undefined') pTitle="Hinweis!";
		oAlert=pics.Windows.infoBox();
		oAlert.showTitleBar=true;
		oAlert.create(400,200,250,0,'wird geladen ...',pTitle,true);
		oAlert.setContent(pMessage);			
		oAlert.addButton("OK");
	}


	/*
	 *	function pics.Windows.lightbox(pUrl, pWidth, pHeight)
	 *		zeigt die angegebene URL als Lightbox an
	 *		@param pUrl 		zu ladende URL
	 *		@param pWidth 		Breite
	 *		@param pHeight 		Höhe
	 */
	pics.Windows.lightbox = function(pUrl, pWidth, pHeight)
	{
		if (typeof pWidth == 'undefined') pWidth=500;
		if (typeof pHeight == 'undefined') pHeight=400;
	
		// Definitionen
		lWindowID='picsLightbox';
		lTotalWidth=window.innerWidth || document.body.clientWidth;
		lTotalHeight=window.innerHeight || document.body.clientHeight;
		lX=(lTotalWidth-pWidth)/2;
 		lY=(lTotalHeight-pHeight)/2;

		if (!pics.Windows.list[lWindowID])
		{
			// fenster exitiert noch nicht
			oWin=pics.Windows.addWindow(lWindowID);
			oWin.create(lX, lY, pWidth, pHeight, "", "", true);
		}
		else
		{
			oWin=pics.Windows.getWindow(lWindowID);
			oWin.show();
		}
		oWin.setIFrame(pUrl);
		pics.Windows.activate(lWindowID);
	} // pics.lightbox
	
		


	/**
	 * pics.Windows.msgBox
	 *	Neue Msgbox erstellen
	 *		@param pText 		anzuzeigender Text
	 *		@param pModal 		Modal anzeigen
	 *		@param pButton 		OK-Button anzeigen
	 *		@param pAutoClose 	autom. nach x Sekunden schliessen
	 */
	pics.Windows.msgBox = function(pText, pModal, pButton, pAutoClose)
	{
		if (typeof pModal == 'undefined') 		pModal=true;
		if (typeof pButton == 'undefined') 		pButton=true;
		if (typeof pAutoClose == 'undefined') 	pAutoClose=0;

		// Definitionen
		lWidth=400;
		lHeight=400;
		
		lTotalWidth=window.innerWidth || document.body.clientWidth;
		lTotalHeight=window.innerHeight || document.body.clientHeight;
		lX=(lTotalWidth-lWidth)/2;
 		lY=(lTotalHeight-lHeight)/2;

		// ERstellen
		oWin=pics.Windows.addWindow();
		// formatioeren
		oWin.showTitleBar=false;
		oWin.classWindow='pics_InfoBoxWindow';
		oWin.classContent='pics_InfoBoxContent';
		oWin.classButtons='pics_InfoBoxButtonContainer';
		// Stil Content zuweisen
		oWin.content.setAttribute("class",oWin.classContent);
		if (pics.isIE) oWin.content.className=oWin.classContent;
		
		// Stil Buttons zuweisen
		oWin.buttons.setAttribute("class",oWin.classButtons);
		if (pics.isIE) oWin.buttons.className=oWin.classButtons;

		// und erstellen
		oWin.create(lX, lY, lWidth, 0, pText, "", pModal);

		// Button einfügen
		if (pButton) oWin.addButton("OK");

		// bei AutoClose einen Timer setzen
		if (pAutoClose>0) oWin.closeTimer=window.setTimeout("oWin.hide()", pAutoClose*1000);
		
		return oWin;
	};
		
