/******************************************************************************
*	MyWindow 2009.2 (20081218)
*
*	Copyright 2004-2009 Carsten Ruppert - carsten.ruppert-at-web.de
*
*
*	Kompatible Browser
*	- Internet Explorer ab v6.0
*	- Firefox ab v0.6 und kompatible
*	- Safari ab v2.0 und kompatible
*	- Opera ab v8.0
*	- Chrome
*
******************************************************************************

Aenderungen:
1.1.1
- Methode setWindowTitle(string Title) hinzugefügt.
- Der Fenstertitel kann nun auch mit HTML eingegeben werden.

1.1.0
- Animationen eingebaut, benoetigt jQuery Library
- Statusleisten Code komplett entfernt
- Diverse Aenderungen
- Ereignisueberwachung der Titelleiste entfernt

1.0.3
- methode resizeTo(int breite,int hoehe) hinzugefuegt

<1.0.3
- methode tellWindow hinzugefügt
- methode moveTo(int x-achse,int y-achse) hinzugefuegt
- teilweise code umgeschrieben


Fixes:
1.1.1
-

1.1.0
- Diverse korrekturen

1.0.3
- "Fliegenede Buttons" gefixt

<1.0.3
- doppelklick auf die titelleiste gefixt
- fenster zentriert beim bewegen nicht mehr
- fenster laesst sich wieder schliessen


Bugs:
1.1.0
- Doppelklick auf die Titelleiste versaut EventListener

*/

var _mywinXgif = './blank.gif'; // transparentes gif. Das ist nicht zwingend erforderlich
function mywindow(cid,w,h,x,y,z,windowname,scrollbars,moveable,resizeable,closeable,basics,classprefix){

	var mywin = this;
	this.content = document.getElementById(cid);

	this.w = Number(w);
	this.h = Number(h);
	this.x = Number(x);
	this.y = Number(y);
	this.z = Number(z);
	
	this.minheight = 200;
	this.minwidth = 200;
	
	this.scrollbars = scrollbars;
	this.resizeable = resizeable;
	this.moveable = moveable;
	this.basics = basics; 
	this.closeable = closeable;
	this.skin = new Array();
	this.windowname = windowname;

	this.status = 'normalmode';
	this.moveing = false;
	this.resizing = false;

	this.useAnimations = true;
	this.animationSpeed = 500;

	this._btnposxd = new Array(0,0,0); // vorgabe button x postionen min,max,close
	this._btnposx = new Array(0,0,0); // tatsaechliche x positionen der buttons
	this._btnposy = new Array(); // y positionen der buttons
	
	this.classprefix = classprefix ? classprefix : 'mywin'; // Skin
	this.restore;	

	/*
	*	Prevent Internet Explorer from selecting the Content (Text etc.) of the document body
	*	while moveing or resizeing the window.
	*/
	this.prevMarking = function(ev){
		return false;
		}
	
	/*
	*	Removes 'px' from a string
	*/
	this.removePx = function(val){ 
		if(val.indexOf('px')){
			val = Number(val.substr(0,val.indexOf('px')));
			}
		return val;
		}
	
	
	this.tellWindow = function(message){
		// Sag dem fenster was es tun soll, in der eingabereihenfolge
		var told = message.split(" ");
		var c = 0; var cmd = told[c];
		do{
			switch(cmd){
				case 'close' : mywin.closeWin(); break;
				case 'maximize' : mywin.maximize();	break;
				case 'minimize' : mywin.minimize();	break;
				case 'reset' : mywin.reSet(); break;
				default : return;
				}
			++c;
			}while(cmd = told[c]);
		}
	
	/*
	*	This one is fired on a click on the Titlebar and starts window movement
	*/
	this.move = function(ev){
		mywin.moveing = true; // mywindow is moved by user right now
		
		// Cursor position inside the titlebar
		var ydif = document.documentElement ? document.documentElement.scrollTop : 0; // y differenz im IE
		mywin._citbx = ev.clientX - mywin.x; // _CursorInTitleBarX
		mywin._citby = (ev.clientY + ydif) - mywin.y;

		// Expand clipzone:
		mywin._mclipzone.style.left = '0px'; mywin._mclipzone.style.top = '0px';
		mywin._mclipzone.style.width = '100%'; mywin._mclipzone.style.zIndex = 5000;
		if(window.innerHeight){
			mywin._mclipzone.style.height = window.innerHeight + 'px';
			}
		else{
			mywin._mclipzone.style.height = document.documentElement ? document.documentElement.offsetHeight + 'px' : '100%';
			}
			
		// Setup event listeners
		if(mywin._mclipzone.attachEvent){
			document.body.attachEvent('onselectstart',mywin.prevMarking);
			mywin._mclipzone.detachEvent('onmousedown',mywin.move);
			mywin._mclipzone.attachEvent('onmouseup',mywin.stopp);
			mywin._mclipzone.attachEvent('onmousemove',mywin.follow);						
			}
		else {
			mywin._mclipzone.removeEventListener('mousedown',mywin.move,false);
			mywin._mclipzone.addEventListener('mouseup',mywin.stopp,false);
			mywin._mclipzone.addEventListener('mousemove',mywin.follow,false);
			}
		}
	
	/*
	*	This one stopps window movement and is fired if the user releases the mousebutton on the Titlebar
	*/
	this.stopp = function(ev){
		// End moving
		
		mywin.moveing = false;
		// clipping zone zusammenfalten
		void mywin.setClipZones();
		
		// Bewegen stoppen
		if(mywin._mclipzone.addEventListener){
			mywin._mclipzone.addEventListener('mousedown',mywin.move,false);
			mywin._mclipzone.removeEventListener('mousemove',mywin.follow,false);
			mywin._mclipzone.removeEventListener('mouseup',mywin.stopp,false);
			}
		else{
			document.body.detachEvent('onselectstart',mywin.prevMarking);
			mywin._mclipzone.attachEvent('onmousedown',mywin.move);
			mywin._mclipzone.detachEvent('onmousemove',mywin.follow);
			mywin._mclipzone.detachEvent('onmouseup',mywin.stopp);
			}
		}
	/*
	*	Set x and y axis while window movement
	*/
	this.follow = function(ev){
		// Follow cursor position
		var x, y;
		var ydif = document.documentElement ? document.documentElement.scrollTop : 0;
		if ( ev.clientX ) {
			x = ev.clientX;	y = ev.clientY + ydif;
			}
		x -= mywin._citbx;
		y -= mywin._citby;
		mywin.moveTo(x,y);
		}
	/*
	*	Start window resizeing
	*/
	this.resize = function(ev){
		mywin.resizing = true;

		// clipping zone ausdehnen...
		mywin._rclipzone.style.top = '0px';	mywin._rclipzone.style.left = '0px';
		mywin._rclipzone.style.width = '100%'; mywin._rclipzone.style.zIndex = 5000;

		if(window.innerHeight){
			mywin._rclipzone.style.height = window.innerHeight + 'px';
			}
		else{
			mywin._rclipzone.style.height = document.documentElement ? document.documentElement.offsetHeight + 'px' : '100%';
			}		

		// resize starten
		if(mywin._rclipzone.addEventListener){
			mywin._rclipzone.removeEventListener('mousedown',mywin.resize,false);
			mywin._rclipzone.addEventListener('mouseup',mywin.stopResize,false);
			mywin._rclipzone.addEventListener('mousemove',mywin.growShrink,false);
			}
		else{
			document.body.attachEvent('onselectstart',mywin.prevMarking);
			mywin._rclipzone.detachEvent('onmousedown',mywin.resize);
			mywin._rclipzone.attachEvent('onmouseup',mywin.stopResize);
			mywin._rclipzone.attachEvent('onmousemove',mywin.growShrink);
			}
		}
	/*
	*	Stop window resizeing
	*/
	this.stopResize = function(ev){
		mywin.resizing = false;
		
		// clipping zone zusammenziehen
		void mywin.setClipZones();
	
		// resize beenden 
		if(mywin._rclipzone.addEventListener){
			mywin._rclipzone.removeEventListener('mouseup',mywin.stopResize,false);
			mywin._rclipzone.removeEventListener('mousemove',mywin.growShrink,false);			
			mywin._rclipzone.addEventListener('mousedown',mywin.resize,false);
			}
		else{
			document.body.detachEvent('onselectstart',mywin.prevMarking);
			mywin._rclipzone.detachEvent('onmouseup',mywin.stopResize);
			mywin._rclipzone.detachEvent('onmousemove',mywin.growShrink);			
			mywin._rclipzone.attachEvent('onmousedown',mywin.resize);			
			}
		}
	
	/*
	*	Set width and height while resizeing
	*/
	this.growShrink = function(ev){
		// zur cursor position schrumpfen oder wachsen
		var x, y;
		var ydif = document.documentElement ? document.documentElement.scrollTop : 0;
		if(ev.clientX){
			x = ev.clientX;
			y = ev.clientY + ydif;
			}else{
			x = ev.pageX;
			y = ev.pageY + ydif;
			}

		var nw = (x - (mywin.w + mywin.x)) + mywin.w; // neue breite
		var nh = (y - (mywin.h + mywin.y)) + mywin.h; // neue hoehe
		
		// minimale breite und hoehe
		mywin.w = (nw <= mywin.minwidth) ? mywin.w : nw;
		mywin.h = (nh <= mywin.minheight) ? mywin.minheight : nh;
		mywin.win.style.width = mywin.w + 'px';
		mywin.win.style.height = mywin.h + 'px';

		// Content groesse setzen
		mywin._wintitle.style.width = mywin.getTitleWidth() + 'px';
		mywin.content.style.width = mywin.getContentWidth() + 'px';
		mywin.content.style.height = mywin.getContentHeight() + 'px';
		void mywin.setButtons();
		}

	
	this.minMaxAnimations = function(){
		var titleWidth = mywin.getTitleWidth();
		var contentWidth = mywin.getContentWidth();
		var contentHeight = mywin.getContentHeight();
		$(mywin.win).animate({width: mywin.w+"px", height: mywin.h+"px", left: mywin.x+"px", top: mywin.y+"px"},mywin.animationSpeed);
		$(mywin._wintitle).animate({width: titleWidth+"px"},mywin.animationSpeed);
		$(mywin.content).animate({width: contentWidth+"px", height: contentHeight+"px"},mywin.animationSpeed);
		return;
		}

	/*
	*	Maximizes the window to the viewable width and height of the browser content (with a border)
	*/
	this.maximize = function (ev) {
		mywin.restore = new Array(mywin.w,mywin.h,mywin.x,mywin.y);

		document.body.removeChild(mywin._btnmax);
		document.body.removeChild(mywin._btnmin);
		document.body.appendChild(mywin._btnreset);

		// Set new window size and position
		mywin.w = (window.innerWidth ? window.innerWidth : document.body.offsetWidth) - 30;
		mywin.h = (window.innerHeight ? window.innerHeight : document.documentElement.offsetHeight) - 30;
		mywin.x = 5; mywin.y = 5;
		
		var titleWidth = mywin.getTitleWidth();
		var contentWidth = mywin.getContentWidth();
		var contentHeight = mywin.getContentHeight();
		
		// Content groesse setzen
		if(mywin.useAnimations){
			mywin.minMaxAnimations();
			}else{
			mywin.win.style.width = mywin.w+'px';
			mywin.win.style.height = mywin.h+'px';
			mywin.win.style.left = mywin.x+'px';
			mywin.win.style.top = mywin.y+'px';

			mywin._wintitle.style.width = titleWidth+'px';
			mywin.content.style.width = contentWidth+'px';
			mywin.content.style.height = contentHeight+'px';
			}
		
		if(mywin._mclipzone.attachEvent){
			mywin._mclipzone.detachEvent('onmousedown',mywin.move);
		//	mywin._mclipzone.detachEvent('ondblclick',mywin.maximize);
		//	mywin._mclipzone.attachEvent('ondblclick',mywin.reSet);
			}
		else{
			mywin._mclipzone.removeEventListener('mousedown',mywin.move,false);
		//	mywin._mclipzone.removeEventListener('dblclick',mywin.maximize,false);
		//	mywin._mclipzone.addEventListener('dblclick',mywin.reSet,false);
			}
		void(mywin.setButtons());
		void(mywin.setClipZones());
		mywin.status = 'maximode';
		return;
		}
	/*
	*	Minimizes the window
	*/
	this.minimize = function (ev) {
		mywin.restore = new Array(mywin.w,mywin.h,mywin.x,mywin.y);

		document.body.removeChild(mywin._btnmax);
		document.body.removeChild(mywin._btnmin);
		document.body.appendChild(mywin._btnreset);

		mywin.w = mywin._btnposxd[2] + 100;
		mywin.h = mywin.headheight;
		mywin.y = 0;
		var titleWidth = mywin.getTitleWidth();
		var contentWidth = mywin.getContentWidth();
		var contentHeight = mywin.getContentHeight();
		
		if(mywin.useAnimations){
			mywin.minMaxAnimations();
			}else{		
			mywin.win.style.width = mywin.w + 'px';
			mywin.win.style.height = mywin.h + 'px';
			mywin.win.style.top = mywin.y + 'px';
			mywin.content.style.width = 0+'px';
			mywin.content.style.height = 0+'px';
	 		mywin._wintitle.style.width = titleWidth+'px';
	 		}
		/*
		if(mywin._mclipzone.attachEvent){
			mywin._mclipzone.detachEvent('ondblclick',mywin.maximize);
			mywin._mclipzone.attachEvent('ondblclick',mywin.reSet);
			}
		else {
			mywin._mclipzone.removeEventListener('dblclick',mywin.maximize,false);
			mywin._mclipzone.addEventListener('dblclick',mywin.reSet,false);
			}
		*/
		void(mywin.setButtons());
		void(mywin.setClipZones());
		mywin.status = 'minimode';
		return;
		}
	/*
	*	Resets the window size and position if the window is mini- or maximized
	*/
	this.reSet = function(ev){
		mywin.w = mywin.restore[0];
		mywin.h = mywin.restore[1];
		mywin.x = mywin.restore[2];
		mywin.y = mywin.restore[3];
		var titleWidth = mywin.getTitleWidth();
		var contentWidth = mywin.getContentWidth();
		var contentHeight = mywin.getContentHeight();
		
		if(mywin.useAnimations){
			mywin.minMaxAnimations();
			}else{
			mywin.win.style.width = mywin.w + 'px';
			mywin.win.style.height = mywin.h + 'px';
			mywin.win.style.left = mywin.x + 'px';
			mywin.win.style.top = mywin.y + 'px';
			mywin._wintitle.style.width = titleWidth + 'px';
			mywin.content.style.width = contentWidth + 'px';
			mywin.content.style.height = contentHeight + 'px';		
			}
		document.body.appendChild(mywin._btnmax);
		document.body.appendChild(mywin._btnmin);
		document.body.removeChild(mywin._btnreset);

		mywin.restore = false;
		if(mywin._mclipzone.attachEvent){
		//	mywin._mclipzone.detachEvent('ondblclick',mywin.reSet);
		//	mywin._mclipzone.attachEvent('ondblclick',mywin.maximize);
			if(mywin.status == 'maximode'){
				mywin._mclipzone.attachEvent('onmousedown',mywin.move)
				}
			}
		else{
		//	mywin._mclipzone.removeEventListener('dblclick',mywin.reSet,false);
		//	mywin._mclipzone.addEventListener('dblclick',mywin.maximize,false);
			if(mywin.status == 'maximode'){
				mywin._mclipzone.addEventListener('mousedown',mywin.move,false)
				}
			}
		void(mywin.setButtons());
		void(mywin.setClipZones());
		mywin.status = 'normalmode';
		}
	
	/*
	*	Close the window
	*/
	this.closeWin = function(ev){
		var attr = mywin.content.setAttribute('mywindow','0');
		var node; var chide;
		while(mywin.win.hasChildNodes){
			node = mywin.win.firstChild;
			if(node && node.id == mywin.content.id && chide != true){ 
				document.body.appendChild(node);
				node.style.display = 'none';
				chide = true;
				}
			else if(node){
				mywin.win.removeChild(node);
				}else{
				break;
				}
			}
		if(mywin.moveable){
			document.body.removeChild(mywin._mclipzone);
			}
		if(mywin.resizeable){
			document.body.removeChild(mywin._rclipzone);
			}
		if(mywin.basics){
			if(mywin.status != 'normalmode'){
				document.body.removeChild(mywin._btnreset);
				}
			else {
				document.body.removeChild(mywin._btnmax);
				document.body.removeChild(mywin._btnmin);
				}
			}
		if(mywin.closeable){
			document.body.removeChild(mywin._btnclose);
			}
		document.body.removeChild(mywin.win);		
		delete(mywin);
		}
	/*
	*	Read Stylesheet data
	*/
	this.getSkin = function(){
		var cssnode;
		for ( i = 0; i < document.styleSheets.length; i++ ) {
			if ( document.styleSheets[i].cssRules ) {
				// DOM
				for ( x = 0; x < document.styleSheets[i].cssRules.length; x++ ) {
					cssnode = document.styleSheets[i].cssRules[x];
					if ( cssnode.selectorText ) {
						if ( cssnode.selectorText.indexOf(this.classprefix) > -1 ) {
							this.skin[cssnode.selectorText] = cssnode; // this.skin['.' + this.classprefix + 'SELECTORNAME'].style.PROPERTY;
							}
						}
					}
				}
			else {
				// Microsoft
				for ( x = 0; x < document.styleSheets[i].rules.length; x++ ) {
					cssnode = document.styleSheets[i].rules[x];
					if ( cssnode.selectorText.indexOf(this.classprefix) > -1 ) {
						this.skin[cssnode.selectorText] = cssnode; // this.skin['.' + this.classprefix + 'SELECTORNAME'].style.PROPERTY;
						}
					}
				}
			}
		return;
		}
	
	/*
	*	Rules the position of the buttons
	*/
	this.setButtons = function(){
		if(mywin.closeable){
			mywin._btnposx[0] = mywin.x + mywin.w - mywin._btnposxd[0];
			mywin._btnposy[0] = (mywin.y + mywin.headheight / 2) - (mywin.removePx(mywin.skin['.' + mywin.classprefix + 'ButtonClose'].style.height) / 2);
			if(mywin.useAnimations && !mywin.moveing && !mywin.resizing){
				$(mywin._btnclose).animate({left: mywin._btnposx[0]+'px', top: mywin._btnposy[0]+'px'},mywin.animationSpeed);
				}else{
				mywin._btnclose.style.left = mywin._btnposx[0]+'px';
				mywin._btnclose.style.top = mywin._btnposy[0]+'px';
				}
			}
		if(mywin.basics){
			// Maximize and Reset button have the same position
			mywin._btnposx[1] = mywin.x + mywin.w - mywin._btnposxd[1];				
			mywin._btnposy[1] = (mywin.y + mywin.headheight / 2) - (mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Maximizer'].style.height) / 2);
			// Minimize button position
			mywin._btnposx[2] = mywin.x + mywin.w - mywin._btnposxd[2];
			mywin._btnposy[2] = (mywin.y + mywin.headheight / 2) - (mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Minimizer'].style.height) / 2);

			if(mywin.useAnimations && !mywin.moveing && !mywin.resizing){
				$(mywin._btnreset).animate({left: mywin._btnposx[1]+'px', top: mywin._btnposy[1]+'px'},mywin.animationSpeed);
				$(mywin._btnmax).animate({left: mywin._btnposx[1]+'px', top: mywin._btnposy[1]+'px'},mywin.animationSpeed);
				$(mywin._btnmin).animate({left: mywin._btnposx[2]+'px', top: mywin._btnposy[2]+'px'},mywin.animationSpeed);
				}
			else{		
				mywin._btnreset.style.left = mywin._btnposx[1] + 'px';				
				mywin._btnreset.style.top = mywin._btnposy[1] + 'px';
				mywin._btnmax.style.left = mywin._btnposx[1]+'px';
				mywin._btnmax.style.top = mywin._btnposy[1]+'px';
				mywin._btnmin.style.left = mywin._btnposx[2]+'px';
				mywin._btnmin.style.top = mywin._btnposy[2]+'px';
				}
			}
		return;
		}

	this.setClipZones = function(){// clipping zonen neu ausrichten
		if(mywin.moveable){
			mywin._mclipzone.style.left = mywin.x + 'px';
			mywin._mclipzone.style.top = mywin.y + 'px';
			mywin._mclipzone.style.width = mywin.w + 'px';
			mywin._mclipzone.style.height = mywin.headheight + 'px';
			mywin._mclipzone.style.zIndex = mywin.z + 1;
			}
		if(mywin.resizeable){
			mywin._rclipzone.style.width = '10px'; mywin._rclipzone.style.height = '10px';
			mywin._rclipzone.style.left = mywin.x + mywin.w - 5 + 'px';
			mywin._rclipzone.style.top = mywin.y + mywin.h - 5 + 'px';
			mywin._rclipzone.style.zIndex = mywin.z + 1;
			}
		return;
		}

	this.getTitleWidth = function(){
		var w = mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Titlebar'].style.borderLeftWidth);
		w += mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Titlebar'].style.borderRightWidth);
		w += mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Titlebar'].style.paddingLeft);
		w += mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Titlebar'].style.paddingRight);
		return mywin.w - w;
		}
	this.getContentWidth = function(){
		var w = mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Content'].style.borderLeftWidth);
		w += mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Content'].style.borderRightWidth);
		w += mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Content'].style.paddingLeft);
		w += mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Content'].style.paddingRight);
		return mywin.w - w;
		}
	this.getContentHeight = function(){
		var h = mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Content'].style.borderTopWidth);
		h += mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Content'].style.borderBottomWidth);
		h += mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Content'].style.paddingTop);
		h += mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Content'].style.paddingBottom);
		h += mywin.headheight;
		return mywin.h - h;
		}
	
	this.setZ = function(z){
		mywin.z = z;
		mywin.win.style.zIndex = z;
		if(mywin.closeable){
			mywin._btnclose.style.zIndex = mywin.z + 2;
			}
		if(mywin.basics){
			mywin._btnmax.style.zIndex = mywin.z + 2;
			mywin._btnmin.style.zIndex = mywin.z + 2;
			mywin._btnreset.style.zIndex = mywin.z + 2;
			}
		if(mywin.moveable){
			mywin._mclipzone.style.zIndex = mywin.z + 1;
			}
		if(mywin.resizeable){
			mywin._rclipzone.style.zIndex = mywin.z + 1;
			}
		return;
		}

	this.moveTo = function(x,y){
		mywin.x = x;
		mywin.y = y;
		if(mywin.useAnimations && !mywin.moveing){
			$(mywin.win).animate({left: mywin.x+'px', top: mywin.y+'px'},mywin.animationSpeed);
			}else{
			mywin.win.style.left = mywin.x + 'px';
			mywin.win.style.top = mywin.y + 'px';
			}
		void(mywin.setButtons());
		if(!mywin.moveing){void(mywin.setClipZones())}
		return;
		}
	this.resizeTo = function(w,h){
		mywin.w = w;
		mywin.h = h;
		
		var titleWidth = mywin.getTitleWidth();
		var contentWidth = mywin.getContentWidth();
		var contentHeight = mywin.getContentHeight();		
		
		if(mywin.useAnimations && !mywin.resizing){
			$(mywin.win).animate({width: mywin.w+'px', height: mywin.h+'px'},mywin.animationSpeed);
			$(mywin.content).animate({width: contentWidth+'px', height: contentHeight+'px'},mywin.animationSpeed);
			$(mywin._wintitle).animate({width: titleWidth+'px'},mywin.animationSpeed);
			}else{
			mywin.win.style.width = mywin.w + 'px';
			mywin.win.style.height = mywin.h + 'px';
			mywin._wintitle.style.width = mywin.getTitleWidth() + 'px';
			mywin.content.style.width = mywin.getContentWidth() + 'px';
			mywin.content.style.height = mywin.getContentHeight() + 'px';
			}
		void mywin.setClipZones();
		void mywin.setButtons();
		return;
		}

	this.setWindowTitle = function(title){
		mywin._wintitle.innerHTML = title;
		return;
		}

	if(this.content){// Construct Window
		var atr = this.content.getAttributeNode('mywindow');
		var iswin = atr ? atr.nodeValue : 0;
		if(iswin == 0){// nein
			iswin = document.createAttribute('mywindow');
			iswin.nodeValue = '1';
			this.content.setAttributeNode(iswin);
			}
		else{// ja, abbruch
			delete(mywin);
			return;
			}
		void this.getSkin(); // Stylesheet

		// hoehe von titelleiste vom stylesheet ziehen
		this.headheight = this.removePx(this.skin['.' + this.classprefix + 'Titlebar'].style.height);
		this.headheight += this.removePx(this.skin['.' + this.classprefix + 'Titlebar'].style.paddingTop);
		this.headheight += this.removePx(this.skin['.' + this.classprefix + 'Titlebar'].style.paddingBottom);
		this.headheight += this.removePx(this.skin['.' + this.classprefix + 'Titlebar'].style.borderTopWidth);
		this.headheight += this.removePx(this.skin['.' + this.classprefix + 'Titlebar'].style.borderBottomWidth);
		this.minheight = this.headheight; // min. hoehe
		
		// Setup Window Container
		this.win = document.createElement('div');
		this.win.className = this.classprefix + 'Window'; // Apply skin
		this.win.style.position = 'absolute';
		this.win.style.left = this.x + 'px'; this.win.style.top = this.y + 'px';
		this.win.style.width = this.w + 'px'; this.win.style.height = this.h + 'px';
		this.win.style.overflow = 'hidden';
		this.win.style.zIndex = this.z;
		document.body.appendChild(this.win);
		
		// Setup Content Container 
		this.content.className = this.classprefix + 'Content';
		this.content.style.position = 'absolute';
		this.content.style.display = 'block';
		this.content.style.top = this.headheight + 'px';
		this.content.style.width = this.getContentWidth() + 'px';
		this.content.style.height = this.getContentHeight() + 'px';

		if(scrollbars){
			this.content.style.overflow = 'auto';
			}else{
			this.content.style.overflow = 'hidden'; // fehler bei iframes
			}
		this.win.appendChild(this.content); // content an win anhaengen

		// Titlebar:
		this._wintitle = document.createElement('div');
		this._wintitle.className = this.classprefix + 'Titlebar'; // Apply Skin to titlebar
		this._wintitle.style.position = 'absolute';
		this._wintitle.style.left = '0px';
		this._wintitle.style.top = '0px';
		this._wintitle.style.width = this.getTitleWidth() + 'px';
		this._wintitle.style.overflow = 'hidden';
		this.win.appendChild(this._wintitle);

		if(this.windowname != ''){
			this._wintitle.innerHTML = ""; //this.windowname;
			}
		
		if(this.moveable){
			this._mclipzone = document.createElement('div');
			this._mclipzone.style.backgroundImage = 'url('+_mywinXgif+')'; // transparentes gif!
			
			//this._mclipzone.style.backgroundColor = "#ff0000";
			
			this._mclipzone.style.position = 'absolute'; this._mclipzone.style.left = this.x + 'px';
			this._mclipzone.style.top = this.y + 'px';
			this._mclipzone.style.width = this.w + 'px';
			this._mclipzone.style.height = this.headheight + 'px';
			this._mclipzone.style.zIndex = this.z + 1;

			if(this._mclipzone.attachEvent){
				this._mclipzone.attachEvent('onmousedown',this.move);
				if(this.resizeable){
				//	this._mclipzone.attachEvent('ondblclick',this.maximize);
					}
				}
			else{
				this._mclipzone.addEventListener('mousedown',this.move,false);
				if(this.resizeable){
				//	this._mclipzone.addEventListener('dblclick',this.maximize,false);
					}
				}
			document.body.appendChild(this._mclipzone);
			}
		
		if(this.resizeable){
			// Resizer clipping zone
			this._rclipzone = document.createElement('div');
			this._rclipzone.className = this.classprefix + 'Resizer';
			this._rclipzone.style.position = 'absolute';
			this._rclipzone.style.left = mywin.x + mywin.w - 5 + 'px';
			this._rclipzone.style.top = mywin.y + mywin.h - 5 + 'px';
			this._rclipzone.style.cursor = 'se-resize';
			this._rclipzone.style.width = '10px';
			this._rclipzone.style.height = '10px';
			this._rclipzone.style.backgroundImage = 'url('+_mywinXgif+')';
			this._rclipzone.style.zIndex = this.z + 1;

			if(this._rclipzone.attachEvent){
				this._rclipzone.attachEvent('onmousedown',this.resize);
				}
			else{
				this._rclipzone.addEventListener('mousedown',this.resize,false);
				}
			document.body.appendChild(this._rclipzone);
			}
		if(this.closeable){
			// Close button
			this._btnclose = document.createElement('a');
			this._btnposxd[0] = this.removePx(this.skin['.' + this.classprefix + 'ButtonClose'].style.width) + 5;
			this._btnposx[0] = this.x + this.w - this._btnposxd[0];
						
			this._btnposy[0] = (this.y + this.headheight / 2) - (this.removePx(this.skin['.' + this.classprefix + 'ButtonClose'].style.height) / 2);
			
			this._btnclose.className = this.classprefix + 'ButtonClose';
			this._btnclose.style.position = 'absolute';
			this._btnclose.style.left = this._btnposx[0] + 'px';
			this._btnclose.style.top = this._btnposy[0] + 'px';
			this._btnclose.style.zIndex = this.z + 2;
			
			if(this._btnclose.attachEvent){
				this._btnclose.attachEvent('onclick',this.closeWin);
				}
			else{
				this._btnclose.addEventListener('click',this.closeWin,false);
				}
			document.body.appendChild(this._btnclose);
			}
		if(this.basics){
			// Maxi- and Minimize buttons
			this._btnmax = document.createElement('a');
			this._btnmin = document.createElement('a');
			
			this._btnposxd[1] = this.removePx(this.skin['.' + this.classprefix + 'Maximizer'].style.width) + 5;
			this._btnposxd[1] += this.closeable ? this._btnposxd[0] : 0;
			
			this._btnposxd[2] = this._btnposxd[1] + 5 + this.removePx(this.skin['.' + this.classprefix + 'Minimizer'].style.width);

			this._btnposx[1] = this.x + this.w - this._btnposxd[1];
			this._btnposx[2] = this.x + this.w - this._btnposxd[2];
			
			this._btnposy[1] = (this.y + this.headheight / 2) - (this.removePx(this.skin['.' + this.classprefix + 'Maximizer'].style.height) / 2);
			this._btnposy[2] = (this.y + this.headheight / 2) - (this.removePx(this.skin['.' + this.classprefix + 'Minimizer'].style.height) / 2);

			this._btnmax.className = this.classprefix + 'Maximizer';
			this._btnmax.style.position = 'absolute';
			this._btnmax.style.left = this._btnposx[1] + 'px';
			this._btnmax.style.top = this._btnposy[1] + 'px';
			this._btnmax.style.zIndex = this.z + 2;

			this._btnmin.className = this.classprefix + 'Minimizer';
			this._btnmin.style.position = 'absolute';
			this._btnmin.style.left = this._btnposx[2] + 'px';
			this._btnmin.style.top = this._btnposy[2] + 'px';
			this._btnmin.style.zIndex = this.z + 2;
			
			this._btnreset = document.createElement('a');
			this._btnreset.className = this.classprefix + 'ButtonReset';
			this._btnreset.style.position = 'absolute';
			this._btnreset.style.left = this._btnposx[1] + 'px';
			this._btnreset.style.top = this._btnposy[1] + 'px';
			this._btnreset.style.zIndex = this.z + 2;
			
			if(this._btnmax.attachEvent){
				this._btnmax.attachEvent('onclick',this.maximize);
				this._btnmin.attachEvent('onclick',this.minimize);
				this._btnreset.attachEvent('onclick',this.reSet);
				}
			else{
				this._btnmax.addEventListener('click',this.maximize,false);
				this._btnmin.addEventListener('click',this.minimize,false);
				this._btnreset.addEventListener('click',this.reSet,false);
				}
			document.body.appendChild(this._btnmax);
			document.body.appendChild(this._btnmin);
			}
		return this.content.id;
		}
	else {
		return false;
		}
}