/******************************************************************************
*	MyWindow 2011.1 (20110314)
*
*	Copyright 2004 - 2011 Carsten Ruppert - carsten.ruppert-at-web.de
*
******************************************************************************/
function MyWindow( cid, w, h, x, y, z, windowname, config )
{
	var mywin = this;
	this.content 		= document.getElementById(cid);
	this.ismanaged 		= false;

	// Größe und Koordinaten
	this.w 				= w;
	this.h 				= h;
	this.x 				= x;
	this.y 				= y;
	this.z 				= z;
	
	// Fensterparameter
	this.minheight 		= config.minwidth ? config.minwidth : 200;
	this.minwidth 		= config.minheight ? config.minheight : 200;
	this.scrollbars 	= config.scrollbars != undefined ? config.scrollbars : true;
	this.resizeable 	= config.resizeable != undefined ? config.resizeable : true;
	this.moveable 		= config.moveable != undefined ? config.moveable : true;
	this.basics 		= config.basicButtons != undefined ? config.basicButtons : true; 
	this.closeable 		= config.closeable != undefined ? config.closeable : true;
	this.classprefix 	= config.customTheme ? config.customTheme : (config.theme ? config.theme : 'mywin');
	this.useAnimations 	= config.animations != undefined ? config.animations : false;
	this.animationSpeed = config.animationSpeed ? config.animationSpeed : 800;

	this.skin 			= new Array();
	this.windowname 	= windowname;

	this.status 		= 'standard';
	this.moveing 		= false;
	this.resizing 		= false;

	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.restore 		= new Array();	


	/***
		2011.x
		Bind an event to a DOM Element
		win.eventBind( DOM Node/Obj, String event, Method Name etc... );
		win.eventBind(win.titlebar, 'click', function(){alert('bla')});
	*/
	this.eventBind = function( target, firesOn, execFunc )
	{
		if(window.addEventListener)
		{ // Use standard W3 DOM for modern Browsers
			target.addEventListener( firesOn, execFunc, false );
			
		}
		else if(window.attachEvent)
		{ // Use Microsoft Bubbling model for old IE versions
			target.attachEvent( 'on' + firesOn, execFunc );
		}
	}
	
	
	/***
		2011.x
		Unbind an event
	*/
	this.eventUnbind = function( target, firesOn, execFunc )
	{
		if(window.removeEventListener)
		{
			target.removeEventListener( firesOn, execFunc, false );
		}
		else if(window.detachEvent)
		{
			target.detachEvent( 'on' + firesOn, execFunc );
		}		
	}


	/*
	this.isOldExplorer = function()
	{
		var versions = array('MSIE 6.0', 'MSIE 7.0', 'MSIE 8.0');
		for(i = 0; i < versions.length; i++)
		{
			if( navigator.appVersion.indexOf( versions[i] ) )
			{
				return true;
			}
		}
		return false;
	}
	*/
	

	/***
		2011.x
		Eine Clipping-Zone ausdehnen
	*/
	this.expandClipArea = function( area )
	{
		area.style.left 	= '0px';
		area.style.top 		= '0px';
		area.style.width 	= '100%'; 
		area.style.zIndex 	= mywin.z + 1000;
		if(window.innerHeight)
		{
			area.style.height = window.innerHeight + 'px';
		}
		else
		{
			area.style.height = document.documentElement ? document.documentElement.offsetHeight + 'px' : '100%';
		}
	}


	/***
		Entfernt 'px' aus einem String und gibt eine Nummer zurück
	*/
	this.removePx = function( val )
	{ 
		if( val.indexOf('px') )
		{
			val = Number( val.substr(0, val.indexOf('px')) );
		}
		return val;
	}
	
	
	/***
		Initialisiere Fenster Bewegung
	*/
	this.move = function(ev)
	{
		// We start interacting with the window 
		mywin.moveing = true;
		
		// 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;

		// Set Events
		mywin.eventUnbind( mywin._mclipzone, 'mousedown', mywin.move );
		mywin.eventBind( mywin._mclipzone, 'mouseup', mywin.stopp );
		mywin.eventBind( mywin._mclipzone, 'mousemove', mywin.follow );
		
		mywin.expandClipArea( mywin._mclipzone );
	}


	/***
		Fensterbewegung beenden
	*/
	this.stopp = function(ev)
	{
		// We're not moveing the window anymore
		mywin.moveing = false;
		
		// Set Events
		mywin.eventUnbind( mywin._mclipzone, 'mousemove', mywin.follow );
		mywin.eventUnbind( mywin._mclipzone, 'mouseup', mywin.stopp );
		mywin.eventBind( mywin._mclipzone, 'mousedown', mywin.move );		
		
		// Reset the dimensions of the clipping area to the titlebar width and height
		mywin.setClipZones();
	}


	/***
		Setzt X und Y Achsen beim bewegen des Fensters
	*/
	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);
		}


	/***
		Initialisiert fenster resizeing
	*/
	this.resize = function(ev)
	{
		mywin.resizing = true;
		
		mywin.expandClipArea( mywin._rclipzone );
		
		// Set Events
		mywin.eventUnbind( mywin._rclipzone, 'mousedown', mywin.resize );
		mywin.eventBind( mywin._rclipzone, 'mouseup', mywin.stopResize );
		mywin.eventBind( mywin._rclipzone, 'mousemove', mywin.growShrink );
	}
	
	
	/***
		Beendet fenster resizeing
	*/
	this.stopResize = function(ev)
	{
		// We've stopped window resizeing
		mywin.resizing = false;
	
		// Set Events
		mywin.eventUnbind( mywin._rclipzone, 'mouseup', mywin.stopResize );
		mywin.eventUnbind( mywin._rclipzone, 'mousemove', mywin.growShrink );
		mywin.eventBind( mywin._rclipzone, 'mousedown', mywin.resize );		
		// Reset the dimensions of the clipping area to the default width and height
		mywin.setClipZones();
	}


	/***
		Setzt höhe und breite beim 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 - (Number(mywin.w) + Number(mywin.x))) + Number(mywin.w); // neue breite
		var nh = (y - (mywin.h + mywin.y)) + mywin.h; // neue höhe
		
		// Minimale breite und höhe berechnen
		mywin.w = (nw <= mywin.minwidth) ? mywin.w : nw;
		mywin.h = (nh <= mywin.minheight) ? mywin.minheight : nh;
		
		// Fenstergröße setzen
		mywin.win.style.width 	= mywin.w + 'px';
		mywin.win.style.height 	= mywin.h + 'px';

		// Contentgröße setzen
		mywin._wintitle.style.width = mywin.getTitleWidth() + 'px';
		mywin.content.style.width 	= mywin.getContentWidth() + 'px';
		mywin.content.style.height 	= mywin.getContentHeight() + 'px';
		mywin.setButtons();
	}

	
	this.minMaxAnimations = function()
	{
		var titleWidth 		= mywin.getTitleWidth();
		var contentWidth 	= mywin.getContentWidth();
		var contentHeight 	= mywin.getContentHeight();
		
		jQuery(mywin.win).animate( {width: mywin.w + 'px', height: mywin.h + 'px', left: mywin.x + 'px', top: mywin.y + 'px'}, mywin.animationSpeed );
		jQuery(mywin._wintitle).animate( {width: titleWidth+"px"}, mywin.animationSpeed );
		jQuery(mywin.content).animate( {width: contentWidth+"px", height: contentHeight+"px"}, mywin.animationSpeed );
		return;
	}


	/***
		Fenster maximieren
	*/
	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 );
		document.body.removeChild( mywin._rclipzone );

		// 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;

		// 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	 = mywin.getTitleWidth() + 'px';
			mywin.content.style.width	 = mywin.getContentWidth() + 'px';
			mywin.content.style.height	 = mywin.getContentHeight() + 'px';
		}
		
		mywin.eventUnbind( mywin._mclipzone, 'mousedown', mywin.move );

		mywin.status = 'maximized';

		mywin.setButtons();
		mywin.setClipZones();
		return;
		}


	/***
		Fenster minimieren
	*/
	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);
		
		document.body.removeChild( mywin._rclipzone );

		mywin.w = mywin._btnposxd[2] + 100;
		mywin.h = mywin.headheight;
		mywin.y = 0;
		
		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 	= '0px';
			mywin.content.style.height	= '0px';
	 		mywin._wintitle.style.width = mywin.getTitleWidth() + 'px';
	 	}

		mywin.status = 'minimized';

		mywin.setButtons();
		mywin.setClipZones();

		return;
	}


	/***
		Fenster in Standardmodus zurücksetzen
	*/
	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';		
		}
		
		if( mywin.status == 'maximized' )
		{
			mywin.eventBind( mywin._mclipzone, 'mousedown', mywin.move );
		}
		
		document.body.appendChild( mywin._btnmax );
		document.body.appendChild( mywin._btnmin );
		
		document.body.removeChild( mywin._btnreset );
		document.body.appendChild( mywin._rclipzone );

		mywin.restore = false;

		mywin.status = 'standard';

		mywin.setButtons();
		mywin.setClipZones();
		}

	
	/***
		Fenster schließen
	*/
	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 && mywin.status == 'standard'){
			document.body.removeChild(mywin._rclipzone);
			}
		if(mywin.basics){
			if(mywin.status != 'standard'){
				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);
		}


	/***
		Stylesheets lesen und in array Speichern
	*/
	this.getSkin = function(){
		// this.skin['.' + this.classprefix + 'SELECTORNAME'].style.PROPERTY;
		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;
							}
						}
					}
				}
			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;
						}
					}
				}
			}
		return;
		}

	
	/***
		Regelt die positionen der Buttons
	*/
	this.setButtons = function(){
		if(mywin.closeable){
			mywin._btnposx[0] = Number(mywin.x) + Number(mywin.w) - Number(mywin._btnposxd[0]);
			mywin._btnposy[0] = mywin.y + mywin.removePx(mywin.skin['.' + mywin.classprefix + 'ButtonClose'].style.top);
			
			if(mywin.useAnimations && !mywin.moveing && !mywin.resizing){
				jQuery(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] = Number(mywin.x) + Number(mywin.w) - Number(mywin._btnposxd[1]);				
			mywin._btnposy[1] = Number(mywin.y) + mywin.removePx( mywin.skin['.' + mywin.classprefix + 'Maximizer'].style.top );
			
			// Minimize button position
			mywin._btnposx[2] = Number(mywin.x) + Number(mywin.w) - Number(mywin._btnposxd[2]);
			mywin._btnposy[2] = mywin.y + mywin.removePx( mywin.skin['.' + mywin.classprefix + 'Minimizer'].style.top );

			if(mywin.useAnimations && !mywin.moveing && !mywin.resizing){
				jQuery(mywin._btnreset).animate({left: mywin._btnposx[1]+'px', top: mywin._btnposy[1]+'px'},mywin.animationSpeed);
				jQuery(mywin._btnmax).animate({left: mywin._btnposx[1]+'px', top: mywin._btnposy[1]+'px'},mywin.animationSpeed);
				jQuery(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;
		}


	/***
		Clipping Zonen nach Interaktion neu ausrichten
	*/
	this.setClipZones = function(){
		if(mywin.moveable){
			document.body.removeChild( mywin._mclipzone ); // die beiden zeilen sind ein workaround für firefox um das "ziehen" der Clipping Area aus dem Browserfenster zu verhindern
			document.body.appendChild( mywin._mclipzone );
		
			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){
			if( mywin.status == 'standard' )
			{
				document.body.removeChild( mywin._rclipzone );
				document.body.appendChild( mywin._rclipzone );
			}
			
			mywin._rclipzone.style.width = '15px'; 
			mywin._rclipzone.style.height = '15px';
			mywin._rclipzone.style.left = Number(mywin.x) + Number(mywin.w) + 'px';
			mywin._rclipzone.style.top = Number(mywin.y) + Number(mywin.h) + 'px';
			mywin._rclipzone.style.zIndex = mywin.z + 1;
			}
		return;
		}


	/***
		Helper Funktionen 
	*/
	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);
		
		w += mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Titlebar'].style.marginLeft);
		w += mywin.removePx(mywin.skin['.' + mywin.classprefix + 'Titlebar'].style.marginRight);

		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;
		}


	/***
		Setzt die z-Achse des Fensters
		win.setZ( int z-axis )
	*/
	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;
	}


	/***
		Bewegt das Fenster zu den angegebenen Koordinaten
		mywin.moveTo( int x-axis, int y-axis )
	*/
	this.moveTo = function(x, y){
		mywin.x = x;
		mywin.y = y;
		
		if( mywin.useAnimations && !mywin.moveing )
		{
			jQuery(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';
		}
		
		// Set button positions
		mywin.setButtons();
		
		if(!mywin.moveing)
		{
			mywin.setClipZones();
		}
		return;
		}


	/***
		Resize the window to the given dimensions
		win.resizeTo( int width, int height )
	*/
	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){
			jQuery(mywin.win).animate({width: mywin.w+'px', height: mywin.h+'px'},mywin.animationSpeed);
			jQuery(mywin.content).animate({width: contentWidth+'px', height: contentHeight+'px'},mywin.animationSpeed);
			jQuery(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';
			}
		mywin.setClipZones();
		mywin.setButtons();
		return;
	}


	/***
		Set the window name in the title bar
	*/
	this.setWindowTitle = function(title){
		mywin.winname.innerHTML = title;
		return;
		}


	if(this.content){ 
		// Construct Window
		var atr = this.content.getAttributeNode('mywindow');
		var iswin = atr ? atr.nodeValue : 0;
		if( iswin == 0 )
		{ // Container ist noch kein Fenster
			iswin = document.createAttribute('mywindow');
			iswin.nodeValue = '1';
			this.content.setAttributeNode(iswin);
		}
		else
		{ // Container ist bereits ein Fenster
			delete(mywin);
			return;
		}
		
		this.getSkin(); // Stylesheet

		// Höhe der Titelleiste ausrechnen
		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.marginTop);
		this.headheight 	+= this.removePx(this.skin['.' + this.classprefix + 'Titlebar'].style.marginBottom);
		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(this.scrollbars){
			this.content.style.overflow = 'auto';
			}else{
			this.content.style.overflow = 'hidden'; // fehler bei iframes
			}
		this.win.appendChild(this.content); // content an win anhaengen

		// Titelleiste:
		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 );
		
		// Window Titel:
		this.winname = document.createElement('div');
		this.winname.className = this.classprefix + 'Name'; // Apply Skin to titlebar
		this._wintitle.appendChild( this.winname );
				
		if(this.windowname != ''){
			this.winname.innerHTML = this.windowname;
			}
		
		if(this.moveable){
			this._mclipzone = document.createElement('div');
			this._mclipzone.className = 'mywindowClippingArea';
			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.width = this.getTitleWidth() + 'px';
			this._mclipzone.style.height = this.headheight + 'px';
			this._mclipzone.style.zIndex = this.z + 1;
				
			this.eventBind( this._mclipzone, 'mousedown', mywin.move );
			document.body.appendChild(this._mclipzone);
			}
		
		if(this.resizeable){
			// Resizer Clipping Zone
			this._rclipzone = document.createElement('div');
			this._rclipzone.className = this.classprefix + 'Resizer mywindowClippingArea';
			this._rclipzone.style.position = 'absolute';
		
			this._rclipzone.style.left = Number(mywin.x) + Number(mywin.w) + 'px';
			this._rclipzone.style.top = Number(mywin.y) + Number(mywin.h) + 'px';
			
			this._rclipzone.style.cursor = 'se-resize';
			this._rclipzone.style.width = '15px';
			this._rclipzone.style.height = '15px';
			this._rclipzone.style.zIndex = this.z + 1;
			
			// this._rclipzone.style.backgroundColor = '#ffffff';
			
			this.eventBind( this._rclipzone, 'mousedown', this.resize );
			document.body.appendChild(this._rclipzone);
			}
		if(this.closeable){
			// Schließen Button
			this._btnclose 		= document.createElement('a');
			this._btnposxd[0] 	= this.removePx(this.skin['.' + this.classprefix + 'ButtonClose'].style.width);
			this._btnposx[0] 	= Number(this.x) + Number(this.w) - Number(this._btnposxd[0]);

			this._btnposy[0] 	= this.y + this.removePx( this.skin['.' + this.classprefix + 'ButtonClose'].style.top );
			
			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.ismanaged )
			{
				this.eventBind( this._btnclose, 'click', this.closeWin );
			}
						
			document.body.appendChild(this._btnclose);
			}
		if(this.basics){
			// Maximieren und Minimieren Buttons
			this._btnmax = document.createElement('a');
			this._btnmin = document.createElement('a');
			
			this._btnposxd[1] = this.removePx(this.skin['.' + this.classprefix + 'Maximizer'].style.width);
			this._btnposxd[1] += this.closeable ? this._btnposxd[0] : 0;
			
			this._btnposxd[2] = this._btnposxd[1] + this.removePx(this.skin['.' + this.classprefix + 'Minimizer'].style.width);
			
			this._btnposx[1] = Number(this.x) + Number(this.w) - Number(this._btnposxd[1]);
			this._btnposx[2] = Number(this.x) + Number(this.w) - Number(this._btnposxd[2]);
			
			this._btnposy[1] = this.y + this.removePx( this.skin['.' + this.classprefix + 'Maximizer'].style.top );
			this._btnposy[2] = this.y + this.removePx( this.skin['.' + this.classprefix + 'Minimizer'].style.top );
			
			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;
			
			this.eventBind( this._btnmax, 'click', this.maximize );
			this.eventBind( this._btnmin, 'click', this.minimize );
			this.eventBind( this._btnreset, 'click', this.reSet );
			
			document.body.appendChild(this._btnmax);
			document.body.appendChild(this._btnmin);
			}
		return this.content.id;
		}
	else {
		return false;
		}
}
