﻿///<reference path="MicrosoftAjax.debug.js" > 
///<reference path="shared.js" > 
///<reference path="control.js" > 


Type.registerNamespace("SLx"); 


SLx.StackPanel = function( visual , sizeToContent )
{
    this._orientation = SLx.Orientation.Vertical ; 
    SLx.StackPanel.initializeBase (this , [visual, sizeToContent ] ); 
                
} 

SLx.StackPanel.prototype = 
{ 
 
    measure : function SLx$StackPanel$measure (  constrainwidth , constrainheight )        
    { 
        var width, height;  
        if ( this._sizeToContent ) 
        { 
            width = Number.MAX_VALUE ; 
            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 =  (this._sizeToContent)? 0: width  ; 
        
        
        
        
        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 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 ); 
                } 
                
                var desiredSize = item.get_desiredSize () ;                                 
                if ( this._orientation == SLx.Orientation.Horizontal ) 
                { 
                    item.measure ( desiredSize.get_width()  , height - marginHeight ) ; 
                } 
                else 
                { 
                    Sys.Debug.assert ( this._orientation == SLx.Orientation.Vertical ); 
                    item.measure ( width , desiredSize.get_height()); 
                } 
                
                
                if ( this._sizeToContent ) 
                { 
                     
                    var size = item.get_desiredSize () ; 
                    if ( this._orientation == SLx.Orientation.Horizontal ) 
                    { 
                        maxheight = Math.max ( maxheight, size.get_height () + item.get_margin().Top);                          
                    //    maxwidth = Math.max ( maxwidth , size.get_width ()); 
                        currentwidth += size.get_width (); 
                    } 
                    else if ( this._orientation == SLx.Orientation.Vertical ) 
                    {                     
                        maxwidth = Math.max ( maxwidth , size.get_width () + item.get_margin().Left); 
                      //  maxheight = Math.max ( maxheight, size.get_height ());                          
                        currentheight += size.get_height(); 
                    }   
                } 
             }
             else if ( this._sizeToContent ) 
             { 
                maxheight = Math.max ( maxheight , item.height ); 
                maxwidth  = Math.max ( maxwidth , item.width ); 
                currentwidth += item.width ; 
                currentheight += item.height ;                            
             }                                    
        }
        
    
        
        if ( this._sizeToContent ) 
        { 
            if ( this._orientation == SLx.Orientation.Horizontal ) 
            { 
                this._desiredSize.Width = currentwidth   ; 
                this._desiredSize.Height =  maxheight  ; 
            }
            else 
            {
                Sys.Debug.assert(this._orientation == SLx.Orientation.Vertical ); 
                this._desiredSize.Height  = currentheight  ; 
                this._desiredSize.Width = maxwidth  ; 
            } 
         }   
         else 
         { 
            this._desiredSize.Height =  this.get_height(); 
            this._desiredSize.Width =  this.get_width () ; 
            
         } 
         this._isMeasureInvalidated = false; 
         Sys.Debug.trace ( "measure StackStackPanel h:" + this._desiredSize.get_height() + ",w:" + this._desiredSize.get_width()) ; 
         
         if ( this.get_clipToBounds () ) 
         { 
            this.set_clipToBounds (true); 
         } 
         
         this._isMeasureInvalidated = false ; 
    }, 
    
   
   
    
    
    arrange : function SLx$StackPanel$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 ; 
        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 )); 
    
                // TODO is there a better way to do margins?? Right now they add up..
                item.arrange ( alignmentoffset.left + currentleft + margin.Left - margin.Right , 
                               alignmentoffset.top  + currenttop + margin.Top - margin.Bottom,  size.get_width(), size.get_height ()) ;                 
                if ( this._orientation == SLx.Orientation.Horizontal ) 
                { 
                    currentleft += size.get_width() + margin.Left;                     
                } 
                else if ( this._orientation == SLx.Orientation.Vertical ) 
                { 
                     currenttop += size.get_height() + margin.Top ; 
                }   
            }
            else 
            {   
                SLx.LayoutHelper.Arrange ( item, currentleft, currenttop ) ; 
                
                if ( this._orientation == SLx.Orientation.Horizontal ) 
                { 
                    currentleft += item.width ;                      
                } 
                else if ( this._orientation == SLx.Orientation.Vertical ) 
                { 
                     currenttop += item.height ; 
                }
            }
                   
            if ( this.get_clipToBounds ()) 
            { 
                this._clip (); 
            }              
        } 
        this._isArrangeInvalidated = false ;        
    },
    

    set_orientation: function(value) { 
         
        if ( value != this._orientation ) 
        {         
            this._orientation = value ; 
            this.invalidateLayout();                                                                                                                   
        }         
    },
    get_orientation: function() { return this._orientation },
    
    

   
    toString : function  () 
    { 
        return this._uniqueSystemId.toString ()  + "    " + Object.getTypeName(this) + "    " + this._id ; 
    }

} 


SLx.StackPanel.registerClass ( 'SLx.StackPanel', SLx.Panel, SLx.ILayout ); 