/**************************************************************************
*
*  @@@BUILDINFO@@@ 95favoritesPrefs-2.jsx 2.0.1.61  08-Feb-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.
**************************************************************************/

// Each Prefs object needs the following methods:
// prefsObj.create (parentPane) - create the pane and return the Pane object.
// pane.preProcess()			- preprocess the pane just before being shown.
// pane.layoutDone()			- informs the pane that the initial layout is done (optional)
// pane.postProcess()			- do any work just before the pane is being hidden.
// pane.store()					- store the preferences. Return true if the Next Time dialog is needed.
// pane.toDefault()				- set back to default values

var favoritesPrefs = { todefault : false };

favoritesPrefs.create = function (parentPane)
{
    this.pane = parentPane.add (
                "group {																	                                    \
	                orientation:'column', alignChildren:['fill','fill'], visible:false, alignment: ['fill','fill'],				\
	                list        : ListBox                                                                                       \
	                {                                                                                                           \
		                helpTip : '$$$/ESToolkit/PreferencesDlg/hiFavrites=List of defined favorites used in the Scripts panel' \
	                },                                                                                                          \
	                buttons     : Group                                                                                         \
	                {                                                                                                           \
		                orientation : 'row',                                                                                    \
		                alignment   : ['left','bottom']\
		                btAdd       : Button                                                                                    \
		                {                                                                                                       \
			                text        : '$$$/ESToolkit/PreferencesDlg/addFav=Add...',                                         \
			                helpTip     : '$$$/ESToolkit/PreferencesDlg/hiAddFav=Add new favorite'                              \
		                },                                                                                                      \
		                btModify    : Button                                                                                    \
		                {                                                                                                       \
			                text        : '$$$/ESToolkit/PreferencesDlg/modFav=&Modify',                                         \
			                helpTip     : '$$$/ESToolkit/PreferencesDlg/hiModFav=Modify selected favorite'                      \
		                }                                                                                                       \
		                btRemove    : Button                                                                                    \
		                {                                                                                                       \
			                text        : '$$$/ESToolkit/PreferencesDlg/remFav=&Remove',                                         \
			                helpTip     : '$$$/ESToolkit/PreferencesDlg/hiRemFav=Remove selected favorite'                      \
		                }                                                                                                       \
	                }                                                                                                           \
                }");
                
	this.pane.prefsObj = this;
	
	this.pane.layoutDone = function()
	{
		this.list.textWidth = this.list.size.width / 2;
		var items = this.list.items;
		for (var i = 0; i < items.length; i++)
		{
			var item = items [i];
			var favorite = item.favorite;
			item.setTabbedText (favorite.name, this.list.textWidth, decodeURIComponent( favorite.path ) );
		}
	}
	
	this.pane.preProcess = function()
	{
		// Needs always to be loaded - favorites may have changed
		this.prefsObj.load();
	}
	this.pane.postProcess = function()
	{}

	this.pane.toDefault = function()
	{
		for( var i=0; i<this.list.items.length; i++ )
		{
			if( !this.list.items[i].favorite.isDefault )
			{
				this.list.remove(i);
				i--;
			}
		}
	}

	return this.pane;
}

/////////////////////////////////////////////////////////////////////////
// initialize the favorites options pane

favoritesPrefs.load = function()
{
	with (this.pane) 
	{
//	    list.graphics.font = ScriptUI.newFont( "FixedWidth" );
	    
	    list.onChange = function()
	    {
	        this.parent.buttons.btModify.enabled = 
	        this.parent.buttons.btRemove.enabled = ( this.selection && !this.selection.favorite.isDefault );
	    }
	    
	    buttons.btAdd.onClick = function()
	    {
	        var newItem = favorites.dialog();
	        
	        if( newItem )
	        {
	            var item        = list.add( 'item', "");
	            item.favorite   = new FavoriteItem( newItem.name, newItem.path, false, newItem.filter, newItem.recursive );
				item.setTabbedText (newItem.name, list.textWidth, decodeURIComponent( newItem.path ) );
	        }
	    }
	    
	    buttons.btRemove.onClick = function()
	    {
			var list = this.parent.parent.list;
	        var selItem = list.selection;
	        if( selItem )
	        {
	            list.remove( selItem );
				list.selection = 0;
	        }
	    }
	    
	    buttons.btModify.onClick = function()
	    {
			var list = this.parent.parent.list;
	        var selItem = list.selection;
	        
	        if( selItem )
	        {
	            var newItem = favorites.dialog( list.selection.favorite );
	            
	            if( newItem )
	            {
	                selItem.favorite = newItem;
					selItem.setTabbedText (newItem.name, list.textWidth, decodeURIComponent( newItem.path ) );
	            }
	        }
	    }
	    
	    list.removeAll();
	    
	    if( favorites )
	    {
	        for( var i=0; i<favorites.length; i++ )
	        {
	            var item        = list.add( 'item', "" );
	            item.favorite   = new FavoriteItem( favorites.items[i].name, 
	                                                favorites.items[i].path, 
	                                                favorites.items[i].isDefault, 
	                                                favorites.items[i].filter, 
	                                                favorites.items[i].recursive );
				if (list.textWidth)
					item.setTabbedText (favorites.items[i].name, list.textWidth, decodeURIComponent( favorites.items[i].path ) );
	        }
	        
	        list.selection = 0;
	    }
	}
}

/////////////////////////////////////////////////////////////////////////
// store preferences from the selected favorites options
favoritesPrefs.store = function()
{
    favorites.removeAll();
    
    if( this.pane.list.items.length > 0 )
    {
        for( var i=0; i<this.pane.list.items.length; i++ )
        {
            if( !this.pane.list.items[i].favorite.isDefault )
                favorites.add( this.pane.list.items[i].favorite.name, 
                               this.pane.list.items[i].favorite.path, 
                               false, 
                               this.pane.list.items[i].favorite.filter,
                               this.pane.list.items[i].favorite.recursive );
        }
    }
	
	globalBroadcaster.notifyClients( 'newFavorites' );
	
	return false;
}

