/**************************************************************************
*
*  @@@BUILDINFO@@@ 01startup-2.jsx 2.0.1.75  16-June-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;

// This global variable reflects whether we were launched by another application (via IDEBackend::launchIDE) or not

var remoteLaunched = 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;
	remoteLaunched      = remote;

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

    // 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");
	this.currentFolder = String (f.exists ? f : f.parent);
    
	if( !shift )
	{
        app.loadPrefs();
        scintillaPrefs.load();
        favorites.readPrefs();
    }
    
    this.createConsole		( "after:'none'", shift, remoteLaunched );
    this.createBreakpoints	( "below:'console'", shift, remoteLaunched );
    this.createCallStack	( "after:'breakpoints'", shift, remoteLaunched );
    this.createScripts		( "before:'breakpoints'", shift, remoteLaunched );
    this.createDataBrowser	( "below:'scripts'", shift, remoteLaunched );
    this.createFR           ( "after:'none'", shift, remoteLaunched );
    this.createFResult      ( "after:'findAndReplace'", shift, remoteLaunched );
    
    // init targets
    targetMgr.loadTargets();

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

    // start checking targets
    this.scheduleTargetChecker();

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

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

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

    return true;
}

app.showMainWindow = function()
{
    if( Folder.fs == 'Windows' && workspace.mainWindow && !workspace.mainWindow.visible )
        workspace.mainWindow.show();
}

app.initWorkspace = function()
{
    app.showMainWindow();

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

    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();
        }
    }
}
