/**************************************************************************
*
*  @@@BUILDINFO@@@ 21Console-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.
**************************************************************************/

console = null;

app.createConsole = function (where)
{
	console = new Window( 
		"dockpalette {													    \
			text:'$$$/ESToolkit/Panes/Console/Title=JavaScript Console',	\
			properties: {													\
			    flyoutmenu:true,											\
			    defaultVisibility : true,									\
				name:'console',												\
				icon : '#PConsole_N',	\
				rollover : '#PConsole_R',"
			 +  where
			 +  "},															\
			preferredSize: [100, 100],										\
			id: 'console',													\
			margins:2,														\
            spacing      : 2,                                       \
			orientation : 'column',                                         \
			output: ConsoleEdit                                             \
			{											                    \
				properties: { multiline: true },							\
				preferredSize: [10, 20],									\
				alignment: ['fill', 'fill' ]                                \
			},						                                \
            infogrp : Group                                         \
            {                                                       \
                orientation : 'row',                                \
                margins      : 0,                                   \
                spacing      : 0,                                   \
                alignment	: ['fill','bottom'],					\
                targetField: StaticText                             \
                {								                    \
                    alignment	: ['fill','bottom'],				\
                    characters : 40                                 \
                },													\
                sizeBoxSpacer    : Group                            \
                {                                                   \
                    alignment	: ['right','bottom'],				\
                    preferredSize        : [14,1]                   \
                }                                                   \
            }                                                       \
		}");
	
	console.menuText = localize ("$$$/ESToolkit/Menu/Window/Console/Title=&JavaScript Console");

	var item = new MenuElement( 'command', '$$$/ESToolkit/Panes/Console/Flyout/Clear=Clear', "at the end of console/flyout", "console/flyout/clear" )
	item.output = console.output;
	item.onSelect = function()
	{
	    this.output.text = '';
	}

	console.onResize = 
	console.onResizing = function ()
	{
		this.layout.resize();
	}
	
	console.onActivate = function()
	{
	    console.output.active = true;
	}

	// set to true by eval'ing the string "#debug", switch back with "#target"
	console.debug = false;
	
	console.output.onEnterKey = function()
	{
		var text = this.textselection;
		if (!text.length)
			return false;
            
		if (text[text.length-1] != "\n")
			text += "\n";
			
		if (!this.isCaretInLastLine())
			this.appendText (text);
		else
			this.appendText ("\n");
		
		this.parent.eval (text);

		this.active = true;
		return true;
	}

	panes.push (console);

	console.eval = function (script)
	{
		if (script == "#debug\n")
		{
			this.debug = true;
			print ("# Switching to debug engine #");
			return;
		}
		else if (script == "#target\n")
		{
			this.debug = false;
			print ("# Switching to target engine #");
			return;
		}
		var res;
		if (this.debug)
		{
			var oldLevel = $.level;
			$.level = 0;
			try
			{
				res = eval (script);
				this.print (localize ("$$$/ESToolkit/Panes/Console/Result=Result:") 
							+ " " + res);
			}
			catch (e)
			{
				this.print (localize ("$$$/ESToolkit/Panes/Console/Error=Error:") 
							+ " " + e.message);
			}
			$.level = oldLevel;
		}
		else
			currentDebugger.eval (script);
		return res;
	}

	console.write = function()
	{
		var s = "";
		for (var i = 0; i < arguments.length; i++)
			s += arguments [i];
		this.output.appendText (s);
	}
	
	console.print = function()
	{
		var s = "";
		for (var i = 0; i < arguments.length; i++)
			s += arguments [i];
		this.output.appendText (s + "\n");
	}
	
	console.onNotify = function( reason )
	{
	    switch( reason )
	    {
	        case 'shutdown':
	            globalBroadcaster.unregisterClient( this );
	            break;
	            
	        case 'changeActiveTarget':
	        case 'changeActiveEngine':
	        {
	            this.enabled = targetMgr.getActiveTarget().getConnected();
	            
	            if( this.enabled )
	            {
	                var targetName = BridgeTalk.getDisplayName( targetMgr.getActiveTarget().targetName )   ;
	                console.infogrp.targetField.text = ( targetName ? targetName : targetMgr.getActiveTarget().targetName );
	            }
	            else
	                console.infogrp.targetField.text = '';
            }
            break;
	    }
	}
    
    targetMgr.registerClient( console );
	console.show();
}
