/**************************************************************************
*
*  @@@BUILDINFO@@@ 91generalPrefs-2.jsx 2.0.1.62  22-Mar-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 five methods:
// prefsObject.create (parentPane)  - create the pane and return the Pane object.
// prefsObject.preProcess()         - preprocess the pane just before being shown.
// prefsObject.postProcess()        - do any work just before the pane is being hidden.
// prefsObject.store()              - store the preferences. Return true if the Next Time dialog is needed.
// prefsObject.toDefault()          - set back to default values

var generalPrefs = {};

generalPrefs.create = function (parentPane)
{
	this.pane = parentPane.add (
	"group {									\
		orientation		: 'column',				\
		alignChildren	: 'left',				\
		visible			: false,				\
		alignment		: ['left','top'],		\
		startup			: Group					\
		{										\
			orientation		: 'column',			\
			alignChildren	: 'left',			\
			opt1			: RadioButton		\
			{									\
			    text			: '$$$/ESToolkit/PreferencesDlg/startNewDoc=Open New &Document', \
			    helpTip			: '$$$/ESToolkit/PreferencesDlg/htStartNewDoc=Open a new blank JavaScript document at startup.' \
			},									\
			opt2			: RadioButton		\
			{									\
			    text			: '$$$/ESToolkit/PreferencesDlg/startLastDoc=Open Last &Open Documents', \
			    helpTip			: '$$$/ESToolkit/PreferencesDlg/htLastDoc=Open the documents which were opened at last shutdown.' \
			},									\
			opt3			: RadioButton		\
			{									\
			    text			: '$$$/ESToolkit/PreferencesDlg/startNoDoc=&No Document', \
                helpTip			: '$$$/ESToolkit/PreferencesDlg/htNoDoc=Open no document at startup.' \
            }									\
        },										\
        autolaunch		: Checkbox				\
		{										\
		    text			: '$$$/ESToolkit/PreferencesDlg/autolaunch=Auto-Launch Application when Setting Target', \
            helpTip			: '$$$/ESToolkit/PreferencesDlg/htAutolaunch=Automatically launches application when setting target.' \
        },										\
        locale			: Group					\
		{										\
            orientation		: 'row',			\
            lbl				: StaticText		\
			{									\
				text			: '$$$/ESToolkit/PreferencesDlg/languages=&Languages:' \
			},									\
			list			: DropDownList		\
			{									\
				helpTip			: '$$$/ESToolkit/PreferencesDlg/htLanguage=Select the language to use in the user interface'\
			}									\
		}										\
	}");

	this.pane.locale.list.minimumSize.width = 120;
/*	if (_win)
	{
		var fontSize = this.pane.locale.list.graphics.font.size;
		// Make sure to select a Unicode font for Japanese and other languages
		this.pane.locale.list.graphics.font = ScriptUI.newFont( "Arial Unicode MS", fontSize );
	}
*/
	this.pane.prefsObj = this;
	this.pane.preProcess = function()
	{
		if (!this.prefsObj.loaded)
			this.prefsObj.load();
	}
	this.pane.postProcess = function()
	{}
	
	this.pane.toDefault = function()
	{
        this.startup.opt1.value    = PrefUtils.getDefaultValue( 'prefs.startup.newDoc', 'Boolean' );
        this.startup.opt2.value    = PrefUtils.getDefaultValue( 'prefs.startup.oldDocs', 'Boolean' );
        this.startup.opt3.value    = PrefUtils.getDefaultValue( 'prefs.startup.noDocs', 'Boolean' );
        this.autolaunch.value      = PrefUtils.getDefaultValue( 'prefs.autolaunch', 'Boolean' );
        this.locale.list.selection = 0;
	}

	this.loaded = false;
	return this.pane;
}

/////////////////////////////////////////////////////////////////////////
// Load prefs into the general prefs pane

generalPrefs.load = function()
{
 	if (this.loaded)
		return;
	this.loaded = true;

	if( locales.length == 0 )
    {
        //
        // take only estk dat files
        //
		var datFiles = Folder( app.requiredFolder ).getFiles( '??_??.dat' );

        for( var i=0; i<datFiles.length; i++ )
        {
			var f = datFiles[i];
			if (f.open())
			{
				// Search for "$$$/ESToolkit/A/Language=XXXX"
				var name = null;
				for (var j = 0; !name && j < 10; j++)
				{
					var text = f.readln();
					name = /"\$\$\$\/ESToolkit\/A\/Language=([^"]+)"/.exec (text);
				}
				f.close();
				if (name)
				{
					var locale = datFiles[i].name.substr( 0, 5 );
		            locales.push( [ locale, name[1] ] );
				}
			}
        }
        
        locales.sort();
    }
    
	with (this.pane) 
	{
	    startup.opt1.value    = PrefUtils.getValue( 'prefs.startup.newDoc', 'Boolean' );
	    startup.opt2.value    = PrefUtils.getValue( 'prefs.startup.oldDocs', 'Boolean' );
	    startup.opt3.value    = PrefUtils.getValue( 'prefs.startup.noDocs', 'Boolean' );
	    
	    autolaunch.value = PrefUtils.getValue( 'prefs.autolaunch', 'Boolean' );
	    
	    var selItem = null;
	    
		var curLocale = PrefUtils.getValue( 'prefs.locale', 'String' );

        locale.list.removeAll();
		var item = locale.list.add( 'item', localize( "$$$/ESToolkit/PreferencesDlg/DefaultLanguage=Default" ) );
		item.locale = "";
		selItem = item;
		locale.list.add( 'separator', "" );

		for( var i=0; i<locales.length; i++ )
	    {
	        var item = locale.list.add( 'item', locales[i][1] );
	        item.locale = locales[i][0];
	        
	        if( item.locale == curLocale )
	            selItem = item;
	    }
	    
	    locale.list.selection = selItem;
	}
}

/////////////////////////////////////////////////////////////////////////
// set preferences from the selected general options
generalPrefs.store = function()
{
	var nextTimeNeeded = false;
	if (this.loaded)
	{
		with (this.pane) 
		{
			prefs.startup.newDoc    = startup.opt1.value;
			prefs.startup.oldDocs   = startup.opt2.value;
			prefs.startup.noDocs    = startup.opt3.value;
			prefs.autolaunch        = autolaunch.value;
		    
			if( locale.list.selection )
			{
				nextTimeNeeded = (prefs.locale != locale.list.selection.locale);
				prefs.locale = locale.list.selection.locale;
			}
		}
	}
	return nextTimeNeeded;
}
