Type.registerNamespace('WindowsSite.Ajax.Controls');

WindowsSite.Ajax.Controls.CommandControlContainerBehavior = function( element ) 
{
    WindowsSite.Ajax.Controls.CommandControlContainerBehavior.initializeBase( this, [element] );
}

WindowsSite.Ajax.Controls.CommandControlContainerBehavior.prototype = 
{    
    initialize : function() 
    {
        WindowsSite.Ajax.Controls.CommandControlContainerBehavior.callBaseMethod(this, 'initialize');
        
        this._currentlyShownHeroItemIndex = 1;
    },
    
    dispose : function() 
    {
        $clearHandlers(this.get_element());
        WindowsSite.Ajax.Controls.CommandControlContainerBehavior.callBaseMethod( this, 'dispose' );
    },
    
    ChangeHero : function(newShownHeroItemIndex)
    {
        if (this._currentlyShownHeroItemIndex != newShownHeroItemIndex)
        {
            //set current selected nav item background color to grey:
            var currentlyShownHeroItem = document.getElementById(this._itemIDs[this._currentlyShownHeroItemIndex]);
            currentlyShownHeroItem.className = "hero_nav_item_not_selected";
            currentlyShownHeroItem.style.backgroundColor = '#CCCCCC';
            
            //set new selected nav item background color:
            this._currentlyShownHeroItemIndex = newShownHeroItemIndex;
            var newShownHeroItem = document.getElementById(this._itemIDs[this._currentlyShownHeroItemIndex]);
            newShownHeroItem.className = "hero_nav_item_selected";
            newShownHeroItem.style.backgroundColor = this.get_HeroItemList()[this._currentlyShownHeroItemIndex-1].NavButtonsColor;
            
            //set new left and right arrow images:
            var leftArrow = document.getElementById(this._itemIDs[0]);
            leftArrow.style.backgroundImage = "url("+this.get_HeroItemList()[this._currentlyShownHeroItemIndex-1].LeftNavArrowImageSource+")";
            
            var rightArrow = document.getElementById(this._itemIDs[this._itemIDs.length-1]);
            rightArrow.style.backgroundImage = "url("+this.get_HeroItemList()[this._currentlyShownHeroItemIndex-1].RightNavArrowImageSource+")";
        }
    },
    
    
    _onControlCommand : function( sender, args )
    {
        if (args != null)
        {
            this._commandName = args.get_CommandName();
            this._commandArgument = args.get_CommandArgument();
        }
        this.MoveSelectedNavButton( this._commandName );
        
    },
    
    MoveSelectedNavButton : function( _commandName )
    {
        switch ( _commandName )
        {
            case "LeftArrowClicked":
                var newShownHeroItemIndex = parseInt(this._currentlyShownHeroItemIndex)-1; //select a previous hero item
                if (newShownHeroItemIndex == 0) //if the currently selected hero item is the first one, clicking the left arrow will select the last item on the list
                {
                    newShownHeroItemIndex = this._itemIDs.length - 2; //-2 because the last element is the right arrow
                }
                this.ChangeHero(newShownHeroItemIndex);
                eval(this.get_WebTrendsImpressionsCommandsList()[this._currentlyShownHeroItemIndex-1]);
                break;
            case "HeroNavButtonClicked":
                this.ChangeHero(this._commandArgument);
                eval(this.get_WebTrendsImpressionsCommandsList()[this._currentlyShownHeroItemIndex-1]);
                break;
            case "RightArrowClicked":
                this._showNextHero();
                eval(this.get_WebTrendsImpressionsCommandsList()[this._currentlyShownHeroItemIndex-1]);
                break;
            case "AutoDisplayNextHero":
                this._showNextHero();
                break;
            default:
                WindowsSite.Ajax.Controls.CommandControlContainerBehavior.callBaseMethod( this, '_onControlCommand' );
                break;
        }
    },
    
    _showNextHero : function ()
    {
        var newShownHeroItemIndex = parseInt(this._currentlyShownHeroItemIndex)+1; //select the next hero item
        if (newShownHeroItemIndex == this._itemIDs.length-1) //if the currently selected hero item is the last hero nav item, clicking the right arrow will select the first item on the list
        {
            newShownHeroItemIndex = 1;
        }
        this.ChangeHero(newShownHeroItemIndex);
    },
    
    _setSelected : function ( itemID ) {
        for (var i=0; i < this.get_ItemIDs().length; i++)
        {
            var value = ( itemID == this.get_ItemIDs()[i] ) ? true : false;
            this.get_Items()[i].set_IsSelected( value );
        }
    },
    
    
    
    //Properties:
    get_ItemIDs : function() {
        return this._itemIDs;
    },
    set_ItemIDs : function(value) {
        if (this._itemIDs != value) {
            this._itemIDs = value ? Array.parse( value ) : [];
            this.raisePropertyChanged('ItemIDs');
        }
    },
    
    get_HeroItemList : function() {
        return this._heroItemList;
    },
    set_HeroItemList : function(value) {
        if ( this._heroItemList != value )
		{
			this._heroItemList = value ? Array.parse( value ) : [];
			this.raisePropertyChanged( "HeroItemList" );
		}
    },
    
    get_WebTrendsImpressionsCommandsList : function() {
        return this._webTrendsImpressionsCommandsList;
    },
    set_WebTrendsImpressionsCommandsList : function(value) {
        if ( this._webTrendsImpressionsCommandsList != value )
		{
			this._webTrendsImpressionsCommandsList = value ? Array.parse( value ) : [];
			this.raisePropertyChanged( "WebTrendsImpressionsCommandsList" );
		}
    }
}

WindowsSite.Ajax.Controls.CommandControlContainerBehavior.registerClass('WindowsSite.Ajax.Controls.CommandControlContainerBehavior', WindowsSite.Ajax.Common.ControlBaseBehavior );