﻿///<reference path="MicrosoftAjax.debug.js" > 
///<reference path="shared.js" > 
///<reference path="control.js" > 


Type.registerNamespace("SLx"); 


SLx.WrapPanel = function( visual , sizeToContent )
{    
    SLx.WrapPanel.initializeBase (this , [visual, sizeToContent ] );                
} 

SLx.WrapPanel.prototype = 
{ 
 
    measure : function SLx$WrapPanel$measure (  constrainwidth , constrainheight )        
    { 
        var width, height;  
        if ( this._sizeToContent ) 
        { 
            width = this.get_width () ; 
            height = Number.MAX_VALUE ; 
        }
        else 
        { 
            width = this.get_width () ; 
            height = this.get_height () ; 
        }  
        
        var currentwidth = 0 ; 
        var currentheight =  0 ; 
        var maxheight =  (this._sizeToContent)? 0 : height  ; 
        var maxwidth =  width  ; 
        var rowwidth = 0 ; 
        var rowheight = 0 ; 
        
        for ( var i = 0 ; i < this._children.get_length() ; i++ ) 
        {   
             var item = this._children.getItem ( i ) ;              
             var size = null ; 
             if ( SLx.VisualTreeHelper.isControl ( item ) && Object.getType(item).implementsInterface ( SLx.ILayout )) 
             { 
                var marginWidth = item.get_margin().Left + item.get_margin().Right ; 
                var marginHeight = item.get_margin().Top + item.get_margin().Bottom ; 
                
                
                if ( this._sizeToContent && item.get_sizeToParent () ) 
                {   
                    Sys.Debug.assert ( false ,  "You should not have children set to SizeToParent if Parent is SizeToContent; this used to be an assertion ");                   
                    item.set_sizeToParent (false ); 
                } 
    
                item.measure ( width - marginWidth, height - marginHeight ) ; 
                
                size = item.get_desiredSize(); 
             } 
             else 
             { 
                size = new SLx.Size ( item.width , item.Height );  
             } 
                
            // if it fits on current row.. 
            if ( rowwidth + size.Width < width ) 
            { 
                rowwidth += size.Width ; 
                rowheight = Math.max ( rowheight, size.Height + marginHeight ); 
            } 
            else 
            { 
                currentheight += rowheight ; 
                //start a new row..  ( everything is 0 + current item )                 
                rowwidth = size.Width ;                      
                rowheight = size.Height + marginHeight ;                     
            }                                                  
        }
        
    
        
        if ( this._sizeToContent ) 
        { 
            this._desiredSize.Width = this.get_width(); 
            this._desiredSize.Height = currentheight + rowheight ;              
         }   
         else 
         { 
            this._desiredSize.Height =  this.get_height(); 
            this._desiredSize.Width =  this.get_width () ; 
            
         } 
          
         Sys.Debug.trace ( "measure WrapPanel h:" + this._desiredSize.get_height() + ",w:" + this._desiredSize.get_width()) ; 
         
        
         this._isMeasureInvalidated = false ; 
    }, 
    
   
   
    
    
    arrange : function SLx$WrapPanel$arrange ( left, top, width , height ) 
    { 
        Sys.Debug.assert ( this._isMeasureInvalidated == false ); 
        
        
        this._visualElement["Canvas.Left"] = left; 
        this._visualElement["Canvas.Top"] = top; 
        
        this._visualElement.width = width ; 
        this._visualElement.height = height ; 
        
        /* 
        this._visualElement.width = this._desiredSize.get_width () ; 
        this._visualElement.height = this._desiredSize.get_height () ; 
        */ 
        var currentleft = 0 ; 
        var currenttop  = 0 ; 
        var currentRowHeight= 0 ; 
        for ( var i = 0 ; i < this._children.get_length () ; i++ ) 
        {   
             var item = this._children.getItem(i);              
             if ( SLx.VisualTreeHelper.isControl ( item ) && Object.getType(item).implementsInterface ( SLx.ILayout )) 
             { 
                var size = item.get_desiredSize () ;      
                var margin = item.get_margin();        
                var alignmentoffset = SLx.VisualTreeHelper.GetAlignment ( item , this , 
                    ( this._orientation == SLx.Orientation.Vertical) , (this._orientation == SLx.Orientation.Horizontal )); 
    
                marginWidth = margin.Left +  margin.Right
                marginHeight = margin.Top + margin.Bottom ; 
                
                if ( ( currentleft + size.Width + marginWidth ) > width ) 
                {                  
                    currenttop += currentRowHeight ; 
                    currentleft = 0 ;                        
                } 
                
                item.arrange ( currentleft + margin.Left ,  currenttop + margin.Top , size.Width , size.Height ) ;                         
                currentRowHeight  = Math.max ( currentRowHeight , size.Height + margin.Top + margin.Bottom ) ; 
                currentleft += (size.Width + margin.Left + margin.Right );                             
            }
            else 
            {   
                if ( ( currentleft + size.Width + marginWidth ) > width ) 
                {                  
                    currenttop += currentRowHeight ; 
                    currentleft = 0 ;                        
                }
                SLx.LayoutHelper.Arrange ( item, currentleft, currenttop ) ;  
                currentleft += item.Width ;                                
            }                                   
        } 
        if ( this.get_clipToBounds ()) 
        { 
                this._clip (); 
        }      
        this._isArrangeInvalidated = false ;        
    },
    
   
    toString : function  () 
    { 
        return this._uniqueSystemId.toString ()  + "    " + Object.getTypeName(this) + "    " + this._id ; 
    }

} 


SLx.WrapPanel.registerClass ( 'SLx.WrapPanel', SLx.Panel, SLx.ILayout ); 