﻿var log = getLogFile();
var assetsPath = null;
var extensionID = "";

//---------------------------
function onPanelInit(){}

//---------------------------
function onPanelClose(){}

//---------------------------
function onPanelHide(){}

//---------------------------
function onPanelShow(){}

//-------------------------------------------------------------------------------------------------
function setExtensionID(id)
{
    extensionID = id;
}

//-------------------------------------------------------------------------------------------------
function id_invoke_script_file(filepath) {
      var strXMLResult = '<object><property id="bSuccess">';

    try {
        strXMLResult += '<true/></property>';
        strXMLResult += '<property id="strError"><string></string></property>';
        var scriptFile =  decode (filepath);
        scriptFile = translatePath (scriptFile);
        writelog("invoke script file " + scriptFile);
        var f = new File(scriptFile);
        if (f.exists) {
            $.evalFile(f, 300000);
        } else {
        alert("File does not exist: " + scriptFile);
        }
    }catch(err){
        alert(err);
        strXMLResult = '<object><property id="bSuccess">';
        strXMLResult += '<false/></property>';
        strXMlResult += '<property id="strError"><string></string></property>';
    }
    strXMLResult += '</object>';
    return strXMLResult;
};

//-------------------------------------------------------------------------------------------------
function translatePath(path)
{
   //handle relative path
    var f = new File(path)
    {
        if (!f.exists)
        {
            if (assetsPath == null)
            {
                assetsPath = getAssetsPath();
             }
            f = new File(assetsPath + "/" + path);
            if (f.exists)
            {
                return f.fsName;
             }
        }
    }
    return path;
}

//-------------------------------------------------------------------------------------------------
function setAssetsPath(path)
{
    assetsPath = path;
}

//-------------------------------------------------------------------------------------------------
function getAssetsPath()
{
    var mypath = $.fileName;
    writelog("my path =" + mypath);
    var myfile = new File(mypath);
    var folder = myfile.parent;
    var name = myfile.name;
    var index = name.lastIndexOf(".jsx");
    var name = name.substr(0, index);
    return folder.fsName + "/" + name + ".assets";
}

//-------------------------------------------------------------------------------------------------
function decode(s)
{
        var code = s.replace(/#0D;/g, "\n");
        code = code.replace(/#0A;/g, "\r");
        code = code.replace(/#22;/g, "\"");
        code = code.replace(/#27;/g, "'");
        code = code.replace(/#23;/g, "#");
        return code;
}

//-------------------------------------------------------------------------------------------------
function id_invoke_script(script)
{
    var strXMLResult = '<object><property id="bSuccess">';
    try
    {  
        strXMLResult += '<true/></property>';
        strXMLResult += '<property id="strError"><string></string></property>';
        writelog("-----------------------");
        var code = decode(script);
        writelog(code);
        writelog("-----------------------");        
        eval(code);
    }
    catch(err)
    {
        alert(err);
        strXMLResult = '<object><property id="bSuccess">';
        strXMLResult += '<false/></property>';
        strXMlResult += '<property id="strError"><string></string></property>';
    }
    strXMLResult += '</object>';
    return strXMLResult;
}
//-------------------------------------------------------------------------------------------------
function id_invoke_command(id)
{
    writelog("invoke command " + id);
    var n = (new Number(id)).valueOf();
    var a=app.menuActions.itemByID(n); 
    a.invoke();
}

//---------------------------
function getHostLocale()
{ 
    writelog("get host locale " + $.locale);
    return '<object><property id="result"><string>' + $.locale + '</string></property></object>';
}

//---------------------------
function getHostPath()
{ 
    var f = new File(app.filePath);
    var path = f.fsName;
    writelog("get host path " + path);
    return '<object><property id="result"><string>' + path + '</string></property></object>';
}
//---------------------------
function writelog(message)
{
    log.open("a");
    log.writeln(message);
    log.close();
}

//---------------------------
function getLogFile() {
        if (IsMacintoshOS ())
        {
            var f = new File("~/Library/Logs/Adobe/ID07_panel.log");
            return f;
        } else{
            var f = new File(Folder.userData + "/ID07_panel.log");
            return f;
        }
}

//---------------------------
function IsMacintoshOS() {
    if ( $.os.search(/macintosh/i) != -1 ) {
        return true;
    } else {
        return false;
    }
}

//---------------------------
function IsWindowsOS() {
    if ( $.os.search(/windows/i) != -1 ) {
        return true;
    } else {
        return false;
    }
}

//---------------------------
function addListeners()
{
    writelog("add listeners");
    app.addEventListener( 'afterAttributeChanged', onAttrMsg );
    //app.addEventListener('onIdle', onIdle);
}

//---------------------------
function removeListeners()
{
    writelog("remove listeners");
    app.removeEventListener( 'afterAttributeChanged', onAttrMsg );
    //app.removeEventListener('onIdle', onIdle);
}

//---------------------------
function onIdle()
{
    writelog("On idle");
    invokePlayerFunction('<invoke name="handleIdle" returntype="void"><arguments></arguments></invoke>');
}

//---------------------------
function onAttrMsg(event)
{
    var tool = new String(event.attributeValue);
    writelog("onAttrMsg " + tool);
    var index = "$ID/" + tool;
    var menuAction = app.menuActions.item(index);
    var id = menuAction.id;
    writelog("onAttrMsg - " + id);
    invokePlayerFunction('<invoke name="handleAttrMsg_' + extensionID + '" returntype="void"><arguments><string>' + id + '</string></arguments></invoke>' );
}

//---------------------------
function getCurrentToolID()
{
    var tool = app.toolBoxTools.currentToolName;
    writelog("currentToolName " + tool);
    var index = "$ID/" + tool;
    var menuAction = app.menuActions.item(index);
    var id = menuAction.id;
    writelog("currentToolID - " + id);
    var xml = '<object>';
    xml += '<property id="success"><true/></property>';
    xml += '<property id="currentToolID"><string>';
    xml += id;
    xml += '</string></property>';
    xml += '</object>';
    return xml;
}

//---------------------------
function resolveCfgFilePath()
{
    if (IsMacintoshOS ())
    {
        var f = "file://~/Library/Preferences/Macromedia/Flash Player/#Security/FlashPlayerTrust/Configurator.cfg";
        return f;
    } else {
        var f = "file://" + Folder.userData + "/Macromedia/Flash Player/#Security/FlashPlayerTrust/Configurator.cfg";
        return f;
    }
}

//---------------------------
function addSecurityEntry(path)
{
    writelog("call addSecuirytEntry");
    //Try to add this folder to flash player trusted cfg file
    var cfgpath = resolveCfgFilePath ();
    writelog("cfgpath = " + cfgpath);
    var f = new File(cfgpath);
    if (!f.exists)
        f.open("w");
    else 
        f.open("a");
    //var mypath = getMyPath();
    var mypath = path;
    writelog("resolve path is " + mypath);
    f.writeln("\r\n" + mypath);
    f.close();
}

//---------------------------
function checkSecurityEntry(path)
{
    writelog("call checkSecuirytEntry");
    //Try to add this folder to flash player trusted cfg file
    var cfgpath = resolveCfgFilePath ();
    writelog("cfgpath = " + cfgpath);
    var f = new File(cfgpath);
    var result = '<object><property id="result"><string>false</string></property></object>';
    if (f.exists)
    {
        f.open("r");
        while (!f.eof)
        {
            var s = f.readln();
            if (s!=null)
            {
                if (s == path)
                {
                        result = '<object><property id="result"><string>true</string></property></object>';
                        break;
                }
            }
        }
        f.close();
    }
    return result;
}
