Zapatec.Window.minWinWidth 	= 160;   // copied/edited this here, because it does not seem to work when edited elsewhere..  Jeff-

/*	Function:		sortMin: Rearranges the minimised window positions.
 *	Created by:		Rod McTainsh
 *	NOTE:			Overriden to fix Jira bug WEB-215. The minimized windows are sorted differently now that
 *					multiple rows of minimized windows are handled.
 */
Zapatec.Window.sortMin = function( raised )
{
	var raisedPos 				= raised.getScreenPosition(), win = null, winPos = null, newLeft = null, newTop = null;
	var docWidth 				= YAHOO.util.Dom.getDocumentWidth();
	var windowMinWidth 			= Zapatec.Window.minWinWidth + 5;
	var numMinimisedWindows		= 0;
	var minimisedWindowHeight	= 25;
	
	for( var i = 0; i < Zapatec.Widget.all.length; ++i ) {
		if( Zapatec.Widget.all[i].widgetType == "window" ) {
			win = Zapatec.Widget.all[i];
			
			if( win.state.state == "min" ) {
				winPos = win.getScreenPosition();
				numMinimisedWindows++;
				
				if ( winPos.y < raisedPos.y ) {
					// win is above raised
					if ( winPos.x < Zapatec.Window.minWinWidth ) {
						// Move to previous row
						newTop = winPos.y + minimisedWindowHeight;
						newLeft = 0;
						
						while ((newLeft + windowMinWidth) < docWidth ) {
							newLeft += windowMinWidth;
						}
						
						newLeft -= windowMinWidth;
					} else {
						// Move left
						newLeft = winPos.x - windowMinWidth;
						newTop = winPos.y;
					}
				} else if ( winPos.y == raisedPos.y && winPos.x > raisedPos.x ) {
					// win is on the same "row" as raised move it left
					newLeft = winPos.x - windowMinWidth;
					newTop = winPos.y;
				} else {
					// win is below raised, don't move it
					continue;
				}
				
				// Update the windows position
				win.setScreenPosition( newLeft - ( Zapatec.Utils.bodyOffset ? Zapatec.Utils.bodyOffset.left : 0 ), newTop );
			}
		}
	}
	
	// Check if the minimize left and top variables need changing.
	if ( Zapatec.Window.minimizeLeft == 0 && numMinimisedWindows ) {
		Zapatec.Window.minimizeTop += minimisedWindowHeight;
		
		var left = 0;
		
		while ((left + windowMinWidth) < docWidth ) {
			left += windowMinWidth;
		}
		
		Zapatec.Window.minimizeLeft = left - windowMinWidth;
	} else {
		Zapatec.Window.minimizeLeft -= windowMinWidth;
	}
	
	win = null;
	winPos = null;
	raisedPos = null;
};

/*	Function:		minimize: Minimizes a window.
 *	Created by:		Rod McTainsh
 *	NOTE:			Overriden to fix Jira bug WEB-215. Now when a window is minimized, if it is going to appear off
 *					the right side of the window, it is pushed up onto a new row (above previously minimized windows).
 */
Zapatec.Window.prototype.minimize = function() 
{
	if(	!this.config.showMinButton ) { 
		return false;
	}
	
	if(	!this.fireOnState( "ready", function() { this.minimize(); } )) {
		return;
	}
	
	if( this.state.state != "min" && this.titleArea ) { 
		var self = this; 
		
		function hideExcept( el, exc ) { 
			if ( el != self.container ) { 
				var first = el.firstChild;
				
				while ( first ) { 
					if ( first && first.style && first != exc ) {
						if( !first.restorer ) {
							first.restorer = new Zapatec.SRProp( first ); 
						}
						
						first.restorer.saveProp( "style.display" );
						first.style.display = "none";
					}
					
					first = first.nextSibling;
				}
				
				hideExcept( el.parentNode, el );
			}
		}
		
		if ( this.state.state != "simple" ) {
			this.restore();
		}
		
		hideExcept( this.titleArea.parentNode, this.titleArea );
		this.content.hide();
		this.restorer.saveProps( "_updateState", "content", "statusText", "getConfiguration().limit.minHeight" );
		this.content = this.statusText = this.getConfiguration().limit.minHeight = null;
		this._updateState = function(){};
		this.setHeight( Zapatec.Utils.getHeight( this.face ));
		
		if( this.config.bottomMinimize ) {
			// Get document dimensions
			var documentWidth = YAHOO.util.Dom.getDocumentWidth();
			var documentHeight = YAHOO.util.Dom.getDocumentHeight();
			var winHeight = Zapatec.Utils.getHeight( this.face );
			
			this.setWidth( Zapatec.Window.minWinWidth );
			
			// Move to the next available spot at the current minimizeTop position
			if ( !Zapatec.Window.minimizeTop ) {
				Zapatec.Window.minimizeTop = documentHeight - winHeight;
			}
			
			this.setScreenPosition( Zapatec.Window.minimizeLeft, Zapatec.Window.minimizeTop );
			
			// Increment the next available left pos
			Zapatec.Window.minimizeLeft += Zapatec.Window.minWinWidth + 5;
			
			// Check if the window needs to go above the current row of minimized windows
			if ((Zapatec.Window.minimizeLeft + Zapatec.Window.minWinWidth + 5) > documentWidth ) {
				Zapatec.Window.minimizeTop -= winHeight;
				Zapatec.Window.minimizeLeft = 0;				
			}
		}
		
		if( !this.config.dragMin ) {
			this._setCanDrag( false );
		} else { 
			this._setCanDrag( true );
			this.restorer.restoreProp("_updateState");
		}
		
		this._setCanResize(false);
		
		if( this.state.state == "max" ){
			this.showButton( "maxButton" );
		} else {
			this.showButton( "restoreButton" );
		}
		
		this.hideButton( "minButton" );
		this.state.prevState = this.state.state;
		this.state.state = "min";
		
		if( !this.getConfiguration().fixed ){
			Zapatec.FixateOnScreen.register( this.getContainer());
			Zapatec.FixateOnScreen.register( this.WCH );
		}
		this.deactivate();
	} else {
		return false;
	}
	
	this.fireEvent( "onMinimize", this );
	return true;
}

function windowResizeEvent()
{
	if ( !Zapatec || !Zapatec.Widget ) {
		return;
	}
	
	// Reposition each minimised window & Check for missing windows
	var minWinHeight 	= 25;
	var newClientHeight = YAHOO.util.Dom.getClientHeight();
	var newClientWidth 	= YAHOO.util.Dom.getClientWidth();
	
	Zapatec.Window.minimizeTop 	= newClientHeight - minWinHeight;
	Zapatec.Window.minimizeLeft	= 0;
	
	var win = null;
	var position = null;
	var recoveredWindowX = 100;
	var recoveredWindowY = 100;
	
	for( var i = 0; i < Zapatec.Widget.all.length; ++i ) {
		if( Zapatec.Widget.all[i].widgetType == "window" ) {
			win = Zapatec.Widget.all[i];
			
			if( win.state.state == "min" ) {
				win.setScreenPosition( Zapatec.Window.minimizeLeft, Zapatec.Window.minimizeTop );
				
				// Increment the next available left pos
				Zapatec.Window.minimizeLeft += Zapatec.Window.minWinWidth + 5;
				
				// Check if the window needs to go above the current row of minimized windows
				if ((Zapatec.Window.minimizeLeft + Zapatec.Window.minWinWidth + 5) > newClientWidth ) {
					Zapatec.Window.minimizeTop -= minWinHeight;
					Zapatec.Window.minimizeLeft = 0;				
				}
			} else {
				position = win.getScreenPosition();
				
				if ( position.x > newClientWidth || position.y > newClientHeight ) {
					win.setScreenPosition( recoveredWindowX, recoveredWindowY );
					recoveredWindowX += minWinHeight;
					recoveredWindowY += minWinHeight;
				}
			}
		}
	}
}

window.onresize = windowResizeEvent;
