/**************************************************************************
*
*  @@@BUILDINFO@@@ 01startup-2.jsx 2.0.0.54  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.
**************************************************************************/

// This global variable reflects whether prefs have been loaded or not during startup.

var startupPrefsLoaded = false;

// A method to load the menu keys from the menuKeys variable

menus.loadKeys = function()
{
    this.loadShortcutKeys( menus, '' );
}

menus.loadShortcutKeys = function( menu, keyName )
{
    for( var name in menu )
    {
        if( menu[name] instanceof MenuElement )
        {
            if( menu[name].type == 'command' )
                menus.setShortcutKey( menu[name] );
            else
                menus.loadShortcutKeys( menu[name], ( keyName.length > 0 ? keyName + '.' + name : name ) );
        }
    }
}

menus.setShortcutKey = function( item )
{
    var prefsKey = 'prefs.shortcutkeys.' + item.id.replace( /\//g, '_' );
    
    if( !PrefUtils.hasValue( prefsKey, 'String' ) )
        prefsKey += '.' + ( $.os.indexOf( 'Windows' ) > -1 ? 'win' : 'mac' );
    
    var shortcutKey =  '';
    
    if( PrefUtils.hasValue( prefsKey, 'String' ) )
    {
		shortcutKey =  PrefUtils.getValue( prefsKey, 'String' );
		
		if( shortcutKey == 'none' )
			shortcutKey = '';
    }
    
    if( shortcutKey.length > 0 || item.shortcutKey.length > 0 )
        item.shortcutKey = shortcutKey;
}

///////////////////////////////////////////////////////////////////////////////
//
// check targets
//

app.scheduleTargetChecker = function( seconds )
{
    if( !seconds )
        seconds = 2;

    this.checkTargetsTaskDelay = seconds;
    
    if( this.checkTargetsTaskID )
        this.cancelTask( this.checkTargetsTaskID );

    this.checkTargetsTaskID = this.scheduleTask( "app.suspendTargetChecker();targetMgr.checkConnections();app.resumeTargetChecker();", seconds * 1000, false );
}

app.suspendTargetChecker = function()
{
    if( this.checkTargetsTaskID )
        this.cancelTask( this.checkTargetsTaskID );
}

app.resumeTargetChecker = function()
{
    this.scheduleTargetChecker( this.checkTargetsTaskDelay );
}

///////////////////////////////////////////////////////////////////////////////
//
// startup
//

app.onStartup = function( shift, remote, docFile )
{
	startupPrefsLoaded = !shift;

    globalBroadcaster = new Broadcaster;
    globalBroadcaster.registerClient( app );

    if( $.os.indexOf( 'Windows' ) > -1 )
    {
        app.resourceFolder = Folder (Folder.appPackage + "/required");
        var f = app.resourceFolder.resolve();
        if (f)
            app.resourceFolder = f;
    
        app.requiredFolder = app.resourceFolder;
        app.resourceFolder += '/icons/';
    }
    else
    {
		app.requiredFolder = Folder.appPackage.absoluteURI + '/Contents/SharedSupport/Required/';
        app.resourceFolder = Folder.appPackage.absoluteURI + '/Contents/Resources/';
	}

    // initialize the lang object
    lang.initialize();
	scintillaPrefs.init();
	
    targetMgr = new TargetManager();
    favorites = new Favorites();

	// Set the current folder to Adobe Scripts
	var f = Folder (Folder.myDocuments + "/Adobe Scripts");
	/* created in 06favorites.jsx
	if (!f.exists)
		f.create();
	*/
	this.currentFolder = String (f.exists ? f : f.parent);
    
	if( !shift )
	{
        app.loadPrefs();
        scintillaPrefs.load();
        favorites.readPrefs();
    }
    
    this.createConsole		( "after:'none'", shift, remote );
    this.createBreakpoints	( "below:'console'", shift, remote );
    this.createCallStack	( "after:'breakpoints'", shift, remote );
    this.createScripts		( "before:'breakpoints'", shift, remote );
    this.createDataBrowser	( "below:'scripts'", shift, remote );
    this.createFR           ( "after:'none'", shift, remote );
    this.createFResult      ( "after:'findAndReplace'", shift, remote );
    
    // init targets
    targetMgr.loadTargets();

    // set up the debugging subsystem
    setupDebugger( remote );

    // start checking targets
    this.scheduleTargetChecker();

    // open initial document when the UI is up and running
    if( docFile )
        Document.load( docFile );
    else if( !remote )
		addDelayedTask (app.initWorkspace);

    // workspace visibility
    workspaceVisible = !PrefUtils.getValue( 'prefs.startup.hideWS', 'Boolean' );
    
    if( !workspaceVisible )
    {
        workspace.togglePalettes();
    }

    globalBroadcaster.notifyClients( 'startup', shift, remote );

	// start running any late startup tasks that have been scheduled
	// during startup
	startDelayedTasks();

    return true;
}

app.initWorkspace = function()
{
    if( !PrefUtils.getValue( 'prefs.startup.noDocs', 'Boolean' ) )
    {
        if( PrefUtils.getValue( 'prefs.startup.newDoc', 'Boolean' ) || !startupPrefsLoaded )
            Document.create();
        else if( PrefUtils.getValue( 'prefs.startup.oldDocs', 'Boolean' ) )
        {
            if( !Document.loadPrefs() )
                Document.create();
        }
    }
}
