//Point Product: PS
var log = getLogFile();

/*****************************************************************************/
function getLogFile() {
		if (IsMacintoshOS ())
		{
			var f = new File("~/Library/Logs/Adobe/panel_log.txt");
			return f;
		} else{
			var f = new File(Folder.userData + "/panel_log.txt");
			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 IsPhotoShop32bit() {
	if (app.systemInformation.indexOf("X64") > -1) {
		return '<object><property id="result"><string>64bit</string></property></object>';
	} else {
         return '<object><property id="result"><string>32bit</string></property></object>';
	}
}


/*****************************************************************************/
var writelog = function(message)
{
	//log.open("a");
	//log.writeln(message);
	//log.close();
}

/*****************************************************************************/
var UnloadCSXSPanel = function()
{
	writelog("++++++++++++ Unload CSXSPanel ++++++++++++");
}

/*****************************************************************************/
function runFile(fileName) {
  	  var strXMLResult = '<object><property id="bSuccess">';

	try {
		strXMLResult += '<true/></property>';
		strXMLResult += '<property id="strError"><string></string></property>';

		var f = new File(fileName);
	    if (f.exists) {
			$.evalFile(f, 300000);
	    } else {
	  	alert("File does not exist: " + fileName);
	    }
	}catch(err){
		alert(err);
		strXMLResult = '<object><property id="bSuccess">';
		strXMLResult += '<false/></property>';
		strXMlResult += '<property id="strError"><string></string></property>';
	}
	strXMLResult += '</object>';
	return strXMLResult;

};

/*****************************************************************************/
var runJS = function( script )
{
	var strXMLResult = '<object><property id="bSuccess">';
	try
	{  
		strXMLResult += '<true/></property>';
		strXMLResult += '<property id="strError"><string></string></property>';
		var code = script.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, "#");
		writelog("-----------------------");
		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;
}

/*****************************************************************************/
var createResult = function(key, value)
{
  	var strXMLResult = '<object><property id="bSuccess"><true/></property><property id="strError"><string></string></property>';
	strXMLResult += '<property id="' + key + '"><string>' + value + '</string></property>';
	strXMLResult += '</object>';
	return strXMLResult;
}

/*****************************************************************************/
var createErrorResult = function(message)
{
  	var strXMLResult = '<object><property id="bSuccess">';
  	strXMLResult = '<object><property id="bSuccess"><false/></property>';
	strXMlResult += '<property id="strError"><string>' +  message + '</string></property>';
	strXMLResult += '</object>';
	return strXMLResult;
}

/*****************************************************************************/
var getAppPath = function() 
{
	var str = '<object><property id="path"><string>' + 
				app.path.fsName + '</string></property></object>';
	return str;
}

/*****************************************************************************/
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;
}


/*****************************************************************************/
var getApplicationName = function() 
{ 
	return 'Photoshop';
	//return app.name;
}
var getApplicationVersion = function() 
{ 
	return app.version;
}
var getApplicationLocale = function() 
{ 
	return $.locale;
}
var getLocalHelpFilePath = function() 
{
	var path = "file://" + Folder.commonFiles;
	path = path + "/Adobe/Help/";	
	path += $.locale + "/";
	path += "Photoshop" + "/";
	path += "11.0" + "/";
	return path;
}

var getFeatureFilePath = function() 
{
	var appFile = new File(app.path);
	var path = "file://" + appFile.fsName;
	path = path + "/Locales/";	
	path += $.locale + "/Support%20Files/Feature%20Help/featureSearch.xml";
	writelog ("getFeatureFilePath " + path);
	var str = '<object><property id="path"><string>' + 
				path + '</string></property></object>';
	return str;
}

var getHighlightColor = function()
{
	return "0x3a7ba4";
}	

var getOnlinePreference = function()
{
	return "online";
}	

var getUIBrightness = function()
{
	return "0.5";	// less than .5 is dark
}	

var performAction = function(command)
{
	try
	{
		writelog ('performAction ' + command);
		eval(command);
	}
	catch(error)
	{
		Window.alert("error evaluating command: " + error);
	}
	return;
}	
/*****************************************************************************/

function getToolID(descID)
{
	try {
		var xmlResult = '<object><property id="bSuccess">';
		try
		{  
			xmlResult += '<true/></property>';
			var charID  = getToolCharStringID(descID);
			xmlResult += '<property id="charID"><string>' + 
				charID + '</string></property>';
		}
		catch(err)
		{
			writelog ("error:" + err);
			xmlResult = '<object><property id="bSuccess">';
			xmlResult += '<false/></property>';
			xmlResult += '<property id="strError"><string>' + err + '</string></property>';
		}
		xmlResult += '</object>';
		writelog("result:" + xmlResult);
		return xmlResult;
	}catch(err){
		writelog ("error:" + err);
	}

}


/*****************************************************************************/
//var getToolID = function( indescIDFromSWF ) 
function getToolCharStringID(indescIDFromSWF)
{
	try {
		//convert the string from SWF to a number
		var id = Number(indescIDFromSWF); 
		writelog("id:" + id);
		var desc = new ActionDescriptor();
		desc.fromID(id);
		var idNull = charIDToTypeID('null');
		var ref = desc.getReference(idNull);
		var toolID = ref.getDesiredClass();
		writelog("toolID:" + toolID);
		//use this to compare against spotHealingBrushTool
		var stringID = typeIDToStringID(toolID); 
		writelog("stringID:" + stringID);
		//use this to compare against other tool codes
		var charID = typeIDToCharID(toolID); 
		writelog("charID:" + charID);
		return (charID!=null && charID.length > 0) ? charID : stringID;;
	}catch(err){
		writelog ("error:" + err);
	}
}
