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

WindowsSite.Ajax.Controls.CommandControlBehavior = function( element ) 
{
    WindowsSite.Ajax.Controls.CommandControlBehavior.initializeBase( this, [element] );
    this.blurDelegate = Function.createDelegate(this, this.blurElement);
    this.handleEventDelegate = Function.createDelegate(this, this.eventHandler);
    this.trackingDelegate = Function.createDelegate(this, this.trackingHandler);
    this.atlasDelegate = Function.createDelegate(this, this.atlasHandler);
}

WindowsSite.Ajax.Controls.CommandControlBehavior.prototype = 
{
    blurElement : function ()
    {
        this.get_element().blur();
    },
    
    initialize : function() 
    {
        WindowsSite.Ajax.Controls.CommandControlBehavior.callBaseMethod(this, 'initialize');
        if ( this.get_HideNavigateUrlOnLoad() ) this.get_element().href = "javascript:void(0)";
       
        if (this.get_AtlasTag() && this.get_AtlasTag() != "") this.add_Handler( "click", this.atlasDelegate);
        this.add_Handler( "click", this.trackingDelegate );
        this.initializeCommands();
        this.set_IsEnabled( true );
        if ( this.get_SelectedOnPageLoad() ) this.set_IsSelected( true );
    },
    
    initializeCommands : function()
    {
        var events = new Array();
        for ( var i = 0; i < this._commands.length; i++ )
        {
            var eventType = this._getMouseEvent( this._commands[i].MouseEvent );
            if ( events.toString().indexOf( eventType ) < 0 )
            {
                events.push( eventType );
                this.add_Handler( this._getMouseEvent( this._commands[i].MouseEvent ), this.handleEventDelegate );
            }
        }
    },

    dispose : function() 
    {
        $clearHandlers(this.get_element());
        WindowsSite.Ajax.Controls.CommandControlBehavior.callBaseMethod( this, 'dispose' );
    },
    
    get_HideNavigateUrlOnLoad : function() {
        return this._hideNavigateUrlOnLoad;
    },
    set_HideNavigateUrlOnLoad : function(value) {
        if (this._hideNavigateUrlOnLoad != value) {
            this._hideNavigateUrlOnLoad = value;
            this.raisePropertyChanged('HideNavigateUrlOnLoad');
        }
    },
    
    get_IsSelected : function() {
        return this._isSelected;
    },
    set_IsSelected : function(value) {
        if (this._isSelected != value) {
            this._isSelected = value;
            this._refresh();
            this.raisePropertyChanged('IsSelected');
        }
    },
    
    get_IsEnabled : function() {
        return this._isEnabled;
    },
    set_IsEnabled : function(value) {
        if (this._isEnabled != value) {
            this._isEnabled = value;
            this._refresh();
            this.raisePropertyChanged('IsEnabled');
        }
    },
    
    get_Commands : function() {
        return this._commands;
    },
    set_Commands : function(value) {
        if ( this._commands != value )
		{
			this._commands = value ? Array.parse( value ) : [];
			this.raisePropertyChanged( "Commands" );
		}
    },
    
    get_AtlasTag : function() {
        return this._atlasTag;
    },
    set_AtlasTag : function(value) {
        if (this._atlasTag != value) {
            this._atlasTag = value;
            this.raisePropertyChanged('AtlasTag');
        }
    },
    
    get_NormalCssClass : function() {
        return this._normalCssClass;
    },
    set_NormalCssClass : function(value) {
        if (this._normalCssClass != value) {
            this._normalCssClass = value;
            this.raisePropertyChanged('NormalCssClass');
        }
    },
    
    get_SelectedCssClass : function() {
        return this._selectedCssClass;
    },
    set_SelectedCssClass : function(value) {
        if (this._selectedCssClass != value) {
            this._selectedCssClass = value;
            this.raisePropertyChanged('SelectedCssClass');
        }
    },
    
    get_DisabledCssClass : function() {
        return this._disabledCssClass;
    },
    set_DisabledCssClass : function(value) {
        if (this._disabledCssClass != value) {
            this._disabledCssClass = value;
            this.raisePropertyChanged('DisabledCssClass');
        }
    },
    
    get_SelectedOnPageLoad : function() {
        return this._selectedOnPageLoad;
    },
    set_SelectedOnPageLoad : function(value) {
        if (this._selectedOnPageLoad != value) {
            this._selectedOnPageLoad = value;
            this.raisePropertyChanged('SelectedOnPageLoad');
        }
    },
    
    trackingHandler : function ( args )
    {
			
    },
    
    atlasHandler : function ( args )
    {
		CreateAtlasImage( this.get_AtlasTag() );
    },
    
    eventHandler : function ( args )
    {
        if ( this._isValidEvent( args ) )
        {
            for ( var i = 0; i < this._commands.length; i++ )
            {
                if ( this._getMouseEvent( this._commands[i].MouseEvent ) == args.type )
                {
                    var targetID = this._commands[i].TargetID;
                    var eventArgs = new WindowsSite.Ajax.Common.CommandEventArgs( this._commands[i].Name, this._commands[i].Argument );
                    if ( targetID != null && targetID != "" )
						this.performCommand( targetID, eventArgs );
					else
						this._onControlCommand( this, eventArgs );
                }
            }
        }
    },
    
    performCommand : function ( targetID, eventArgs )
    {
        $find( targetID )._onControlCommand( this, eventArgs );
    },
    
    _isValidEvent : function ( args )
    {
		if ( !this.get_IsEnabled() ) return false;
        var to, from;
        switch ( args.type )
        {
            case "mouseover":
                to = args.target;
                from = ( args.rawEvent.fromElement ) ? args.rawEvent.fromElement : args.rawEvent.relatedTarget;
                if ( to == null || from == this.get_element() || this.isChildNode( this.get_element(), from ) )
                    return false;
                break;
            case "mouseout":
                to = ( args.rawEvent.toElement ) ? args.rawEvent.toElement : args.rawEvent.relatedTarget;
                from = args.target;
                if ( from == null || to == this.get_element() || this.isChildNode( this.get_element(), to ) )
                    return false;
                break;    
        }
        return true;
    },
    
    _onControlCommand : function( sender, args )
    {
        if (args != null)
        {
            this._commandName = args.get_CommandName();
            this._commandArgument = args.get_CommandArgument();
        }
        switch ( this._commandName )
        {
			case "SetIsSelected":
				this.set_IsSelected( this._commandArgument );
				break;
            default:
                WindowsSite.Ajax.Controls.CommandControlBehavior.callBaseMethod( this, '_onControlCommand' );
                break;
        }
    },
    
    _getMouseEvent : function ( eventValue ) {
        switch ( eventValue )
        {
            case 0:
                return "mouseover";
            case 1:
                return "mouseout";
            case 2:
                return "click";
            case 3:
                return "mousedown";
            case 4:
                return "mouseup";
        }
    },
    
    _refresh : function()
    {
		var className;
		if ( !this.get_IsEnabled() ) {
			this.set_IsSelected( false );
			this.disabled = true;
			className = this.get_DisabledCssClass();
		}
		else if ( this.get_IsSelected() ) {
			className = this.get_SelectedCssClass();
			this.disabled = false;
		}
		else {
			className = this.get_NormalCssClass();
			this.disabled = false;
		}
		
		if ( className != null && className != "" ) {
			this.get_element().className = this.get_element().className.replace( this.get_NormalCssClass(), "" );
			this.get_element().className = this.get_element().className.replace( this.get_SelectedCssClass(), "" );
			this.get_element().className = this.get_element().className.replace( this.get_DisabledCssClass(), "" );
			this.get_element().className += (" " + className);
		}
    },
	
	isChildNode : function ( ancestor, child )
    {
        for ( var i=0; i < ancestor.childNodes.length; i++ )
        {
            if ( ancestor.childNodes[i] == child ) return true;
            if ( this.isChildNode( ancestor.childNodes[i], child ) ) return true;
        }
        return false;
    }
}

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