// begin boilerplate
// at this point encore_menu is defined as a File object
//	tvStandard is defined as "NTSC" or "PAL"
//	isWideScreenMenu is defined as boolean true or false

// Uncomment the line below to break into the debugger
//	make sure you have enabled the script debugger in the After Effects preferences
// $.bp(true);

// tell the Application to keep running after the end of this script
app.exitAfterLaunchAndEval = false;

// Close out any open project
if (app.project != null)
{
	if( app.project.close(CloseOptions.PROMPT_TO_SAVE_CHANGES) )
	{	// if the user closed it (i.e. didn't cancel the save)
		// create a new project
		app.newProject();
	}
}
else
{
	app.newProject();
}

// Import the file as a new comp item, "newComp"
var import_options = new ImportOptions(encore_menu);
import_options.importAs = ImportAsType.COMP;
var newComp = app.project.importFile(import_options);

newComp.width = inWidth;
if( tvStandard == "NTSC" )
{
	newComp.frameRate = 29.97;
	newComp.height = inHeight;
	if (newComp.width == 720)
	{
		if( isWideScreenMenu )
			newComp.pixelAspect = 1.2121;
		else
			newComp.pixelAspect = 0.9091;
	}
	else	
	{
		if (newComp.width == 1440)
			newComp.pixelAspect = 1.3333;
		else
			newComp.pixelAspect = 1.0;
	}	
}
else if(tvStandard == "PAL" )
{
	newComp.frameRate = 25;
	newComp.height = inHeight;
	if (newComp.width == 720)
	{
		if( isWideScreenMenu )
			newComp.pixelAspect = 1.4587;
		else
			newComp.pixelAspect = 1.0940;
	}
	else	
	{
		if (newComp.width == 1440)
			newComp.pixelAspect = 1.3333;
		else
			newComp.pixelAspect = 1.0;
	}
}

// add the comp to the render queue, then display the render queue window
var newRQItem = app.project.renderQueue.items.add(newComp);
app.project.renderQueue.showWindow(true);

var	renderTemplate;
var	moduleTemplate;

// app.language check is deprecated, it will work for all languages but KOREAN
// https://zerowing.corp.adobe.com/display/aewiki/CS4+Scripting+Changes

if (app.language == Language.ENGLISH) 
{
	renderTemplate = "Best Settings";
	moduleTemplate = "Lossless";
}
else if (app.language == Language.JAPANESE) 
{
	renderTemplate = "最良設定";
	moduleTemplate = "ロスレス圧縮";	// ロスレス圧縮を基準
}
else if( app.language == Language.FRENCH)
{
	renderTemplate = "Rendu optimal";
	moduleTemplate = "Non destructif";
}
else if( app.language == Language.GERMAN)
{
	renderTemplate = "Optimale Einstellungen";
	moduleTemplate = "Verlustfrei";
}
else if( app.language == Language.ITALIAN)
{
	renderTemplate = "Impostazioni migliori";
	moduleTemplate = "Senza perdite";
}
else if( app.language == Language.SPANISH)
{
	renderTemplate = "Configuración óptima";
	moduleTemplate = "Sin pérdida";
}

// app.language is set to ENGLISH in case of KOREAN
// so checking for isolanguage
if(app.isoLanguage == "ko_KR")
{
	renderTemplate = "최고 설정";
	moduleTemplate = "손실 없음";
}

newRQItem.applyTemplate(renderTemplate);
newRQItem.outputModule(1).applyTemplate(moduleTemplate);
if(system.osName.indexOf("Windows") != -1)
	newRQItem.outputModule(1).file = new File( encore_menu_without_ext.toString() + ".avi");
else
	newRQItem.outputModule(1).file = new File( encore_menu_without_ext.toString() + ".mov");

if( ! app.project.file )
{
	app.project.save( new File( encore_menu_without_ext.toString() + ".aep") );
}
