/* Zum Fenster öffnen... */

function openWindow (containerid,windowname) {
	var mywin = new mywindow(containerid,800,626,180,50,2000,windowname,true,false,true,true,true,false,'');
}

/****************************************************
*	MyWindow 2005.1 (050114)						*
*	Carsten Ruppert - carsten.ruppert-at-web.de		*
*													*
*	Copyright 2004 - 2005 by Carsten Ruppert		*
*****************************************************

Changes in Version 050114
-------------------------------
- Added maximum/minimum width & height to window, but is not functional yet.



Changes in Version 050111
-------------------------------
- Fixed cursor positions on move/resize
- Fixed Statusbar on resizeing
- Fixed resizeing in IE


Not implemented
-------------------------------
- Fenster können nicht aktiviert werden.


Bugs
-------------------------------
- none found yet



Changes in Version 050104
-------------------------------
- Mostly rewritten

- Maximize and minimize (basics) does not work yet
- Activateing a window (focus it) does not work yet

- A Window can be closed and reopened now
- myWindow now checks if the given content-container is already a window (this is essential for closeing and reopening the window)
- Everything can now be sitched on or off (statusbar, resizeable etc...)
- Basic Skin Support based on CSS, each window can now have a different skin (skinname).

Bugs
- Cursor position is incorrect if the page is scrollable, this causes a strange behavior when moveing or resizeing the window
- Statusbar disapears when resizeing the window
- Resizeing in IE-Windows works strange
-------------------------------

****************************************************/

function mywindow(cid,w,h,x,y,z,windowname,scrollbars,statusbar,moveable,resizeable,closeable,basics,skinname) {
	
	var mywin = this;
	this.content = document.getElementById(cid);
	
	// Size		
	this.w = w;
	this.h = h;
	// Position
	this.x = x;
	this.y = y;
	this.z = z; // is in use yet
	
	this.bheight; // body height
	this.scrollbars = scrollbars;
	this.resizeable = resizeable;
	this.statusbar = statusbar;
	this.moveable = moveable;
	//this.basics = basics;
	this.basics = false // Basic window actions are not ready yet...
	this.closeable = closeable;
	this.skin = new Array(); // The Skin of this window
	this.windowname = windowname;
	
	this.minw = 0; // ko
	this.maxw = 0; // ko
	
	this.minh = 0; // ko -> headheight + statusheight + minh = minheight ... 
	this.maxh = 0; // ko
	
	this.skinname = skinname ? skinname : 'mywin'; // Skin support based on stylesheets
	this.reSet = new Array(null); // Saves size and position when minimizeing/maximizeing (x,y,w,h)
		
	/* Methods */
	this.prevMarking = function (ev) {
		// This function will prevent MSIE-W from marking the document body's content while moveing or resizeing the window
		return false;
		}	
	this.move = function (ev) { // Set Event Listeners for moveing the window
		if ( mywin.winmove.addEventListener ) {
			mywin.winmove.addEventListener('mouseup',mywin.stopp,false);
			mywin.winmove.addEventListener('mousemove',mywin.follow,false);			
			}
		else {
			// Prevent IE from marking content while moveing
			document.body.attachEvent('onselectstart',mywin.prevMarking);

			// On IE the events must be attached to the document body
			document.body.attachEvent('onmouseup',mywin.stopp);
			document.body.attachEvent('onmousemove',mywin.follow);			
			}

		if ( window.innerHeight ) {
			mywin.bheight = window.innerHeight;
			}
		else {
			mywin.bheight = document.body.offsetHeight;
			}
		mywin.winmove.style.left = '0px';
		mywin.winmove.style.top = '0px';
		mywin.winmove.style.width = '100%';
		mywin.winmove.style.height = (document.documentElement) ? document.documentElement.scrollHeight + 'px' : '100%';
		}
	this.stopp = function(ev) { // Reset Event Listeners to stop the window
		if ( mywin.winmove.addEventListener ) {
			mywin.winmove.removeEventListener('mousemove',mywin.follow,false);
			mywin.winmove.removeEventListener('mouseup',mywin.stopp,false);
			}
		else {
			document.body.detachEvent('onselectstart',mywin.prevMarking);
			document.body.detachEvent('onmousemove',mywin.follow);
			document.body.detachEvent('mouseup',mywin.stopp);			
			}
			
		mywin.winmove.style.left = mywin.x + 'px';
		mywin.winmove.style.top = mywin.y + 'px';
		mywin.winmove.style.width = mywin.basics || mywin.closeable ? mywin.w - 36 + 'px' : mywin.w + 'px';
		mywin.winmove.style.height = mywin.headheight + 'px';
		}
	this.follow = function(ev) { // Follow Cursor position ...
		
		// Cursor position for IE and others
		var x, y;
		var ydif = (document.documentElement) ? document.documentElement.scrollTop : 0; // The difference between the cursor position in browser window, and the real position inside the doc content
		if ( ev.clientX ) {
			x = ev.clientX;
			y = ev.clientY + ydif;
			}
		else {
			x = ev.pageX;
			y = ev.pageY + ydif;
			}

		ny = (y - (mywin.headheight / 2)); // new y
		mywin.win.style.top = ny + 'px';
		mywin.y = ny;
			
		nx = (x - ( mywin.w / 2)); // new x - window will center on cursor position
		mywin.win.style.left = nx + 'px';
		mywin.x = nx;

		if ( mywin.resizeable ) {		
			// Move Resizer
			mywin.winres.style.top = mywin.y + mywin.h - 15 + 'px';
			mywin.winres.style.left = mywin.x + mywin.w - 15 + 'px';
			}
		if ( mywin.statusbar ) {
			// Set statusbar position
			mywin.winstatus.style.top = mywin.y + mywin.h - mywin.statusheight + 'px';
			mywin.winstatus.style.left = mywin.x + 'px';
			}
		}
	this.resize = function(ev) { // Set Event Listeners for resizeing the window
		// Set new Events
		if ( mywin.winres.addEventListener ) {
			mywin.winres.removeEventListener('mousedown',mywin.resize,false);
			mywin.winres.addEventListener('mouseup',mywin.stopResize,false);
			mywin.winres.addEventListener('mousemove',mywin.growShrink,false);
			}
		else {
			mywin.winres.detachEvent('onmousedown',mywin.resize);

			// Prevent IE from marking content while moveing
			document.body.attachEvent('onselectstart',mywin.prevMarking);		
			document.body.attachEvent('onmouseup',mywin.stopResize);
			document.body.attachEvent('onmousemove',mywin.growShrink);
			}

		mywin.winres.style.top = '0px';
		mywin.winres.style.left = '0px';
		mywin.winres.style.width = '100%';
		if ( window.innerHeight ) {
			mywin.bheight = window.innerHeight;
			}
		else {
			mywin.bheight = document.body.offsetHeight;
			}		
		mywin.winres.style.height = (document.documentElement) ? document.documentElement.scrollHeight + 'px' : '100%';
		}
	this.stopResize = function(ev) { // Reset Event Listeners to stop resizeing
		mywin.winres.style.width = '15px';
		mywin.winres.style.height = '15px';

		mywin.winres.style.top = mywin.h + mywin.y - 15 + 'px';
		mywin.winres.style.left = mywin.w + mywin.x - 15 + 'px';
		
		// Reset Events
		if ( mywin.winres.addEventListener ) {
			mywin.winres.removeEventListener('mouseup',mywin.stopResize,false);
			mywin.winres.removeEventListener('mousemove',mywin.growShrink,false);			
			mywin.winres.addEventListener('mousedown',mywin.resize,false);
			}
		else {
			document.body.detachEvent('onselectstart',mywin.prevMarking);
			document.body.detachEvent('onmouseup',mywin.stopResize);
			document.body.detachEvent('onmousemove',mywin.growShrink);			
			
			mywin.winres.attachEvent('onmousedown',mywin.resize);
			}
		}
	this.growShrink = function(ev) { // Enlarge or shrink to Cursor position...
		// Cursor position for IE and others
		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 nh = (y - (mywin.h + mywin.y)) + mywin.h;
		var nw = (x - (mywin.w + mywin.x)) + mywin.w;
		
		mywin.h = nh;
		mywin.w = nw;
		
		mywin.win.style.width = nw + 'px';
		mywin.win.style.height = nh + 'px';
		
		
		//if ( nh < mywin.minheight ) { nh = mywin.minheight; }	// min height
		
		// check for minimum/maximum width <------------------------------------------------------ DOES NOT WORK CORRECTLY
		
		/* SCHROTT !
		if ( mywin.minw > 0 && mywin.maxw > 0 && nw > mywin.minw && nw < mywin.maxw ) {
			mywin.w = nw; mywin.win.style.width = nw + 'px';
			}
		else if ( mywin.minw > 0 && nw < mywin.minw ) {
			mywin.w = mywin.minw; mywin.win.style.width = mywin.minw + 'px';
			}
		else if ( mywin.maxw > 0 && nw > mywin.maxw ) {
			mywin.w = mywin.maxw; mywin.win.style.width = mywin.maxw + 'px';
			}
		else {
			mywin.w = nw;
			mywin.win.style.widht = nw + 'px';
			}
			
			
		// check for minimum/maximum height
		if ( nh > mywin.minheight && nh < mywin.maxh ) {
			mywin.h = nh; mywin.win.style.height = nh + 'px';
			}
		else if ( mywin.maxh > 0 && nh < mywin.maxh ) {
			mywin.h = mywin.minheight; mywin.win.style.height = mywin.minheight + 'px';
			}*/
		

		// check for maximum sizes...
		


		
		// set move div size
		if ( mywin.moveable ) { mywin.winmove.style.width = mywin.basics || mywin.closeable ? mywin.w - 36 + 'px' : mywin.w + 'px'; }
		// Set statusbar size & position
		if ( mywin.statusbar ) {
			mywin.winstatus.style.left = mywin.x + 'px';
			mywin.winstatus.style.top = mywin.y + mywin.h - mywin.statusheight + 'px';
			mywin.winstatus.style.width = mywin.w + 'px';
			}
		// set content div size (statusbar and titlebar)
		var cheight;
		if ( mywin.statusbar ) { cheight = mywin.headheight + mywin.statusheight } else { cheight = mywin.headheight }
		mywin.content.style.height = mywin.h - cheight + 'px';
		}
	
	this.maximize = function (ev) {
		
		// Enter code here :)		
		
		var inw = window.innerWidth;
		var inh = 0;
		
				
		
		if ( mywin.moveable ) {
			mywin.winmove.style.left = win.x + 'px';
			mywin.winmove.style.top = win.y + 'px';
			mywin.winmove.style.width = mywin.basics || mywin.closeable ? mywin.w - 36 + 'px' : mywin.w + 'px';
			}
		
		}
	this.minimize = function (ev) {
		
		// Also enter code here...
		void(0);
		}
	
	this.closeWin = function (ev) {
		// Destruct Window
		var attr = mywin.content.setAttribute('mywindow','0');

		var node; var chide;
		while ( mywin.win.hasChildNodes ) {
			node = mywin.win.firstChild;
			if ( node && node.id == cid && 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.winmove) }
		if ( mywin.resizeable ) {document.body.removeChild(mywin.winres) }
		if ( mywin.statusbar ) { document.body.removeChild(mywin.winstatus) }
		document.body.removeChild(mywin.win);		
		delete mywin;
		}

	this.getSkin = function () {
		// first creating a assoc. array to access the stylesheet rules by their selector text.
		var cssnode;
		for ( i = 0; i < document.styleSheets.length; i++ ) {
			if ( document.styleSheets[i].cssRules ) {
				// Gecko
				for ( x = 0; x < document.styleSheets[i].cssRules.length; x++ ) {
					cssnode = document.styleSheets[i].cssRules[x];
					if ( cssnode.selectorText.indexOf(this.skinname) ) {
						this.skin[cssnode.selectorText] = cssnode; // access skin properties via this(mywin).skin['.' + this.skinname + 'SELECTORNAME'].style.PROPERTY;
						}
					}
				}
			else {
				// IE-W
				for ( x = 0; x < document.styleSheets[i].rules.length; x++ ) {
					cssnode = document.styleSheets[i].rules[x];
					if ( cssnode.selectorText.indexOf(this.skinname) ) {
						this.skin[cssnode.selectorText] = cssnode; // access skin properties via this(mywin).skin['.' + this.skinname + 'SELECTORNAME'].style.PROPERTY;
						}
					}
				}
			}
		return;
		}
	
	
	if ( this.content ) { // Construct Window
		
		this.getSkin();
		
		// Get titlebar and statusbar height from stylesheet
		this.headheight = Number(this.skin['.' + this.skinname + 'Titlebar'].style.height.substring(0, this.skin['.' + this.skinname + 'Titlebar'].style.height.indexOf('px')));
		this.statusheight = statusbar ? Number(this.skin['.' + this.skinname + 'Statusbar'].style.height.substring(0, this.skin['.' + this.skinname + 'Statusbar'].style.height.indexOf('px'))) : 0;

		this.minheight = this.minh > 0 ? (this.headheight + this.statusheight) + this.minh : this.headheight + this.statusheight; // <-------------------------------- Conflict -> this.minh !!!
		
		// Check if the content	container is already a window
		var atr = this.content.getAttributeNode('mywindow');
		var iswin = atr ? atr.nodeValue : 0;
		if ( iswin == 0 ) { // The content container is not a window yet
			iswin = document.createAttribute('mywindow');
			iswin.nodeValue = '1';
			this.content.setAttributeNode(iswin);
			}
		else { // The conten container is a already window
			delete this.content;
			delete mywin;
			return;
			}		
		
		// Main Window Container
		this.win = document.createElement('div');
		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.className = this.skinname + 'Window'; // Apply Skin
		this.win.style.zIndex = this.z;

		document.body.appendChild(this.win); // append window to body;
		
		// Set Content div properties
		this.content.style.position = 'absolute';
		this.content.style.top = this.headheight + 'px';
		this.content.style.width = '100%';
		this.content.style.display = '';
		
		var cheight;
		if ( this.statusbar ) { cheight = this.headheight + this.statusheight } else { cheight = this.headheight }
		this.content.style.height = this.h - cheight + 'px';

		// Scrollbars
		if ( scrollbars ) { this.content.style.overflow = 'auto'; }
		else { this.content.style.overflow = 'hidden'; }

		this.win.appendChild(this.content); // append content to window;

		// Titlebar
		this.winhead = document.createElement('div');
		this.winhead.style.position = 'absolute';
		this.winhead.style.left = '0px';
		this.winhead.style.top = '0px';
		this.winhead.style.width = '100%';
		
		this.winhead.className = this.skinname + 'Titlebar'; // Apply Skin to titlebar
		this.win.appendChild(this.winhead); // append titlebar to window;

		if ( this.windowname != '' ) {
			var nameNode = document.createTextNode(this.windowname);
			this.winhead.appendChild(nameNode);
			}


		/* INTERACTING ... */
		
		// Div for moveing
		if ( this.moveable ) {
			this.winmove = document.createElement('div');
			this.winmove.style.position = 'absolute';
		
			this.winmove.style.left = this.x + 'px';
			this.winmove.style.top = this.y + 'px';
			this.winmove.style.width = this.basics || this.closeable ? this.w - 37 + 'px' : this.w + 'px';
		
			this.winmove.style.height = this.headheight + 'px';
			/* to change */	//this.winmove.style.border = '1px solid green';

			if ( this.winmove.attachEvent ) {
				mywin.winmove.attachEvent('onmousedown',this.move);
				}
			else { 
				this.winmove.addEventListener('mousedown',this.move,false);
				} // enable event handler for move div;
			
			
			this.winmove.style.zIndex = this.z + 1;
			document.body.appendChild(this.winmove); // append move div to body;		
			}
		
		// A Statusbar without any content :)
		if ( this.statusbar ) {
			this.winstatus = document.createElement('div');
			this.winstatus.style.position = 'absolute';
			this.winstatus.style.top = this.y + this.h - this.statusheight + 'px';
			this.winstatus.style.left = this.x + 'px';
			this.winstatus.style.width = this.w + 'px';
			
			this.winstatus.className = this.skinname + 'Statusbar';
			this.winstatus.style.zIndex = this.z + 1;
			
			document.body.appendChild(this.winstatus);
			}	
		// Div for resizeing
		if ( this.resizeable ) {
			this.winres = document.createElement('div');
			this.winres.style.position = 'absolute';
			this.winres.style.top = mywin.y + mywin.h - 15 + 'px';
			this.winres.style.left = mywin.x + mywin.w - 15 + 'px';
			this.winres.style.cursor = 'se-resize';
			this.winres.style.width = '15px';
			this.winres.style.height = '15px';
			this.winres.className = this.skinname + 'Resizer'; // Apply Skin
			this.winres.style.zIndex = this.z + 1;

			if ( this.winres.addEventListener ) {
				this.winres.addEventListener('mousedown',this.resize,false);
				}
			else {
				this.winres.attachEvent('onmousedown',this.resize);
				}			
			document.body.appendChild(this.winres);
			}
		// Basic window actions (maximize, minimize, restore) - like MS Windows
		if ( this.basics ) {
			var wmaxh = Number(this.skin['.' + this.skinname + 'Maximizer'].style.height.substring(0, this.skin['.' + this.skinname + 'Maximizer'].style.height.indexOf('px')));
		
		
			this.winmax = document.createElement('div'); // restore in mini mode
			this.winmin = document.createElement('div'); // no function in mini mode - no display
			
			this.winmax.style.position = 'absolute';
			this.winmax.style.right = '10px';
			this.winmax.style.top = (this.headheight / 2) / 2 + 'px';
			this.winmax.style.width = '10px';
			this.winmax.style.height = '10px';
			/* debug */ this.winmax.style.border = '1px solid green';

			this.winmin.style.position = 'absolute';
			this.winmin.style.right = '25px';
			this.winmin.style.top = (this.headheight / 2) / 2 + 'px';
			this.winmin.style.width = '10px';
			this.winmin.style.height = '10px';
			/* debug */ this.winmin.style.border = '1px solid green';
			
			if ( this.winmax.addEventListener ) {
				this.winmax.addEventListener('click',this.maximize,false);
				this.winmin.addEventListener('click',this.minimize,false);
				}
			else {
				this.winmax.attachEvent('onclick',this.maximize);
				this.winmin.attachEvent('onclick',this.minimize);
				}
			
			this.winhead.appendChild(this.winmax);
			this.winhead.appendChild(this.winmin);
			}
		
		if ( this.closeable ) {
			this.winclose = document.createElement('div');
			this.winclose.style.position = 'absolute';
			this.winclose.style.right = '10px';
			this.winclose.style.top = (this.headheight / 2) / 2 + 'px';
			this.winclose.className = this.skinname + 'ButtonClose'; // Apply Skin
			this.winhead.appendChild(this.winclose);
			
			if ( this.winclose.addEventListener ) {
				this.winclose.addEventListener('click',this.closeWin,false);
				}
			else {
				this.winclose.attachEvent('onclick',this.closeWin);
				}			
			}
		}
	else {
		return false;
		}
}