//----------------------------------------------------------------------- 
//
// 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.
//
//----------------------------------------------------------------------- 
// <asm:name>Live Trace</asm:name>
// <asm:description>The script allows the user to select a number of images (BMP,GIF,JPEG,JPG,PCT,PSD,PNG,TIF,TIFF) and create an Illustrator file containing a traced version of the image using Illustrator CS2's new Live Trace feature.</asm:description>
// <asm:help>Select a set of images in Bridge, select menu item Tools->Illustrator->Live Trace... </asm:help>
// <asm:help>Select the Live Trace preset to use.</asm:help>
// <asm:help>Images can be combined into a single document or output as separate documents.</asm:help>
// <asm:help>If the images are going to be saved, you must select the output directory for the operation.</asm:help>
// <asm:help>The generated documents can be saved using a specified filename format, or left open as unsaved documents in Illustrator.</asm:help>
// <asm:author>Chandler McWilliams</asm:author>
// <asm:copyright>Copyright 2004 Adobe Systems Incorporated 2004, all rights reserved</asm:copyright>
// <asm:version>0.7.3</asm:version>
// <asm:date>02-09-2004</asm:date>
// <asm:website>http://www.adobe.com</asm:website>
//----------------------------------------------------------------------- 


// make sure bridge is the current app
if (BridgeTalk.appName == "bridge" )	 {

	// change this value to increase the maximum number of files that can be processes in one go
	var MAX_BATCH_SIZE = 10;













	// namespace, just in case
	VectorizeAI = {};



// ===========================================================
// - DUPLICATED FROM ADOBE_LIBRARY.JSX
// ===========================================================
	isValidReference = function( ref )  {
	  return ( ref != undefined && ref != null );
	}
	isEmpty = function( str )  {
		if ( !isValidReference( str ) ) {
			return true;
		}
		return ( str == null || str == "" )
	}

	trim = function( st )  {
		var str = new String( st );
	    str = str.replace( /^\s+/ , "" );
	    str = str.replace( /\s+$/ , "" );
	    return str
	}

	getCharsBefore = function( str, ch )  {
		s = new String( trim( str ) );
		var idx = s.indexOf( ch );
		if (idx == -1 )  {
			return s.toString();
		} else {
			return s.substr( 0, idx );		
		}
	}


	VectorizeAI.createMenu = function( type, text, where, id)  {
		var aMenu = null;
		aMenu = MenuElement.find( id );
		if ( aMenu == null )  {
			aMenu = new MenuElement( type, text, where, id );
		}
		aMenu.enabled = true;
		return aMenu;
	}

	VectorizeAI.pathinfo = function (fname) {
		var dot = fname.lastIndexOf('.');
		if (dot == -1) dot = fname.length;
		var out = {};
		out.dirname = fname.substring(0, fname.lastIndexOf('/'));
		out.basename = fname.substring(fname.lastIndexOf('/'), dot);
		out.extension = fname.substr(dot+1);
		return out;
	}


	VectorizeAI.sendBridgeTalkMessage = function( target, script, onResultHandler ) {
		try {
			var bt = new BridgeTalk();
			bt.target = target;
			bt.body = script;
			if (!BridgeTalk.isRunning( target ) )  {
				BridgeTalk.launch( target );
			}
		} catch (a ) {
			alert( a );
		}
		// bt message interaction handlers

		bt.onResult = onResultHandler;

		bt.onError = function( r )  {
			alert( TranslateErrorCodes.getMessage( r ) );
		}
		var p = bt.send();
	}







	VectorizeAI.thumbnailsToFiles = function( thumbnails ) {
		var files = new Array();
		var paths = new Array();
		var vcNodes = false;
		for ( var i = 0; i < thumbnails.length; i++ ) {
			if ( thumbnails[ i ].isVersionCueNode() ) {
				vcNodes = true;
				paths.push( thumbnails[ i ].path );
			}
			files.push( thumbnails[ i ].spec );
		}
		if ( vcNodes ) {
			app.preflightFiles( paths );
		}
		return files;
	}




	VectorizeAI.getBridgeFiles = function( mask, getFolderChildren, filesOnly, firstOnly ) {
		return VectorizeAI.thumbnailsToFiles( VectorizeAI.getBridgeThumbnails( mask, getFolderChildren, filesOnly, firstOnly ) );
	}
	
	
	VectorizeAI.getBridgeThumbnails = function( mask, getFolderChildren, filesOnly, firstOnly )  {
		var folderChildren = isValidReference( getFolderChildren ) ? getFolderChildren : true;
		var getFirstFile = isValidReference( firstOnly ) ? firstOnly : false;
		var getFilesOnly = isValidReference( filesOnly ) ? filesOnly : false;
		
		VectorizeAI.hasSkipped = false;
		
		mask = isValidReference( mask ) ? mask : "";
		var thumbnails = new Array();
		if ( isValidReference( app.document ) && isValidReference( app.document.thumbnail ) ) {
			var kids = app.document.selections;
			if ( kids.length > 0 ) { // user selected individual thumbnails in the content pane
				for ( var i = 0; i < kids.length ; i++ ) {
					var thumb = kids[ i ].resolveAlias();
					if ( thumb.isFileNode() ) {
						if ( thumb.isFileType( mask ) ) {
							thumbnails.push( thumb );
							if ( getFirstFile ) {
								return thumbnails;
							}
						} else {
							VectorizeAI.hasSkipped = true;
						}
					} else if ( thumb.isFolderNode() ) {
						if ( folderChildren ) { // if they selected a folder type, go ahead and get the children
							thumbnails = thumbnails.concat( VectorizeAI.getFilesinFolderUsingMask( thumb, mask, getFirstFile ) );
						} else { // unless, of course, we don't want to (getting the children is the default)
							if ( !getFirstFile && !getFilesOnly ) {
								thumbnails.push( thumb );
							}
						}
					} else {
						throw localize( "$$$/WAS/Library/local=This script only operates on your local file system" );
					}					
				}
			}
			else if ( app.document.thumbnail.children.length > 0 ) { // user did not select any specific thumbnail, look at all thumbnails in the content pane
				var kids = app.document.thumbnail.children;
				for ( var i = 0; i < kids.length ; i++ ) {
					var thumb = kids[ i ];
					thumb = thumb.resolveAlias();
					if ( !thumb.hidden ) { // because they didn't select anything, it's possible a hidden file will be in there somewhere...
						if ( thumb.isFileNode() ) {
							if ( thumb.isFileType( mask ) ) { // only take those that fit the mask
								thumbnails.push( thumb );
								if ( getFirstFile ) {
									return thumbnails;
								}
							} else {
								VectorizeAI.hasSkipped = true;
							}
						} else if ( thumb.isFolderNode() ) {
							if ( !getFirstFile && !getFilesOnly ) {
								thumbnails.push( thumb );
							}
						} else {
							throw localize( "$$$/WAS/Library/local=This script only operates on your local file system" );
						}
					}
				}
			}
		}
		return thumbnails;
	}



	VectorizeAI.getFilesinFolderUsingMask = function ( aThumbnail, mask, getFirstFile ) {
		var stopOnFirstFile = isValidReference( getFirstFile ) ? getFirstFile : false;
		var output = [];
		var kids = aThumbnail.children;
		for ( var i=0; i < kids.length; i++) {
			var thumb = kids[ i ];
			if ( !thumb.hidden ) { // again, skip any hidden files
				if ( thumb.isFileType( mask ) ) {
					output.push( thumb );
					if ( stopOnFirstFile ) {
						return output;
					}
				} else {
					VectorizeAI.hasSkipped = true;
				}
			}
		}
		return output;
	}



// FILE PROTOTYPE EXTENSIONS ---------------------------------
	// add isA methods to file object
	// this handles most of it, checks if the file has the extension (win) / type (mac) of
	// the comma separated string passed as ex
	if ( !isValidReference( File.prototype.isFileType ) ) {
		File.prototype.isFileType = function( ex )  {
			if ( isEmpty( ex ) ) {
				return true;
			}
			var ex = ex.toUpperCase();
			ext = this.getExtension();
			if ( !isValidReference( ext ) ) {
				ext = "";
			}
			ext = trim(ext);
			// this is tricky now because OS X uses extensions
			// so if the extension can be used to determine type,
			// use it, otherwise try the type
			if (ex.indexOf( ext.toUpperCase() ) > -1) return true;
			// only try this on mac
			if ( File.fs == "Macintosh" ) {
				ext = ( this.type == '????' ) ? ext : this.type;
				return ex.indexOf( ext.toUpperCase() ) > -1;
			}
			// pc will be false at this point
			return false;
		}
	}


	if ( !isValidReference( File.prototype.getExtension ) ) {
		File.prototype.getExtension = function() {
			var idx = unescape( this.name ).lastIndexOf( "." );
			var ext = undefined;
			if ( idx > -1 ) {
				ext = unescape( this.name ).substr( ( idx + 1 ) );
			}
			return ext;
		}
	}

// THUMBANAIL PROTOTYPE EXTENSIONS ---------------------------------
	if ( !isValidReference( Thumbnail.prototype.isFileType ) ) {
		Thumbnail.prototype.isFileType = function( ex ) { // for browsing Thumbnails
			if ( this.isLocalNode() &&  this.isFileNode() ) { // if it's not a File, return false
				return this.spec.isFileType( ex );
			}
			return false; 
		}
	}
	if ( !isValidReference( Thumbnail.prototype.isFolderNode ) ) {
		Thumbnail.prototype.isFolderNode = function() {
			if ( isValidReference( this.spec ) ) {
				return this.spec instanceof Folder;
			}
			return false;
		}
	}
	if ( !isValidReference( Thumbnail.prototype.isFileNode ) ) {
		Thumbnail.prototype.isFileNode = function() {
			if ( isValidReference( this.spec ) ) {
				return this.spec instanceof File;
			}
			var protocol = getCharsBefore( this.path, "://" );
			return ( ( protocol == "file" ) || ( protocol == this.path ) );
		}
	}
	if ( !isValidReference( Thumbnail.prototype.isVersionCueNode ) ) {
		Thumbnail.prototype.isVersionCueNode = function() {
			var protocol = getCharsBefore( this.path, "://" );
			return ( protocol == "vcbridge" );
		}
	}
	if ( !isValidReference( Thumbnail.prototype.isWebNode ) ) {
		Thumbnail.prototype.isWebNode = function() {
			var protocol = getCharsBefore( this.path, "://" );
			return ( ( protocol == "http" ) || ( protocol == "https" ) );
		}
	}	
	if ( !isValidReference( Thumbnail.prototype.isLocalNode ) ) {
		Thumbnail.prototype.isLocalNode = function() {
			return ( this.isFileNode() || this.isFolderNode() || this.isVersionCueNode() );
		}
	}
	if ( !isValidReference( Thumbnail.prototype.isAlias ) ) {
		Thumbnail.prototype.isAlias = function() {
			if ( this.isFileNode() ) {
				return this.spec.alias;
			}
			return false;
		}
	}
	if ( !isValidReference( Thumbnail.prototype.resolveAlias ) ) {
		Thumbnail.prototype.resolveAlias = function() {
			if ( this.isAlias() ) {
				return new Thumbnail( this.spec.resolve() );
			}
			return this;
		}
	}
	if ( !isValidReference( Thumbnail.prototype.relativePath ) ) {
		Thumbnail.prototype.relativePath = function( base ) {
			if ( !isValidReference( base ) ) {
				if ( this.isVersionCueNode() ) {
					if ( contains( this.path, "soap" ) ) {
						return getCharsAfter( this.path, "/soap/" );
					} else {
						return "/soap/";
					}
				} else {
					return this.spec.absoluteURI;
				}
			} else {
				if ( this.isVersionCueNode() ) {
					return getCharsAfter( this.path, base );
				} else {
					return this.spec.getRelativeURI( base );
				}
			}
		}
	}














/**
* SETUP --------------------------------------------------------
*/

	// make sure generic menus are there
	// create illustrator tools menu
	VectorizeAI.createMenu( "menu", 'Illustrator', "-after submenu/VersionCue", "tools/ai" );

	
	// vectorizable types
	VectorizeAI.TRACEABLE = "BMP,GIF,JPEG,JPG,PCT,PSD,PNG,TIF,TIFF,";


	// create main menu object
	VectorizeAI.menu = VectorizeAI.createMenu( "command", localize("$$$/WAS/VAIVectorize=Live Trace..."), "at the end of tools/ai", "tools/ai/Vectorize");
	VectorizeAI.menu.onSelect = function() {
		// make sure something is selected
		var files = VectorizeAI.getBridgeFiles(VectorizeAI.TRACEABLE, true, true);
		if (!files.length) {
			var e = localize( "$$$/WAS/Library/NONEselected=There was a problem with \""+localize("$$$/WAS/VAIVectorize=Live Trace...")+"\" because no valid items were selected. Please make a selection and try again." );
			alert(e);
			VectorizeAI.cancel();
			return;
		} else if (VectorizeAI.hasSkipped) {
			var e = localize( "$$$/WAS/Library/SomeSkipped=Some files were skipped because they were not of the correct type." );
			alert(e.replace(/ScriptName/g, localize("$$$/WAS/VAIVectorize=Live Trace...")));
		}
		
		if (files.length> MAX_BATCH_SIZE) {
			alert(localize("$$$/WAS/VAILimit=A maximum of "+MAX_BATCH_SIZE+" items can be traced at a time. Only the first "+MAX_BATCH_SIZE+" will be traced."));
		}

	
		// send a message to AI getting the list of presets
		// when it returns it'll init and show the dialog 
		BridgeTalk.bringToFront('illustrator');
		var scpt = "function go(){return app.tracingPresetsList.toSource();};go();";
		VectorizeAI.sendBridgeTalkMessage(
			'illustrator',
			scpt,
			function (msg) {
				BridgeTalk.bringToFront('bridge');
				eval("var list = "+msg.body);
				VectorizeAI.initDialog(list);
				
				// show the dialog
				VectorizeAI.showDialog();
			}
		);

	
	}

	// make a display object to hold all the input references
	VectorizeAI.display = {};	


//
// ACTIONS --------------------------------------------------------
//

	VectorizeAI.ok = function () {
		var files, me;
		// make sure a dest is choosen
		if (VectorizeAI.dialog.destinationPnl.enabled && VectorizeAI.display.dest == null) {
			alert(localize("$$$/WAS/VAIYouMustChooseAnOutputDirectory=You must choose an output directory"));
			return;
		}

		VectorizeAI.dialog.close();

		// get selected files
		files = VectorizeAI.getBridgeFiles(VectorizeAI.TRACEABLE, true, true);
	
		BridgeTalk.bringToFront('illustrator');
	
	// create the script
		var selectedNum = files.length;
		if (selectedNum > MAX_BATCH_SIZE) selectedNum = MAX_BATCH_SIZE;
		var scpt = 'app.userInteractionLevel=UserInteractionLevel.DONTDISPLAYALERTS;\r';
		if (VectorizeAI.display.layersCb.value) {
			// if we are using a single doc
			// create a script to:
			//	- create the doc
			//	- for each image
			//		add a layer
			//		place the image
			//		trace the image
			
			// make the doc
			scpt += 'BridgeTalk.bringToFront("illustrator");\r';
			scpt += 'var doc = app.documents.add();\r';
			VectorizeAI.sendBridgeTalkMessage(
				'illustrator',
				scpt
			);

			for (var i=0; i<selectedNum; i++) {
				scpt = "BridgeTalk.bringToFront('illustrator');\r";
				scpt += 'var l = activeDocument.layers.add();\r';
				scpt += 'var p = l.placedItems.add();\r';
				scpt += 'p.file = new File("'+files[i]+'");\r';
				scpt += 'var t = p.trace();\r';
				scpt += 't.tracing.tracingOptions.loadFromPreset("'+VectorizeAI.display.presetDD.selection.value+'");\r';
				scpt += 'app.redraw();\r';
			
				VectorizeAI.sendBridgeTalkMessage(
					'illustrator',
					scpt
				);
			}


		} else {
			// if we are using a multiple docs
			// create a script to:
			//	- for each image
			//		create a doc
			//		place the image
			//		trace the image
			//	- if save-and-close
			//		save and close it using filename format

			var newname = '';
			for (var i=0; i<selectedNum; i++) {
				scpt = 'app.userInteractionLevel=UserInteractionLevel.DONTDISPLAYALERTS;\r';
				scpt += "BridgeTalk.bringToFront('illustrator');\r";
				scpt += 'var doc = app.documents.add();\r';
				scpt += 'var l = activeDocument.layers.add();\r';
				scpt += 'var p = l.placedItems.add();\r';
				scpt += 'p.file = new File("'+files[i]+'");\r';
				scpt += 'var t = p.trace();\r';
				scpt += 't.tracing.tracingOptions.loadFromPreset("'+VectorizeAI.display.presetDD.selection.value+'");\r';
				scpt += 'app.redraw();\r';
				if (VectorizeAI.display.closeCb.value) {
					newname = VectorizeAI.display.dest +'/'+ VectorizeAI.getUserFilename(VectorizeAI.pathinfo(files[i].name).basename);
					scpt += 'var outfile = new File("'+newname+'");\r'
					scpt += 'doc.saveAs(outfile);\r';
					scpt += 'doc.close();\r';
			
					// if this is the last one, add commands to return to bridge and show a success alert
					if (i == selectedNum-1) {
						scpt += "BridgeTalk.bringToFront('bridge');\r";
						scpt += "bridge.executeScript('alert(\"Your files were saved in: "+unescape(VectorizeAI.display.dest)+"\")');\r";
					}
				}
				VectorizeAI.sendBridgeTalkMessage(
					'illustrator',
					scpt
				);
			}


		}
	}
	
	VectorizeAI.cancel = function () {
		this.dialog.close();
	}
	
	VectorizeAI.showDialog = function() {
		this.dialog.show();
	}

	VectorizeAI.initDialog = function(presetList)  {
		this.dialog = new Window("dialog", localize("$$$/WAS/VAIVectorize=Live Trace"));
		this.dialog.orientation = "row";
		this.dialog.alignChildren = 'top';


		//
		// FIRST COLUMN -----------------------------------------------------------
		//

		this.dialog.mainCol = this.dialog.add( "group" );
		this.dialog.mainCol.orientation = 'column';
		this.dialog.mainCol.alignChildren = 'left';

		var preset = this.dialog.mainCol.add( "group" );
		preset.add( "statictext", undefined, localize("$$$/WAS/VAIPreset=Preset:")  );
	
		preset.presetDD = preset.add( "dropdownlist" );
		// loop presetList and add items for each
		var item;
		for (var i=0; i<presetList.length; i++) {
			item = preset.presetDD.add('item', presetList[i]);
			item.value = presetList[i];
		}
		

		// choose first one
		preset.presetDD.selection = preset.presetDD.items[0];

		this.display.presetDD = preset.presetDD;

		// vectorize to layers in single document
		this.dialog.mainCol.layersCb = this.dialog.mainCol.add( "checkbox", undefined, localize("$$$/WAS/VAIVectorizeToLayersInSingleDocument=Vectorize To Layers In Single Document") );
		this.display.layersCb = this.dialog.mainCol.layersCb;
		// add some actions here, if we are going to a single doc, disable saveandclose and naming
		this.dialog.mainCol.layersCb.onClick = function() {
			VectorizeAI.dialog.mainCol.closeCb.enabled = !this.value;
			
			if (this.value) {
				VectorizeAI.dialog.destinationPnl.enabled = false;
			} else {
				VectorizeAI.dialog.destinationPnl.enabled = VectorizeAI.dialog.mainCol.closeCb.value;
			}
		}
		

		// keep open
		this.dialog.mainCol.closeCb = this.dialog.mainCol.add( "checkbox", undefined, localize("$$$/WAS/VAISaveAndClose=Save and Close Results") );
		this.dialog.mainCol.closeCb.value = true;
		this.display.closeCb = this.dialog.mainCol.closeCb;
		this.dialog.mainCol.closeCb.onClick = function() {
			VectorizeAI.dialog.destinationPnl.enabled = this.value;
		}

		
		// source panel
		this.dialog.srcPnl = this.dialog.mainCol.add( "panel", undefined, localize("$$$/WAS/VAISource=Source:") );
		this.dialog.srcPnl.orientation = "column";		
		this.dialog.srcPnl.alignChildren = "left";
		this.dialog.srcPnl.alignment = "fill";
		
		var srclb = this.dialog.srcPnl.add('listbox');
		srclb.alignment = "fill";
		srclb.preferredSize.height = 100;
		srclb.enabled = false;
		var files = VectorizeAI.getBridgeFiles(VectorizeAI.TRACEABLE);
		var l = (files.length>MAX_BATCH_SIZE)?MAX_BATCH_SIZE:files.length;
		for (var i=0; i<l; i++) {
			srclb.add('item', unescape(files[i].name));
		}


		this.dialog.destinationPnl = this.dialog.mainCol.add( "panel", undefined, localize("$$$/WAS/VAIDestination=Destination:") );
		this.dialog.destinationPnl.orientation = "column";		
		this.dialog.destinationPnl.alignChildren = "left";

		// destination
		var dest = this.dialog.destinationPnl.add( "group" ); 
		dest.btn 	= dest.add( "button", undefined, localize("$$$/WAS/VAIChoose=Choose...") );
		this.display.dest = null;
		dest.btn.onClick = function() {
			VectorizeAI.display.dest = Folder.selectDialog(localize("$$$/WAS/VAIChooseOutputDirectory=Choose output directory:"));
			VectorizeAI.display.path.text = unescape(VectorizeAI.display.dest);
		}
		VectorizeAI.display.path = dest.add('statictext', undefined, '');
		VectorizeAI.display.path.preferredSize.width = 150;
		
		// File Name
		var filename = this.dialog.filename = this.dialog.destinationPnl.add("panel", undefined, localize("$$$/WAS/VAIFileNaming=File Naming") );
		filename.orientation = "column";
		filename.alignment = "fill";
		filename.alignChildren = "left";

		// show a sample
		var row = filename.add( "group" );
		var namesample = row.add("statictext", undefined, "MyDocument.ai");
		namesample.preferredSize.width = 200

		// make an array to hold the name parts
		this.display.filename = [];
		var row = filename.add( "group" ); 		
			var name1 = row.add( "dropdownlist" );
			name1.add('item', ' ');
			name1.add('item', localize("$$$/WAS/VAIDocumentName=Document Name") );
			name1.add('item', ".ai" );
			name1.selection = name1.items[1];
			this.display.filename.push(name1);
			
			row.add( "statictext", undefined, " + " );
	
			name2 = row.add( "edittext", undefined, "" );
			name2.preferredSize.width = 100;
			this.display.filename.push(name2);
	
			row.add( "statictext", undefined, " + " );

		var row = filename.add( "group" ); 		
			var name3 = row.add( "dropdownlist" );
			name3.add('item', ' ');
			name3.add('item', localize("$$$/WAS/VAIDocumentName=Document Name") );
			name3.add('item', ".ai");
			name3.selection = name3.items[2];
			this.display.filename.push(name3);
			
			row.add( "statictext", undefined, " + " );
	
			name4 = row.add( "edittext", undefined, "" );
			name4.preferredSize.width = 100;
			this.display.filename.push(name4);
	
			row.add( "statictext", undefined, " + " );


		var row = filename.add( "group" ); 		
			var name5 = row.add( "dropdownlist" );
			name5.add('item', ' ');
			name5.add('item', localize("$$$/WAS/VAIDocumentName=Document Name") );
			name5.add('item', ".ai" );
			this.display.filename.push(name5);
			
			row.add( "statictext", undefined, " + " );
	
			name6 = row.add( "edittext", undefined, "" );
			name6.preferredSize.width = 100;
			this.display.filename.push(name6);

		// add on change actions for all naming items
		name1.onChange = name3.onChange = name5.onChange = function() {
			namesample.text = VectorizeAI.getUserFilename('MyDocument');
		}

		name2.onChange = name4.onChange = name6.onChange = function() {
			namesample.text = VectorizeAI.getUserFilename('MyDocument');
		}



	//
	// SECOND COLUMN --------------------------------------------------------
	//
		// this is the OK/Cancel button group
		this.dialog.btn = this.dialog.add( "group" );
		this.dialog.btn.orientation = "column";
		this.dialog.btn.alignChildren = 'top';
		this.dialog.btn.spacing = 30;
		
		this.dialog.buttonGroup = this.dialog.btn.add( "group", undefined );
		this.dialog.buttonGroup.preferredSize.width = 100;
		this.dialog.buttonGroup.orientation = "column";
		this.dialog.buttonGroup.okButton = this.dialog.buttonGroup.add( "button", undefined, "OK" );
		this.dialog.buttonGroup.cancelButton = this.dialog.buttonGroup.add( "button", undefined, "Cancel" );		

		this.dialog.buttonGroup.okButton.onClick		= this.ok;
		this.dialog.buttonGroup.cancelButton.onClick	= this.cancel;

		this.dialog.center();
	}


	// get the filename using the user specified format
	VectorizeAI.getUserFilename = function(basename) {
		var format = '';
		for (var i=0; i<VectorizeAI.display.filename.length; i++) {
			if (VectorizeAI.display.filename[i].type == 'dropdownlist') {
				// if doc name is choosen
				if (VectorizeAI.display.filename[i].selection == VectorizeAI.display.filename[i].items[1]) {
					format += '\\1';
				} else if (VectorizeAI.display.filename[i].selection == VectorizeAI.display.filename[i].items[2]) {
					format += '.ai';
				}
			} else if (VectorizeAI.display.filename[i].type == 'edittext') {
				format += VectorizeAI.display.filename[i].text;
			}
		}
		return format.replace(/\\1/g, basename);
	}







}