isChangingRemoteDirectory = false;
remoteFileList = null;
remoteWorkDir = null;
remotePreviousValidDir = null
activeDownloads = 0;

function remoteRenderFileListFrame(files) {
	var remoteDoc = remoteFrame.document;
	try
	{
		var renderer = new FTPRenderer(remoteDoc);
		renderer.showSeconds = false;
		renderer.renderNameHeading = function() {
			this.document.write("<th class='ftpHead ftpNameHead' colspan='2'>");
			this.document.write("<a href='javascript:parent.remoteReorder(\"name\");'>Name</a></th>");
			this.document.write("<th class='ftpHead ftpOptions' colspan='4'>Options</th>");
		};
		renderer.renderSizeHeading = function() {
			this.document.write("<th class='ftpHead ftpSizeHead' colspan='2'>");
			this.document.write("<a href='javascript:parent.remoteReorder(\"size\");'>Size</a></th>");
		};
		renderer.renderDateHeading = function() {
			this.document.write("<th class='ftpHead ftpDateHead'>");
			this.document.write("<a href='javascript:parent.remoteReorder(\"date\");'>Date</a></th>");
		};
		renderer.renderFileName = function(fileName, filePath, isTextFile) {
            var safeFileName = fileName.replace(/\\/g, "\\\\").replace(/\'/g, "\\\'");
			this.document.writeln("<td valign='bottom' class='ftpFile ftpIconCell'><img align='bottom' src='images/file.gif'></td>");
			this.document.write("<td class='ftpFile ftpName'");
			this.document.write("<div style='overflow:hidden'>");
			this.document.write("<a href=\"javascript:parent.remoteViewFile('" + safeFileName + "');\"");
			this.document.write(" onmouseover=\"javascript:parent.setToolTip('View \\'" + safeFileName + "\\'')\"")
			this.document.write(" onmouseout=\"javascript:parent.clearToolTip()\"")
			this.document.write(">" + safeFileName + "<\/a>");
			this.document.write("</div>");
			this.document.writeln("<\/td>");
			renderImageLink(this.document, "images/download.png", "Download this file", "downloadFile", safeFileName, null);
			renderImageLink(this.document, "images/rename.png", "Rename this file", "remoteRename", safeFileName, null);
			renderImageLink(this.document, "images/delete.png", "Delete this file", "remoteDeleteFile", safeFileName, null);
			if (isTextFile) {
				renderImageLink(this.document, "images/edit.png", "Edit this file", "remoteEditFile", safeFileName, null);
			} else {
				this.document.write("<td class='ftpFile ftpOptions'></td>");
			}
		};
		renderer.renderDirectoryName = function(directoryName) {
            var safeDirectoryName = directoryName.replace(/\\/g, "\\\\").replace(/\'/g, "\\\'");
			this.document.writeln("<td valign='bottom' class='ftpFile ftpIconCell'><img align='bottom' src='images/folder.gif'></td>");
			this.document.write("<td class='ftpFile ftpName'>");
			this.document.write("<a href=\"javascript:parent.remoteChangeDirectory('" + safeDirectoryName + "');\"");
			this.document.write(" onmouseover=\"javascript:parent.setToolTip('Change to \\'" + safeDirectoryName + "\\'')\"")
			this.document.write(" onmouseout=\"javascript:parent.clearToolTip()\"")
			this.document.write(">" + safeDirectoryName + "<\/a>");
			this.document.writeln("<\/td>");
			if (directoryName!="..")
			{
				this.document.writeln("<td class='ftpFile ftpOptions'><img width=16 height=16 src=\"images/space.gif\"></td>");
				//renderImageLink(this.document, "images/download.png", "Download this directory", "downloadDirectory", directoryName, null);
				renderImageLink(this.document, "images/rename.png", "Rename this directory", "remoteRename", safeDirectoryName, null);
				renderImageLink(this.document, "images/delete.png", "Delete this directory", "remoteDeleteDirectory", safeDirectoryName, null);
				this.document.writeln("<td class='ftpFile ftpOptions'><img width=16 height=16 src=\"images/space.gif\"></td>");
			} else {
				this.document.writeln("<td class='ftpFile ftpOptions' colspan='4'><img width=16 height=16 src=\"images/space.gif\"></td>");
			}
		};
		renderer.renderNameFooter = function() {
			this.document.write("<td class='ftpFooter ftpNameFooter' colspan='2'></td>");
			this.document.write("<td class='ftpFooter ftpOptionsFooter' colspan='4'></td>");
		};
		
		//remoteDoc.clear();
		remoteDoc.writeln("<html>");
		remoteDoc.writeln("<head>");
		remoteDoc.writeln("<link rel='StyleSheet' href='styles/ftpclient.css' type='text/css'>");
		remoteDoc.writeln("<link rel='StyleSheet' href='styles/ftpapp.css' type='text/css'>");
		remoteDoc.writeln("<\/head>");
		remoteDoc.writeln("<body class='ftpBody' onunload='javascript:document.clear()'>");
		renderer.renderFileList(files);
		remoteDoc.writeln("<\/body><\/html>");
		
		isChangingRemoteDirectory = false;
		
		setStatus("Server: " + remoteWorkDir, false, false);
	}
	catch (e)
	{
		ftp.alert(e);
	}
	finally
	{
		remoteDoc.close();
	}
}

function remoteDirRefresh() {
	remoteEnabled(false);
	ftp.directoryList();
}

function remoteEnabled(enabled)
{
	if (remoteDirTextBox!==null) {
		remoteDirTextBox.disabled = !enabled;
	}
	if (remoteGoButton!==null) {
		remoteGoButton.disabled = !enabled;
	}
	if (remoteNewButton!==null) {
		remoteNewButton.disabled = !enabled;
	}
	if (remoteFrame!==null && !enabled) {
		remoteFrame.location = "blankremote.html";
//		remoteFrame.document.clear();
//		remoteFrame.document.writeln("<html><body style='background-color:#eeeeee'></body></html>")
//		remoteFrame.document.close();
	}
	if (remoteListBox!==null) {
		remoteListBox.disabled = !enabled;
	}
}

function remoteDirTextBox_KeyPress(e)
{
	var keyCode = null;
	if (window.event) {
		keyCode = window.event.keyCode;
	} else if (e) {
		keyCode = e.which;
	}
	if (keyCode===13) {
		remoteChangeDirectory(remoteDirTextBox.value);
	}
}

function remoteHomeButton_Click()
{
    remoteChangeDirectory("");
}

function remoteDirButton_Click()
{
    remoteChangeDirectory(remoteDirTextBox.value);
}

function remoteReorder(sortColumn)
{
	if (ftp.remoteSortColumn==sortColumn) {
		ftp.remoteSortAscending = !ftp.remoteSortAscending;
	} else {
		ftp.remoteSortAscending = true;
	}
	ftp.remoteSortColumn = sortColumn;
	remoteDirRefresh();
	setStatus("Getting file-list...", false);
}

function downloadFile(fileName)
{
	if (!ftp.isConnected()) {
		ftp.alert("Not connected to server.");
		return;
	}
	var exists = localFileList!==null && localFileList.containsFileName(fileName);
	if (!exists) {
		doDownloadFile(fileName, ftp.WRITEMODE_OVERWRITE, 0);
	} else {
		var remoteFile = remoteFileList.findByFileName(fileName);
		var localFile = localFileList.findByFileName(fileName);
		var tag = {fileName:fileName, localFileSize:localFile.size};
		if (remoteFile!==null && localFile!==null && remoteFile.size>localFile.size) {
			var text = "The file, " + fileName + ", already exists in the local directory.\n" + 
				"Do you want to overwrite it or resume downloading it?";
			ftp.ask(text, onDownloadFileResponse, tag, "Overwrite", "Resume", "Cancel");
		} else {
			var text = "The file, " + fileName + ", already exists in the local directory.\n" + 
				"Do you want to overwrite it?";
			ftp.ask(text, onDownloadFileResponse, tag, "Overwrite", "Cancel");
		}
	}
}

function onDownloadFileResponse(askResponse, tag)
{
	if (askResponse.response=="Overwrite")
		doDownloadFile(tag.fileName, ftp.WRITEMODE_OVERWRITE, 0);
	else if (askResponse.response=="Resume")
		doDownloadFile(tag.fileName, ftp.WRITEMODE_RESUME, tag.localFileSize);
}

function doDownloadFile(fileName, writeMode, offset)
{
	var filePath = localWorkDir + fileName;
	setStatus("Downloading " + fileName + "...");
	var taskID = ftp.downloadFile(filePath, fileName, writeMode);
	activeDownloads++;
	var progressRow = addProgressBar(taskID, "Downloading " + fileName);
	var file = remoteFileList.findByFileName(fileName);
	transfers.add(taskID, fileName, filePath, file.size, offset, progressRow);
}

function downloadDirectory(directoryName)
{
	ftp.alert("Not implemented");
}

function remoteRename(fileName)
{
	var newName = prompt("Enter the new name", fileName);
	if (newName!==null && newName!=fileName) {
		setStatus("Renaming " + fileName + " to " + newName + "...", false);
		ftp.rename(fileName, newName);
	}
}

function remoteDeleteFile(fileName, doDirListAfter)
{
	if (doDirListAfter!==false) {
		doDirListAfter = true;
	}
	var tag = {fileName:fileName, doDirListAfter:doDirListAfter};
	ftp.ask("Are you sure you want to delete '" + fileName + "'?", "onRemoteDeleteFile", tag, "Delete", "Cancel");
}

function onRemoteDeleteFile(askResponse, tag)
{
	if (askResponse.response=="Delete") {
		setStatus("Deleting " + tag.fileName + "...", false);
		ftp.deleteFile(tag.fileName, tag.doDirListAfter);
	}
}

function remoteDeleteDirectory(dirName, doDirListAfter)
{
	if (doDirListAfter!==false) {
		doDirListAfter = true;
	}
	var tag = {dirName:dirName, doDirListAfter:doDirListAfter};
	ftp.ask("Are you sure you want to delete '" + dirName + "'?", "onRemoteDeleteDirectory", tag, "Delete", "Cancel");
}

function onRemoteDeleteDirectory(askResponse, tag)
{
	if (askResponse.response=="Delete") {
		setStatus("Deleting " + tag.dirName + "...", false);
		ftp.deleteDirectory(tag.dirName, tag.doDirListAfter);
	}
}

function remoteViewFile(fileName, filePath)
{
	setStatus("Viewing file " + fileName + "...", true);
	var filePath = remoteWorkDir;
	if (filePath.substring(filePath.length-1)!=='/') {
		filePath = filePath + "/";
	}
	filePath += fileName;
	var url = "http://127.0.0.1:" + ftp.getRemoteAcceptorPort() + "/INTEGRALFTP_ASK" + filePath + "?_integralftp=yes";
	url = url.replace("#", "%23");
	window.open(url);
}

function remoteEditFile(fileName, filePath)
{
	setStatus("Editing file " + fileName + "...", true);
	var filePath = remoteWorkDir;
	if (filePath.substring(filePath.length-1)!=='/') {
		filePath = filePath + "/";
	}
	filePath += fileName;
	var url = "http://127.0.0.1:" + ftp.getRemoteAcceptorPort() + "/INTEGRALFTP_EDIT" + filePath + "?_integralftp=yes";
	url = url.replace("#", "%23");
	window.open(url);
}

function remoteCreateDir()
{
	if (!ftp.isConnected())
		alert("Not yet connected to server.");
	else {
		var dirName = prompt("Name of directory to create", "");
		if (dirName!==null && dirName!==undefined && dirName!=="") {
			setStatus("Creating directory " + dirName + "...", false);
			ftp.createDirectory(dirName);
		}
	}
}

function remoteChangeDirectory(dirName)
{
	if (!ftp.isConnected())
		alert("You haven't yet connected to the server.\nPlease press Connect/Go.");
	else if (!isChangingRemoteDirectory) {
		setStatus("Changing directory to " + dirName + "...", false);
		isChangingRemoteDirectory = true;
		remoteEnabled(false);
		ftp.changeDirectory(dirName);
	}
}
