/******************************************************************************
*	MyWindow 2011.1.1 (20110314)
*
*	Copyright 2004 - 2011 Carsten Ruppert - carsten.ruppert-at-web.de
*
******************************************************************************/
function MyWindowManager( config )
{
	var winman 	= this;
	
	this.stack 	= new Array();
	this.overlayStack = new Array();
	
	this.active = null;
	this.xc 	= null;

	this.useOverlay 		= config.overlay ? config.overlay : false;
	this.overlayColor 		= config.overlayColor ? config.overlayColor : '#000000';
	this.overlayOpacity 	= config.overlayOpacity ? config.overlayOpacity : 0.7;

	this.animationSpeed 	= config.animSpeed ? config.animSpeed : 600;
	this.useXMLConnector 	= config.ajax ? config.ajax : false;
	
	this.zindex				= config.zindex ? config.zindex : 100;
	
	this.overlay 		= false;
	this.lastScrollPosY	= 0;
	this.lastScrollPosX	= 0;
	this.currentWindow 	= undefined; // Das Fenster im Vordergrund
	
	/***
		XML Connector
		returns XML Connector Object
	*/
	this.initConnector = function()
	{
		if(window.XMLHttpRequest)
		{ 	// Standard
			winman.xc = new XMLHttpRequest();
			return true;
		}
		else if(window.ActiveXObject)
		{ 
			// Microsoft
			try
			{
				winman.xc = new ActiveXObject("Msxml2.XMLHTTP");
				return true;
			}
			catch( err )
			{
				try
				{
					winman.xc = new ActiveXObject("Microsoft.XMLHTTP");
					return true;
				}
				catch( err )
				{
					return false;
				}
			}
		}
		else
		{
			return false;
		}
	}

	this.destructConnector = function()
	{
		delete( winman.xc );
		return true;
	}

	this.appendOverlay = function()
	{
		var w = "100%";
		var h = window.innerHeight ? window.innerHeight : document.documentElement.offsetHeight;
		
		var overlay = document.createElement('div');
		overlay.id = "overlay";
		overlay.style.display = 'none';
		overlay.style.position = 'absolute';
		overlay.style.background = winman.overlayColor;
		overlay.style.top = '0';
		overlay.style.left = '0';
		overlay.style.width = w;
		overlay.style.height = h + 'px';
		overlay.style.opacity = winman.overlayOpacity;
		overlay.style.filter = 'alpha(opacity=' + (100 * winman.overlayOpacity) + ')';
		overlay.style.zIndex = winman.zindex;

		// Store last Y Position
		if(document.documentElement.scrollTop)
		{
			winman.lastScrollPosY = document.documentElement.scrollTop;
		}
		else
		{
			winman.lastScrollPosY = window.pageYOffset;
		}
		window.scrollTo(0,0);
		document.body.style.overflow = 'hidden';
		document.body.appendChild( overlay );
		
		// Fade in overlay and open the first window after animation complete -> this needs jQuery
		jQuery(overlay).fadeIn( winman.animationSpeed, function(){ 
			winman.openWindow(); return true;
			});
		return overlay;
	}
		
	this.detachOverlay = function()
	{
		
		// All windows are close, fade out overlay
		
		jQuery(winman.overlay).fadeOut( winman.animationSpeed, function()
		{		
			var overlay = document.getElementById('overlay');
			document.body.removeChild(overlay);
			document.body.style.overflow = '';
			window.scrollTo( 0, winman.lastScrollPosY );
		});
		winman.overlay = false;
		return;
	}

	this.addWindow = function( url, windowname, width, height, theme )
	{
		this.lastUri 		= url;
		this.lastName 		= windowname;
		this.lastWidth 		= width;
		this.lastHeight 	= height;
		this.lastTheme 		= theme;
		
		if( !this.overlay && this.useOverlay )
		{
			this.overlay = this.appendOverlay();
		}
		else
		{
			this.openWindow();
			return true;
		}
	}
		
	this.openWindow = function()
	{
		var w = winman.lastWidth;
		var h = winman.lastHeight;

		if(w == 0)
		{ // auto width
			w = window.innerWidth ? window.innerWidth - 100 : document.documentElement.offsetWidth - 100;
		}
		if(h == 0)
		{ // auto height
			h = window.innerHeight ? window.innerHeight - 100 : document.documentElement.offsetHeight - 100;
		}

		var x = window.innerWidth ? (window.innerWidth / 2) - (w / 2) : (document.documentElement.offsetWidth / 2) - (w / 2);
		var y = window.innerHeight ? (window.innerHeight / 2) - (h / 2) : (document.documentElement.offsetHeight / 2) - (h / 2);
		
		var z = winman.zindex + ((winman.stack.length + 1) * 100);
		var winid = winman.stack.length;

		if(winman.useXMLConnector){
			// Experimental...
			// use XML Connector Object to load window content
			var container = document.createElement('div');
			container.style.display = "none";
			container.id = 'mywincontent' + winid;
			document.body.appendChild(container);

			var connector = winman.initConnector();
			if(connector){	
				winman.xc.onreadystatechange = function(){
					if(winman.xc.readyState == 4 && winman.xc.status == 200){
						container.innerHTML = winman.xc.responseText;
						winman.destructConnector();
						}
					}
				winman.xc.open('GET',winman.lastUri,true);
				winman.xc.send(null);
				}
			}
		else{
			// Use iframe to load window content
			var container = document.createElement('iframe');
			container.id = 'mywincontent' + winid;
			container.style.display = 'none';
			container.style.borderWidth = '0';
			container.frameBorder = 0;
			container.frameborder = 0;
			container.marginheight = 0;
			container.marginHeight = 0;
			container.marginwidth = 0;
			container.marginWidth = 0;
			document.body.appendChild(container);
			container.src = winman.lastUri;
			}

		myWindowConfig.customTheme = winman.lastTheme;

		var win = new MyWindow( container.id, w, h, x, y, z, winman.lastName, myWindowConfig );
		win.win.style.display = 'none';
		winman.stack[winman.stack.length] = win;
		winman.overlayStack[winman.overlayStack.length] = document.createElement('div');
		winman.currentWindow = win;
		win.win.id = winid;
		win.ismanaged = true;
		
		jQuery(win.win).fadeIn(winman.animationSpeed);
		if( win.moveable )
		{
			win._mclipzone.id = 'mclip'+winid;
			win.eventBind( win._mclipzone, 'mousedown', winman.activate );
		}
		if( win.resizeable )
		{
			win._rclipzone.id = 'rclip'+winid;
			win.eventBind( win._rclipzone, 'mousedown', winman.activate );
		}
		if( win.basics )
		{
			win._btnmin.style.display = 'none';
			win._btnmax.style.display = 'none';
			win._btnreset.id 	= 'btres'+winid;
			win._btnmax.id 		= 'btmax'+winid;
			win._btnmin.id 		= 'btmin'+winid;
			win.eventBind( win._btnreset, 'mosedown', winman.activate );
			win.eventBind( win._btnmax, 'mousedown', winman.activate );
			win.eventBind( win._btnmin, 'mousedown', winman.activate );
			jQuery(win._btnmin).fadeIn( winman.animationSpeed );
			jQuery(win._btnmax).fadeIn( winman.animationSpeed );
		}
		if( win.closeable )
		{
			win._btnclose.style.display = 'none';
			win._btnclose.id = 'btcls'+winid;
			win.eventBind( win._btnclose, 'mousedown', winman.windowClosed );
			jQuery(win._btnclose).fadeIn( winman.animationSpeed );
		}
	}

	this.getWindowId = function(ev){
		var winno;
		if( ev.target )
		{ // Most modern Browsers
			if( ev.target.id != '' )
			{
				return ev.target.id.substring( 5, ev.target.id.length );
			}
			else
			{
				alert('Unable to get Window id.');
				return 0;
			}
		}
		else if( ev.srcElement )
		{ // Old Internet Explorer
			if( ev.srcElement.id != '' )
			{
				return ev.srcElement.id.substring( 5, ev.srcElement.id.length );
			}
			else
			{
				alert('Unable to get Window id in your Internet Explorer.');
				return 0;
			}
		}
		else
		{
			alert('Your Browser is not supported');
			return 0;
		}
	}


	this.resetIds = function()
	{
		var tmpw;
		
		// alert( winman.stack.length );
		
		if( winman.stack.length > 1 )
		{
			for ( i = 0; i < winman.stack.length; i++ )
			{
				tmpw = winman.stack[i];
				tmpw.win.id = i;
				tmpw.content.id = 'mywincontent' + i;
				
				if(tmpw.basics){
					tmpw._btnreset.id 	= 'btres' + i;
					tmpw._btnmax.id 	= 'btmax' + i;
					tmpw._btnmin.id 	= 'btmin' + i;
					}
				if(tmpw.closeable)
				{
					tmpw._btnclose.id = 'btcls' + i;
				}
				if(tmpw.resizeable)
				{
					tmpw._rclipzone.id = 'rclip' + i;
				}
				if(tmpw.moveable)
				{
					tmpw._mclipzone.id = 'mclip' + i;
				}
				tmpw.setZ( ((i + 1) * 100) );
				// tmpw.setZ( i + 10 );
			}
		}
	}
		
	this.windowClosed = function( ev )
	{
		var affected = winman.getWindowId( ev );
		
		//alert(affected);
		
		// Content Container loeschen
		var container = document.getElementById( 'mywincontent' + affected );
		var parent = container.parentNode;
		try
		{
			parent.removeChild(container);
		}
		catch(err)
		{
			alert(err.description);
		}
		// Fenster vom Stapel loeschen:
		winman.stack.splice( affected, 1);
		
		if(winman.stack.length > 0)
		{
			// Es sind noch Fenster offen -> Ids neu setzen...		
			winman.resetIds();
		}
		else
		{
			// Keine weiteren Fenster offen, overlay ausblenden wenn aktiv
			if( winman.useOverlay && winman.overlay )
			{
				winman.detachOverlay();
			}
		}
	}
	
	this.activate = function (ev)
	{
		var winno = winman.getWindowId( ev );
		
		// alert('activate window '+winno);
		
		if( winman.stack.length > 1 )
		{
			var win = winman.stack[winno];
			var newstack = new Array();
			for ( i = 0; i < winman.stack.length; i++ )
			{
				if ( i == winno && (i + 1) < winman.stack.length )
				{
					++i;
					newstack[newstack.length] = winman.stack[i];
				}
				else if (i != winno)
				{
					newstack[newstack.length] = winman.stack[i];
				}
			}
			newstack[newstack.length] = winman.stack[winno];
			winman.stack = newstack;
			delete( newstack );
			winman.resetIds();
			winman.currentWindow = winman.stack[winman.stack.length -1];
			return;
		}
	}
}
