﻿///<reference path="..\..\Core\jscript\CliPropConfig6.js" />
///<reference path="..\..\Core\jscript\CliHtmlUtility6.js" />

//--------------------------------------------------------------------------------
function CliLibraryActionButtons( app )
{
    ///<summary>Manages the states of the action buttons at the bottom of
    ///the library page.</summary>
    
    this._app = app; 
    
    //- count of selected items of a certain state ---------
    this._selectedBookmarks   = 0;
    this._selectedDownloading = 0;
    this._selectedPaused      = 0;
    this._selectedReady       = 0;
    this._selectedTotal       = 0;
    
    this.DownloadButtonIds = null;  //array
    this.ResumeButtonIds = null;
    this.PauseButtonIds = null;
    this.SyncButtonIds = null;
    this.DeleteButtonIds = null;
}

CliLibraryActionButtons.prototype =
{
    //--------------------------------------------------------------------------------
    Reset : function()
    {
        this._selectedBookmarks   = 0;
        this._selectedDownloading = 0;
        this._selectedPaused      = 0;
        this._selectedReady       = 0;
        this._selectedTotal       = 0;
    },
    
    //--------------------------------------------------------------------------------
    ItemRowSelectionChanged : function( mediaContentId, checked )
    {
        ///<summary>function to call when the state of a check box associated with a
        ///library item changes</summary>
        ///<param name="libraryItemState">state of library item associated with the
        ///checkbox</param>
        ///<param name="checked">true if the item is now checked</param>
        
        this._UpdateSelectedCounts( checked, mediaContentId );
        this.EnableButtons();
    },
    
    //--------------------------------------------------------------------------------
    EnableButtons: function()
    {
        ///<summary>based on the count of items selected and their states, enable
        ///or disable each button</summary>
        
         this._SetButtonEnabledState( this.DownloadButtonIds, this._selectedBookmarks <= 0 );
         this._SetButtonEnabledState( this.ResumeButtonIds, this._selectedPaused <= 0 );
         this._SetButtonEnabledState( this.PauseButtonIds, this._selectedDownloading <= 0 );
         this._SetButtonEnabledState( this.SyncButtonIds, this._selectedReady <= 0 );
         this._SetButtonEnabledState( this.DeleteButtonIds, this._selectedTotal <= 0 );
    },
   
    //--------------------------------------------------------------------------------
    _UpdateSelectedCounts : function( selected, mediaContentId )
    {
        ///<summary>updates the count of items selected in the current library view</summary>
        ///<param name="selected">true if the item is now selected, otherwise false</param>
        ///<param name="mediaContentId">mediaContentId of item</param>
        
        var increment = 1;
        if( !selected )
            increment = -1;
        
        var app = this._app;
        var item = app.GetItem( mediaContentId );
        var fnSyncReady = function()
        {
            return CliIsItemSyncOrDownloadable( mediaContentId )
             && CliDoesItemHaveSyncsRemaining( mediaContentId );
        }
        
        var status = item.State;
        
        switch( status )
        {
            case 5:
            {
                if( fnSyncReady() )
                    this._selectedBookmarks += increment; 
            } break;
            case 10:
            {
                if( fnSyncReady() )
                    this._selectedReady += increment;
            } break;
            
            case 6: this._selectedDownloading += increment; break;
            case 9:  this._selectedPaused += increment; break;
        }
        
        if( this._selectedBookmarks < 0 || this._selectedDownloading < 0 || this._selectedPaused < 0 || this._selectedReady < 0 )
            CliDebug( "Library action counts have dropped below zero" );
            
        this._selectedTotal += increment;
    },
    
    //--------------------------------------------------------------------------------
    _SetButtonEnabledState : function( buttonIds, disabled )
    {
        ///<summary>Sets the button to be disabled, or enabled</summary>
        ///<param name="buttonIds">array of ids of elements that are the button</param>
        ///<param name="disabled">true if the item should be disabled</param>
        
        for( var i = 0; i < buttonIds.length; i++ )
        {
            try
            {
                var button = document.getElementById( buttonIds[ i ] );
                
                if( disabled )
                {
                    var title = button.getAttribute( "title" );
                    if( title != null )
                    {
                        button.setAttribute( "hidden-title", title );
                        button.removeAttribute( "title" );
                    }
                        
                    CliAddClassName( button.parentNode, "disabledbut" );
                    CliRemoveClassName( button.parentNode, "activebut" );
                }
                else
                {
                    var title = button.getAttribute( "hidden-title" );
                    if( title != null )
                    {
                        button.setAttribute( "title", title );
                        button.removeAttribute( "hidden-title" );
                    }

                    CliAddClassName( button.parentNode, "activebut" );
                    CliRemoveClassName( button.parentNode, "disabledbut" );
                }
                
                button.disabled = disabled;
            }
            catch( e )
            {
                //CliDebug( "CliLibraryActionButtons._SetButtonEnabledState-->" + e.message );
            }
        }
    }//
}//