/**************************************************************************
*
*  @@@BUILDINFO@@@ 06favorites.jsx 2.0.1.63   27-March-2007
*  Copyright 2006-2007 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains the property of
* Adobe Systems Incorporated  and its suppliers,  if any.  The intellectual
* and technical concepts contained herein are proprietary to  Adobe Systems
* Incorporated  and its suppliers  and may be  covered by U.S.  and Foreign
* Patents,patents in process,and are protected by trade secret or copyright
* law.  Dissemination of this  information or reproduction of this material
* is strictly  forbidden  unless prior written permission is  obtained from
* Adobe Systems Incorporated.
**************************************************************************/

function Favorites()
{
    this.length = 0;
    this.items  = [];

    var trusted = Folder.myDocuments;
    trusted.changePath( 'Adobe Scripts' );
    
    if( !trusted.exists )
        trusted.create();
        
    this.add( localize( "$$$/ESToolkit/Panes/Scripts/DefaultFavorite=Default" ), 
			  trusted.absoluteURI, true, "*.jsx", true );
    globalBroadcaster.registerClient( this );
}

Favorites.prototype.add = function( name, path, isDefault, filter, recursive )
{
    for( var i=0; i<this.items.length; i++ )
    {
        if( name        == this.items[i].name       &&
            path        == this.items[i].path       &&
            isDefault   == this.items[i].isDefault  &&
            filter      == this.items[i].filter     &&
            recursive   == this.items[i].recursive      )
            // exactly the same Favorite already exists
            return this.items[i];
    }

    for( var i=0; i<this.items.length; i++ )
    {
        if( name == this.items[i].name )
        {
            var count = 1;
            var failed = false;
            
            do
            {
                failed = false;
                var tmp = name + count;
                
                for( var j=0; j<this.items.length; j++ )
                {
                    if( tmp == this.items[j].name )
                    {
                        count++;
                        failed = true;
                        break;
                    }
                }
                
            } while( failed );
            
            name += count;
            break;
        }
    }
            
    var item = new FavoriteItem( name, path, isDefault, filter, recursive );
    this.items.push( item );
    this.length++;
    globalBroadcaster.notifyClients( 'favoritesChanged' );
	
	return item;
}

Favorites.prototype.remove = function( favorite )
{
    for( var i=0; i<this.items.length; i++ )
    {
        if( favorite.name        == this.items[i].name       &&
            favorite.path        == this.items[i].path       &&
            favorite.isDefault   == this.items[i].isDefault  &&
            favorite.filter      == this.items[i].filter     &&
            favorite.recursive   == this.items[i].recursive      )
        {
            this.items.splice(i,1);
            this.length--;
            globalBroadcaster.notifyClients( 'favoritesChanged' );
            break;
        }
    }
}

Favorites.prototype.removeAll = function()
{
    this.length = 0;
    this.items  = [];

    var trusted = Folder.myDocuments;
    trusted.changePath( 'Adobe Scripts' );
    this.add( localize( "$$$/ESToolkit/Panes/Scripts/DefaultFavorite=Default" ), 
			  trusted.absoluteURI, true, "*.jsx", true );
}

Favorites.prototype.replace = function( oldItem, newItem )
{
    for( var i=0; i<this.length; i++ )
    {
        if( this.items[i] == oldItem )
        {
            oldItem.name       = newItem.name;
            oldItem.path       = newItem.path;
            oldItem.isDefault  = newItem.isDefault;
            oldItem.filter     = newItem.filter;
            oldItem.recursive  = newItem.recursive;
            
            globalBroadcaster.notifyClients( 'favoritesChanged' );
            
            break;
        }
    }
}

Favorites.prototype.readPrefs = function()
{
    var length = parseInt( prefs.scriptFavorites.length );
    
    if( !isNaN( length ) && length > 0 )
    {
        while( this.items.length > 1 )
            if( !this.items[this.items.length-1].isDefault )
                this.items.pop();
        
        for( var i=0; i<length; i++ )
        {
            var name        = prefs.scriptFavorites[i].name.toString();
            var path        = prefs.scriptFavorites[i].path.toString();
            var filter      = prefs.scriptFavorites[i].filter.toString();
            var recursive   = ( prefs.scriptFavorites[i].recursive == true );
            
            this.add( name, path, false, filter, recursive );
        }
    }
}

Favorites.prototype.writePrefs = function()
{
    var c = 0;
        
    if( this.length > 0 )
    {
        for( var i=0; i<this.length; i++ )
        {
            if( !this.items[i].isDefault )
            {
                prefs.scriptFavorites[c].name       = this.items[i].name;
                prefs.scriptFavorites[c].path       = this.items[i].path;
                prefs.scriptFavorites[c].filter     = this.items[i].filter;
                prefs.scriptFavorites[c].recursive  = this.items[i].recursive;
                
                c++;
            }
        }
    }
    
    prefs.scriptFavorites.length = c;
}

Favorites.prototype.onNotify = function( reason, param01 )
{
    if( reason == 'shutdown' )
    {
        globalBroadcaster.unregisterClient( this );
        
        if( !param01 )
            this.writePrefs();
    }
}

Favorites.prototype.dialog = function( item )
{
    var ret = null;
    var text = '';
    var touchedName = false;
    
    if( item )
    {
        text = "text:'$$$/ESToolkit/Dialog/Favorites/TitleMod=Modify Favorites'";
        touchedName = true;
    }
    else
        text = "text:'$$$/ESToolkit/Dialog/Favorites/TitleNew=Create New Favorite'";

    var dlg = new Window( 
        "prefdialog { " + text + ",                                                                                                                                                         \
            orientation : 'column',                                                                                                                                                     \
            g1  : Group                                                                                                                                                                 \
            {                                                                                                                                                                           \
                alignChildren:'left',                                                                                                                                                   \
                orientation : 'column',                                                                                                                                                 \
                grp : Group                                                                                                                                                             \
                {                                                                                                                                                                       \
                    orientation : 'row',                                                                                                                                                \
                    namegrp : Group                                                                                                                                                     \
                    {                                                                                                                                                                   \
                        orientation : 'column',                                                                                                                                         \
                        tn  : StaticText                                                                                                                                                \
                        {                                                                                                                                                               \
                            alignment : 'left',                                                                                                                                         \
                            text    : '$$$/ESToolkit/Dialog/Favorites/Name=Name'                                                                                                        \
                        },                                                                                                                                                              \
                        favName    : EditText                                                                                                                                           \
                        {                                                                                                                                                               \
                            alignment : ['fill','top'],                                                                                                                                 \
                            characters  : 40,                                                                                                                                           \
                            helpTip : '$$$/ESToolkit/Dialog/Favorites/htName=A unique name of the favorite.'                                                                            \
                        }                                                                                                                                                               \
                    },                                                                                                                                                                  \
                    filtergrp   : Group                                                                                                                                                 \
                    {                                                                                                                                                                   \
                        orientation : 'column',                                                                                                                                         \
                        fn  : StaticText                                                                                                                                                \
                        {                                                                                                                                                               \
                            alignment : 'left',                                                                                                                                         \
                            text    : '$$$/ESToolkit/Dialog/Favorites/Filter=Filter'                                                                                                    \
                        },                                                                                                                                                              \
                        filter  : EditText                                                                                                                                              \
                        {                                                                                                                                                               \
                            alignment : ['fill','top'],                                                                                                                                 \
                            charachters : 15,                                                                                                                                           \
                            helpTip : '$$$/ESToolkit/Dialog/Favorites/htFilter=Filter files based on extension. Wildcards are allowed.' \
                        }                                                                                                                                                               \
                    }                                                                                                                                                                   \
                }                                                                                                                                                                       \
                path    : Group                                                                                                                                                         \
                {                                                                                                                                                                       \
                    orientation : 'row',                                                                                                                                                \
                    pathStr : EditText                                                                                                                                                  \
                    {                                                                                                                                                                   \
                        alignment:'fill',                                                                                                                                               \
                        text    : '',                                                                                                                                                   \
                        characters  : 40,                                                                                                                                               \
                        properties :{readonly    : true }                                                                                                                               \
                    },                                                                                                                                                                  \
                    button  : IconButton                                                                                                                                                \
                    {                                                                                                                                                                   \
                        alignment:'right',                                                                                                                                              \
                        icon    : '#FolderOpened',                                                                                                                                      \
                        preferredSize: [20,20],                                                                                                                                         \
                        helpTip : '$$$/ESToolkit/Dialog/Favorites/htFolder=Select a folder for this Favorite.'                                        \
                    }                                                                                                                                                                   \
                },                                                                                                                                                                      \
                recursive   : Checkbox                                                                                                                                                  \
                {                                                                                                                                                                       \
                    text    : '$$$/ESToolkit/Dialog/Favorites/Recursive=Recursive Folders',                                                                                             \
                    helpTip : '$$$/ESToolkit/Dialog/Favorites/htRecursive=Scan subfolders.'                                                                   \
                }                                                                                                                                                                       \
            },                                                                                                                                                                          \
            g2  : Group                                                                                                                                                                 \
            {                                                                                                                                                                           \
                orientation : 'row',                                                                                                                                                    \
                btOK  : Button                                                                                                                                                          \
                {                                                                                                                                                                       \
                    text : '$$$/CT/ExtendScript/UI/OK=&OK'                                                                                                                              \
                },                                                                                                                                                                      \
                btCancel  : Button                                                                                                                                                      \
                {                                                                                                                                                                       \
                    text : '$$$/CT/ExtendScript/UI/Cancel=&Cancel'                                                                                                                      \
                }                                                                                                                                                                       \
            }                                                                                                                                                                           \
        }" );
            
    dlg.nameField   = dlg.g1.grp.namegrp.favName;
    dlg.filterField = dlg.g1.grp.filtergrp.filter;
    dlg.pathField   = dlg.g1.path.pathStr;
    dlg.pathBtn     = dlg.g1.path.button;
    dlg.recursBtn   = dlg.g1.recursive;
    
    if( item )
    {
        dlg.nameField.text      = item.name;
        dlg.pathField.text      = File(item.path).fsName;
        dlg.filterField.text    = item.filter;
        dlg.recursBtn.value     = item.recursive;
    }
    else
    {
        dlg.nameField.text      = localize( "$$$/ESToolkit/Dialog/Favorites/NewFavorite=New Favorite" );
        dlg.pathField.text      = '';
        dlg.filterField.text    = '*.jsx';
        dlg.recursBtn.value     = false;
    }
    
    dlg.nameField.onChange   = 
	dlg.nameField.onChanging = function()
    {
        touchedName = true;
    }
    
    dlg.pathBtn.onClick = function()
    {
        var defFolder = ( item ? Folder( item.path ) : app.currentFolder );
        var path = Folder.selectDialog( localize( '$$$/ESToolkit/FavDlg/selectScripts=Select a scripts folder' ), defFolder ); 
        
        if( path )
        {
			var pathName = path.fsName;
			if (pathName == "")
				pathName = "/";
            this.window.pathField.text = pathName;
            
            if( !touchedName )
				this.window.nameField.text = (pathName == "/") ? pathName : decodeURI( path.name );
        }
    }
    
    dlg.g2.btCancel.onClick = function()
    {
        this.window.close();
    }
    
    dlg.g2.btOK.onClick = function()
    {
        if( this.window.pathField.text.length > 0 )
            ret = new FavoriteItem( this.window.nameField.text, 
                                    this.window.pathField.text, 
                                    false, 
                                    this.window.filterField.text, 
                                    this.window.recursBtn.value );
            
        this.window.close();
    }
    
    dlg.onShow = function()
    {
        this.window.nameField.active = true;
    }
    
    dlg.defaultElement = dlg.g2.btOK;
    dlg.cancelElement  = dlg.g2.btCancel;
    dlg.show();
    
    return ret;
}

function FavoriteItem( name, path, isDefault, filter, recursive )
{
    this.name       = name;
    this.path       = path;
    this.isDefault  = ( isDefault ? isDefault : false );
    this.filter     = ( filter ? filter : '*.*' );
    this.recursive  = ( recursive ? recursive : false );
}

FavoriteItem.prototype.equal = function( favorite )
{
	return ( favorite.name        == this.name       &&
 			 favorite.path        == this.path       &&
 			 favorite.isDefault   == this.isDefault  &&
			 favorite.filter      == this.filter     &&
			 favorite.recursive   == this.recursive      );
}
