// WAX_FTPZONE - tools
WAX_FTPZONE.tools = {
		initAddFolder		: function (cur_dir) {
			if(new_dir = prompt("Name of the new directory")) {

				if(new_dir != "")
					WAX_FTPZONE.tools.doAddFolder(cur_dir, new_dir);
				else
					return false;
			} else {
				return false;
			}
		},
		
		doAddFolder			: function (cur_dir, new_dir) {
			var sPostBody = "new_dir="+new_dir+"&action=AJAX_addFolder";	

			new Ajax.Request(
				window.location,
				{
					method		: "post",
					postBody	: sPostBody,
					onSuccess	: function(t) {
						var json	= t.responseText.parseJSON();

						if (json.status.code == "success")
							WAX_FTPZONE.tools.doneAddFolder(cur_dir,new_dir);							
						else
							alert("Error while adding file: " + json.status.message);						
					},
					onFailure	: function(t) {
						alert("Critical Error, please contact administration with this info:\n" + t.status + " -- " + t.statusText);						
					}
				}
			);			
		},
		
		doneAddFolder		: function(cur_dir, new_dir) {	
	
				// do the insertion
				new Insertion.Top($('dir_list'), "<li id=\"dir_"+new_dir+"\">"
		 			+ "<img src=\"images/icons/dir.gif\" alt=\""+new_dir+"\" title=\"directory\" class=\"dirIcon\" />"
					+ '<a href="'+cur_dir+new_dir+'">'
					+ new_dir + '</a>' 
					+ '<a href="#" onclick="javascript:WAX_FTPZONE.tools.initDeleteFolder(\''+new_dir+'\'); return false;" class="delete">Delete</a>'
					+ "</li>");

				// highlight
				new Effect.Highlight('dir_' + new_dir,
					{startcolor:'#ffff33', endcolor:'#FFFFFF', duration:'2'});			
		},
		
		initAddFile			: function (cur_dir, next_dir) {
			OVERLAY.messageOverlay.init();
			
			var form	= "<h2 class=\"addFile\">Add new file</h2>"
						+ "<form id=\"addFileForm\" action=\"" + window.location + "/\" method=\"post\" enctype=\"multipart/form-data\" accept-charset=\"ISO-8895-1\" target=\"addfileframe\" onsubmit=\"javascript:return WAX_FTPZONE.tools.doAddFile(this);\">"
						+ "	<fieldset>"
						+ "		<label>Select a file (pdf, doc, xls, txt, rar, zip, jpg, gif, png)</label>"
						+ "		<input type=\"file\" name=\"new_file\" id=\"dl_file\" class=\"string\" value=\"\" />"
						+ "	</fieldset>"
						+ "	<fieldset>"
						+ "		<img id=\"spinner\" style=\"visibility: hidden; float: left; margin-top: 8px; margin-right: 10px;\" alt=\"\" title=\"\" src=\"images/spinner.gif\" width=\"16\" height=\"16\" />"
						+ "		<input type=\"hidden\" name=\"action\" value=\"AJAX_addFile\" />"
						+ "		<input type=\"hidden\" name=\"dir\" value=\""+next_dir+"\" />"
						+ "		<input type=\"submit\" id=\"formSubmit\" value=\"Upload file\" class=\"button upload\" />"
						+ "	</fieldset>"
						+ "</form>"
						+ "<iframe name=\"addfileframe\" src=\"\" width=\"400\" height=\"100\" style=\"display: none;\"></iframe>";						
		
			 OVERLAY.messageOverlay.showMessage(form, 200);
			 
			 Form.focusFirstElement($('addFileForm'));

		},
		
		doAddFile			: function (form) {
			if (!formChecker.checkForm(form)) {
				return false;
			} else {
				return true;	// returning true since we're upping to a hidden iframe.
			}			
		},
		
		doneAddFile			: function (new_file, new_dir) {
			$('spinner').style.visibility	= "hidden";
			$('formSubmit').disabled		= null;
			OVERLAY.messageOverlay.hideMessage();
			
			// do the insertion
			new Insertion.Top($('files_list'), "<li id=\"file_"+new_file+"\">"
				+ "<img src=\"images/icons/file.gif\" alt=\"file\" title=\"file\" class=\"fileIcon\"/>"
				+ '<a href="'+new_dir+new_file+'">'
				+ new_file + '</a>'
				+ '<a href="#" onclick="javascript:WAX_FTPZONE.tools.initDeleteFile(\''+new_file+'\'); return false;" class="delete">Delete</a>'				
				+ "</li>");

			// highlight
			new Effect.Highlight('file_' + new_file,
				{startcolor:'#ffff33', endcolor:'#FFFFFF', duration:'2'});					
		},
		
		errorAddFile		: function (message) {
			alert("Error while adding file: " + message);
			$('spinner').style.visibility	= "hidden";
			$('formSubmit').disabled		= null;			
		},
		
		initDeleteFile		: function (file) {
			if(confirm("Do you really want to delete this file?")) {
				WAX_FTPZONE.tools.doDeleteFile(file);
			} else {
				return false;
			}
		},
		
		doDeleteFile		: function (file) {
			
			var sPostBody = "del_file="+file+"&action=AJAX_deleteFile";	

			new Ajax.Request(
				window.location,
				{
					method		: "post",
					postBody	: sPostBody,
					onSuccess	: function(t) {
						var json	= t.responseText.parseJSON();

						if (json.status.code == "success")
							WAX_FTPZONE.tools.doneDeleteFile(file);							
						else
							alert("Error while deleting file: " + json.status.message);						
					},
					onFailure	: function(t) {
						alert("Critical Error, please contact administration with this info:\n" + t.status + " -- " + t.statusText);						
					}
				}
			);	
		},
		
		doneDeleteFile	: function(file) {
		
			new Effect.Highlight('file_'+file, {startcolor:'#FFFFFF', endcolor:'#FFB5AF', duration:'2', 
				afterFinish: function() { 
					new Effect.Fold( ($('file_'+file)), {duration:'2'});					
				}});			
		},
		
		initDeleteFolder	: function (folder) {
			if(confirm("Do you really want to delete this directory and all his files?")) {
				WAX_FTPZONE.tools.doDeleteFolder(folder);
			} else {
				return false;
			}
		},
		
		doDeleteFolder		: function (folder) {
			
			var sPostBody = "del_dir="+folder+"&action=AJAX_deleteFolder";	

			new Ajax.Request(
				window.location,
				{
					method		: "post",
					postBody	: sPostBody,
					onSuccess	: function(t) {
						var json	= t.responseText.parseJSON();

						if (json.status.code == "success")
							WAX_FTPZONE.tools.doneDeleteFolder(folder);							
						else
							alert("Error while deleting folder: " + json.status.message);						
					},
					onFailure	: function(t) {
						alert("Critical Error, please contact administration with this info:\n" + t.status + " -- " + t.statusText);						
					}
				}
			);	
		},
		
		doneDeleteFolder	: function(folder) {
		
			new Effect.Highlight('dir_'+folder, {startcolor:'#FFFFFF', endcolor:'#FFB5AF', duration:'2', 
				afterFinish: function() { 
					new Effect.Fold( ($('dir_'+folder)), {duration:'2'});					
				}});			
		},
		
		initReplyComment	: function(form) {
			if (!formChecker.checkForm(form)) {
				return false;
			} else {
				form.action.value = "AJAX_SENDCOMMENT"; 
				WAX_FTPZONE.tools.replyComment(form);	
			}
		},

		replyComment			: function (form) {
			
			var postBody	= Form.serialize(form);

			new Ajax.Request(
				window.location,
				{
					method		: "post",
					postBody	: postBody,
					onSuccess	: function(t) {
						WAX_FTPZONE.tools.doneReplyComment(t.responseText);
					},
					onFailure	: function(t) {
						alert("Critical Error, please contact administration with this info:\n" + t.status + " -- " + t.statusText);
				
						// hide the spinner + re-enable the submitbutton
						$('spinner').style.visibility 	= 'hidden';
						$('formSubmit').disabled		= false;							
					}
				}
			);

		},			
		
		doneReplyComment	: function(responseText) {
			
			var json	= responseText.parseJSON();

			if (json.status.code == "success") {
				
				new Effect.Highlight('comments_reply',
					{startcolor:'#ffff33', endcolor:'#FFFFFF', duration:'2'});	
				
			} else {
				new Effect.Highlight('comments_reply',
					{startcolor:'#FFB5AF', endcolor:'#ffffff', duration:'2'});
				
				alert("Error while sending reply: ".json.status.message);
															
			}

			// hide the spinner + re-enable the submitbutton
			$('spinner').style.visibility 	= 'hidden';
			$('formSubmit').disabled		= false;			
		},
		
		doAddComment		: function(form) {
			form.action.value = "AJAX_ADDCOMMENT"; 
			var postBody	= Form.serialize(form);

			new Ajax.Request(
				window.location,
				{
					method		: "post",
					postBody	: postBody,
					onSuccess	: function(t) {
						WAX_FTPZONE.tools.doneAddComment(t.responseText);
					},
					onFailure	: function(t) {
						alert("Critical Error, please contact administration with this info:\n" + t.status + " -- " + t.statusText);
				
						// hide the spinner + re-enable the submitbutton
						$('spinner').style.visibility 	= 'hidden';
						$('formSubmit').disabled		= false;							
					}
				}
			);			
		},
		
		doneAddComment		: function(responseText) {
			var json	= responseText.parseJSON();

			if (json.status.code == "success") {
				
				new Effect.Highlight('comments',
					{startcolor:'#ffff33', endcolor:'#FFFFFF', duration:'2'});	
				
			} else {
				new Effect.Highlight('comments',
					{startcolor:'#FFB5AF', endcolor:'#ffffff', duration:'2'});
				
				alert("Error while changing comments: ".json.status.message);
															
			}

			// hide the spinner + re-enable the submitbutton
			$('spinner').style.visibility 	= 'hidden';
			$('formSubmit').disabled		= false;			
		}
}