isChangingLocalDirectory = false;
localFileList = null;
localWorkDir = null;
localPreviousValidDir = null
activeUploads = 0;

function localRenderFileListFrame(files) {
	var localDoc = localFrame.document;
	try
	{
		var renderer = new FTPRenderer(localDoc);
		renderer.showSeconds = true;
		renderer.renderNameHeading = function() {
			this.document.write("<th class='ftpHead ftpNameHead' colspan='2'>");
			this.document.write("<a href='javascript:parent.localReorder(\"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.localReorder(\"size\");'>Size</a></th>");
		};
		renderer.renderDateHeading = function() {
			this.document.write("<th class='ftpHead ftpDateHead'>");
			this.document.write("<a href='javascript:parent.localReorder(\"date\");'>Date</a></th>");
		};
		renderer.renderFileName = function(fileName, filePath, isTextFile) {
            var safeFileName = fileName.replace(/\\/g, "\\\\").replace(/\'/g, "\\\'");
            var safeFilePath = filePath.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.localViewFile('" + safeFileName + "', '" + safeFilePath + "');\"");
			this.document.write(" onmouseover=\"javascript:parent.setToolTip('View \\'" + safeFileName + "\\'')\"")
			this.document.write(" onmouseout=\"javascript:parent.clearToolTip()\"")
			this.document.write(">" + fileName + "<\/a>");
			this.document.write("</div>");
			this.document.writeln("<\/td>");
			renderImageLink(this.document, "images/upload.png", "Upload this file", "uploadFile", safeFileName, safeFilePath);
			renderImageLink(this.document, "images/rename.png", "Rename this file", "localRename", safeFileName, safeFilePath);
			renderImageLink(this.document, "images/delete.png", "Delete this file", "localDeleteFile", safeFileName, safeFilePath);
			if (isTextFile) {
				renderImageLink(this.document, "images/edit.png", "Edit this file", "localEditFile", safeFileName, safeFilePath);
			} else {
				this.document.write("<td class='ftpFile ftpOptions'></td>");
			}
		};
		renderer.renderDirectoryName = function(directoryName, directoryPath) {
			this.document.writeln("<td valign='bottom' class='ftpFile ftpIconCell'><img align='bottom' src='images/folder.gif'></td>");
			if (directoryName==="") {
				directoryName = directoryPath;
			}
			var safeDirectoryName = directoryName.replace(/\\/g, "\\\\").replace(/\'/g, "\\\'");
            var safeDirectoryPath = directoryPath.replace(/\\/g, "\\\\").replace(/\'/g, "\\\'");
			this.document.write("<td class='ftpFile ftpName'>");
			this.document.write("<a href=\"javascript:parent.localChangeDirectory('" + safeDirectoryPath + "');\"");
			this.document.write(" onmouseover=\"javascript:parent.setToolTip('Change to \\'" + safeDirectoryPath + "\\'')\"")
			this.document.write(" onmouseout=\"javascript:parent.clearToolTip()\"")
			this.document.write(">" + directoryName + "<\/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/upload.png", "Upload this file", "uploadDirectory", safeDirectoryName, safeDirectoryPath);
				renderImageLink(this.document, "images/rename.png", "Rename this file", "localRename", safeDirectoryName, safeDirectoryPath);
				renderImageLink(this.document, "images/delete.png", "Delete this file", "localDeleteFile", safeDirectoryName, safeDirectoryPath);
				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>");
		};
		
		localDoc.clear();
		localDoc.writeln("<html>");
		localDoc.writeln("<head>");
		localDoc.writeln("<link rel='StyleSheet' href='styles/ftpclient.css' type='text/css'>");
		localDoc.writeln("<link rel='StyleSheet' href='styles/ftpapp.css' type='text/css'>");
		localDoc.writeln("<\/head>");
		localDoc.writeln("<body class='ftpBody'>");
		renderer.renderFileList(files);
		localDoc.writeln("<\/body><\/html>");
		
		isChangingLocalDirectory = false;
		
		setStatus("Local:" + localWorkDir, false, false);
	}
	finally
	{
		localDoc.close();
	}
}

function localDirRefresh(dir) 
{
	if (!ftp.isInitialized())
		alert("You haven't yet connected to the server.\nPlease press Connect/Go.");
	else {
		localEnabled(false);
		ftp.localDirectoryList(dir);
	}
}

function localEnabled(enabled)
{
	if (localDirTextBox!==null) {
		localDirTextBox.disabled = !enabled;
	}
	if (localGoButton!==null) {
		localGoButton.disabled = !enabled;
	}
	if (localNewButton!==null) {
		localNewButton.disabled = !enabled;
	}
	if (localFrame!==null && !enabled) {
		localFrame.location = "blanklocal.html";
//		localFrame.document.clear();
//		localFrame.document.writeln("<html><body style='background-color:#eeeeee'></body></html>")
//		localFrame.document.close();
	}
	if (localListBox!==null) {
		localListBox.disabled = !enabled;
	}
}

function localHomeButton_Click()
{
    localDirRefresh("");
}

function localDirButton_Click()
{
	localDirRefresh(localDirTextBox.value);
}

function localDirTextBox_KeyPress(ev)
{
	if (enterPressed(ev)) {
		setStatus("Getting file-list...", false, false);
		localDirRefresh(localDirTextBox.value);
	}
}

function localReorder(sortColumn)
{
	if (ftp.localSortColumn==sortColumn) {
		ftp.localSortAscending = !ftp.localSortAscending;
	} else {
		ftp.localSortAscending = true;
	}
	ftp.localSortColumn = sortColumn;
	setStatus("Getting file-list...", false, false);
	localDirRefresh(localWorkDir);
}

function uploadFile(fileName, filePath)
{
	if (!ftp.isConnected()) {
		ftp.alert("Not connected to server.");
		return;
	}
	var exists = remoteFileList!==null && remoteFileList.containsFileName(fileName);
	if (!exists) {
		doUploadFile(fileName, filePath, ftp.WRITEMODE_OVERWRITE, 0);
	} else {
		var remoteFile = remoteFileList.findByFileName(fileName);
		var localFile = localFileList.findByFileName(fileName);
		var tag = {fileName:fileName, filePath:filePath, remoteFileSize:remoteFile.size};
		if (remoteFile!==null && localFile!==null && remoteFile.size<localFile.size) {
			var text = "The file, " + fileName + ", already exists on the server.\n" + 
				"Do you want to overwrite it or resume uploading it?";
			ftp.ask(text, onUploadFileResponse, tag, "Overwrite", "Resume", "Cancel");
		} else {
			var text = "The file, " + fileName + ", already exists on the server.\n" + 
				"Do you want to overwrite it?";
			ftp.ask(text, onUploadFileResponse, tag, "Overwrite", "Cancel");
		}
	}
}

function onUploadFileResponse(askResponse, tag)
{
	if (askResponse.response=="Overwrite")
		doUploadFile(tag.fileName, tag.filePath, ftp.WRITEMODE_OVERWRITE, 0);
	else if (askResponse.response=="Resume")
		doUploadFile(tag.fileName, tag.filePath, ftp.WRITEMODE_RESUME, tag.remoteFileSize);
}

function doUploadFile(fileName, filePath, writeMode, offset)
{
	setStatus("Uploading " + fileName + "...", false);
	var taskID = ftp.uploadFile(filePath, fileName, writeMode);
	activeUploads++;
	var progressRow = addProgressBar(taskID, "Uploading " + fileName);
	var file = localFileList.findByFileName(fileName);
	transfers.add(taskID, fileName, filePath, file.size, offset, progressRow, false);
}

function uploadDirectory(directoryName, directoryPath)
{
	ftp.alert("Not implemented");
}

function localRename(fileName, filePath)
{
	var newName = prompt("Enter the new name", fileName);
	if (newName!==null && newName!=fileName) {
		var dir = filePath;
		if (dir.substring(dir.length-1)==ftp.separator) {
			dir = dir.substring(0, dir.length-1);
		}
		dir = dir.substring(0, dir.lastIndexOf(ftp.separator));
		setStatus("Renaming " + fileName + " to " + newName + "...", false);
		ftp.localRename(filePath, dir + ftp.separator + newName);
	}
}

function localDeleteFile(fileName, filePath, doDirListAfter)
{
	if (doDirListAfter===undefined) {
		doDirListAfter = true;
	}
	var tag = {fileName:fileName, filePath:filePath, doDirListAfter:doDirListAfter};
	ftp.ask("Are you sure you want to delete '" + fileName + "'?", "onLocalDeleteFile", tag, "Delete", "Cancel");
}

function onLocalDeleteFile(askResponse, tag)
{
	if (askResponse.response=="Delete") {
		setStatus("Deleting " + tag.fileName + "...", false);
		ftp.localDeleteFile(tag.filePath, tag.doDirListAfter);
	}
}

function localViewFile(fileName, filePath)
{
	setStatus("Viewing file " + fileName + "...", true);
	filePath = filePath.replace(/\\/g,"/");
	var url = "http://127.0.0.1:" + ftp.getLocalAcceptorPort() + "/INTEGRALFTP_ASK/" + filePath + "?_integralftp=yes";
	url = url.replace("#", "%23");
	window.open(url);
}

function localEditFile(fileName, filePath)
{
	setStatus("Viewing file " + fileName + "...", true);
	filePath = filePath.replace(/\\/g,"/");
	var url = "http://127.0.0.1:" + ftp.getLocalAcceptorPort() + "/INTEGRALFTP_EDIT/" + filePath + "?_integralftp=yes";
	url = url.replace("#", "%23");
	window.open(url);
}

function localChangeDirectory(directoryName)
{
	if (!ftp.isInitialized())
		alert("You haven't yet connected to the server.\nPlease press Connect/Go.");
	else if (!isChangingLocalDirectory) {
		setStatus("Changing directory to " + directoryName + "...", false);
		isChangingLocalDirectory = true;
		localWorkDir = directoryName;
		localDirRefresh(localWorkDir);
	}
}

function localCreateDir()
{
	var dirName = prompt("Name of directory to create", "");
	if (dirName!==null && dirName!==undefined && dirName!=="") {
		setStatus("Creating directory " + dirName + "...", false);
		ftp.localCreateDirectory(localWorkDir + dirName);
	}
}
