/**************************************************************************
*
*  @@@BUILDINFO@@@ 61fileMenu-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.
**************************************************************************/

/////////////////////////////////////////////////////////////////////////////
// The File menu

// Set up the File menu.

menus.file = new MenuElement ("menu", "$$$/ESToolkit/Menu/File=&File",
								 "at the end of menubar", "file");

addDelayedTask (setupFileMenu);

function setupFileMenu()
{
	///////////////////////////////////////////////////////////////////////////////

	menus.file.newFile = new MenuElement ("command", "$$$/ESToolkit/Menu/File/New=&New JavaScript",
									 "at the beginning of file", "file/new");

	menus.file.newFile.onSelect = function()
	{
		Document.create();
	}

	///////////////////////////////////////////////////////////////////////////////

	menus.file.open = new MenuElement ("command", "$$$/ESToolkit/Menu/File/Open=&Open...",
									   "at the end of file", "file/open");

	menus.file.open.onSelect = function()
	{
		app.open();
	}

	///////////////////////////////////////////////////////////////////////////////

	// The Recent Files menu
	// This menu holds as many as 9 entries. The entries are part of the this menu's
	// elements array. The menu needs to be set up as the number of entries grow.

	menus.file.recent = new MenuElement ("menu", "$$$/ESToolkit/Menu/File/Recent=Recent &Files",
											  "at the end of file", "file/recent");

	// the elements of this array are objects with { file:uri, langID:langID }

	menus.file.recent.elements = [];

	menus.file.recent.update = function()
	{
		// Cannot use "this", since we call this from a dealyed task
		var self = menus.file.recent;
		// Discard the Recent menu contents
		for (var i = 1; i <= 10; i++)
		{
			var cmdID = "file/recent/" + i;
			var element = MenuElement.find (cmdID);
			if (element)
				element.remove();
		}

		// Strip the own user folder
		var userFolder = File ('~').fsName;

		for (i = 0; i < self.elements.length; i++)
		{
			var obj = self.elements [i];
			var cmdID = "file/recent/" + (i + 1);
			var text = "";
			if ($.os.indexOf ("Win" >= 0))
				// Windows: start with an index
				text = "&" + (i+1) + " ";
			var name = decodeURIComponent (File (obj.file).fsName);
			if (name.indexOf (userFolder) == 0)
				name = "~" + name.substr (userFolder.length);
			if (name.length > 200)
				name = "..." + name.substr (name.length-197);
			// Make sure that ampersands are displayed correctly
			name.replace (/&/, "&&");
			text += name;
			element = new MenuElement ("command", text, "at the end of file/recent", cmdID);
			element.file = obj.file;
			element.index = i;
			element.enabled = true;
			element.onSelect = function()
			{
				// Move the entry to the top
				obj = menus.file.recent.elements [this.index];
				menus.file.recent.elements.splice (this.index, 1);
				menus.file.recent.elements.unshift (obj);

				var f = new File (this.file);
				if (Document.load (f) == null)
				{
					// Remove from menu
					for (var i = 0; i < menus.file.recent.elements.length; i++)
					{
						if (menus.file.recent.elements [i].file == f.absoluteURI)
						{
							menus.file.recent.elements.splice (i, 1);
							break;
						}
					}
				}
				// Reflect in menu, but please after we leave!
				addDelayedTask (menus.file.recent.update);
			}
		}
	}

	menus.file.recent.add = function (path, langID)
	{
		path = path.toString();
		for (var i = 0; i < this.elements.length; i++)
		{
			if (this.elements [i].file == path)
			{
				// remove if present
				this.elements.splice (i, 1);
				break;
			}
		}
		if (this.elements.length == 9)
			this.elements.pop();
		// insert the new name at the top
		this.elements.splice (0, 0, { file:path, langID:langID } );
		// update the Recent Files menu, delayed
		addDelayedTask (this.update);
	}

	menus.file.recent.onDisplay = function()
	{
		this.enabled = (this.elements.length > 0);
	}

	menus.file.recent.loadPrefs = function()
	{
		this.elements = [];
		var count = prefs.recent.files.length;
		if (count == "")
			count = 0;
		if (count)
		{
			for (var i = 0; i < count; i++)
			    this.add( prefs.recent.files[i], prefs.recent.langIDs[i] );
				
			// update the Recent Files menu
		    this.update();
		}
	}

	menus.file.recent.writePrefs = function()
	{
		prefs.recent.files.length = this.elements.length;
		for (var i = 0; i < this.elements.length; i++)
		{
			prefs.recent.files [i] = this.elements [i].file;
			prefs.recent.langIDs [i] = this.elements [i].langID;
		}
	}

	menus.file.recent.onNotify = function( reason, shift )
	{
		if( reason == 'shutdown' )
		{
		    globalBroadcaster.unregisterClient( this );
		    
		    if( !shift )
			    this.writePrefs();
		}
	}

	///////////////////////////////////////////////////////////////////////////////

	menus.file.save = new MenuElement ("command", "$$$/ESToolkit/Menu/File/Save=&Save",
									   "at the end of file", "file/save");

	menus.file.save.onDisplay = function()
	{
		this.enabled = document && document.isModified() && document.window == workspace.activeDocument;
	}

	menus.file.save.onSelect = function()
	{
		if (document)
			document.save();
	}

	///////////////////////////////////////////////////////////////////////////////

	menus.file.saveAs = new MenuElement ("command", "$$$/ESToolkit/Menu/File/SaveAs=Save &As...",
										 "at the end of file", "file/saveAs");

	menus.file.saveAs.onDisplay = function()
	{
		this.enabled = document && document.window == workspace.activeDocument;
	}

	menus.file.saveAs.onSelect = function()
	{
		if (document)
			document.save (true);
	}

	///////////////////////////////////////////////////////////////////////////////

	menus.file.saveAll = new MenuElement ("command", "$$$/ESToolkit/Menu/File/SaveAll=Save A&ll",
										  "at the end of file", "file/saveAll");

	menus.file.saveAll.onSelect = function()
	{
		Document.saveAll();
	}

	///////////////////////////////////////////////////////////////////////////////

	menus.file.exportAs = new MenuElement ("command", "$$$/ESToolkit/Menu/File/ExportAs=&Export as Binary...",
										   "---at the end of file", "file/exportAs");

	menus.file.exportAs.onDisplay = function()
	{
		this.enabled = document && (document.langID == "js") && (document.window == workspace.activeDocument);
	}

	menus.file.exportAs.onSelect = function()
	{
		if (document)
			document.exportAsBinary();
	}

	///////////////////////////////////////////////////////////////////////////////

	menus.file.close = new MenuElement ("command", "$$$/ESToolkit/Menu/File/Close=&Close",
										"---at the end of file", "file/close");

	menus.file.close.onDisplay = function()
	{
		this.enabled = document && (document.window == workspace.activeDocument);
	}

	menus.file.close.onSelect = function()
	{
		if (document)
			document.close();
	}

	///////////////////////////////////////////////////////////////////////////////

	menus.file.closeAll = new MenuElement ("command", "$$$/ESToolkit/Menu/File/CloseAll=Close All",
										   "at the end of file", "file/closeAll");

	menus.file.closeAll.onDisplay = function()
	{
		this.enabled = (documents.length > 0);
	}

	menus.file.closeAll.onSelect = function()
	{
	    var tmp = [];
	    
	    for( var i=0; i<documents.length; i++ )
	        tmp.push( documents[i] );
	    
	    for( i=0; i<tmp.length; i++ )
	        tmp[i].close();
	}

	///////////////////////////////////////////////////////////////////////////////

	menus.file.page = new MenuElement ("command", "$$$/ESToolkit/Menu/File/PageSetup=Page Set&up...",
										"---at the end of file", "file/page");

	menus.file.print = new MenuElement ("command", "$$$/ESToolkit/Menu/File/Print=&Print...",
										   "at the end of file", "file/print");

	menus.file.page.onDisplay = function()
	{
		this.enabled = (documents.length > 0 );
	}
	menus.file.print.onDisplay = function()
	{
		this.enabled = document && (document.window == workspace.activeDocument);
	}

	menus.file.page.onSelect = function()
	{
		app.print.pageSetup();
	}

	menus.file.print.onSelect = function()
	{
		if( app.print.printSettings( document.textselection.length > 0 ) )
		{
			if( !document.print( app.print ) )
				errorBox( localize( "$$$/ESToolkit/Error/Print=Can't print %1", document.scriptID ) );
		}
	}

	///////////////////////////////////////////////////////////////////////////////

	var itemText = '';
	
	if( _win )
		itemText = localize( "$$$/ESToolkit/Menu/File/Exit=E&xit" );
	else
		itemText = localize( "$$$/ESToolkit/Menu/File/Quit=Quit %1", app.name );
		
	menus.file.quit = new MenuElement ("command", itemText, "---at the end of file", "file/quit" );

	menus.file.quit.onSelect = function()
	{
		app.quit();
	}

	// Finally, load the Recent Files from prefs
    if( startupPrefsLoaded )
		menus.file.recent.loadPrefs();

    globalBroadcaster.registerClient( menus.file.recent );
}
