String.prototype.trim = function() {
    return this.replace(/^\s+/, '').replace(/\s+$/, '');
};

function ancestorByClass($node, className) {
	if($node[0].tagName == "BODY") {
		console.error("Reached document.body, ancestor not found.");
		return;
	}
	if($node.hasClass(className)) {
		return $node;
	}
	return ancestorByClass($node.parent(), className);	
}

function getFileDiv($node) {
	return ancestorByClass($node,"div-file");l
}

function getIdFromNode($node) {
	return getFileDiv($node).attr("id").split("|")[1];
}

var htmlStorage = {};

function tagsAddSpans(s) {
	var tags = s.split(",");
	for(var i = 0; i < tags.length; i++) {
		tags[i] = "<span>"+tags[i].trim()+"</span>";
	}
	return tags.join(",");
}

function tagsRemoveSpans(s) {
	s = s.replace(/<span>/ig,"");
	s = s.replace(/<\/span>/ig,"");
	return s;
}

function tagsAddQuotes(s) {
	return s;
	var tagArr = s.split(",");
	for(var i = 0; i < tagArr.length; i++) {
		var t = tagArr[i];
		var tArr = t.split(" ");
		if(tArr.length > 1) {
			tagArr[i] = "\""+t+"\"";						
		}
	}
	return tagArr.join(",");
}


$(function(){

$(".uploaded").each(function(i,n) {
	var date = $(n).html();
	var newhtml = Date.parse(date).toString("MMMM d, yyyy");
	$(n).html(newhtml);
});

$(".tags").live("click",function(event) {
	var $target = $(event.target);
	var tag = escape($target.html());//.replace(/\s/ig,"");
	window.location.href = "index.php?tag="+tag;
});

$(".link-edit").live("click", function(){
	var $fileDiv = getFileDiv($(this));
	var fileId = getIdFromNode($(this));
	htmlStorage[fileId] = $fileDiv.html();
	
	var file = {};
	file.filename = $fileDiv.find(".filename").html();
	file.description = $fileDiv.find(".description").html();
	file.tags = $fileDiv.find(".tags").html();
	
	$fileDiv.html($("#hidden-edit").html());
	
	$fileDiv.find("[name='filename']").val(file.filename);
	$fileDiv.find("[name='description']").val(file.description);
	$fileDiv.find("[name='tags']").val(tagsAddQuotes(tagsRemoveSpans(file.tags)));
	
});

$(".link-delete").live("click", function() {
	var $fileDiv = getFileDiv($(this));
	var fileId = getIdFromNode($(this));
	
	var filename = $fileDiv.find(".filename").html();
	
	var sure = confirm("Are you sure you want to delete: "+filename+"?");
	
	if(sure) {
		$.get("process.php?action=deletefile",{fileId:fileId},function(response){
			$fileDiv.fadeOut("slow",function() {
				$(this).remove();
			});
		});
	}
	
});

$(".button-edit-save").live("click", function(){
	var $fileDiv = getFileDiv($(this));
	var fileId = getIdFromNode($(this));
	var newFilename = $fileDiv.find("[name='filename']").val();
	var newDescription = $fileDiv.find("[name='description']").val();
	var newTags = $fileDiv.find("[name='tags']").val();
	$.get("process.php?action=editfile",{
		fileId:fileId,
		filename:newFilename,
		description:newDescription,
		tags:newTags
		},function(response){
			$fileDiv.html(htmlStorage[fileId]);
			$fileDiv.find(".filename").html(newFilename);
			$fileDiv.find(".description").html(newDescription);
			$fileDiv.find(".tags").html(tagsAddSpans(newTags));
	});	
});

$(".button-edit-cancel").live("click", function(){
	var $fileDiv = getFileDiv($(this));
	var fileId = getIdFromNode($(this));
	
	$fileDiv.html(htmlStorage[fileId]);
});



});

