$(document).ready(function() {
	$(".heading").live("click",showCategory);
	$(".addfile").live("click",addFileDialog);
	$(".downloadfile").live("click",downloadFile);
	$(".deletefile").live("click",deleteFile);
	$(".movefile").live("click",moveFile);
});
function showCategory() {
	var thisId = $(this).attr("id").substr(7);
	if($("#content"+thisId).html().length) {
		$("#content"+thisId).slideToggle("normal",function() {
			if($(this).css("display") == "none") {
				$(this).prev().addClass("collapsed");
			} else {
				$(this).prev().removeClass("collapsed");
			}
		});
	} else {
		$(this).addClass("loading");
		var fileObj = new Object();
		fileObj.method = "loadfiles";
		fileObj.categoryId = thisId;
		rpcCall("files",$.toJSON(fileObj),function(response) {
			$("#content"+response.data.categoryId).html(response.html);
			$("#heading"+response.data.categoryId).removeClass("loading");
			$("#heading"+response.data.categoryId).removeClass("collapsed");
			$("#content"+response.data.categoryId).slideDown();
			createEdit($(".edit",$("#content"+response.data.categoryId)));
			createEdit($(".editarea",$("#content"+response.data.categoryId)),"textarea");
			createEdit($(".edit_select",$("#content"+response.data.categoryId)),"select",publicFields);
			var dataObj = new Object();
			dataObj.action = "files";
			dataObj.data = new Object();
			dataObj.data.method = "changefile";
			$(".edit_upload",$("#content"+response.data.categoryId)).each(function() {
				dataObj.data.id = $(this).attr("id").substr(9);
				$(this).editable(rpcUrl, {
					cancel: "Cancel",
					submit: "Upload",
					type: "ajaxupload",
					data: $.toJSON(dataObj)
				});
			});
			$("table.sort",$("#content"+response.data.categoryId)).tablesorter();
			$(".description_collapse",$("#content"+response.data.categoryId)).click(toggleDescription);
		});
	}
}
function toggleDescription() {
	if($(this).hasClass("collapsed")) {
		$(this).removeClass("collapsed");
	} else {
		$(this).addClass("collapsed");
	}
}
function createEdit(node,editType,editData) {
	if(typeof(editType) == "undefined") editType = "text";
	node.editable(changeFile,{
		cancel: "Cancel",
		submit: "Ok",
		onblur: "ignore",
		type: editType,
		data: editData
	});
}
function addFileDialog() {
	var catId = $(this).attr("id").substr(7);
	showDialog("addfile.php?catId=" + catId,true);
}
function downloadFile() {
	var fileId = $(this).attr("id").substr(12);
	window.open("getfile.php?fileId=" + fileId);
}
function changeFile(val,settings) {
	var changeObj = new Object();
	changeObj.method = "change";
	changeObj.fieldName = $(this).attr("id").substr(0,$(this).attr("id").indexOf("_"));
	changeObj.newValue = val;
	changeObj.id = $(this).attr("id").substr($(this).attr("id").indexOf("_")+1);
	changeObj.revert = this.revert;
	rpcCall("files",$.toJSON(changeObj),function(response) {
		$("#" + response.data.fieldName + "_" + response.data.id).html(response.data.newValue);
	},function(response) {
		alert(response.message);
		$("#" + response.data.fieldName + "_" + response.data.id).html(response.data.revert);
	});
	return "Saving...";
}
function moveFile() {
	var fileId = $(this).attr("id").substr(8);
	showDialog("movefile.php?fileId=" + fileId,false);
}
function deleteFile() {
	var thisId = $(this).attr("id").substr(10);
	if(confirm("Are you sure you wish to delete " + $("#filename_" + thisId).text() + "?")) {
		var fileObj = new Object();
		fileObj.method = "delete";
		fileObj.id = thisId;
		rpcCall("files",$.toJSON(fileObj),function(response) {
			$("#file_1_" + response.data.id).remove();
			$("#file_2_" + response.data.id).remove();
		});
	}
}
