﻿///<reference path="MicrosoftAjax.debug.js" > 
///<reference path="shared.js" > 
///<reference path="control.js" > 



SLx.Button = function ( uiElement, xamlTemplate , templateParts ) 
{ 
    this._clickListeners= [] ; 
    this._autosizeBackground  = true ; 
    this._background = null ; 
    SLx.Button.initializeBase (this , [uiElement, xamlTemplate  ,templateParts] );        
    
    this._addHandler ( this._visualElement ,  "MouseLeftButtonUp" , $delegate ( this, this._onMouseUp ));
     
}, 


SLx.Button.prototype = 
{ 
    
    add_Click : function SLx$Button$add_Click ( handler  ) 
    {         
        Array.add ( this._clickListeners, handler );          
    }, 
    
    remove_Click : function SLx$Button_removeClick ( handler ) 
    { 
        this._removeHandler ( this._clickListeners, handler );               
    },      
    
    _onMouseUp : function SLx$Button$_onMouseUp ( sender , args ) 
    {    
        if ( this.get_isEnabled ())  
            this._onClick ( sender, args );         
        
    },  
    
    _onClick: function ( sender, args ) 
    { 
        this._fireEvent ( this._clickListeners, new SLx.EventArgs ( sender ,args) );        
    }, 
   
    get_hasContent : function () 
    { 
        return this._content != null ; 
    },  
    
    
    set_content : function SLx$Button$set_content (  value ) 
    {
        if ( value != this._content )
        { 
            if ( this._content ==  null ) 
            { 
                this._content = new SLx.ContentPresenter ( value );             
                this._content.set_logicalParent ( this );              
                if ( this.get_sizeToContent ()) 
                { 
                   this._wireContentLayoutChangedCallback  (); 
                } 
            } 
            else 
            {
                this._content.set_content (value)   ; 
            }                    
            // TODO: should we invalidate or only for controls that sizeToCOntent??? 
            this.invalidateLayout (); 
        } 
    }, 
    
    get_content : function SLx$Button_get_content ()
    { 
        if ( this._content == null  ) 
        { 
            return this._visualElement.findName ("PART_Content"); 
        } 
        return this._content; 
    } ,  


    invalidateLayout : function () 
    { 
        if ( ! this._supressInvalidateLayout ) 
        { 
            this._isMeasureInvalidated = true ; 
            this._isArrangeInvalidated = true ;         
            SLx.Application.Current.get_layoutManager().invalidate ( this );         
            
            if ( this._content != null && !this.get_sizeToContent() ) 
            {   
                this._content.invalidateLayout ();                           
            } 
                       
          this._fireInvalidateLayout () ; 
        } 
    }, 
    
    _wireContentLayoutChangedCallback :  function ( ) 
    { 
       if ( this._contentLayoutInvalidatedCallback == null ) 
        this._contentLayoutInvalidatedCallback = $delegate (this, this._onContentSizeChanged ); 
        this._content.add_layoutInvalidated ( this._contentLayoutInvalidatedCallback ) ; 
    } , 
    
    
    set_sizeToContent  : function ( value ) 
    { 
        if ( this._sizeToContent != value ) 
        {  
            this._sizeToContent = value ; 
           
           // NOTE: We must unwire teh calls here first... before we invalidateLayout, else we can 
           // run into infinite loop... 
           
            if ( value ) 
            { 
                this._wireContentLayoutChangedCallback (); 
            } 
            else 
            { 
                if ( this._contentLayoutInvalidatedCallback ) 
                    this._content.remove_layoutInvalidated ( this._contentLayoutInvalidatedCallback); 
            }
            
             this.invalidateLayout(); 
             
        } 
    }, 

    _onContentSizeChanged : function  ( sender , args ) 
    { 
        this.invalidateLayout ( ) ; 
    },  


    measure : function ( width , height ) 
    {    
        if ( ! this._isMeasureInvalidated && SLx.Application.Current.get_cacheLayout() ) 
        { 
            Sys.Debug.trace ( this.toString()  + "cached measure" ) ;
            return ;  
        } 
            
        if ( this._sizeToParent ) 
        {             
            this._calculateDesiredSizeonSizeToParent(  width , height ) ;                                      
        } 
        else if ( this._sizeToContent && this.get_hasContent() ) 
        {  
             var childSize = this._content.get_desiredSize (); 
             this._desiredSize.Width = childSize.Width ; 
             this._desiredSize.Height = childSize.Height ;               
        } 
        else 
        {         
            this._desiredSize.Width  = this.get_width () ; 
            this._desiredSize.Height  = this.get_height () ;             
        }        
        
        // We measure content becasue we will try to arrange it .. 
        // It is RISKY to measure with new  desiredSize... ..
        // 
        if ( this._content != null && !this._sizeToContent ) 
        { 
            this._content.measure ( this._desiredSize.Width, this._desiredSize.Height );  
        } 
        this._isMeasureInvalidated = false ;                
    }, 
    
            
    arrange : function (left, top, width, height ) 
    {            
        var moved =   (  this._visualElement["Canvas.Left"]   != left || this._visualElement["Canvas.Top"] != top ) ; 
        var sized =  ( this._visualElement["width"] != width  || this._visualElement["height"] != height  ); 
        
        
         Sys.Debug.assert ( sized == false || SLx.Warning.ArrangeResizes , "Does this imply a relationship is missing?" ); 
         if ( this._isArrangeInvalidated  || sized || moved )            
         {            
            SLx.Button.callBaseMethod ( this , "arrange" , [left, top, width, height]  ) ;
           
            this._sizeBackground ( width, height ); 
                     
            SLx.VisualTreeHelper.arrangeContent  ( this._content , this ) ;            
            this._isArrangeInvalidated = false ;                 
        }
        else 
        { 
            Sys.Debug.trace ( this.toString() + " cached arrange" ); 
        }           
    },
    
    _sizeBackground : function ( width, height ) 
    {         
        if ( this.get_autosizeBackground () ) 
        { 
            if ( this._background == null ) 
                this._background = this._visualElement.findName ( "PART_Background"); 
            if ( this._background != null ) 
            { 
                this._background.width = width ; this._background.height = height ; 
            }                 
        } 
    }, 
    
    get_autosizeBackground : function  () { return this._autosizeBackground ;  } , 
    set_autosizeBackground : function  ( value )
    { 
        //TODO: I did not fire invalidate layout nor did I set background..  I assume layout is dirty 
        this._autosizeBackground = value ; 
    } , 
    
    dispose: function () 
    { 
        if  ( this._content != null ) 
            this._content.dispose(); 
            
        SLx.Button.callBaseMethod( this, "dispose", [] );         
    }, 
    
    toString : function  () 
    { 
        return this._uniqueSystemId.toString ()  + "    " + Object.getTypeName(this) + "    " + this._id ; 
    }        
}  

SLx.Button.DefaultTemplate =  [ 
{ name : "Button" , ref : "_visualElement", type : "element" }, 
{ name : "PART_Content" , ref: "_content" , type : "ContentPresenter" } 
]; 


SLx.Button.registerClass('SLx.Button', SLx.Control ); 
