/**************************************************************************
*
*  @@@BUILDINFO@@@ 69helpMenu-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.
**************************************************************************/

///////////////////////////////////////////////////////////////////////////////
//
// help menu
//

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

// Add this setup to the late startup tasks.
addDelayedTask (setupHelpMenu);

function setupHelpMenu()
{
	setupHelpDocs();

	menus.help.updates = new MenuElement ("command", "$$$/ESToolkit/Menu/Help/Updates=&Updates...",
										  "---at the end of help", "help/updates");
	menus.help.updates.onDisplay = function()
	{
	    this.enabled = app.isUpdaterInstalled && app.isUpdaterEnabled;
	}
	
	menus.help.updates.onSelect = function()
	{
	    app.checkForUpdates();
	}

	menus.help.about = new MenuElement ("command", 
						localize ("$$$/ESToolkit/Menu/Help/About=&About %1...", app.title), 
								  "at the end of help", "help/about");

	menus.help.about.onSelect = function()
	{
		app.showAboutBox();
	}

	OMV.setup();
}

//-----------------------------------------------------------------------------
// 
// setupHelpDocs(...)
// 
// Purpose: Populate help menu with PDF files of the application folder (or sub folder)
// 
//-----------------------------------------------------------------------------

function setupHelpDocs()
{
    var startFolder = Folder.appPackage;
    
    if( !startFolder )
        return;
    
    if( !_win )
        startFolder = startFolder.parent;
        
    var tree = [];

    //
    // create file info tree 
    //
    iterateHelpDocs( startFolder, tree, true );
    
    //
    // populate help menu
    //
    populateHelpDocs( tree, 'help', true );

    //
    // find PDF files in given folder
    //
    function iterateHelpDocs( folder, info, recursive )
    {
        if( folder instanceof Folder )
        {
            var subFolders = folder.getFiles();
            
            for( var i=0; i<subFolders.length; i++ )
            {
		        if( subFolders[i] instanceof Folder && subFolders[i].name == 'SDK' )
		        {
					if( recursive )
					{
						var obj = { file : subFolders[i], sub : [] };
						info.push( obj );
						iterateHelpDocs( subFolders[i], obj.sub );
					}
		        }
		        else if( subFolders[i].name.lastIndexOf( '.pdf' ) == subFolders[i].name.length-4 )
		        {
		            info.push( { file : subFolders[i], sub : null } );
		        }
            }        
        }
    }

    //
    // populate help menu with PDF file info tree (recursive)
    //
    function populateHelpDocs( tree, parentMenuID, separator )
    {
        var item = null;
        
        for( var i=0; i<tree.length; i++ )
        {
            var location = 'at the end of ' + parentMenuID;
            
            if( separator )
                location = ( separator ? '---' : '' ) + location;

            if( tree[i].file instanceof Folder )
            {
                if( validatePDFFolder( tree[i] ) )
                {
					var menuID = parentMenuID + '/sub' + tree[i].file.name;
                    item = new MenuElement( "menu", tree[i].file.name, location, menuID );
                    separator = false;                    
                    populateHelpDocs( tree[i].sub, menuID, false );
                }
            }
            else
            {
				var menuID = parentMenuID + '/doc' + tree[i].file.name;
				
                var displayName = ( tree[i].file.displayName.length > 0 ? tree[i].file.displayName : decodeURI(tree[i].file.name) );

                if( displayName.lastIndexOf( '.pdf' ) == displayName.length-4 )
                    displayName = displayName.substr( 0, displayName.length-4 );
                    
                item = new MenuElement( "command", displayName, location, menuID );
                item.file = tree[i].file;
                item.disabled = false;
                separator = false;
                
                item.onDisplay = function()
                {
                    this.enabled = !this.disabled;
                }
                
                item.onSelect = function()
                {
                    this.disable = !this.file.execute();
                }
            }
        } 
    }

    function validatePDFFolder( obj )
    {
        var ret = false;
        
        if( obj.sub && obj.file instanceof Folder )
        {
            for( var i=0; i<obj.sub.length; i++ )
            {
                if( obj.sub[i].file instanceof Folder )
                    ret = validatePDFFolder( obj.sub[i] );
                else
                    return true;
            }
        }
        
        return ret;
    }
}