#target bridge

/*-------------------------------------------------------------------------------------------------------------- 
	ADOBE SYSTEMS INCORPORATED
	Copyright 2006 - Adobe Systems Incorporated
	All Rights Reserved
 
	If this file is provided by Adobe in modifiable form (e.g., source code or JavaScript), then you may modify this file.  
	You may use and distribute this file, in modified or unmodified form, provided that you must reproduce the above
	acknowledgement and this notice with all copies of the file.
 
	THIS SOFTWARE IS PROVIDED BY ADOBE SYSTEMS INCORPORATED “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, 
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ADOBE BE LIABLE TO YOU FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
	SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
	WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------------------------------------*/

/* ExportToSWF_AI.jsx
	Author: 		Bob Stucky
	Description:	Exports Selected images to SWF
	Version:		2.0.6
	Dated:		10/25/2006
*/

/*  
@@@BUILDINFO@@@ ExportToSWF_AI.jsx 2.0.0.6  1.0000  25-Oct-2006 
*/
$.level = 0;
if (BridgeTalk.appName == "bridge" )	 {
	var ExportToSWFAI = {};

	WasLib.createAppMenus();
	
	WasLib.createMenu( "menu", localize("$$$/WAS/ETFAIExporttoSWFAIMENU=Illustrator"), "at the end of tools", "tools/ai" );
	// create main menu object
	WasLib.createMenu( "menu", localize("$$$/WAS/ETFAIExporttoSWFExpMENU=Export to SWF"), "at the end of tools/ai", "tools/ai/ExportToSWF" );

	// create menu for Simple Export
	var ExportToSWFAIQuick =  WasLib.createMenu( "command", localize("$$$/WAS/ETFAIQuickExport=Quick Export"), "at the end of tools/ai/ExportToSWF", "tools/ai/ExportToSWF/QuickExport");
	
	ExportToSWFAI.executeQuick = function( )  {
		var options = "var opt = new ExportOptionsFlash(); opt.resolution = 72; ";
		//var files = WasLib.cullFolders( WasLib.getBridgeFiles( WasLib.fileFilters.getValue( "Illustrator Openable" ) ) );
		// Watson 1463927: noted against ContactSheet_ID.jsx but exactly same bug applies to this script
		var files = WasLib.getBridgeFilesOnlyIgnoringFolders( WasLib.fileFilters.getValue( "Illustrator Openable" ) );
		
		var bti = new WasLib.BridgeTalkIterator( true, localize("$$$/WAS/QuickExp=Quick Export..."), localize("$$$/WAS/QuickExp2=Quick Export..."), false );
		for ( var i in files ) {
			var scpt = getExportToSWFScript( files[ i ], options );
			bti.addMessage( "illustrator", scpt, decodeURI( files[ i ].name ) );
		}
		bti.send();
	}
	ExportToSWFAIQuick.onSelect = ExportToSWFAI.executeQuick;

	// create menu for Custom Export
	
	ExportToSWFAI.menu = WasLib.createMenu( "command", localize("$$$/WAS/ETFAICustomExport=Custom Export..."), "at the end of tools/ai/ExportToSWF", "tools/ai/ExportToSWF/CustomExport");
	ExportToSWFAI.executeCustom = function() {		
		// init the dialog
		ExportToSWFAI.initDialog();

		// show the dialog
		ExportToSWFAI.showDialog();
	}
	
	ExportToSWFAI.menu.onSelect = ExportToSWFAI.executeCustom;



	// add actions for ok
	ExportToSWFAI.ok = function() {
		// holds code for the options to pass to AI
		var options = "var opt = new ExportOptionsFlash();";
		
		// set export style
		options += 'opt.exportStyle = FlashExportStyle.';
		switch (ExportToSWFAI.dialog.typeDD.selection.index) {
			case 0:
				options += 'ASFLASHFILE;';
				break;
			
			case 1:
				options += 'LAYERSASFRAMES;';
				options += 'opt.frameRate		= ' + ExportToSWFAI.dialog.options.rateEt.text + ';';
				options += 'opt.looping			= ' + ExportToSWFAI.dialog.options.loopCb.value + ';';
				options += 'opt.layerOrder		= LayerOrderType.' + ExportToSWFAI.dialog.options.layerOrderDD.selection.value + ';';
				
				if(ExportToSWFAI.dialog.options.blendsOptGrp.enabled) {
					if(ExportToSWFAI.dialog.options.blendRb1.value == true) {
						options += 'opt.blendAnimation	= BlendAnimationType.INBUILD;';
					} else if(ExportToSWFAI.dialog.options.blendRb2.value == true) {
						options += 'opt.blendAnimation	= BlendAnimationType.INSEQUENCE;';
					}
				}


				
				break;
			
			case 2:
				options += 'LAYERSASFILES;';
				break;
		}
		//options += 'opt.generateHTML			= ' + ExportToSWFAI.dialog.options.htmlCb.value + ';';
		options += 'opt.artBoardClipping		= ' + ExportToSWFAI.dialog.options.clipCb.value + ';';
		options += 'opt.readOnly 				= ' + ExportToSWFAI.dialog.options.protectCb.value + ';';
		options += 'opt.convertTextToOutlines	= ' + ExportToSWFAI.dialog.options.outlineCb.value + ';';
		options += 'opt.compressed 				= ' + ExportToSWFAI.dialog.options.compressCb.value + ';';


		// set method options
		if (ExportToSWFAI.dialog.options.flattenRb1.value == true) {
			options += 'opt.flattenOutput = OutputFlattening.PRESERVEAPPEARANCE;';
		} else {
			options += 'opt.flattenOutput = OutputFlattening.PRESERVEPATHS;';
		}
		
		// set curve quality
		options += 'opt.curveQuality = '+ExportToSWFAI.dialog.options.curveSlider.value+';';

		// set image options
		if (ExportToSWFAI.dialog.options.lossRb1.value == true) {
			options += 'opt.imageFormat = FlashImageFormat.LOSSLESS;';
		} else {
			options += 'opt.imageFormat = FlashImageFormat.LOSSY;';
			
			if (ExportToSWFAI.dialog.options.methodRb1.value == true) {
				options += 'opt.jpegMethod = FlashJPEGMethod.Standard;';
			} else {
				options += 'opt.jpegMethod = FlashJPEGMethod.Optimized;';
			}
			
			options += 'opt.jpegQuality = ' + ExportToSWFAI.dialog.options.qualityDD.selection.value + ';';
		}

		var scpt = '';

		// get selected files
		//var files = WasLib.cullFolders( WasLib.getBridgeFiles( WasLib.fileFilters.getValue( "Illustrator Openable" ) ) );
	    // Watson 1463927: noted against ContactSheet_ID.jsx but exactly same bug applies to this script
		var files = WasLib.getBridgeFilesOnlyIgnoringFolders( WasLib.fileFilters.getValue( "Illustrator Openable" ) );

		// create iterator... 	WasLib.BridgeTalkIterator = function( showProgress, text, title, notifyComplete ) {
		var bti = new WasLib.BridgeTalkIterator( true, localize("$$$/WAS/ETFAICustomExport=Custom Export..."), localize("$$$/WAS/ETFAICustomExport=Custom Export..."), false );

		for (var i=0; i<files.length; i++) {
			// get script
			scpt = getExportToSWFScript(files[i], options);
			bti.addMessage( 'illustrator', scpt, decodeURI( files[i].name ) );
		}
		bti.send();
		ExportToSWFAI.dialog.close( true );
	}

	ExportToSWFAI.cancel	= function () {
		ExportToSWFAI.dialog.close();
	}

	ExportToSWFAI.initDialog = function()  {
		ExportToSWFAI.dialog = new Window("dialog", localize("$$$/WAS/ETFAIExporttoSWFtitle=Export to SWF"));
		ExportToSWFAI.dialog.options = {};
		ExportToSWFAI.dialog.orientation = "stack";
	
	// Main row...

		ExportToSWFAI.dialog.mainRow = ExportToSWFAI.dialog.add( "group" );
		
		
		/**
		* FIRST COLUMN -----------------------------------------------------------
		*/

		ExportToSWFAI.dialog.exportCol = ExportToSWFAI.dialog.mainRow.add( "panel", undefined, localize("$$$/WAS/ETFAIExportAs=Export As:") );
		ExportToSWFAI.dialog.exportCol.orientation = "row";		

		// first options column
			ExportToSWFAI.dialog.optCol1 = ExportToSWFAI.dialog.exportCol.add( "group" );
			ExportToSWFAI.dialog.optCol1.orientation = "column";
			ExportToSWFAI.dialog.optCol1.alignment = "fill";
			
			// general
			var general = ExportToSWFAI.dialog.optCol1.add("group");
			general.orientation = "column";
	
			// ip Fix W1430452: moved this drop down here so at least it can be used and eliminate the panel-title group, which wasn't working; there were two Export As labels on top of each other and
			// drop down list was dysfunctional. 
			// Prior- it was co-extensive with the title.
			ExportToSWFAI.dialog.typeDD = general.add( "dropdownlist" );
			ExportToSWFAI.dialog.typeDD.add('item', localize("$$$/WAS/ETFAIFileToSWFFile=AI File to SWF File"));
			ExportToSWFAI.dialog.typeDD.add('item', localize("$$$/WAS/ETFAILayersToSWFFrames=AI Layers to SWF Frames"));
			ExportToSWFAI.dialog.typeDD.add('item', localize("$$$/WAS/ETFAILayersToSWFFiles=AI Layers to SWF Files"));
			ExportToSWFAI.dialog.typeDD.selection = ExportToSWFAI.dialog.typeDD.items[0];
            // 1468129
            ExportToSWFAI.dialog.typeDD.preferredSize.width = 200;
			// Generate HTML
			//general.htmlCb = general.add( "checkbox", undefined, localize("$$$/WAS/ETFAIGenerateHTML=Generate HTML") );
			//general.htmlCb.alignment = "left";
			//general.htmlCb.value = true;
			//ExportToSWFAI.dialog.options.htmlCb = general.htmlCb;

			// Protect From Import
			general.protectCb = general.add( "checkbox", undefined, localize("$$$/WAS/ETFAIProtectFromImport=Protect from Import"));
			general.protectCb.alignment = "left";
			ExportToSWFAI.dialog.options.protectCb = general.protectCb;

			// Clip to Artboard size
			general.clipCb = general.add( "checkbox", undefined, localize("$$$/WAS/ETFAIClipToArtboardSize=Clip to Artboard Size"));
			general.clipCb.alignment = "left";
			ExportToSWFAI.dialog.options.clipCb = general.clipCb;

			// outline text
			general.outlineCb = general.add( "checkbox", undefined, localize("$$$/WAS/ETFAIExportTextAsOutlines=Export Text As Outlines") );
			general.outlineCb.alignment = "left";
			ExportToSWFAI.dialog.options.outlineCb = general.outlineCb;

			// compress
			general.compressCb = general.add( "checkbox", undefined, localize("$$$/WAS/ETFAICompressFile=Compress File") );
			general.compressCb.alignment = "left";
			ExportToSWFAI.dialog.options.compressCb = general.compressCb;



			// animation
			var animation = ExportToSWFAI.dialog.optCol1.add("panel", undefined, localize("$$$/WAS/ETFAIAnimation=Animation"));
			animation.orientation = "column";
			animation.alignment = "fill";


			// fps
			var rate = animation.add( "group" ); 
			rate.add( "statictext", undefined, localize("$$$/WAS/ETFAIFrameRate=Frame Rate:") );
			rate.et	= rate.add( "edittext",undefined,12 );
			rate.et.preferredSize.width = 50;
			rate.add( "statictext", undefined, "fps" );
			ExportToSWFAI.dialog.options.rateEt = rate.et;

			// Looping
			animation.loopCb = animation.add( "checkbox", undefined, localize("$$$/WAS/ETFAILooping=Looping"));
			animation.loopCb.alignment = "left";
			ExportToSWFAI.dialog.options.loopCb = animation.loopCb;
		

			// Use as layer order
			var layerOrder = animation.add('group');
			layerOrder.orientation = 'row';
			layerOrder.add( "statictext", undefined, localize("$$$/WAS/ETFAILayerOrder=Layer Order"));
			ExportToSWFAI.dialog.options.layerOrderDD = layerOrder.add( "dropdownlist");
			// Watson 1468134
            ExportToSWFAI.dialog.options.layerOrderDD.preferredSize.width = 150;
			var item = ExportToSWFAI.dialog.options.layerOrderDD.add('item', localize("$$$/WAS/ETFAIBottomUp=Bottom Up"));
			item.value = 'BOTTOMUP';
			item = ExportToSWFAI.dialog.options.layerOrderDD.add('item', localize("$$$/WAS/ETFAITopDown=Top Down"));
			item.value = 'TOPDOWN';
			ExportToSWFAI.dialog.options.layerOrderDD.selection = ExportToSWFAI.dialog.options.layerOrderDD.items[0];

			// blends
			var blends = animation.add( "group" ); 
			blends.orientation = 'column';
			blends.alignChildren = 'left';
			ExportToSWFAI.dialog.options.blendsCb = blends.add( "checkbox", undefined, localize("$$$/WAS/ETFAIAnimateBlends=Animate Blends") );
			ExportToSWFAI.dialog.options.blendsCb.onClick = function() {
				ExportToSWFAI.dialog.options.blendsOptGrp.enabled = this.value;
				// 1468394: these radio buttons have enablement that depends on whether the blend optioon is enabled 
			    ExportToSWFAI.dialog.options.blendRb1.enabled = this.value;
                ExportToSWFAI.dialog.options.blendRb2.enabled = this.value;
			}
			
			ExportToSWFAI.dialog.options.blendsOptGrp = blends.add('group');
			ExportToSWFAI.dialog.options.blendsOptGrp.enabled = false;
			ExportToSWFAI.dialog.options.blendRb1 = ExportToSWFAI.dialog.options.blendsOptGrp.add( "radiobutton", undefined, localize("$$$/WAS/ETFAIInSequence=In Sequence"));
			ExportToSWFAI.dialog.options.blendRb2 = ExportToSWFAI.dialog.options.blendsOptGrp.add( "radiobutton", undefined, localize("$$$/WAS/ETFAIInBuild=In Build"));
			ExportToSWFAI.dialog.options.blendRb2.value = true;

            // Watson 1468394- by default these should not be enabled when checkbox is unchecked
            ExportToSWFAI.dialog.options.blendRb1.enabled = false;
            ExportToSWFAI.dialog.options.blendRb2.enabled = false;
		    animation.enabled = false; // Watson 1468347: default enablement is off, only on when it's AI Layers to SWF Frames

		// second options column
			ExportToSWFAI.dialog.optCol2 = ExportToSWFAI.dialog.exportCol.add( "group" );
			ExportToSWFAI.dialog.optCol2.orientation = "column";
			ExportToSWFAI.dialog.optCol2.alignment = "fill";


			// method
			var methodPnl = ExportToSWFAI.dialog.optCol2.add('panel', undefined, localize("$$$/WAS/ETFAIMethod=Method:"));
			methodPnl.alignChildren = 'left';
			ExportToSWFAI.dialog.options.flattenRb1 = methodPnl.add( "radiobutton", undefined, localize("$$$/WAS/ETFAIPreserveAppearance=Preserve Appearance"));
			ExportToSWFAI.dialog.options.flattenRb2 = methodPnl.add( "radiobutton", undefined, localize("$$$/WAS/ETFAIPreserveEditabilityWherePossible=Preserve Editability Where Possible"));
			ExportToSWFAI.dialog.options.flattenRb2.value = true;



			// curve quality
			var curvePnl = ExportToSWFAI.dialog.optCol2.add('group');
			curvePnl.orientation = 'row';
			curvePnl.alignChildren = 'left';
			curvePnl.add('statictext', undefined, localize("$$$/WAS/ETFAICurveQuality=Curve Quality:"));
			ExportToSWFAI.dialog.options.curveSlider = curvePnl.add('slider', undefined, 7,0,10);
			ExportToSWFAI.dialog.options.curveValEt = curvePnl.add('statictext', undefined, 7);
			ExportToSWFAI.dialog.options.curveValEt.preferredSize.width = 20;
			ExportToSWFAI.dialog.options.curveSlider.onChange = function() {
				ExportToSWFAI.dialog.options.curveValEt.text = Math.round(this.value);
			}
			// Watson 1430472 Define additional behavior for when the slider value changes
	        ExportToSWFAI.dialog.options.curveSlider.onChanging = function() {
		        ExportToSWFAI.dialog.options.curveValEt.text = Math.round(this.value);
	        }

			// image
			var imagePnl = ExportToSWFAI.dialog.optCol2.add('panel', undefined, localize('$$$/WAS/ETDAImageWordHardCoded=Image'));
			
			// format
			var formatRow = imagePnl.add( "group" );
			formatRow.orientation = "row";
			formatRow.alignChildren = "top";
			
			var formatLft = formatRow.add( "group" );
			formatLft.add( "statictext", undefined, localize("$$$/WAS/ETFAIImageFormat=Image Format:"));
			
			var formatRt = formatRow.add( "group" );
			formatRt.orientation = 'column';
			formatRt.alignChildren = 'left';
			ExportToSWFAI.dialog.options.lossRb1 = formatRt.add( "radiobutton", undefined, localize("$$$/WAS/ETFAILossless=Lossless"));
			ExportToSWFAI.dialog.options.lossRb2 = formatRt.add( "radiobutton", undefined, localize("$$$/WAS/ETFAILossy=Lossy (JPEG)"));
			ExportToSWFAI.dialog.options.lossRb1.value = true;
			
			// jpeg quality
			var quality = imagePnl.add( "group" ); 
			quality.add( "statictext", undefined, localize("$$$/WAS/ETFAIJPEGQuality=JPEG Quality"));
			quality.dd	= quality.add( "dropdownlist" );
			quality.dd.preferredSize.width = 80; // Watson 1468390
			var item = quality.dd.add( "item", localize("$$$/WAS/ETFAIJPEGQualityLow=Low" ) );
			item.value = 1;
			item = quality.dd.add( "item", localize("$$$/WAS/ETFAIJPEGQualityMedium=Medium" ) );
			item.value = 3;
			item = quality.dd.add( "item", localize("$$$/WAS/ETFAIJPEGQualityHigh=High" ) );
			item.value = 6;
			item = quality.dd.add( "item", localize("$$$/WAS/ETFAIJPEGQualityMaximum=Maximum" ) );
			item.value = 8;
			quality.dd.selection = 0;
			ExportToSWFAI.dialog.options.qualityDD = quality.dd;

			// method
			var methodRow = imagePnl.add( "group" );
			methodRow.orientation = "row";
			methodRow.alignChildren = "top";
			
			var methodLft = methodRow.add( "group" );
			methodLft.add( "statictext", undefined, localize("$$$/WAS/ETFAIMethod=Method:"));
			
			var methodRt = methodRow.add( "group" );
			methodRt.orientation = 'column';
			methodRt.alignChildren = 'left';
			ExportToSWFAI.dialog.options.methodRb1 = methodRt.add( "radiobutton", undefined, localize("$$$/WAS/ETFAIBaselineStandard=Baseline (Standard)"));
			ExportToSWFAI.dialog.options.methodRb2 = methodRt.add( "radiobutton", undefined, localize("$$$/WAS/ETFAIBaselineOptimized=Baseline Optimized"));
			ExportToSWFAI.dialog.options.methodRb1.value = true;
			
			// resolution
			var resolution = imagePnl.add( "group" ); 
			resolution.add( "statictext", undefined, localize("$$$/WAS/ETFAIResolution=Resolution:") );
			resolution.et 	= resolution.add( "edittext", undefined, "72" );
			resolution.et.preferredSize.width = 50;
			resolution.add( "statictext", undefined, "ppi" );
			ExportToSWFAI.dialog.options.resEt = resolution.et;
			
			// disable jpeg options
			methodRow.enabled = false;
			methodLft.enabled = false;
			methodRt.enabled = false;
			quality.enabled = false;




	/**
	* SECOND COLUMN --------------------------------------------------------
	*/
		// this is the OK/Cancel button group
		ExportToSWFAI.dialog.btn = ExportToSWFAI.dialog.mainRow.add( "group" );
		ExportToSWFAI.dialog.btn.orientation = "column";
		ExportToSWFAI.dialog.btn.alignChildren = 'top';
		ExportToSWFAI.dialog.btn.alignment = 'fill';
		ExportToSWFAI.dialog.btn.spacing = 30;
		
		ExportToSWFAI.dialog.buttonGroup = ExportToSWFAI.dialog.btn.add( "group", undefined );
		ExportToSWFAI.dialog.buttonGroup.preferredSize.width = 100;
		ExportToSWFAI.dialog.buttonGroup.orientation = "column";
		ExportToSWFAI.dialog.buttonGroup.alignChildren = "top";
		ExportToSWFAI.dialog.buttonGroup.alignment = "fill";
		ExportToSWFAI.dialog.buttonGroup.okButton = ExportToSWFAI.dialog.buttonGroup.add( "button", undefined, localize("$$$/WAS/ETFAIOKButton=OK") );
		ExportToSWFAI.dialog.buttonGroup.cancelButton = ExportToSWFAI.dialog.buttonGroup.add( "button", undefined, localize("$$$/WAS/ETFAICancelButton=Cancel") );		

		
		// move the drop down so that it overlapps the panel
		ExportToSWFAI.dialog.onShow = 	function () {
			this.pnlTitleGrp.location.x = this.exportCol.location.x + 30;
			this.pnlTitleGrp.location.y = this.exportCol.location.y + 12;
		}
	
		ExportToSWFAI.dialog.buttonGroup.okButton.onClick		= this.ok;
		ExportToSWFAI.dialog.buttonGroup.cancelButton.onClick	= this.cancel;

		ExportToSWFAI.dialog.center();


	/**
	* ACTIONS --------------------------------------------------------
	*/

		// add actions for animation panel
		ExportToSWFAI.dialog.typeDD.onChange = function() {
			animation.enabled = (this.selection.text == localize("$$$/WAS/ETFAILayersToSWFFrames=AI Layers to SWF Frames"));
		}

	
		// add actions for image panel
		ExportToSWFAI.dialog.options.lossRb1.onClick = function() {
			methodRow.enabled = false;
			methodLft.enabled = false;
			methodRt.enabled = false;
			quality.enabled = false;
		}
		ExportToSWFAI.dialog.options.lossRb2.onClick = function() {
			methodRow.enabled = true;
			methodLft.enabled = true;
			methodRt.enabled = true;
			quality.enabled = true;
		}

		
	}
	
	ExportToSWFAI.showDialog	= function () {
		ExportToSWFAI.dialog.show();
	}

	function getExportToSWFScript( f, options) {
		// get new name
		var newFile = f.getFileWithExtension( 'swf' );
		newFile.uniquify();
		var scpt = "app.userInteractionLevel=UserInteractionLevel.DONTDISPLAYALERTS;\r" +
//				"BridgeTalk.bringToFront('illustrator'); " +
				"try { \n " +
				"var thefile = eval( " + f.toSource() + " ); \n" +
					 "var doc = app.open( thefile ); \n" + options +
					 "var newFile = eval( " + newFile.toSource() + " ); \n" +
					 "doc.exportFile( newFile, ExportType.FLASH, opt); \n" +
				"} \n" +
			"catch( e ) { throw e } finally {doc.close(SaveOptions.DONOTSAVECHANGES);}";
		return scpt;
	}
	"ExportToSWF_AI_CS3.jsx loaded";
}
