/* Tumblr Notes Plugin by Matthew Buchanan v2.02
   Requires jQuery 1.2.6 or higher
   See http://matthewbuchanan.name for installation guide 
   
   [edited a little bit..]*/

$(function(){
	
	$("a.notes").click(function(e){
	
		e.preventDefault();
		
		var id = $(this).attr("rel");
		var node = $("div[tabindex*='" + id + "']");
		var loader = node.find(".notes-loader");
		
		if(loader.find("li").length > 0)
		{
			node.slideToggle();
			return;
		}
		
		var meta = $("ul[tabindex*='" + id + "']");
		var img_node = meta.find("span.loading");
		var notes_url = "http://theme.matthewbuchanan.name/tumblr/tools/proxy/notes/?url=http://www.tumblr.com/dashboard/notes/" + id + "/" + userkey;
		
		meta.parent().attr("id", "hovered");
		
		img_node.prepend('<img class="loading" src="http://tumblr.manasto.info/themes/current/images/ajax-loader.gif" alt="Loading..." />');
		
		$.ajax({ 
			url: notes_url + "&jsonp=1",
			dataType: "jsonp",
			success: function(data){
				img_node.remove();
				loader.html(data);
				node.slideToggle().attr("id", "full_display");
			},
			error: function(d, t, e){
				img_node.remove();
				console.log(d);
			}
		});
		
	});
	
});