﻿/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace('WindowsSite.Ajax.Controls');

WindowsSite.Ajax.Controls.ScrollbarBehavior = function( element ) {
    WindowsSite.Ajax.Controls.ScrollbarBehavior.initializeBase( this, [element] );
    this.listChangedDelegate = Function.createDelegate( this, this.listPropertyChanged );
}

WindowsSite.Ajax.Controls.ScrollbarBehavior.prototype = {


    initialize : function() {
        WindowsSite.Ajax.Controls.ScrollbarBehavior.callBaseMethod(this, 'initialize');
        
        $addHandler( document, "mousemove", 
			Function.createDelegate( this, this.onThumbMove ) );
		$addHandler( document, "mouseup",
			Function.createDelegate( this, this.onThumbUp ) );
		$addHandler( this.get_Thumb(), "mousedown",
			Function.createDelegate( this, this.onThumbDown ) );
		$addHandler( this.get_element(), "mousedown",
			Function.createDelegate( this, this.onGutterDown ) );
			
		this.get_CommandControlContainer().add_propertyChanged( this.listChangedDelegate );
    },

    dispose : function() {
        $clearHandlers(this.get_element());
        WindowsSite.Ajax.Controls.ScrollbarBehavior.callBaseMethod(this, 'dispose');
    },
    
    //////////////////////////////////////////////////////
    // Properties
    //////////////////////////////////////////////////////
    
    get_CommandControlContainer : function() {
        return this._buttonContainer;
    },
    set_CommandControlContainer : function(value) {
        if (this._buttonContainer !== value) {
            this._buttonContainer = value;
            this.raisePropertyChanged('CommandControlContainer');
        }
    },
    
    get_Thumb : function() {
        return this._thumb;
    },
    set_Thumb : function(value) {
        if (this._thumb !== value) {
            this._thumb = value;
            this.raisePropertyChanged('Thumb');
        }
    },
    
    //////////////////////////////////////////////////////
    // Functions
    //////////////////////////////////////////////////////
    
    listPropertyChanged : function ( sender, changedProperty )
    {
		switch( changedProperty.get_propertyName() )
		{
			case ( "HorizontalPosition" ):
				if ( !this._isScrolling ) 
				{
					var gutterBounds = Sys.UI.DomElement.getBounds( this.get_element() );
					var thumbBounds = Sys.UI.DomElement.getBounds( this.get_Thumb() );
					this.get_Thumb().style.left = Math.floor(( gutterBounds.width - thumbBounds.width ) * ( this.get_CommandControlContainer().get_HorizontalPosition() / this.get_CommandControlContainer().get_ScrollWidth() ) * -1) + "px";
				}
				break;
		}
    },
    
    onThumbDown : function ()
	{
	    this._isScrolling = true;
	},
	
	onThumbUp : function ()
	{
        this._isScrolling = false;
	},
	
	onThumbMove : function ( e )
	{
	    if ( this._isScrolling )
	    {
			this.scroll( e );
	    }
	},
    
    onGutterDown : function(e)
	{
		if ( !this._isScrolling )
	    {
			this.scroll(e);
	    }
	},
	
	scroll : function(e)
	{
	    var domEvt = new Sys.UI.DomEvent(e);
		domEvt.preventDefault();
		domEvt.stopPropagation();
		
		var gutterBounds = Sys.UI.DomElement.getBounds( this.get_element() );
		var thumbBounds = Sys.UI.DomElement.getBounds( this.get_Thumb() );
		
        var relativePosition = Math.max( 0,
			Math.min( gutterBounds.width - thumbBounds.width,
			(e.clientX - gutterBounds.x) - ( thumbBounds.width / 2 ) ) );
        this.get_Thumb().style.left = relativePosition + "px";
        
        var percentLeft = relativePosition / ( gutterBounds.width - thumbBounds.width );
        var lastItem = this.get_CommandControlContainer().get_CommandControlList().get_Items()[ this.get_CommandControlContainer().get_CommandControlList().get_Items().length - 1 ].get_element();
        this.get_CommandControlContainer().set_HorizontalPosition( ( lastItem.offsetWidth + lastItem.offsetLeft - this.get_CommandControlContainer().get_element().offsetWidth ) * percentLeft * -1 );
	}
}

// Register the class as a type that inherits from Sys.UI.Control.
WindowsSite.Ajax.Controls.ScrollbarBehavior.registerClass('WindowsSite.Ajax.Controls.ScrollbarBehavior', WindowsSite.Ajax.Common.ControlBaseBehavior);
