
var tagsubj;

$(document).ready(function(){
	
	initTagAutocomplete();
	
	tagsubj = $("#tagsubj").val();
	
	$(".taglist").addClass("nohov").live("mouseover", function(){
		$(this).removeClass("nohov").addClass("hov");
	}).live("mouseout", function(){
		$(this).removeClass("hov").addClass("nohov");
	});
	$(".tagitem").live("mouseover", function(){
		$(this).children(".rm").show();
	}).live("mouseout", function(){
		$(this).children(".rm").hide();
	});
	
	$("#suggested-tags .sugg a").click(function(e){
		
		e.preventDefault();
		var tag = $(this).html();
		newTag.submit(tag);
		$(this).parent().fadeOut(500, function(){
			$(this).remove();
			if(! $("#suggested-tags .sugg").html() ) $("#suggested-tags").hide();
		});
		
	}).each(function(){
		$(this).addClass("tooltip").attr("title", "click me to add to tag list!");
	});
	
	//remove a tag
	$(".tagitem .rm").live("click", function(){
		
		if(!confirm("Remove this tag?")) return;
		
		$(this).closest(".tagitem").fadeOut(600);
		var tagid = $(this).attr("id");
		
		$.post(
			"/bin/php/class.tags.php",
			{ _action: "rm_tag",
				_tag: tagid
			}, function(res) {
				if(res) {
					alert("Error: "+res);
					$(this).closest(".tagitem").show();
				} $(this).closest(".tagitem").animate({opacity:1}, 300, function(){ $(this).remove() });
			}
		);
		
	});
	
	$(".suggestedtag a").live("click", function(){
		if($(this).hasClass("loading")) return;
		$(this).addClass("loading");
		var tag  = $(this).find('dfn').text();
		if(tag == ""){ $(this).removeClass("loading"); return; }
		var handle = $(this).attr("rel").split(";");
		var tagspace = $("#t"+handle[1]+"-newtagshere");
		if($(tagspace).length) var rmEl = $(this).closest(".suggtagitem");
		else{ tagspace=''; var rmEL=''; }
		newTag.submit(tag, handle[0], tagspace, rmEl);
	});
	
});

function initSuggTag(i){
	$("#t"+i+"-tagspace").fadeIn();
	$(".embvideo").css("visibility", "hidden");
};

function submitNewTagForm(el){
	var tag = $(el).find(":input[name='inptag']").val();
	var subj = $(el).find(":input[name='tagsubj']").val();
	var spaceid = $(el).find(":input[name='tagspaceid']").val();
	newTag.submit(tag, subj, $("#"+spaceid));
	return false;
}

window.newTag = {
	submit: function(tag, subj, space, rmElOnAdd){
		
		if(!subj) { $.jGrowl("Error: no subject given"); return; }
		if(!tag) { $.jGrowl("Error: no tag given"); return; }
		
		$(".tagspace .inptag").val('');
		/*$(".tagspace .inptag").unautocomplete();
		$(".ac_results").hide();
		initTagAutocomplete();*/
		
		confirm_exit = false;
		
		$.post(
			"/bin/php/class.tags.php",
			{ _action: "add_tag",
				_subject: subj,
				_tag: tag
			}, function(res) {
				
				if(res.error){ $.jGrowl("Error: "+res.error); }
				if(res.newtag){
					res.newtag = '<li id="tag-'+res.tagid+'" class="tagitem rmable" style="opacity:0;" onmouseover="$(this).addClass(\'hov\');" onmouseout="$(this).removeClass(\'hov\');"><span class="tag-wrap">'+res.newtag+'<a title="Remove this tag" id="'+res.tagrmid+'" class="rm">x</a></span></li>';
					if(rmElOnAdd){
						$(rmElOnAdd).before(res.newtag);
						$(rmElOnAdd).remove();
					} else if(space) {
						if(!$(space).length) console.log("Add Tag space doesn't exist");
						else $(space).children('.sugg').before(res.newtag).siblings(".notags").remove();
					}
					$("#tag-"+res.tagid).animate({ opacity:1 }, 500, function(){
						$(this).animate({ opacity:.1 }, 500, function(){
							$(this).animate({ opacity:1 }, 1000);
						})
					});
					$(space).find(".notags").css("text-decoration", "line-through");
				}
				
			}, "json"
		);
		
	}
}

function initTagAutocomplete(){
	
	$(".inptag").autocomplete(
		"/bin/php/autocomplete_tags.php",
		{ minChars:0,
			max:30,
			width:234,
			selectFirst:false,
			matchContains:true,
			focusGo:true,
			matchSubset:false,
			formatItem:function(row){
				return '<small>'+row[1]+"</small>"+row[0];
			}
		}
	).result(function(event, data, formatted) {
		if(data[3]) $(this).val(data[3]);
		else $(this).val(formatted);
		var _subj = $(this).siblings("[name='tagsubj']").val();
		var _spid = $(this).siblings("[name='tagspaceid']").val();
		newTag.submit( $(this).val(), _subj, $("#"+_spid) );
	});
	
}
