// (c) Copyright 2004.  Adobe Systems, Incorporated.  All rights reserved.

// This JavaScript is to be read by Bridge, Photoshop, and other Adobe apps at
// launch. It generally exposes a larger Bridge dom to the other apps.

// debug level: 0-2 (0:disable, 1:break on error, 2:break at beginning)
$.level = 0;
// debugger; // launch debugger on next line



//=================================================================
// Setup/Support
// This first portion of the script sets up an object to provide
// scope for all Bridge BridgeTalk related routines to prevent
// name collision with other groups' scripts, and defines some common
// utility functions.
//=================================================================

var bridge = new Object;

//-----------------------------------------------------------------
// This routine tries to return an array of File objects created
// from the 'files' argument. It will convert a single File object
// or a single string into an array of File objects, or it will
// convert an array of string and File objects into an array of
// file objects.
//-----------------------------------------------------------------
bridge.ExtractFileArray = function (files)
{
	var fileArray = new Array;
	
	// If it isn't an array, make it a length one array.
	if (!(files instanceof Array))
		files = new Array (files);
	
	// Turn each item in the array into a File, or remove it.
	for (index = 0; index < files.length; ++index)
	{
		var file = files[index];
		
		if (file instanceof File)
			fileArray.push (file);
		else if (typeof file == 'string')
			fileArray.push (File (file));
		else
		{
			// do nothing
		}
	}
	
	return fileArray;
}

bridge.convertPathStrToFile = function( file )
{
	return File( file );
}

//=================================================================
// CrossDOM/X-DOM
// Implements Bridge's cross DOM API - the a small set of
// operations that are common to all Adobe apps.
//=================================================================


//-----------------------------------------------------------------
// executeScript (script) - Performs an "eval" on the given script.
//
// return value		undefined
//
// script			String		The script to be evaled
//-----------------------------------------------------------------

bridge.executeScript = function( script )
{
	if( BridgeTalk.appName != "bridge" )
	{
		// Bring Bridge to front if already running
		BridgeTalk.bringToFront( "bridge" );

		var bt = new BridgeTalk;
		bt.target = "bridge";
		bt.body = script;
		bt.send();
	}
	else
	{
		// Bring Bridge to front if already running
		app.bringToFront();
		eval( script );
	}
}

//-----------------------------------------------------------------
// open (files) - Performs the equivalent of File->New Window on the
// requested paths. Accepts either an Array object or a single
// path.
//
// return value 	undefined
//			
// files			File or Array of File	files to display in new Window
//-----------------------------------------------------------------

bridge.open = function( files )
{
	if( BridgeTalk.appName == "bridge" )
	{		
		var fileArray = bridge.ExtractFileArray ( files ); 
		
		for ( index = 0; index < fileArray.length; ++index )
		{
			var file = fileArray[index];
			
			app.browseTo( file );
		}
		
		app.bringToFront();
	}
	else
	{
		var fileArray = bridge.ExtractFileArray ( files );
		
		var bt = new BridgeTalk;
		bt.target = "bridge";
		bt.body = "bridge.open( " + fileArray.toSource() + " );";
		bt.send();
	}
}

//-----------------------------------------------------------------
// openAsNew ([creation-options]*) - Performs the equivalent of
// File->New Window. The creation-options are app-specific and should
// ideally map on to the app's new() function. Bridge has no creation
// options. Opens a new Bridge Window to the default path.
//
// return value		undefined
//-----------------------------------------------------------------

bridge.openAsNew = function()
{
	if( BridgeTalk.appName == "bridge" )
	{
		if( app.documents.length == 0 )
			app.browseTo();
		else
			app.browseTo( app.document.thumbnail.path );

		app.bringToFront();
	}
	else
	{
		var bt = new BridgeTalk;
		bt.target = "bridge";
		
		if( BridgeTalk.isRunning( "bridge" ) )
			bt.body = "bridge.openAsNew()";
		else
			bt.body = "app.document.thumbnail = new Thumbnail( app.document.thumbnail.path );";
		
		bt.send();	
	}
}

//-----------------------------------------------------------------
// print (files) - Performs the equivalent of File->Print on the
// requested files. Bridge has no such functionality.
//
// return value			undefined			
// files				File or Array of File	files to be printed
//-----------------------------------------------------------------

bridge.print = function( files )
{
	//do nothing. Bridge does not print
}

//-----------------------------------------------------------------
// reveal (file) - Gives the target app focus and brings the
// specified document to the foreground if it is already open.
// Scrolls the file into view in the Bridge content pane.
//
// return value		undefined
//
// file				File		file to be revealed
//-----------------------------------------------------------------

bridge.reveal = function( file )
{
	if( BridgeTalk.appName == "bridge" )
	{
		if( typeof file == "string" )
			file = bridge.convertPathStrToFile( file );
		
		var fileThumb = new Thumbnail( file );
			
		for( var i = 0; i < app.documents.length; i++ )
		{
			var thumbs = app.documents[i].thumbnail.children;
			
			if( thumbs.length > 0 )
				for( var j = 0; j < thumbs.length; j++ )
				{
					if( thumbs[j].spec.fsName == fileThumb.spec.fsName )
					{
						app.documents[i].bringToFront();
						app.documents[i].reveal( fileThumb );
					}
				}
		}
	}
	else
	{		
		if( typeof file == "string" )
			file = bridge.convertPathStrToFile( file );
			
		var bt = new BridgeTalk;
		bt.target = "bridge";
		bt.body = "bridge.reveal(" + file.toSource() + ");";
		bt.send();	
	}
}

//-----------------------------------------------------------------
// quit () - Performs the equivalent of File->Exit or Quit Bridge.
//
//	return value	undefined
//-----------------------------------------------------------------

bridge.quit = function()
{
	if( BridgeTalk.isRunning( "bridge" ) )
		bridge.executeScript( "app.quit();" );
}

