//------------------------------------------------------------------------------
//
// ADOBE SYSTEMS INCORPORATED
// Copyright 2010 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.
//
//------------------------------------------------------------------------------

/* @@@BUILDINFO@@@ devicecentral-ext.jsx 3.0.0 01-Feb-2010 */


// on localized builds we pull the $$$/Strings from a .dat file
$.localize = true;

var DeviceCentral = new Object();

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


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

//
// Add "Test in Device Central..." to File menu and in context menu
//
	
{
	DeviceCentral.testDCMenuItem = MenuElement.create("command", localize ("$$$/MobileCenter/ext/Menu/Test=Test in Device Central..."), "OpenInCameraRaw", "testDCFile");

	DeviceCentral.testDCMenuItem.onDisplay = function(m)
	{
		m.enabled = (typeof app.document != "undefined") && (app.document.selectionLength == 1) && (!app.document.selections[0].container) && (typeof app.document.selections[0].spec != "undefined");
	}
		
	DeviceCentral.testDCMenuItem.onSelect = function(menu)
	{
		try
		{
			DeviceCentral.sendScriptToDeviceCentral( 'app.activate(); app.emulate('+DeviceCentral.thumbnailsToFiles(app.document.selections)+');' );
		}
		catch (error)
		{
			if (error.number != 8007) // Don't report user cancelled errors.
				alert( localize("$$$/MobileCenter/ext/Menu/Error=Bridge command cannot be executed.") );
		}

	}
}


//
// Add "Device Central..." to Tools menu
//
	
{
	DeviceCentral.deviceCentralMenuItem = MenuElement.create("command", localize ("$$$/MobileCenter/ext/Menu/DeviceCentral=Device Central..."), "-after BatchRename", "goToDCTools");
		
	DeviceCentral.deviceCentralMenuItem.onSelect = function(menu)
	{
		try
		{
			DeviceCentral.sendScriptToDeviceCentral( "app.activate(); app.browse();" );
		}
		catch (error)
		{
			if (error.number != 8007) // Don't report user cancelled errors.
				alert( localize("$$$/MobileCenter/ext/Menu/Error=Bridge command cannot be executed.") );
		}
	}
}



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


//---------------------------------------------------------
// sendScriptToDeviceCentral -
//
// Description:   Send script to be evaluated in DeviceCentral
//

DeviceCentral.sendScriptToDeviceCentral = function( code )
{
	if (app.name == 'devicecentral' )
	{	// Just execute the code if we are already in DeviceCentral
		return eval(code);
	} else {
		// Create a new BridgeTalk message and send to DeviceCentral
		var btMessage = new BridgeTalk;
		btMessage.target = 'devicecentral';
		btMessage.body = code;
		btMessage.send();
	}
}



//---------------------------------------------------------
// thumbnailsToFiles -  
//
// Description:
//	Convert a bridge thumbnails list to file references and preflight Version Cue files
//

DeviceCentral.thumbnailsToFiles = function ( thumbList )
{
	var outFileList = new Array();
	var outThumbList = new Array();

	for( var index=0; index<thumbList.length; index++ )
	{
		if (!thumbList[index].container) /* excludes containers */
		{
			outThumbList.push(thumbList[index]);
			outFileList.push('"'+thumbList[index].spec.fsName+'"');
		}
	}
	app.acquirePhysicalFiles(outThumbList);

	return outFileList.join(',');
}

