<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
		<meta name="generator" content="Adobe GoLive"/>
		<title>Bridge Integration</title>
	</head>

	<body>
		<hr/>
		<p><b>ADOBE SYSTEMS INCORPORATED</b></p>
		<p>Copyright 2005 Adobe Systems Incorporated<br/>
			All Rights Reserved</p>
		<p>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.</p>
		<hr/>
		<p><b>Extension Name:</b> Bridge</p>
		<p><b>Folder name:</b> Bridge</p>
		<hr/>
		<p><b>Description:</b></p>
		<p>Integration features for the Suite Bridge application.</p>
		<hr/>
		<p><b>Module Definition:</b></p>
		<jsxmodule info="$$$/extendscript/Bridge/info=Implements Bridge integration features including Menus and Toolbar buttons." name="Bridge" version="1.0"/> 
		<p><b>Scripts:</b></p>
		<script><![CDATA[
//--------------------------------------------------------------------
// GLOBAL VARIABLES
//--------------------------------------------------------------------

var lastFolder = null;


//---------------------------------------------------------------
// MODULE CALLBACKS

//---------------------------------------------------------
// initializeModule - called after the module has been loaded
//
function initializeModule()
{
	// Save the last opened file location for bridge browse location
	if( typeof( prefs.Bridge_lastFolder ) != 'undefined' ) {
		lastFolder = new JSXFile( prefs.Bridge_lastFolder );
	}
	app.addEventListener( 'opened', 
		function(e) {
			if( e.document.type != 'markup' || ( e.document.type == 'markup' && e.document.history != null ) )
				if( e.document.file != null )
					lastFolder=e.document.file.parent;
			}
		);


	// Set this module as the reciever of BridgeTalk messages
	app.addEventListener ( 'btReceive',
		function( e )
		{
			e.response = eval( e.message.body, true );
			e.preventDefault();
		}
	); 
	

}




//---------------------------------------------------------
// terminateModule - called after the module has been unloaded
//
function terminateModule()
{
	if( lastFolder != null )
		prefs.Bridge_lastFolder = lastFolder.absurl;
}

//---------------------------------------------------------
// startModule - called after all modules are initialized
// 
function startModule()
{
	// If Bridge installed, create Browse to bridge menu and enable bridge toolbar buttons
	if( BridgeTalk.getSpecifier('bridge') != null )
	{
		menus['file'].items['btBrowse'].addEventListener('menuSignal', bridgeBrowse);
		menus['file'].items['btBrowse'].setShortCut('control+alt','o');
		dialogs['BridgeToolbar'].children['btBridge'].addEventListener( 'onClick', bridgeBrowse );
		dialogs['BridgeToolbar'].children['btStock'].addEventListener( 'onClick', openStockPhoto );
		
	}
	else	// Disable bridge toolbar buttons
	{
		dialogs['BridgeToolbar'].children['btBridge'].enabled = false;
		dialogs['BridgeToolbar'].children['btStock'].enabled = false;
	}
	
	
}


//---------------------------------------------------------------
// CUSTOM CALLBACKS


//---------------------------------------------------------
// bridgeBrowse - open bridge browser to last used folder
// 
function bridgeBrowse( evObj )
{
	var target = lastFolder;
	
	if (target)
	{
		target = lastFolder.abspath;
	} else {
		target = '';
	}
	
	var mod = app.getModifierState();
	
	var optionAltModState = ( app.getModifierState() & 4); // Mac-Option/Win-Alt

	var commandControlState = ( app.getModifierState() & 8 ); // Mac-Command/Win-Control


	var bt = new BridgeTalk;
	bt.target = 'bridge';
	bt.body = '';

	if( BridgeTalk.isRunning( 'bridge' ) )
	{
		// Mac-Option/Win-Alt
		if ( optionAltModState )
		{
			bt.body += "app.browseTo(folderTarget);";
		} else {
			bt.body +=	"if( app.documents.length == 0 )" +
						"	{" +
						"		app.browseTo(folderTarget);" +
						"	} else {" +
						"		app.document.thumbnail = new Thumbnail(folderTarget);" +
						"	}" +
						"";
		}
	
		bt.body += "BridgeTalk.bringToFront('bridge');";
	
		// Mac-Command/Win-Control
		if( commandControlState )
		{
	 		bt.body += "app.document.maximize();";
		}
	}
	else
	{
		BridgeTalk.bringToFront('bridge');
		bt.body += "app.document.thumbnail = new Thumbnail('" + target + "'); ";
	}
		 
	bt.send();
	BridgeTalk.bringToFront('bridge');
}

//---------------------------------------------------------
// openStockPhoto - handle StockPhoto button
// 
function openStockPhoto( evObj )
{
	var bt		= new BridgeTalk;
	bt.target	= 'bridge';

	// If Mac-Command/Win-Control maximize
	var commandControlKeyState = app.getModifierState() & 8; 

	if( BridgeTalk.isRunning( 'bridge' ) )
	{
		bt.body = "stockphoto.pointApp.displayStartPage(" + commandControlKeyState + ");";
	}
	else // Launching bridge, use first window for stockphoto page
	{	// Note the stockphoto.pointApp.displayStartPage should include an argument to
		//	use the current window if it exists and a 
		BridgeTalk.bringToFront('bridge');
	    bt.body = "if (app.document) app.document.thumbnail = new Thumbnail('stockphoto://home');";
		bt.body += "stockphoto.pointApp.displayStartPage(" + commandControlKeyState + ");";
	}

	bt.send();
	BridgeTalk.bringToFront("bridge");
}



//---------------------------------------------------------
// place -  insert files into active document or site
//
// Description: Place attempts to insert the specified files
//	into the current open file or site. If no file is open
//	this function returns null.
//

app.place = function ( firstFile ) /* argument list of n files */
{
	var defaultUnknownImageSize = [100,100];
	var defaultKnownImageSize = [200,200];
	
	var imageSizeAutoLimit = 1000; // settings.smartObject.autoLimit
	var imageAutoConvert = true; // settings.smartObject.autoConvert


	var placedRelativeURL = '';
	var targetDir = '';

	var imgSettings = new JSXImageSettings();
	imgSettings.outputSize = defaultUnknownImageSize;
	imgSettings.scaleMode = 'showAll';
	imgSettings.setJPEGFormat('quality', 80);
	imgSettings.setJPEGFormat('optimized', true);

	for( var i=0; i<arguments.length; i++ )
	{
		var placeFile = new JSXFile( arguments[i] );
		placedRelativeURL = placeFile.absurl;

		if (document == null) { // If there is no document open
			var doc = placeFile.openDocument(); // Open document in GoLive
		} else if (document.type == 'site') {
			document.website.addFile(placeFile, document.website.root);
		} else if ((document.type == 'markup') && document.mainTextArea) { // If this a markup document and we have a text area to insert the content
			// If this is an new untitled document, just get the window
			if (document.file)
			{ // This is a saved file, get the directory
				targetDir = (document.ref.siteDoc == null) ? document.file.parent : document.ref.siteDoc.website.root;
				placedRelativeURL = relativeURL(placedRelativeURL,targetDir.absurl);
			}
			var filename = placeFile.lastname.toString();
			var fileExt = filename.substring(filename.lastIndexOf('.')+1).toUpperCase();

			document=document; // Bring the current window to the front because 
				// at least insertSmartObject requires document frontmost window
						
			switch (fileExt)
			{
				case 'JPEG' :
				case 'JPG' :
				case 'GIF' :
				case 'PNG' :
				
					// Get the dimensions of the image
					var origSize = app.imageConverter.getImageSize(placeFile);
					if (origSize) // If we can get some image size information
					{
						// If it is too large (a user settable option in pixels) convert using smartObject
						if	( imageAutoConvert && (	(origSize.width > imageSizeAutoLimit) ||
													(origSize.height > imageSizeAutoLimit)
							)					  )
						{
							// Because image dimensions are known set the size and 
							// scale the image to the smallest size that will still 
							// fit within the width and height
							var scalingFactor = Math.min(defaultKnownImageSize[0]/origSize.width, defaultKnownImageSize[1]/origSize.height);
							
							// Scale proportinally so that the maximum does not exceed maximum dimension
							imgSettings.outputSize = [Math.round(origSize.width * scalingFactor),
													Math.round(origSize.height * scalingFactor)];
							imgSettings.scaleMode = 'exactFit';

							app.imageConverter.insertSmartObject(placeFile, imgSettings, false, false, false, targetDir, placeFile.name);
							// Fix the selection
							document.mainTextArea.setCursor(document.mainTextArea.selectionStart + document.mainTextArea.selectionLength);
						} else {
							document.mainTextArea.insertHTML('<img src="' + placedRelativeURL + '" alt="" height="' + origSize.height + '" width="' + origSize.width + '" border="0">');
						}
					} else {
						document.mainTextArea.insertHTML('<a href="' + placedRelativeURL + '">' + placeFile.name + '</a>');
					}
					break;

				case 'SVG' :
				case 'PDF' :
					// Get the dimensions of the image
					var origSize = app.imageConverter.getImageSize(placeFile);
					if (origSize) // If we can get some image size information
					{
						// Because image dimensions are known set the size and 
						// scale the image to the smallest size that will still 
						// fit within the width and height
						var scalingFactor = Math.min(defaultKnownImageSize[0]/origSize.width, defaultKnownImageSize[1]/origSize.height);
						
						// Scale proportinally so that the maximum does not exceed maximum dimension
						imgSettings.outputSize = [Math.round(origSize.width * scalingFactor),
												Math.round(origSize.height * scalingFactor)];
						imgSettings.scaleMode = 'exactFit';

						app.imageConverter.insertSmartObject(placeFile, imgSettings, false, false, false, targetDir, placeFile.name);
						// Fix the selection
						document.mainTextArea.setCursor(document.mainTextArea.selectionStart + document.mainTextArea.selectionLength);
					} else {
						// Default settings to place a small managable image on the page, user can then set to desired size
						imgSettings.outputSize = defaultUnknownImageSize;
						imgSettings.scaleMode = 'showAll';
	
						// Convert using a smartobject
						app.imageConverter.insertSmartObject(placeFile, imgSettings, false, false, false, targetDir, placeFile.name);
						// Fix the selection
						document.mainTextArea.setCursor(document.mainTextArea.selectionStart + document.mainTextArea.selectionLength);
					}
					break;
				
				
				case 'PSD' :	case 'PDD' :	case 'JPE' :	case '8BPS' :	case 'BMP' :	case 'RLE' :	case 'DIB' :
				case 'TIF' :	case 'CRW' :	case 'NEF' :	case 'RAF' :	case 'ORF' :	case 'CIN' :	case 'SDPX' :
				case 'DPX' :	case 'FIDO' :	case 'EPS' :	case 'PS' :		case 'EPSF' :	case 'FLM' :	case 'PSB' :
				case 'EXR' :	case 'AI3' :	case 'AI4' :	case 'AI5' :	case 'AI6' :	case 'AI7' :	case 'AI8' :
				case 'AI' :		case 'PCX' :	case 'PDP' :	case 'PCD' :	case 'PICT' :
				case 'PCT' :	case 'PIC' :	case 'PXR' :	case 'PNG' :	case 'HRR' :	case 'RGBE' :	case 'XYZE' :
				case 'TGA' :	case 'VDA' :	case 'ICB' :	case 'VST' :	case 'TIF' :	case 'TIFF' :	case 'WBM' :
				case 'WBMP' :
					
					// Default settings to place a small managable image on the page, user can then set to desired size
					imgSettings.outputSize = defaultUnknownImageSize;
					imgSettings.scaleMode = 'showAll';

					// Convert using a smartobject
					app.imageConverter.insertSmartObject(placeFile, imgSettings, false, false, false, targetDir, placeFile.name);
					// Fix the selection
					document.mainTextArea.setCursor(document.mainTextArea.selectionStart + document.mainTextArea.selectionLength);
					break;
				
				case 'MOV' :
					// Insert quicktime code in the page
					document.mainTextArea.insertHTML(
						'<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="100" width="100">' +
							'<param name="src" value="' + placedRelativeURL + '" />' +
							'<param name="autoplay" value="true" />' +
							'<param name="controller" value="false" />' +
							'<embed height="100" pluginspage="http://www.apple.com/quicktime/download/" src="' + placedRelativeURL + '" type="video/quicktime" width="100" controller="false" autoplay="true"></embed>' +
						'</object>'
					);
					break;
				
				case 'SWF' :
					// Insert swf code in the page 
					document.mainTextArea.insertHTML(
					'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" height="100" width="100">' +
					'<param name="movie" value="' + placedRelativeURL + '" />' +
					'<param name="quality" value="best" />' +
					'<param name="play" value="true" />' +
					'<embed height="100" pluginspage="http://www.macromedia.com/go/getflashplayer" src="' + placedRelativeURL + '" type="application/x-shockwave-flash" width="100" quality="best" play="true"></embed>' +
					'</object>'
					);
					break;
				
				default :
					// Just insert a link to the document.
					document.mainTextArea.insertHTML('<a href="' + placedRelativeURL + '">' + placeFile.name + '</a>');
					break;
			}			
		}		
	}
}




//]]>
</script>
		<p><b>Images</b></p>
		<p>
			<img src="P_ProjectBridge_Lg_R.png" alt="" name="bridgeToolbarIcon" height="18" width="26" border="0"/> 
			<img src="P_StockPhoto_Lg_R.png" alt="" name="stockToolbarIcon" height="18" width="22" border="0"/></p>
		<p><b>Toolbar</b> </p>
		<hr/>
		</p>
		<jsxtoolbar name="BridgeToolbar" title="$$$/extendscript/bridge/toolbarTitle=Adobe Services Toolbar" width="65">
			<jsxcontrol height="20" helpTip="$$$/extendscript/bridge/ttBridge=Go to Bridge" name="btBridge" picture="bridgeToolbarIcon" name="value" posx="2" posy="3" type="button" width="28"></jsxcontrol>
			 
			<jsxcontrol height="20" helpTip="$$$/extendscript/bridge/ttStockPhoto=Go to Adobe Stock Photos" name="btStock" picture="stockToolbarIcon" name="value" posx="36" posy="3" type="button" width="24"></jsxcontrol>
		</jsxtoolbar>
		<p><strong>Menu:</strong></p>
		<jsxmenubar>
			<jsxmenu name="file">
				<jsxitem name="btBrowse" title="$$$/extendscript/Bridge/menuBrowse=Browse..." lname="OPDC" location="after"/>
			</jsxmenu>
		</jsxmenubar>
	</body>

</html>