/*
<javascriptresource>
<name>SuperSpray</name>
<menu>filter</menu>
<category>gox</category>
<enableinfo>true</enableinfo>
</javascriptresource>
*/

var filename = new File($.fileName);
var myJSXPath = filename.parent;
var objectsPath=myJSXPath+"/objects/";
var objectsFolder= new Folder(String(objectsPath))
var fileList = objectsFolder.getFiles( /\.(png)$/i );
var longList="";
var splitList= new Array();
splitList=String(fileList).split(',');
var fname=""
var filename = new File($.fileName);
var tempJpgFile = new File();
//
if ($.os.match(/windows/i)=="Windows"){
	inputFolder =  new Folder("C:/SuperSprayTemp")
    inputFolder.create();
    var tempJpgFile = new File( inputFolder + '/' +'tempjpg.jpg' );
	} else if ($.os.match(/Macintosh/i)=="Macintosh") {
    var tempJpgFile = new File( myJSXPath + '/' +'tempjpg.jpg' );
}

function saveJPG(){
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 12;
jpegOptions.embedColorProfile = true;
jpegOptions.matte = MatteType.NONE;
try{
app.activeDocument.saveAs(tempJpgFile,jpegOptions,false);
}catch(e){
    alert(e)
    }
}
    
for (i=0; i< splitList.length; i++){
    fname = String(splitList[i]).replace(/^.*[\\\/]/, '');
    longList=longList+fname+","
    }
longList = longList.substring(0, longList.length - 1)
var win = new Window('dialog', 'SuperSpray');
win.preferredSize = "width:456, height:510";
win.margins=0;

//Preview
var previewSWF = myJSXPath + "/SuperSpray.gks"
var previewMovieToPlay = new File (previewSWF);
win.flashpreview = win.add( 'flashplayer', [0,0,456,510], previewMovieToPlay)
win.onShow=function(){
    this.flashpreview.closeApp = closeApp;
    win.flashpreview.invokePlayerFunction("objectList2Flash",longList);
}
function go(){
    if (app.documents.length==0){alert("Need Something To Work On"); }else{
    app.activeDocument.flatten();
    saveJPG();
    app.activeDocument.activeHistoryState = app.activeDocument.historyStates[app.activeDocument.historyStates.length-2]
    win.center();
    win.show();
    }
}

function closeApp(){
        removeTemp();
        win.close();
    }

function removeTemp(){
if ($.os.match(/windows/i)=="Windows"){
if (tempJpgFile.exists){tempJpgFile.remove();}
if (inputFolder.exists){inputFolder.remove();}
}
}

go()