/******************
* Window Object 
*******************/
function kaWindow(id,winManager){
	this.domObj = null;//domObj init
	this.winManager = winManager;
	this.title = null;
	this.active = null;
	this.position = null;
	this.expanded = true;//is a window is full expanded or rolled up (default: true)
	//this.resizable=false;//if a window can be resized (default: false)
	this.resizing=false;// while a window is resised
	//this.draggable=true;//if a window can be dragged (default: true)
	
	/* For our maximize Function */
	this.isMaximized = false;
	this.oldWidth = null;
	this.oldHeight = null;
	this.oldLeft = null;
	this.oldTop = null;
	
	/* visible */
	this.isVisible = true;
	
	/* position in window array */
	var myPosition = null;
	
	this.pushedObj = null;//the domObj that has, or not, been 'pushed' inside.
	
	for(var i=0;i<this.winManager.windows.length;i++){
		if(this.winManager.windows[i].id==id){
			this.domObj = this.winManager.windows[i];
		}
	}
	
	if(!this.domObj) {
		this.domObj = document.createElement('div');
		this.domObj.parent=this;
		this.domObj.id= id;
		this.domObj.overflow= 'none';
		this.domObj.onclick=this.setActive;
		this.domObj.omousedown=this.setActive;
		this.winManager.desktop.appendChild( this.domObj );
	}
	
	
	return this;
};
kaWindow.prototype.setValues = function(title,x,y,w,h,resizable,draggable,minW,minH) {
	var headerHeight = 25;
	var footerHeight = 15;
	this.title = title;
	this.active = true;
	this.expanded = true;
	this.minW=(minW)?minW:100;
	this.minH=(minH)?minH:100;

	this.resizable=resizable;
	this.draggable=draggable;
	
	var initialized=(this.domObj.firstChild)?true:false;

	this.x = x;
	this.y = y;
	this.w = w;
	this.h = h;
	
	//set framebox
	this.domObj.style.position = 'absolute';
	this.domObj.style.left = x+'px';
	this.domObj.style.top = y+'px';
	this.domObj.style.height = h + 'px' ;
	this.domObj.style.width = w + 'px';
	this.domObj.style.zIndex = this.winManager.windows.length+this.winManager.minZindex;
	this.domObj.className="wmDomObj";

	//set header
	this.headerObj = document.createElement('div');
	this.headerObj.className='wmWHeader'; 
	this.headerObj.style.position='absolute'; 
	this.headerObj.style.top='0px'; 
	this.headerObj.style.width= w + 'px'; 
	this.headerObj.style.height= headerHeight + 'px'; 
	
		//set header title
		this.headerTitle = document.createElement('span');
		this.headerTitle.className='wmWHeaderTitle'; 
		this.headerTitle.style.position='absolute'; 
		this.headerTitle.innerHTML= title ; 
/*		this.headerTitle.style.top= '0px' ; 
		this.headerTitle.style.left= '-2px' ; 
		this.headerTitle.style.width= '100%' ; 
		this.headerTitle.style.height= headerHeight-4+'px' ;*/ 
		this.headerObj.appendChild( this.headerTitle );
		
		//set header mask (to avoid title selection)
		this.headerMask = document.createElement('div');
		this.headerMask.className='wmWHeaderTitleMask'; 
		this.headerMask.style.position='absolute'; 
		this.headerMask.style.width='100%'; 
		this.headerMask.style.height= headerHeight-4+'px' ;
		this.headerMask.window= this ;
		if(this.draggable){
			this.headerMask.onmousedown= this.startDrag; 
			this.headerMask.onmouseup = this.stopDrag; 
			/*this.headerTitle.onmousemove = this.resizeMove; 
			this.headerTitle.onmouseout = this.resizeStop; */
			if (this.headerMask.captureEvents) {
			   this.headerMask.captureEvents(Event.MOUSEDOWN);
			   this.headerMask.captureEvents(Event.MOUSEUP);
			  /* this.headerTitle.captureEvents(Event.MOUSEMOVE);
			   this.headerTitle.captureEvents(Event.MOUSEOUT);*/
			}
		}
		this.headerObj.appendChild( this.headerMask );
		
		//set header button
		this.headerButton = document.createElement('span');
		this.headerButton.className='wmWHeaderExpanderToggler'; 
		this.headerButton.style.position='absolute'; 
/*		this.headerButton.style.right= '1px' ; 
		this.headerButton.style.top= '0px' ; 
		this.headerButton.style.width= '10px' ; 
		this.headerButton.style.height= 7+'px' ;*/ 
		this.headerButton.window= this ; 
		this.headerButton.onclick= this.toggleExpanded ; 
		this.headerObj.appendChild( this.headerButton );
	
	/**** set ButtonControl Bar */
		this.buttonBar = document.createElement('div');
		this.buttonBar.className='wmWMainButtonControl'; 
		this.buttonBar.style.position='absolute'; 
		this.buttonBar.window= this ; 
		this.headerObj.appendChild( this.buttonBar );
		
	/* set Maximize Button */
	/*
	if ( resizable )
	{
		this.maxButton = document.createElement('span');
		this.maxButton.className='wmWHeaderMaximizeButton'; 
		this.maxButton.style.position='absolute'; 
		this.maxButton.window= this ; 
		this.maxButton.onclick= this.maximizeWindow ; 
		this.headerObj.appendChild( this.maxButton );
	}
		
	if ( resizable )
	{
		this.fitButton = document.createElement('span');
		this.fitButton.className='wmWHeaderFitButton'; 
		this.fitButton.style.position='absolute'; 
		this.fitButton.window= this ; 
		this.fitButton.onclick= this.fitWindow ; 
		this.headerObj.appendChild( this.fitButton );
	}*/
		
	/* set Close Button */
		this.closeButton = document.createElement('span');
		this.closeButton.className='wmWHeaderCloseButton'; 
		this.closeButton.style.position='absolute'; 
		this.closeButton.window= this ; 
		this.closeButton.onclick= this.closeWindow ; 
		
		
		this.headerObj.appendChild( this.closeButton );
		this.domObj.appendChild( this.headerObj );

	
	//set body
	this.bodyObj = document.createElement('div');
	this.bodyObj.className='wmWBody'; 
	this.bodyObj.style.position='absolute'; 
	this.bodyObj.style.top=headerHeight+'px'; 
	this.bodyObj.style.width= w + 'px'; 
	this.bodyObj.style.height= (h - headerHeight) + 'px'; 
	this.domObj.appendChild( this.bodyObj );
	//set footer
	
	/*this.footerObj = document.createElement('div');
	this.footerObj.className='wmWFooter'; 
	this.footerObj.style.position='absolute'; 
	this.footerObj.style.top=(headerHeight+parseInt(this.bodyObj.style.height))+'px'; 
	this.footerObj.style.width= w + 'px'; 
	this.footerObj.style.height= footerHeight + 'px'; 
		//set header button
		this.footerButton = document.createElement('span');
		this.footerButton.className='wmWFooterResizer'; 
		this.footerButton.style.position='absolute'; 
		this.footerButton.window= this ; 
				
		this.footerButton.onmousedown= this.resize; 
		this.footerButton.onmouseup = this.resizeStop; 
		this.footerButton.onmousemove = this.resizeMove; 
		//this.footerButton.onmouseout = this.resizeStop; 
	
		if (this.footerButton.captureEvents) {
		   this.footerButton.captureEvents(Event.MOUSEDOWN);
		   this.footerButton.captureEvents(Event.MOUSEUP);
		   this.footerButton.captureEvents(Event.MOUSEMOVE);
		  // this.footerButton.captureEvents(Event.MOUSEOUT);
		}
		

	if(this.resizable)this.footerObj.appendChild( this.footerButton );
	this.domObj.appendChild( this.footerObj );	*/
};


kaWindow.prototype.duplicateContent = function( obj ){
	 this.bodyObj.appendChild(obj.cloneNode(true));
};

kaWindow.prototype.setContent = function( text ){
	if(this.pushedObj) this.pushedObj.innerHTML = text;
	else this.bodyObj.innerHTML = text;
};
kaWindow.prototype.setTitle = function( text ){
	this.domObj.firstChild.firstChild.innerHTML = text;
};
kaWindow.prototype.setZindex = function( z ){
	this.position=z;
	this.domObj.style.zIndex = z;
};

kaWindow.prototype.resize = function( e ){
	
	var thisWindow=this.window;
    e = (e)?e:((event)?event:null);   
     
     thisWindow.resizing=true;
     thisWindow.initX=e.clientX;
     thisWindow.initY=e.clientY;
	 
    document.onmousemove = thisWindow.resizeMove ;
    document.onmouseup   = thisWindow.resizeStop;
	//document.onmouseout     = thisWindow.resizeStop;
	if (document.captureEvents) {	   
		   document.captureEvents(Event.MOUSEUP);
		   document.captureEvents(Event.MOUSEMOVE);
		  //document.captureEvents(Event.MOUSEOUT);
	}
		
	document.win=thisWindow;
    e=null;
};

kaWindow.prototype.resizeMove  = function( e ){
    var thisWindow=(this.window)?this.window:this.win;
	e = (e)?e:((event)?event:null); 
	var xMov = 0;
	var yMov = 0;
    if(thisWindow.resizing) {
		if(thisWindow.initX!=e.clientX)  xMov=(thisWindow.initX-e.clientX);
		if(thisWindow.initY!=e.clientY)  yMov=(thisWindow.initY-e.clientY);

        var oX=thisWindow.initX;
        var oY=thisWindow.initY;

		var startW= thisWindow.w; 
		var startH=thisWindow.h;

		var thisBody=thisWindow.bodyObj;
		var thisHeader=thisWindow.headerObj;
		var thisFooter=thisWindow.footerObj;
		var startW=thisWindow.domObj.style.width;
		var startWindowH=thisWindow.domObj.style.height;
		var startBodyH=thisWindow.bodyObj.style.height;
		var startH=thisWindow.domObj.style.height;
                var startFooterTop=thisWindow.footerObj.style.top;	

        	
		var newW = (parseInt(startW)-xMov);
		var newH = (parseInt(startH)-yMov);
		var newWindowH = (parseInt(startWindowH)-yMov);
		var newBodyH = (parseInt(startBodyH)-yMov);
		var newFooterTop = (parseInt(startFooterTop)-yMov);

		if(newW>=thisWindow.minW){
			thisWindow.domObj.style.width=thisBody.style.width=thisHeader.style.width=thisFooter.style.width=newW+'px';
		}
		if(newH>=thisWindow.minH){
	        thisWindow.domObj.style.height=newWindowH+'px';
			thisBody.style.height=newBodyH+'px';
			thisFooter.style.top=newFooterTop+'px';
		}
		
		/*addDebugMsg( "#Resizing Position (included resizing):" );
  	addDebugMsg( "newW: " + newW );
  	addDebugMsg( "newH: " + newH );
  	addDebugMsg( "newWindowH: " + newWindowH );
  	addDebugMsg( "newBodyH: " + newBodyH );
  	addDebugMsg( "newFooterTop: " + newFooterTop );
		*/
		
		if(thisBody.firstChild){
			if(newW>=thisWindow.minW)thisBody.firstChild.style.width= newW-2 +'px'; 
			if(newH>=thisWindow.minH)thisBody.firstChild.style.height= newBodyH-3 +'px'; 
		}
	
		//reset init positions
		thisWindow.initX=e.clientX;
		thisWindow.initY=e.clientY;
    }
};


kaWindow.prototype.resizeStop  = function( e ){
	var thisWindow=(this.window)?this.window:this.win;
	var myID = thisWindow.domObj.id;
	
	if(thisWindow.resizing) {
		document.onmousemove = null ;
		document.onmouseup   = null;
 		document.onmouseuout   = null;
		document.win=null;
		e = (e)?e:((event)?event:null); 
        thisWindow.resizing=false;
    
		/* dirty: call extern resize function to tell script above that window size have changed */
		resizeEventHandler( myID );
		
    }
};


/* Funktion zum ein- und ausklappen */
kaWindow.prototype.toggleExpanded  = function(){
	if(this.window.expanded) {
		this.window.bodyObj.style.display='none';
		//this.window.footerObj.style.display='none';
		this.window.domObj.style.height='10px';
		this.window.headerButton.style.backgroundImage = "url('layout/images/btn_d.gif')";
		
	} else { 
		this.window.bodyObj.style.display='block';
		//this.window.footerObj.style.display='block';
		this.window.domObj.style.height=this.window.h+'px';
		this.window.headerButton.style.backgroundImage = "url('layout/images/btn_u.gif')";
	}
	this.window.expanded=(this.window.expanded)?false:true;

};

/* Funktion zum schließen des Fensters */
kaWindow.prototype.closeWindow  = function(){
	Finder_AddJob( this.window );
};

/* Fenster maximieren */
kaWindow.prototype.maximizeWindow  = function(){

	/* get the window objects */
	var thisWindow=(this.window)?this.window:this.win;
	var myID = thisWindow.domObj.id;
	var thisBody=thisWindow.bodyObj;
	var thisHeader=thisWindow.headerObj;
	var thisFooter=thisWindow.footerObj;
	var thisFooterButton=thisWindow.footerButton;
	
	var newHeight = 0;
	var newWidth = 0;
	var newLeft = 0;
	var newTop = 0;
	
	if ( ! thisWindow.isMaximized )
	{
	 	thisWindow.oldLeft = parseInt( thisWindow.domObj.style.left );
		thisWindow.oldTop = parseInt( thisWindow.domObj.style.top );
		thisWindow.oldWidth = parseInt( thisWindow.domObj.style.width );
		thisWindow.oldHeight = parseInt( thisWindow.domObj.style.height );
		
		thisWindow.isMaximized = true;
		
		/* get viewport infos */
		var pageHeaderSize = thisWindow.winManager.headerSize;
  	var viewportHeight = parseInt( thisWindow.winManager.desktop.style.height );
  	var viewportWidth = parseInt( thisWindow.winManager.desktop.style.width );
		
		var newWidth = viewportWidth;
		var newHeight = viewportHeight - pageHeaderSize;
		
		/* hide footer button */
		thisFooterButton.style.display="none";
		
	}
	else
	{
	 	var newHeight = thisWindow.oldHeight;
  	var newWidth = thisWindow.oldWidth;
  	var newLeft = thisWindow.oldLeft;
  	var newTop = thisWindow.oldTop;
		
		thisWindow.isMaximized = false;
		
		/* show footer button */
		thisFooterButton.style.display="block";
	}
	
	thisWindow.setSize ( thisWindow, newWidth, newHeight, newLeft, newTop );
	
	/* dirty: call extern resize function to tell the script above, that window size have changed */
	resizeEventHandler( myID );

};


/*
 * Um das Fenster die perfekte Größe zuzuweisen, wird zuerst die maximal mögliche Größe sowie
 * die Koordinaten des Quellfensters berechnet.
 *
 */
kaWindow.prototype.fitWindow = function(){
 	addDebugMsg("fitting window...");
	
	var leaveBorder = 5; // Rand welche zwischen den Objekten bestehend bleiben muss.
		
	var thisWindow= (this.window) ? this.window : this.win;
	
	var windowList = thisWindow.winManager.windows;
	var myID = thisWindow.domObj.id;

	/* viewport dimensions */
	var pageHeaderSize = thisWindow.winManager.headerSize;
	var viewportHeight = parseInt( thisWindow.winManager.desktop.style.height );
  var viewportWidth = parseInt( thisWindow.winManager.desktop.style.width );
	
	/* old positions */
	var thisLeft = parseInt( thisWindow.domObj.style.left );
	var thisTop = parseInt( thisWindow.domObj.style.top );
	var thisWidth = parseInt( thisWindow.domObj.style.width );
	var thisHeight = parseInt( thisWindow.domObj.style.height );
	var thisRight = thisLeft + thisWidth;
	var thisBottom = thisTop + thisHeight;
	
	addDebugMsg("### old Positions:" );
	addDebugMsg("# thisLeft=" + thisLeft );
	addDebugMsg("# thisTop=" + thisTop );
	addDebugMsg("# thisWidth=" + thisWidth );
	addDebugMsg("# thisHeight=" + thisHeight );
	
	/* center position for minimal align */
	/*var centerX = thisLeft + Math.round( thisWidth / 2);
	var centerY = thisTop + Math.round( thisHeight / 2);*/

	/* maximum values */
	var maxRight = viewportWidth;
	var maxBottom = viewportHeight - pageHeaderSize;
	
	/* starting values */
	var newRight = viewportWidth;
	var newLeft = 0;
	var newTop = 0;
	var newBottom = maxBottom;
	
	/* minimum values */
	/*var minLeft = centerX - ( thisWindow.minW / 2 );
	var minTop = centerY - ( thisWindow.minH / 2 );
	var minRight = centerX + ( thisWindow.minW / 2 );
	var minBottom = centerY + ( thisWindow.minH / 2 );*/
	
	/*
	 * the logical fit algorythm for every edge
	 */
	addDebugMsg( "### RUNNING COLLISION TEST ALGORYTHM" );
	for ( var i = 0 ; i < windowList.length ; i++ )
	{
	 var curWindow = windowList[ i ];
	 var winID = curWindow.domObj.id;
	 var winTitle = curWindow.title;
	 
	 
	 if ( winID != myID && curWindow.isVisible )
	 {
			
	 		var trgLeft = parseInt( curWindow.domObj.style.left );
			var trgTop = parseInt( curWindow.domObj.style.top );
			var trgWidth = parseInt( curWindow.domObj.style.width );
			var trgHeight = parseInt( curWindow.domObj.style.height );
			var trgRight = trgLeft + trgWidth;
			var trgBottom = trgTop + trgHeight;
			
			addDebugMsg( "## Collision Test for " + winTitle );
			addDebugMsg( "trgLeft: " + trgLeft );
			addDebugMsg( "trgTop: " + trgTop );
			addDebugMsg( "trgWidth: " + trgWidth );
			addDebugMsg( "trgHeight: " + trgHeight );
			addDebugMsg( "trgRight: " + trgRight );
			addDebugMsg( "trgBottom: " + trgBottom );

	 
	 		/* lets start with the top site */
			if ( trgBottom > newTop ) // cross target the current edge?
			{
			 if ( trgBottom < thisTop ) // is higher then started edge?
			 {
			 	if ( trgLeft <= newRight && trgLeft >= newLeft ||
					 	 trgRight <= newRight && trgRight >= newLeft  ) // is right in the same dimension
				{
				 addDebugMsg( "-> Obere Seite wird wegen Fenster " + winTitle + " eingeschränkt.");
				 newTop = trgBottom + leaveBorder;
				}
			 }
			}
			
			/* the right site */
			if ( trgLeft < newRight ) // cross target the current edge?
			{
			 if ( trgLeft > thisRight ) // is higher then started edge?
			 {
			 	if ( trgBottom >= newTop && trgBottom <= newBottom ||
					 	 trgTop <= newBottom && trgTop >= newTop ) // is right in the same dimension
				{
				 addDebugMsg( "-> Rechte Seite wird wegen Fenster " + winTitle + " eingeschränkt.");
				 newRight = trgLeft - leaveBorder;
				}
			 }
			}
			
			/* lets start with the top site */
			if ( trgTop < newBottom ) // cross target the current edge?
			{
			 if ( trgTop > thisBottom ) // is higher then started edge?
			 {
			 	if ( trgLeft <= newRight && trgLeft >= newLeft ||
					 	 trgRight <= newRight && trgRight >= newLeft  ) // is right in the same dimension
				{
				 addDebugMsg( "-> Untere Seite wird wegen Fenster " + winTitle + " eingeschränkt.");
				 newBottom = trgTop - leaveBorder;
				}
			 }
			}
			
			/* the left site */
			if ( trgRight > newLeft ) // cross target the current edge?
			{
			 //addDebugMsg( "left Collision test: 1" );
			 if ( trgRight < thisLeft ) // is higher then started edge?
			 {
			 	if ( trgBottom >= newTop && trgBottom <= newBottom ||
					 	 trgTop <= newBottom && trgTop >= newTop) // is right in the same dimension
				{
				 //addDebugMsg( "left Collision test: 3" );
				 //addDebugMsg( "-> Linke Seite wird wegen Fenster " + winTitle + " eingeschränkt.");
				 newLeft = trgRight + leaveBorder;
				}
			 }
			}
	 }
	}
	
	/* transfer computed values to the domObj */
	var newWidth = newRight - newLeft;
	var newHeight = newBottom - newTop;
	
/*	alert ( "computed positions: " + "\n" +
					" newWidth= " + newWidth + "\n" + 
					" newHeight= " + newHeight + "\n" + 
					" newLeft= " + newLeft + "\n" + 
					" newTop= " + newTop + "\n");*/
	thisWindow.setSize ( thisWindow, newWidth , newHeight, newLeft, newTop );
	
	/* dirty: call extern resize function to tell the script above, that window size have changed */
	resizeEventHandler( myID );
};

/*
 * sets the window size
 */
kaWindow.prototype.setSize = function( thisWindow, newWidth, newHeight, newLeft, newTop ){
	
	/* get the window objects */
	var thisBody=thisWindow.bodyObj;
	var thisHeader=thisWindow.headerObj;
	var thisFooter=thisWindow.footerObj;
	var thisFooterButton=thisWindow.footerButton;
	
	var footerH = parseInt(thisFooter.style.height);
	var headerH = parseInt(thisHeader.style.height);
	
	/* set window size and position */
	thisWindow.domObj.style.top = newTop + "px";
	thisWindow.domObj.style.left = newLeft + "px";

	var newBodyH = (newHeight - headerH - footerH );
	var newFooterTop = headerH + newBodyH;

	thisWindow.domObj.style.width=thisBody.style.width=thisHeader.style.width=thisFooter.style.width=newWidth+'px';
			
	thisWindow.domObj.style.height = newHeight + 'px';
	thisBody.style.height = newBodyH + 'px';
	thisFooter.style.top = newFooterTop + 'px';
	
	addDebugMsg( "#Resizing Position (extern function):" );
	addDebugMsg( "newWidth: " + newWidth );
	addDebugMsg( "newHeight: " + newHeight );
	addDebugMsg( "newLeft: " + newLeft );
	addDebugMsg( "newTop: " + newTop );
	addDebugMsg( "newBodyH: " + newBodyH );
	addDebugMsg( "newFooterTop: " + newFooterTop );
	
	if( thisBody.firstChild )
	{
		thisBody.firstChild.style.width = (newWidth - 2) + 'px'; 
		thisBody.firstChild.style.height = (newBodyH - 3) + 'px'; 
		
	}
};

kaWindow.prototype.setActive = function(){
	this.parent.winManager.setActive(this);
};

kaWindow.prototype.pushContent = function( obj ){
	var pushedObj = obj;
	var targetObj = this.bodyObj;
	this.pushedObj = obj;
	targetObj.appendChild(pushedObj);
	pushedObj.style.position = 'absolute';
	pushedObj.style.height = parseInt(targetObj.style.height)-3+'px';
	pushedObj.style.width = parseInt(targetObj.style.width)-2+'px';
	pushedObj.style.top = 0;
	pushedObj.style.left = 0;
	//pushedObj.className = targetObj.className;
};

kaWindow.prototype.appendContent = function( obj ){
	this.pushedObj.appendChild ( obj );
};


kaWindow.prototype.startDrag = function( e )
{
	var minX=0;
	var maxX=parseInt(getObjectWidth(this.window.winManager.desktop))-153;
	var minY=0;
	var maxY=parseInt(getObjectHeight(this.window.winManager.desktop))-23;	
	e = e ? e : window.event;
	moveMyWindow.startMoving ( e, this.window.winManager.desktop, this.window.domObj, 0,maxX,0,maxY );
	
};
kaWindow.prototype.stopDrag = function()
{
 var winID = curWindow.domObj.id;
 moveMyWindow.stopMoving();
 moveEventHandler( winID );
};


/* login on debug manager */
registerStartedComponent( 'jwindow' );

