/****************************************************************************/
/*                                                                          */
/*  Copyright 2006 Adobe Systems Incorporated. All rights reserved.         */
/*                                                                          */
/*  NOTICE:  Adobe permits you to use, modify, and distribute this file in  */
/*  accordance with the terms of the Adobe license agreement accompanying   */
/*  it. If you have received this file from a source other than Adobe,      */
/*  then your use, modification, or distribution of it requires the prior   */
/*  written permission of Adobe.                                            */  
/*                                                                          */
/****************************************************************************/

///////////////////////////////////////////////////////////////////////////////
// Script to load versioncueSDK using ExternalObject.
// Important: Do not load & unload VCSDK multiple times. Ideally VCSDK should be 
// loaded once , use it & unload it when the use is over.
///////////////////////////////////////////////////////////////////////////////

VersionCueSDKLoader = new Object;
var vcSDKHandle = null;

VersionCueSDKLoader.loadSDK = function()
{
	try
	{
		//Load SDK only once
		if(vcSDKHandle == null)
		{
			var sdkFilePath = "lib:";
			if (File.fs == "Windows")
			{
				var vcSideBySideMode = $.getenv("ADOBEVC_USELOCAL_LIBS");				
				if (vcSideBySideMode != null)
				{
					sdkFilePath += "VersionCueSDK.dll";
				}
				else
				{	
					// The env variable delivers backslashes.
					sdkFilePath += $.getenv("CommonProgramFiles");
					sdkFilePath += "/Adobe/Adobe Version Cue CS3/Client/";
					vccVersion  = "3.1.0";
					
					appName    = $.getenv("com.adobe.versioncue.client.appname");
					appVersion = $.getenv("com.adobe.versioncue.client.appversion");
					appLocale  = $.getenv("com.adobe.versioncue.client.applocale");

					/*
					when we install patches we need to tabulate which apps are to use it.
					this file can be updated whenever we install VC patches.
					eg: if (appName == .... && appVersion ==....) 
						{ vcVersion = "3.0.1"; }
					*/
					
					sdkFilePath += vccVersion;
					sdkFilePath += "/VersionCueSDK.dll";
					
					//Replace backslashes to forward
					var sdkFilePathArr = new Array();
					sdkFilePathArr = sdkFilePath.split ("\\");
					var tempPath = "";
					for(var i = 0 ; i < sdkFilePathArr.length; i++)
					{
						if(i !=  sdkFilePathArr.length - 1)
							tempPath += sdkFilePathArr[i] + "/";
						else
							tempPath += sdkFilePathArr[i];
					}
					sdkFilePath = Folder.decode(tempPath);
				}
				//alert(sdkFilePath);
				vcSDKHandle = new ExternalObject(sdkFilePath);
			}
			else
			{
				// Right now this is the only path in MAC.
				sdkFilePath += "//Library/Application Support/Adobe/Adobe Version Cue CS3/Client/"
				vccVersion  = "3.1.0";
				
				appName    = $.getenv("com.adobe.versioncue.client.appname");
				appVersion = $.getenv("com.adobe.versioncue.client.appversion");
				appLocale  = $.getenv("com.adobe.versioncue.client.applocale");
				
				/*
				when we install patches we need to tabulate which apps are to use it.
				this file can be updated whenever we install VC patches.
				eg: if (appName == .... && appVersion ==....) 
					{ vcVersion = "3.0.1"; }
				*/
				
				sdkFilePath += vccVersion;
				sdkFilePath += "/VersionCueSDK";
				sdkFilePath = Folder.decode(sdkFilePath);
				//alert(sdkFilePath);
				vcSDKHandle = new ExternalObject(sdkFilePath);
			}
		}
	}
	catch(e)
	{
		//Log exception
		alert("Exception:" + e.message + "\nFilename: " +  $.fileName + "\nLine:" + e.line);
	}
	return vcSDKHandle;
}

VersionCueSDKLoader.unLoadSDK = function()
{
	var Success = false;
	try
	{
		if(vcSDKHandle != null)
		{
			// externalObject.terminate() only defined in ES version 3.7.72 and higher
			var versionComponents = $.version.split(".");
			if ((versionComponents[0] > 3) ||
				(versionComponents[0] == 3 && versionComponents[1] > 7) || 
				(versionComponents[0] == 3 && versionComponents[1] == 7 && versionComponents[2] >= 72) )
			{
				vcSDKHandle.terminate();
			}
			else
			{
				vcSDKHandle.unload();
			}
			vcSDKHandle = null;
			Success = true;
		}
	}
	catch(e)
	{
		//Log exception
		alert("Exception:" + e.message + "\nFilename: " +  $.fileName + "\nLine:" + e.line);
	}
	return Success;
}