function WindowManager(){
	var windows = new Array();
	this.self = this;
	var windowIndex = 0;
	
	Event.observe(window, 'keydown', keyboardHandler, false);
	this.windowExec = new PeriodicalExecuter( function (){ _WM.windowGarbageCollector(); }, 10 );
	
	function keyboardHandler( event ){
		if( event.altKey && ( event.keyCode == event.DOM_VK_W ) ){
			_WM.closeWindow( -1 );
		}
	}
	
	var getNewWindowId = function (){
		return ++windowIndex;
	}
	
	this.getWindow = function ( windowId ){
		return windows[windowId];
	}
	
	this.openWindowBySource = function ( sourceText, title, width, height, initX, initY, name ){
		var container = document.createElement( "DIV" );
		container.id = "container["+getNewWindowId()+"]";
		container.innerHTML = sourceText;
		document.body.appendChild( container );
		this.openWindow( container, title, width, height, initX, initY, name );
	}
	
	this.openWindow = function ( obj, titleText, width, height, initX, initY, name ){
		var p = new PopUpWindow( obj, titleText, width, height, initX, initY, name );
		p.id = windows.length;
		windows[ windows.length ] = p;
		
		return p.id;
	}
	
	this.openSingleWindow = function ( obj, titleText, width, height, initX, initY, name ){
		this.closeAllWindows();
		try{
			return this.openWindow( obj, titleText, width, height, initX, initY );
		}catch( e ){
			log( "_WM.openSingleWindow(): "+e.message );
			return -1;
		}
	}
	
	this.closeWindow = function ( windowId ){
		try{
			if( windowId == -1 )
				windowId = windows[ windows.length -1 ].id;
		}catch( e ){
			log( "_WM.closeWindow() failed for windowId("+windowId+"): "+e.message );
			return false;
		}
		
		try{
			windows[ windowId ].dispose();
		}catch( e ){
			log( "_WM.closeWindow() failed for windowId("+windowId+"): "+e.message );
			return false;
		}
		
		return true;
	}
	
	this.removeDisposedWindow = function ( windowId ){
		windows = windows.without( windows[windowId] );
	}

	this.closeAllWindows = function (){
		for( var winId = 0; winId < windows.length; winId++ )
			this.closeWindow( winId );
	}
	
	this.windowGarbageCollector = function (){
		for( var i=0; i<windows.length; i++ ){
			if( windows[i].state == "closed" ){
				windows[i].removeChildFromDom();
				_WM.removeDisposedWindow( i );
			}				
		}
	}
}

function PopUpWindow( contObj, titleText, pWidth, pHeight, initX, initY ){
	this.state = "open";
	
	this.contentObj = contObj;
	this.title	= titleText;
	this.width	= pWidth;
	this.height	= pHeight;
	this.initX	= initX;
	this.initY	= initY;
	this.draggable;
	this.mainDiv;
	this.iframe;
	var divStub;

	this.id = null;

	this.dispose = function ( ev ){
		try{
			divStub.removeChild( this.iframe );
		}catch( e ){
			log( "dispose, iframe remove failed: "+e.message  );
		}
		
		try{
			Effect.Shrink( this.mainDiv.id );
			this.state = "closed";
		}catch( e ){
			log( "FloatingDiv["+this.contentObj.id+"].dispose() failed: " + e.message );
		}
	}
	
	this.removeChildFromDom = function (){
		try{
			this.mainDiv.parentNode.removeChild( this.mainDiv );
		}catch( e ){
			log( "FlotingDiv[" + this.contentObj.id + "].removeChildFromDom() failed: " + e.message );
		}
	}
	
	this.display = function (){
		try{
			var winDiv = document.createElement( "DIV" );
			winDiv.id  = (Math.random()+"").replace( ".", "" );
			winDiv.className	  = "popUpWindow";
			winDiv.style.position = "absolute";
			winDiv.style.zIndex   = parseInt( Math.random()*10000 );
			
			if( this.width != undefined ){
				winDiv.style.width = this.width+"px";
			}else log( "no width has been given for "+this.contentObj.id );
			
			if( this.height != undefined ){
				winDiv.style.height = this.height+"px";
			}else log( "no height has been given for "+this.contentObj.id );

			if( this.initX != undefined )
				winDiv.style.left = initX+"px";
			else 
				winDiv.style.left = "400px";
			
			if( this.initY != undefined ){
				winDiv.style.top = initY+"px";
			}else 
				winDiv.style.top = "96px";

			var titleDiv = document.createElement( "DIV" );
			titleDiv.className = "popUpHandle";
			
			var titleSpan = document.createElement( "DIV" );
			titleSpan.className = "popUpWinTitleText";
			titleSpan.innerHTML = this.title;
		
			var closeButton = document.createElement( "DIV" );
//			var img = document.createElement( "IMG" );
//			img.src = _CONTEXT_PATH+"/images/buttons/closeButton1.gif";
//			img.className = "windowCloseButton";
//			img.alt = "X "
//			closeButton.appendChild( img );
			closeButton.innerHTML = "x";
			
			closeButton.className = "popUpWinCloseButton";
			closeButton.onclick = this.dispose.bindAsEventListener(this);
			
			var contentDiv = document.createElement( "DIV" );
			contentDiv.appendChild( this.contentObj );
			contentDiv.className = "popUpContentContainer";
			
			var ifr = document.createElement( "IFRAME" );
			ifr.style.position	 = "absolute";
			ifr.style.paddingTop = "20px";
//			ifr.style.top		 = winDiv.style.top;
//			ifr.style.left		 = winDiv.style.left;
			ifr.style.width		 = winDiv.style.width;
			ifr.style.height	 = winDiv.style.height;
			ifr.style.zIndex	 = "-1";
			ifr.style.border 	 = "0px";
			
			titleDiv.appendChild( titleSpan );
			titleDiv.appendChild( closeButton );
			winDiv.appendChild( ifr );
			winDiv.appendChild( titleDiv );
			winDiv.appendChild( contentDiv );
			document.body.appendChild( winDiv );
			
			this.mainDiv = winDiv;
			this.iframe  = ifr;
			divStub		 = winDiv;
			
			this.draggable = new Draggable( winDiv, {handle: titleDiv} );
		}catch( e ){
			log( "FloatingDiv["+this.contentObj.id+"].display() failed: "+e.message ); 
		}
	}
	
	this.logPosition = function ( obj ){
		try{
			log( "x = "+ divStub.style.left + " y = " + divStub.style.top );
		}catch( e ){
			log( e.message+"\n"+obj.constructor );
		}
	}

	this.display();
}
