﻿///<reference path="MicrosoftAjax.debug.js" > 
///<reference path="shared.js" > 
///<reference path="control.js" > 


SLx.ControlFactory.Slider = function () 
{ 
   
    
    var template = SLx.Application.Current.get_resources().findName ( SLx.Constants.Slider); 
    if ( template  != "" ) 
    {     
        var Slider= new SLx.Slider ( null,  template , SLx.Slider.DefaultTemplate );          
        return Slider ; 
        
    } 
    Sys.Debug.assert( false , "this should not fail");    
        
    return null ; 
} 


SLx.Slider = function ( visual , xamlTemplate , templateParts )
{
    this._track = null ; 
    this._thumb = null ; 
        
    SLx.Slider.initializeBase (this , [ visual , xamlTemplate , templateParts ] );   

    this._valueChangedCallbacks = [] ; 
    this._valueUpdatedCallbacks = [] ;  
     
    this._low = 0 ;
    this._high = 100; 
    
    Sys.Debug.assert ( this._thumb != null ) ; 
    Sys.Debug.assert ( this._visualElement != null ) ; 
    
    
    this._thumb.addEventListener ( "MouseMove", $delegate ( this, this._handleThumbMouseMove )); 
    this._thumb.addEventListener ( "MouseLeftButtonDown", $delegate ( this,this._handleThumbMouseDown )); 
    this._thumb.addEventListener ( "MouseLeftButtonUp", $delegate ( this,this._handleThumbMouseUp)); 
    this._thumb.addEventListener ( "MouseLeave", $delegate ( this,this._handleMouseLeave)); 
    this._thumb.getHost().content.root.addEventListener ( "MouseLeave"  , $delegate (  this, this._handleMouseLeavePlugin )); 
    this._track.addEventListener ( "MouseLeftButtonUp",  $delegate ( this , this._handleThumbTrackMouseClick )); 
    

    this._isMouseDown = false;         
    this._isHorizontal = (this._visualElement.width >= this._visualElement.height);
    this._isEnabled = true; 
    this._useThumbPosition = true ; 
    this._inScopeCapture = false ; 
    
    
    
    
}, 

SLx.Slider.DefaultTemplate = [ 
{ name: "Slider" , ref: "_visualElement", type: "element"} ,  
{ name: "PART_Thumb" , ref: "_thumb", type: "element"} , 
{ name: "PART_Track" , ref: "_track", type: "element"} 
]; 

SLx.Slider.prototype = 
{   

    _handleThumbTrackMouseClick : function ( sender, args ) 
    { 
        if ( this.get_isEnabled ()) 
        { 
            var position = args.getPosition ( this._track ); 
            
            if ( this.get_isHorizontal () ) 
                this._applyPercent ( ( position.X )/ this._track.Width , SLx.Slider.NotifyValueChanged) ;                 
            else 
                this._applyPercent ( ( position.Y)/ this._track.Height , SLx.Slider.NotifyValueChanged ) ;
        } 
    }, 
    
    _handleThumbMouseMove : function ( sender, args ) 
    { 
        // assumes it is enabled.. 
        if ( this._isMouseDown ) 
        { 
            var position = args.getPosition( this._track); 
            var thumbposition = args.getPosition  ( this._thumb ); 
            if ( this.get_inScopeCapture () && !this.get_isInScope ( position ))                    
            { 
                this._changeMouseState ( false ); 
            } 
            else  
            {  
                if ( this.get_isHorizontal ()) 
                    this._applyPercent( ( position.X - this._mouseOffset )/ this._track.Width , SLx.Slider.NotifyValueChanged) ;                                 
                else 
                    this._applyPercent( ( position.Y - this._mouseOffset )/ this._track.Height, SLx.Slider.NotifyValueChanged) ;                                 
            } 
                        
        }     
    }, 
    
     _changeMouseState : function ( newval , args ) 
    { 
        
            this._isMouseDown = newval ; 
            if ( newval ) 
            { 
                this._thumb.CaptureMouse();
                this._mouseOffset = 0; 
                
                if ( args != null ) 
                { 
                    this._mouseOffset = args.getPosition ( this._thumb ).X; 
                } 
            } 
            else
            { 
                this._thumb.ReleaseMouseCapture(); 
            }         
    }, 
    
    get_inScopeCapture : function ( val ) 
    {   
        return this._inScopeCapture ; 
    }, 
    
    set_inScopeCapture : function (val ) 
    { 
        this._inScopeCapture = val; 
    }, 
    
    get_isInScope : function ( position ) 
    {
         
            return (  position.X < this._track.Width  ||  
                  position.X >= 0 || 
                  position.Y < this._track.Height || 
                  position.Y >= 0 ); 
                  
          
           
    }, 
    
    
    _handleThumbMouseDown : function ( sender, args ) 
    {
        if ( this.get_isEnabled ())  
            this._changeMouseState ( true , args );         
    }, 
    
    _handleThumbMouseUp : function ( sender, args ) 
    { 
        this._changeMouseState ( false , args );          
    }, 
    
      
    _handleMouseLeave : function ( sender, args ) 
    { 
        if ( this.get_inScopeCapture ())  
        this._changeMouseState (false, null ); 
    }, 
    
    _handleMouseLeavePlugin : function ( sender, args ) 
    { 
        this._changeMouseState (false, null ); 
    }, 
    
    set_isHorizontal : function ( val ) 
    { 
        this._isHorizontal = val; 
    }, 
    
    get_isHorizontal : function ( val ) 
    { 
        return this._isHorizontal; 
    }, 
    
    get_value : function ( ) 
    { 
        
        if (this.get_useThumbPosition ()) 
        { 
            if ( this.get_isHorizontal() ) 
            { 
                var fraction = ( this._thumb["Canvas.Left"] - this._track["Canvas.Left"] ) / ( this._track.Width - this._thumb.Width ) ; 
            }
            else 
            { 
             var fraction = ( this._thumb["Canvas.Top"] - this._track["Canvas.Top"] ) / ( this._track.Height - this._thumb.Height ) ; 
            }
        }
        else 
        { 
          if ( this.get_isHorizontal() ) 
            { 
                var fraction = ( this._thumb.Width ) / ( this._track.Width ) ; 
            }
            else 
            { 
             var fraction = ( this._thumb.Height ) / ( this._track.Height ) ; 
            }
        }   
        if ( isNaN ( fraction )) 
            fraction = 0; 
            
        var val = ( fraction * ( this._high - this._low ))+ this._low ; 
        return val; 
        
    }, 
    
    set_percent : function ( pct ) 
    { 
        this._applyPercent ( pct, SLx.Slider.NotifyValueUpdated ) ;         
    }, 
    
    get_percent : function () 
    { 
        return ( this.get_value () / ( this._high - this._low )); 
    } , 
    
    
    _applyPercent : function ( pct , eventtofire  ) 
    { 
        var val = pct * (this._high - this._low ); 
        this._applyValue ( val, eventtofire  );  
    },  
    
    _applyValue  : function ( val , eventtofire  ) 
    { 
        var fraction = val / ( this._high - this._low ); 
        if ( isNaN ( fraction )) 
            fraction = 0.0; 
            
        var offset = 0; 
        
        if ( this.get_useThumbPosition ()) 
        { 
            if ( this.get_isHorizontal ()) 
            { 
                offset = this._track["Canvas.Left"] ; 
                if ( fraction <= 0 ) 
                    this._thumb["Canvas.Left"] = offset; 
                else if ( fraction >= 1 ) 
                    this._thumb["Canvas.Left"] =  offset + this._track.Width - this._thumb.Width ; 
                else 
                    this._thumb["Canvas.Left"]  = offset + Math.min ( (this._track.Width - this._thumb.Width) * fraction, this._track.Width );                
            }
            else 
            { 
                offset = this._track["Canvas.Top"] ; 
                if ( fraction <= 0 ) 
                    this._thumb["Canvas.Top"] = offset; 
                else if ( fraction >= 1 ) 
                    this._thumb["Canvas.Top"] =  offset + this._track.Height- this._thumb.Height ; 
                else 
                    this._thumb["Canvas.Top"]  = offset + Math.min ( (this._track.Height - this._thumb.Height) * fraction, this._track.Height);                
            }                      
        } 
        else 
        { 
            if ( this.get_isHorizontal () ) 
            { 
               if ( fraction <= 0 ) 
                    this._thumb.Width = 0 ; 
                else if ( fraction >= 1 ) 
                    this._thumb.Width =  this._track.Width ; 
                else 
                    this._thumb.Width = this._track.Width * fraction ;                
           }
           else 
           { 
            
               if ( fraction <= 0 ) 
                    this._thumb.Height= 0 ; 
                else if ( fraction >= 1 ) 
                    this._thumb.Height =  this._track.Height ; 
                else 
                    this._thumb.Height = this._track.Height * fraction ;                
           }
       }
       
        this._notify ( eventtofire );              
    }, 
    
    _notify  : function ( eventtofire ) 
    { 
        var eventlist = null ; 
       if ( eventtofire == SLx.Slider.NotifyValueChanged )
       { 
            eventlist = this._valueChangedCallbacks; 
       } 
       else if ( eventtofire == SLx.Slider.NotifyValueUpdated )
       { 
            eventlist = this._valueUpdatedCallbacks ; 
       } 
       else 
            Sys.Debug.assert ( "invalid event" ); 
            
       for ( var i = 0 ; i < eventlist.length ; i++ ) 
       { 
            eventlist[i]( this, null); 
       } 
    }, 
    
    
    set_value  : function ( val ) 
    {    
        this._applyValue  ( val , SLx.Slider.NotifyValueUpdated );          
    }, 
    
    
    //TODO: remove this method..     
    set_valueChanged  : function ( func ) 
    { 
        this.add_valueChanged ( func );           
    },  
    
    
    add_valueChanged : function ( func ) 
    { 
        Sys.Debug.assert ( !Array.contains ( this._valueChangedCallbacks , func ), "Same event twice, is this desired" ) ; 
        Array.add ( this._valueChangedCallbacks , func ) ;         
    }, 
    
    remove_valueChanged : function ( func ) 
    { 
        Array.remove ( this._valueChangedCallbacks , func ) ;         
    }, 
     
     add_valueUpdated : function ( func ) 
    { 
        Sys.Debug.assert ( !Array.contains ( this._valueUpdatedCallbacks , func ), "Same event twice, is this desired" ) ; 
        Array.add ( this._valueUpdatedCallbacks , func ) ;         
    }, 
    
    remove_valueUpdated : function ( func ) 
    { 
        Array.remove ( this._valueUpdatedCallbacks , func ) ;         
    }, 
    
    // TODO : Remove this method.. it is here for backwards compat 
    set_valueUpdated : function ( func ) 
    { 
        this.add_valueUpdated ( func ) ; 
    },  
   
      
    set_range : function ( low, high ) 
    { 
        this._low = low; 
        this._high = high; 
    },


    set_Cursor : function (val ) 
    { 
        this._thumb.Cursor = val; 
    } ,
    
    get_Cursor : function () 
    { 
        return this._thumb.Cursor; 
    }  ,   
    
    set_isEnabled : function ( val ) 
    { 
        this._isEnabled = val ; 
    } ,
    
    get_isEnabled : function ( ) 
    { 
        return this._isEnabled ;  
    }, 
    
    get_useThumbPosition : function () 
    { 
        return this._useThumbPosition ; 
    },     
    
    set_useThumbPosition : function (val ) 
    { 
        this._useThumbPosition = val ; 
        
        if ( val == false ) 
        { 
             
        } 
    }  , 
    
    arrange : function SLx$Slider$arrange ( left, top, width, height ) 
    { 
       SLx.Slider.callBaseMethod ( this , "arrange" , [left, top, width, height]  ) ;
       
       if ( this._isHorizontal ) 
       { 
            this._track.width = this._visualElement.width ; 
       }
       else 
             this._track.height = this._visualElement.height ;         
    } ,
    
    measure : function SLx$Slider$measure ( width, height ) 
    { 
         SLx.Slider.callBaseMethod ( this , "measure" , [width, height]  ) ;
    }, 
    
     toString : function  () 
    { 
        return this._uniqueSystemId.toString ()  + "    " + Object.getTypeName(this) + "    " + this._id ; 
    }
    
    
}  


SLx.Slider.registerClass('SLx.Slider', SLx.Control ); 




SLx.Slider.NotifyNothing = 0; 
SLx.Slider.NotifyValueChanged = 1 ; 
SLx.Slider.NotifyValueUpdated = 2 ; 



/* 
0000000000000000000000000

  




SLx.Slider.prototype = 
{ 
   
    
   
     
     
    
  
    
    
    
   
      
}

*/ 
