//------------------------------------------------------------------------------
//
// ADOBE SYSTEMS INCORPORATED
// Copyright 2005 Adobe Systems Incorporated 
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute 
// this file in accordance with the terms of the Adobe license 
// agreement accompanying it. If you have received this file 
// from a source other than Adobe, then your use, modification, 
// or distribution of it requires the prior written permission 
// of Adobe.
//
//------------------------------------------------------------------------------

/*
@@@START_XML@@@
<?xml version="1.0" encoding="UTF-8"?>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="en_US">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>This script enables other applications to communicate with Adobe Soundbooth CS5.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="fr_FR">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>Ce script permet à d'autres applications de communiquer avec Adobe Soundbooth CS5.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="ja_JP">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>このスクリプトは、他のアプリケーションと Adobe Soundbooth CS5 との通信を有効にします。</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="de_DE">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>Mithilfe dieses Skripts können andere Anwendungen mit Adobe Soundbooth CS5 kommunizieren.</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="it_IT">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>Questo script consente ad altre applicazioni di comunicare con Adobe Soundbooth CS5</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="es_ES">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>Este script posibilita que otras aplicaciones se comuniquen con Adobe Soundbooth CS5</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="nl_NL">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>Dit script laat andere toepassingen toe te communiceren met Adobe Soundbooth CS5</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="pt_BR">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>Este script permite que outros aplicativos se comuniquem com o Adobe Soundbooth CS5</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="nb_NO">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>Skriptet gjør at andre programmer kan kommunisere med Adobe Soundbooth CS5</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="da_DA">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>Dette script betyder, at andre programmer kan kommunikere med Adobe Soundbooth CS5</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="fi_FI">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>Tämän komentosarjan avulla muut sovellukset ja Adobe Soundbooth CS5 voivat kommunikoida keskenään</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="se_SE">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>Det här skriptet gör det möjligt för andra program att kommunicera med Adobe Soundbooth CS5</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="zh_TW">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>"此指令碼能讓其他應用程式與 Adobe Soundbooth CS5 進行通訊。</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="zh_CN">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>"此脚本使其它应用程序能够与 Adobe Soundbooth CS5 进行通信</dc:description>
</ScriptInfo>
<ScriptInfo xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="ko_KO">
     <dc:title>Adobe Soundbooth CS5</dc:title>
     <dc:description>"이 스크립트를 사용하면 다른 응용 프로그램에서 Adobe Soundbooth CS5과(와) 통신할 수 있습니다.</dc:description>
</ScriptInfo>
@@@END_XML@@@
*/

//--------------------------------------------------------------------
// SOUNDBOOTH COMMON INTERFACE
//--------------------------------------------------------------------


var Soundbooth				= new Object();
var soundbooth				= Soundbooth;	// Also support
var soundbooth_identifier	= "soundbooth-3.0"

/**
**	Open Event
**
**	The event is:
**		type - document
**		location - document
**		object - document
**
**	To get the selection use object.selections.
**
*/
Soundbooth.eventHandler = function(event)
{
	var retval = {};
	retval.handled = false;

	if ( event.type == "open" &&
		event.location == "document" &&
		event.object.constructor.name == "Document" &&
		app.document.owner == soundbooth_identifier)
	{
		var sels = event.object.selections;
		var files = [];
		
		for (var i = 0; i < sels.length; i++)
		{
			files[i] = sels[i].spec;
		}

		Soundbooth.open(files);
		retval.handled = true;
	}

	return retval;
}

if(BridgeTalk.appName == "bridge")
{
	app.eventHandlers.push( { handler:Soundbooth.eventHandler } );
}

//---------------------------------------------------------
// quit -  If open, Quit the Soundbooth application
//

Soundbooth.quit = function ( )
{
	if (BridgeTalk.isRunning('soundbooth')) // First check if Soundbooth is open 
	{
		Soundbooth.sendScriptToSoundbooth( 'app.quit();' );
	}
}


//---------------------------------------------------------
// activate -  Bring Soundbooth to frontmost
//

Soundbooth.activate = function ( )
{
	BridgeTalk.bringToFront(soundbooth_identifier);
}


//---------------------------------------------------------
// open - Open list of files or sites
//

Soundbooth.open = function ( files )
{
	Soundbooth.activate();
	
	for (var index = 0; index < files.length; index++)
	{
		Soundbooth.sendScriptToSoundbooth( 'app.open("'+files[index].fsName+'");');
	}
}

//---------------------------------------------------------
// performMatchVolume - Performs a match volume operation on given list of files using a normalized method and gain
//

Soundbooth.performMatchVolume = function ( files, matchToFile, method, gain, saveDirtyFiles, showResult )
{
	Soundbooth.activate();
	Soundbooth.sendScriptToSoundbooth( 'app.performMatchVolume('+files+', '+matchToFile+', '+method+', '+gain+', '+saveDirtyFiles+', '+showResult+');');
}

//---------------------------------------------------------
// requestMatchVolume - Requests a match volume operation on given list of files
//

Soundbooth.requestMatchVolume = function ( files )
{
	Soundbooth.activate();
	Soundbooth.sendScriptToSoundbooth( 'app.requestMatchVolume('+files+');');
}

//---------------------------------------------------------
// performEqualizeVolume - Performs an equalize volume operation on given file
//

Soundbooth.performEqualizeVolume = function ( file, saveDirtyFile )
{
	Soundbooth.activate();
	Soundbooth.sendScriptToSoundbooth( 'app.performEqualizeVolume('+file+', '+saveDirtyFile+');');
}

//--------------------------------------------------------------------
// BRIDGE UI
//--------------------------------------------------------------------


//---------------------------------------------------------
// Create Bridge Specific UI
//

if( BridgeTalk.appName == 'bridge' )
{
	//
	// Place In Soundbooth menu command
	//
	soundbooth.placeInSoundboothMenuItem = MenuElement.create( 'command', 
		localize("$$$/extendscript/soundbooth/PlaceInSoundbooth=In Soundbooth"), 
		'at the end of submenu/Place', 'PlaceInSoundbooth' );
	
	soundbooth.placeInSoundboothMenuItem.onSelect = function()
	{
		// Get the files, may be remote/version cue
		app.preflightFiles( app.document.selections );
		Soundbooth.activate();

		// Process file types and only send those files
		Soundbooth.sendScriptToSoundbooth( 'app.open('+Soundbooth.thumbnailsToFiles(app.document.selections)+');' );

	}
   
	soundbooth.placeInSoundboothMenuItem.onDisplay = function()
	{
		if( BridgeTalk.isRunning(soundbooth_identifier) && app.document.selections.length > 0 )
		{
			if( app.document.selections.length > 0 )
			{
				// You can put almost anything into a site or file
				if( true )	// If there was filtering, this is where it would go
				{
					this.enabled = true;
				}
			}
		}
	}
	
	//
	// Equalize Volume in Soundbooth menu command
	//
	soundbooth.equalizeVolumeInSoundboothMenuItem =  MenuElement.create( 'command', 
		localize("$$$/extendscript/soundbooth/EqualizeVolumeInSoundbooth=Equalize Volume in Soundbooth..."), 
		'-after RevealInBridge', 'EqualizeVolumeInSoundbooth' );

	soundbooth.equalizeVolumeInSoundboothMenuItem.onSelect = function()
	{
		// Get the files, may be remote/version cue
		app.preflightFiles( app.document.selections );
		Soundbooth.activate();

		// Process file types and only send those files
		for( var i=0; i<app.document.selectionLength; i++ )
		{
			if (!app.document.selections[i].container) // exclude folders
				Soundbooth.sendScriptToSoundbooth( 'app.performEqualizeVolume('+Soundbooth.getThumbnailAsFile(app.document.selections, i)+', false);');
		}
	}
   
	soundbooth.equalizeVolumeInSoundboothMenuItem.onDisplay = function(m)
	{
		m.enabled = (typeof app.document != "undefined") && (app.document.selectionLength > 0);
	}

	soundbooth.equalizeVolumeInSoundboothContextMenuItem =  MenuElement.create( 'command', 
		localize("$$$/extendscript/soundbooth/EqualizeVolumeInSoundbooth=Equalize Volume in Soundbooth..."), 
		'-after Thumbnail/RevealLocation', 'EqualizeVolumeInSoundboothContextMenu' );

	soundbooth.equalizeVolumeInSoundboothContextMenuItem.onSelect = function()
	{
		// Get the files, may be remote/version cue
		app.preflightFiles( app.document.selections );
		Soundbooth.activate();

		// Process file types and only send those files
		for( var i=0; i<app.document.selectionLength; i++ )
		{
			if (!app.document.selections[i].container) // exclude folders
				Soundbooth.sendScriptToSoundbooth( 'app.performEqualizeVolume('+Soundbooth.getThumbnailAsFile(app.document.selections, i)+', false);');
		}
	}
   
	soundbooth.equalizeVolumeInSoundboothContextMenuItem.onDisplay = function(m)
	{
		m.enabled = (typeof app.document != "undefined") && (app.document.selectionLength > 0);
	}

	//
	// Match Volume in Soundbooth menu command
	//
	soundbooth.matchVolumeInSoundboothMenuItem =  MenuElement.create( 'command', 
		localize("$$$/extendscript/soundbooth/MatchVolumeInSoundbooth=Match Volume in Soundbooth..."), 
		'after EqualizeVolumeInSoundbooth', 'MatchVolumeInSoundbooth' );
	

	soundbooth.matchVolumeInSoundboothMenuItem.onSelect = function()
	{
		// Get the files, may be remote/version cue
		app.preflightFiles( app.document.selections );
		Soundbooth.activate();

		// Process file types and only send those files
		for( var i=0; i<app.document.selectionLength; i++ )
		{
			if (!app.document.selections[i].container) // exclude folders
				Soundbooth.sendScriptToSoundbooth( 'app.requestMatchVolume('+Soundbooth.getThumbnailAsFile(app.document.selections, i)+');');
		}
	}
   
	soundbooth.matchVolumeInSoundboothMenuItem.onDisplay = function(m)
	{
		m.enabled = (typeof app.document != "undefined") && (app.document.selectionLength > 1);
	}
	
	soundbooth.matchVolumeInSoundboothContextMenuItem =  MenuElement.create( 'command', 
		localize("$$$/extendscript/soundbooth/MatchVolumeInSoundbooth=Match Volume in Soundbooth..."), 
		'after EqualizeVolumeInSoundboothContextMenu-', 'MatchVolumeInSoundboothContextMenu' );
	

	soundbooth.matchVolumeInSoundboothContextMenuItem.onSelect = function()
	{
		// Get the files, may be remote/version cue
		app.preflightFiles( app.document.selections );
		Soundbooth.activate();

		// Process file types and only send those files
		for( var i=0; i<app.document.selectionLength; i++ )
		{
			if (!app.document.selections[i].container) // exclude folders
				Soundbooth.sendScriptToSoundbooth( 'app.requestMatchVolume('+Soundbooth.getThumbnailAsFile(app.document.selections, i)+');');
		}
	}
   
	soundbooth.matchVolumeInSoundboothContextMenuItem.onDisplay = function(m)
	{
		m.enabled = (typeof app.document != "undefined") && (app.document.selectionLength > 1);
	}
} // End of Bridge specific code


//--------------------------------------------------------------------
// UTILITIES
//--------------------------------------------------------------------


//---------------------------------------------------------
// sendScriptToSoundbooth - Send script to be evaluated in Soundbooth
//
// Description:
//

Soundbooth.sendScriptToSoundbooth = function( code )
{
	if (app.name == soundbooth_identifier )
	{	// Just execute the code if we are already in Soundbooth
		return eval(code);
	} else {
		// Create a new BridgeTalk message for Soundbooth to invoke Quit
		var btMessage = new BridgeTalk;
		btMessage.target = soundbooth_identifier;
		btMessage.body = code;
		btMessage.send();
	}
}


//---------------------------------------------------------
// argsToString -  
//
// Description:
//	This routine create a string for the arguments that we can transmit
//	over BridgeTalk as text.
//

Soundbooth.argsToString = function ( args )
{
	var outArgs = new Array();
	
	for( var index=0; index<args.length; index++ )
	{
		outArgs.push(args[index].toSource());
	}
		
	return outArgs.join(',');
}


//---------------------------------------------------------
// thumbnailsToFiles -  
//
// Description:
//	Convert a bridge thumbnails list to file references.
//

Soundbooth.thumbnailsToFiles = function ( thumbList )
{
	var outFileList = new Array();
	
	for( var index=0; index<thumbList.length; index++ )
	{
		outFileList.push('"'+thumbList[index].spec.fsName+'"');
	}
	return outFileList.join(',');
}

//---------------------------------------------------------
// getThumbnailAsFile -  
//
// Description:
//	Convert a bridge thumbnail to file references.
//

Soundbooth.getThumbnailAsFile = function ( thumbList, index )
{
	var outFileList = new Array();
	outFileList.push('"'+thumbList[index].spec.fsName+'"');
	return outFileList;
}
