//Ask the user for a folder whose contents are to be imported



var targetFolder = folderGetDialog("Import Items from Folder..."); //returns a folder or null
if (targetFolder) {

	// if no project open, create a new project to toss the files into. [23839]
	if (!app.project) {
		app.newProject();
	}

	function processFile (theFile) {
		try {
			var importOptions = new ImportOptions (theFile); //create a variable containing ImportOptions
			importSafeWithError (importOptions);
		} catch( error ) {
			// eat errors.
		}
	}
	
	function testForSequence (files){
		var searcher = new RegExp ("[0-9]+");
		var movieFileSearcher = new RegExp ("(mov|avi|mpg)$", "i");
		var parseResults = new Array;
		
		for (x = 0; (x < files.length) & x < 10; x++) { //test that we have a sequence, stop parsing after 10 files
			
			var movieFileResult = movieFileSearcher.exec(files[x].name);
			if (! movieFileResult) {
			
				var currentResult = searcher.exec(files[x].name);
				//regular expressions return null if no match was found
				//otherwise they return an array with the following information:
				//array[0] = the matched string
				//array[1..n] = the matched capturing parentheses
			
				if (currentResult) { //we have a match - the string contains numbers
					//the match of those numbers is stored in the array[1]
					//take that number and save it into parseResults
					parseResults[parseResults.length] = currentResult[0];
				}
				else {
					parseResults[parseResults.length] = null;
				}
			}
			else {
				parseResults[parseResults.length] = null;
			}
		}
		
		//if all the files we just went through have a number in their file names, 
		//assume they are part of a sequence & return the first file
		
		var result = null;
		for (i = 0; i < parseResults.length; ++i) {
			if (parseResults[i]) {
				if (! result) {
					result = files[i];		
				}	
			} else {
				//case in which a file name did not contain a number
				result = null;
				break;
			}
		}
		return result;
	}

	function importSafeWithError (importOptions) {
		try { 
			app.project.importFile (importOptions);
		} catch (error) {
			alert(error.toString() + importOptions.file.fsName);
		}
	}

	function processFolder(theFolder) {
		var files = theFolder.getFiles(); //Get an array of files in the target folder
			
		//test whether theFolder contains a sequence
		var sequenceStartFile = testForSequence(files);
		
		//if it does contain a sequence, import the sequence
		if (sequenceStartFile) {
			try {
				var importOptions = new ImportOptions (sequenceStartFile); //create a variable containing ImportOptions
				
				importOptions.sequence = true;
				//importOptions.forceAlphabetical = true; //un-comment this if you want to force alpha order by default
				importSafeWithError (importOptions);
			} catch (error) {
			
			}
		}
		
		//otherwise, import the files and recurse
		
		for (index in files) { //Go through the array and set each element to singleFile, then run the following
			if (files[index] instanceof File) {
				if (! sequenceStartFile) { //if file is already part of a sequence, don't import it individually
					processFile (files[index]); //calls the processFile function above
				}
			}
			if (files[index] instanceof Folder) {
				processFolder (files[index]); // recursion
			}
		}
	}
	
	processFolder(targetFolder);
}

//Recursively examine that folder
