﻿/// <reference name="MicrosoftAjax.js"/>
Type.registerNamespace('WindowsSite.Ajax.Controls');

WindowsSite.Ajax.Controls.CommandControlScrollContainerBehavior = function( element ) {
    WindowsSite.Ajax.Controls.CommandControlScrollContainerBehavior.initializeBase( this, [element] );
    
    this._scrollSpeed = null;
    this._scrollTimer = null;
    this.animateStepTick = Function.createDelegate( this, this._animationStep );
}

WindowsSite.Ajax.Controls.CommandControlScrollContainerBehavior.prototype = {

    initialize : function() {
        WindowsSite.Ajax.Controls.CommandControlScrollContainerBehavior.callBaseMethod(this, 'initialize');

		if ( this.get_AutoScroll() != WindowsSite.Ajax.AutoScrollType.None )
		{
		    var items = this.get_CommandControlList().get_Items();
            for ( var i = 0; i < items.length; i++ )
            {
			    $addHandler( items[i].get_element(), ( this.get_AutoScroll() == WindowsSite.Ajax.AutoScrollType.Hover ) ? 'mouseover' : 'click', Function.createDelegate(this, this._autoShift) );
            }
        }
        
        //check orientation and set scrolling based on value ( 0 = horizontal)
        if (this.get_Orientation() == 0)
        {
			this.set_ScrollWidth();
			this.set_HorizontalPosition( 0 );
		}
		else
		{
			this.set_ScrollHeight();
			var items = this.get_CommandControlList().get_Items();
			this.set_VerticalPosition( 0 );
			for (var i in items)
			{
				if (items[i].get_IsSelected())
				{
					this.set_VerticalPosition( items[i].get_element().offsetTop * -1 );
					break;
				}
			}
		}
    },

    dispose : function() {
        $clearHandlers(this.get_element());
        WindowsSite.Ajax.Controls.CommandControlScrollContainerBehavior.callBaseMethod(this, 'dispose');
    },
    
    get_CommandControlList : function() {
        return this._CommandControlList;
    },
    set_CommandControlList : function(value) {
        if (this._CommandControlList != value) {
            this._CommandControlList = value;
            this.raisePropertyChanged('CommandControlList');
        }
    },
    
    get_FramesPerSecond : function() {
        return this._framesPerSecond;
    },
    set_FramesPerSecond : function(value) {
        if (this._framesPerSecond !== value) {
            this._framesPerSecond = value;
            this.raisePropertyChanged('FramesPerSecond');
        }
    },
    
    get_AutoScroll : function() {
        return this._autoScroll;
    },
    set_AutoScroll : function(value) {
        if (this._autoScroll !== value) {
            this._autoScroll = value;
            this.raisePropertyChanged('AutoScroll');
        }
    },
      
    get_ScrollWidth : function() {
        return this._scrollWidth;
    },
    set_ScrollWidth : function() {
		var items = this.get_CommandControlList().get_Items();
		var lastItem = items[items.length - 1].get_element();
		var elementBounds = Sys.UI.DomElement.getBounds( this.get_element() );
        this._scrollWidth = ( lastItem.offsetLeft + lastItem.offsetWidth ) - elementBounds.width + 2;
        this.raisePropertyChanged('ScrollWidth');
    },
    
    get_HorizontalPosition : function() {
        return this._horizontalPosition;
    },
    set_HorizontalPosition : function( location ) {
        if (!isNaN(this._scrollWidth))
        {
		    this._horizontalPosition = Math.min( 0 , Math.max( this._scrollWidth * -1, location ) );
            this.get_CommandControlList().get_element().style.left = this._horizontalPosition + "px";
        }
        this.raisePropertyChanged('HorizontalPosition');
        return ( this._horizontalPosition != 0 && this._horizontalPosition != this._scrollWidth * -1 );
    },

		//ScrollHeight, VerticalPosition, and Orientation added for Vertical Scrolling
    get_ScrollHeight : function() {
        return this._scrollHeight;
    },
    set_ScrollHeight : function() {
		var items = this.get_CommandControlList().get_Items();
		var lastItem = items[items.length - 1].get_element();
		var elementBounds = Sys.UI.DomElement.getBounds( this.get_element() );
        this._scrollHeight = ( lastItem.offsetTop + lastItem.offsetHeight ) - elementBounds.height + 2;
        this.raisePropertyChanged('ScrollHeight');
    },
    
    get_VerticalPosition : function() {
        return this._verticalPosition;
    },
    set_VerticalPosition : function( location ) {
        if (!isNaN(this._scrollHeight))
        {
		    this._verticalPosition = Math.min( 0 , Math.max( this._scrollHeight * -1, location ) );
            this.get_CommandControlList().get_element().style.top = this._verticalPosition + "px";
        }
        this.raisePropertyChanged('VerticalPosition');
        return ( this._verticalPosition != 0 && this._verticalPosition != this._scrollHeight * -1 );
    },
        
    get_Orientation : function() {
			return this._orientation;
    },
    set_Orientation : function(value) {
        if (this._orientation !== value) {
            this._orientation = value;
            this.raisePropertyChanged('Orientation');
        }
    },
    
    _onControlCommand : function ( sender, args )
    {
		if (args != null)
        {
            this._commandName = args.get_CommandName();
            this._commandArgument = args.get_CommandArgument();
        }
        switch ( this._commandName )
        {
            case "BeginScrollLeft":
								this.scrollSpeed = parseInt( this._commandArgument );
								this._scrollTimer = setInterval( this.animateStepTick, this.get_FramesPerSecond() );
                break;
            case "BeginScrollRight":
                this.scrollSpeed = parseInt( this._commandArgument ) * -1;
								this._scrollTimer = setInterval( this.animateStepTick, this.get_FramesPerSecond() );
                break;
            case "BeginScrollUp":
								this.scrollSpeed = parseInt( this._commandArgument );
								this._scrollTimer = setInterval( this.animateStepTick, this.get_FramesPerSecond() );
                break;
            case "BeginScrollDown":
                this.scrollSpeed = parseInt( this._commandArgument ) * -1;
								this._scrollTimer = setInterval( this.animateStepTick, this.get_FramesPerSecond() );
                break;                
            case "HaltScroll":
								this._haltScroll();
								break;
            default:
                WindowsSite.Ajax.Controls.CommandControlScrollContainerBehavior.callBaseMethod( this, '_onControlCommand' );
                break;
        }
    },
    
    _animationStep : function()
    {
			//check orientation and set scrolling based on value ( 0 = horizontal)
			if (this.get_Orientation() == 0)
			{
				if ( !this.set_HorizontalPosition( parseInt( this.get_CommandControlList().get_element().style.left ) + this.scrollSpeed ) )
					this._haltScroll();
			}
			else
			{
					if ( !this.set_VerticalPosition( parseInt( this.get_CommandControlList().get_element().style.top ) + this.scrollSpeed ) )
						this._haltScroll();
			}
    },
    
    _haltScroll : function()
    {
		clearInterval( this._scrollTimer );
    },
    
    _autoShift : function( args )
    {
        var sender = args.target;
        while ( sender.nodeName != "A" && sender.parentNode != null ) sender = sender.parentNode;
        if ( sender.offsetLeft + this.get_HorizontalPosition() < 0 ) 
            this.set_HorizontalPosition( sender.offsetLeft * -1 );
        else if ( this.get_HorizontalPosition() * -1 < sender.offsetLeft + sender.offsetWidth - parseInt(this.get_element().style.width) ) 
            this.set_HorizontalPosition( ( sender.offsetLeft + sender.offsetWidth - parseInt(this.get_element().style.width) ) * -1 );
    }
}


WindowsSite.Ajax.Controls.CommandControlScrollContainerBehavior.registerClass('WindowsSite.Ajax.Controls.CommandControlScrollContainerBehavior', WindowsSite.Ajax.Common.ControlBaseBehavior);


WindowsSite.Ajax.AutoScrollType = function() 
{
    throw Error.invalidOperation();
}

WindowsSite.Ajax.AutoScrollType.prototype = 
{
	None : 0,
	Click : 1,
	Hover : 2
}
WindowsSite.Ajax.AutoScrollType.registerEnum( 'WindowsSite.Ajax.AutoScrollType' );

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
