var ftpURL = "ftp://ftp.playstream.com";
var username = "";
var password = "";

var local = new ESLocalizer;

// Localised Strings
LoginDialogTitle = local.Localize("/Scripting/FTP/ServerLogin","Server Login");
LoginPanelLabel = local.Localize("/Scripting/FTP/Login","Login");
LoginMessageToUser = local.Localize("/Scripting/FTP/LoginMessage","Please enter login information for uploading file to Streaming Server");
UsernameString = local.Localize("/Scripting/FTP/UserName","Username");
PasswordString = local.Localize("/Scripting/FTP/Password","Password");
ConnectionRetryMessageString = local.Localize("/Scripting/FTP/ConnectionRetryMessage","Could not open FTP connection. Do you want to try again?");
ConnectionRetryTitleString = local.Localize("/Scripting/FTP/ConnectionRetryTitle","Connection Failed");
OKButtonLabel = local.Localize("/Dialogs/OKButton","OK");
UploadFailureMessagePartOne = local.Localize("/Scripting/FTP/UploadFailureMessagePartOne","Failed to upload file ");
UploadFailureMessagePartTwo = local.Localize("/Scripting/FTP/UploadFailureMessagePartTwo"," to the server: ");
UploadFailureTitle = local.Localize("/Scripting/FTP/UploadFailureTitle","Upload Error");


function GetUserNamePasswordThroughUI()
{
	res = "dialog { 	alignChildren: \"center\",\
							text:\"" + localize(LoginDialogTitle) + "\",\
							LoginInfo: Panel { 	orientation: \"column\", alignChildren:\"left\", \
														text: \"" + localize(LoginPanelLabel) + "\",\
														infoStatic: StaticText { text:\"" + localize(LoginMessageToUser) + "\", creation_properties:{multiline:true} }\
														LoginGroup:Group{orientation: \"column\", alignChildren:\"right\",\
																username: Group { orientation: \"row\", \
																							s: StaticText { text:\"" + localize(UsernameString) + ":\" }, \
																							e: EditText { preferredSize: [200, 20] } \
																							} \
																password: Group { orientation: \"row\", \
																							s: StaticText { text:\"" + localize(PasswordString) + ":\" }, \
																							e: EditText { preferredSize: [200, 20] , properties: {noecho: true} } \
																							} \
																}, \
														}\
							buttons: Group { orientation: \"row\", alignment: \"center\", \
													okBtn: Button { text:\"" + localize(OKButtonLabel) + "\", properties:{name:\"ok\"} }, \
													} \
			}"; 

	var ftp = new FTP;
	
	var success = false;
	
	while (!success)
	{
		win = new Window (res); 
		win.LoginInfo.LoginGroup.username.e.text = username;
		win.LoginInfo.LoginGroup.password.e.text = password;
		win.center(); 
		if (win.show() == 2)
		{
			return false;
		}
		username = win.LoginInfo.LoginGroup.username.e.text;
		password = win.LoginInfo.LoginGroup.password.e.text;
		
		
		success = ftp.open(ftpURL, username, password);
		
		if (!success)
		{
			if (!confirm( localize(ConnectionRetryMessageString), false, localize(ConnectionRetryTitleString) ))
			{
				return false;
			}
		}
	}
	
	ftp.close();
	return true;
}


function GetFileNamePlusTitle(inFileName)
{
		var ar = new Object();
		
		var splitFileArray = inFileName.split(".");
		
		ar["extn"] = new String(splitFileArray.pop());
		ar["title"] = splitFileArray.join(".");
		return ar;
}

function Init()
{
	return GetUserNamePasswordThroughUI();
}

function Call( inFilePath)
{
	var ftp = new FTP;

	var fi = new File(inFilePath)
	var fileTitleExtn  = GetFileNamePlusTitle(fi.displayName);
	
	var newFileTitle = fileTitleExtn["title"] + String(Math.random());

	var newFileName = newFileTitle +"." + fileTitleExtn["extn"];
	
	ftp.open(ftpURL, username, password);
	
	while(ftp.exists(newFileName))
	{
		newFileTitle  = newFileTitle + String(Math.random());
		newFileName = newFileTitle +"." + fileTitleExtn["extn"] ;
	}
	
	
	if (!ftp.put(inFilePath, newFileName))
	{
		alert (UploadFailureMessagePartOne + fi.displayName+ UploadFailureMessagePartTwo + ftpURL , UploadFailureTitle);
		ftp.close();
		return "";
	}

	ftp.close();
	//alert(fi.displayName.slice(0, -4));
	return "rtmp://fvss.playstream.com/fvssod/" +username + "/" + newFileTitle;
}

function Close()
{
	username = "";
	password = "";
}
