/*function loadJSON(url) {
  var headID = document.getElementsByTagName("head")[0];
  var newScript = document.createElement('script');
      newScript.type = 'text/javascript';
      newScript.src = url;
  headID.appendChild(newScript);

}*/

function tagsJSON(data){

	if ($(data).length > 0){
		$(".tagContainer").append("<ul></ul>")
		$("<li class='seeAll' onclick='seeAll()'>Show All</li>").appendTo(".tagContainer ul")
		$.each(data, function(tagName,tagCount){
			html = "";
			html += "<li><a href='#' onclick='tagCall(\""+tagName+"\")'>"+tagName+"</a> <span>("+tagCount+")</span></li>";
			$(html).appendTo(".tagContainer ul");
		});
	}
}
function processJSON(data){
	$.each(data, function(i,ele){
		var html = "";
		html += "<div class='bookmarksDiv'>";
		html += "<h2>"+data[i].d+"</h2>";
		html +="<p>"+data[i].n+"</p><p>Link: <a class='links' href='"+data[i].u+"' target='_blank'>"+data[i].u+"</a>";
		html += "<br />Tags: ";
		for(var s=0; s <= data[i].t.length-1; s++ ){
			html += "<a href='#' onclick='tagCall(\""+data[i].t[s]+"\")'>"+data[i].t[s]+"</a>";
			if (s < data[i].t.length-1){ html += ", "; }
		}
		html += "</p></div>";
		$(".bookmarkContent").append(html);
	});
}

function tagCall(tagname){
	$.getJSON("http://feeds.delicious.com/v2/json/berryny/"+tagname+"?callback=?",
		function(data){
			$(".bookmarkContent").hide();
			$(".bookmarkTagContent").empty().show();
			$.each(data, function(i,ele){
				var html = "";
				html += "<div class='bookmarksDiv'>";
				html += "<h2>"+data[i].d+"</h2>";
				html +="<p>"+data[i].n+"</p><p>Link: <a class='links' href='"+data[i].u+"' target='_blank'>"+data[i].u+"</a>";
				html += "<br />Tags: ";
				for(var s=0; s <= data[i].t.length-1; s++ ){
					html += "<a href='#' onclick='tagCall(\""+data[i].t[s]+"\")'>"+data[i].t[s]+"</a>";
					if (s < data[i].t.length-1){ html += ", "; }
				}
				html += "</p></div>";
				$(".bookmarkTagContent").append(html);
			});
		}
	);
	
}

function seeAll() {
	$(".bookmarkTagContent").empty().hide();
	$(".bookmarkContent").show();
}

$().ready(function() {
	$.getJSON("http://feeds.delicious.com/v2/json/berryny?count=40&callback=processJSON&jsoncallback=?");
	$.getJSON("http://feeds.delicious.com/v2/json/tags/berryny?count=20&callback=tagsJSON&jsoncallback=?");
	/*loadJSON('http://feeds.delicious.com/v2/json/berryny?count=100&format=JSON&callback=processJSON'); */
	
});//main