﻿//Tweetcloud

     $(function() {
	// Creat cloud container mark-up
	var cloud = $("div#cloud");
	$(cloud).html("<div class=\"top\"><div class=\"right\"></div></div><div class=\"content\"></div><div class=\"bottom\"><div class=\"right\"></div></div>");
	var content = $(cloud).find("div.content");
	$(content).empty();
	
	// Get XML data for the Tweetcloud
	// Inject an <a> tag into the cloud contents container for each one
	// Filter out tags that match the search terms
	// Display the cloud with a fade in effect
	$.get("/NR/rdonlyres/8F3D3F90-1F5A-427B-9DCF-EF47EE47A592/0/tweetcloud.xml",
		{},
		function(data){
			$("tag", data).each(
				function(node){
					var size = $(this).attr("size");
					var tag = $(this).attr("tag");
					var searchTerms = ["XboxE3"];
					var url = "http://search.twitter.com/search?q="+ searchTerms.join("+") +"+" + tag;
					if (searchTerms.join(" ").toLowerCase().indexOf(tag.toLowerCase()) == -1) {
						$(content).append("<a href=" + url + " class=\"type" + size + "\" target=\"_blank\">" + tag + "</a> ");
					}
				}
			);
			$(cloud).fadeIn();
		}
	);
	
	// Capture any errors in the AJAX response and display a friendly message.
	$(content).ajaxError(
		function(event, request, settings){
			$(this).append("Sorry, the What's Hot Tweetcloud is not currently unavailable.");
			$(cloud).fadeIn();
		}
	);
});
