if(typeof(Wordle) == 'undefined') Wordle = {};

// --------------- Start TechBrew.net mod ---------------

/**
 * Call a custom Yahoo Pipe which will extract terms 
 * from an RSS feed and return them via JSONP
 */
Wordle.tb = function (applethost) {
	
	// Disable submit button while Pipe runs
	$('#submit').attr("disabled", "true");
	
	// Get feed URL
	var feed = encodeURIComponent($('#feedurl').val());
	
	// Build Pipe params
	var url = 'http://pipes.yahoo.com/techbrew/wordle?feed=' + feed + '&_render=json&_callback=?';
	
	// Call Pipe with JSONP results
	$('#status').text("Examining feed...");
	$.getJSON(url,
			function(data) {		
				  var terms = "";
				  
				  // Get term lists from each feed item
		      $.each(data.value.items, 
		      			 function(i,item) {
					         terms += (item.content + " ");
					       });
					       
										
					// Pass all terms from all items to existing free-text Wordle function
					$('#status').text("Loading Wordle applet...");
					Wordle.a('text',terms,applethost);
					
					// Enable submit button again
					window.setTimeout("$('#submit').removeAttr('disabled')",1000);
					window.setTimeout("$('#status').text('')",2000);
		   });
};

// --------------- End TechBrew.net mod ---------------

Wordle.a = function (name, value,applethost) {
	$('#controlpanel').hide();
	var param = '<param name="' + name + '" value="' + value + '"/>';
	$('#ac').append('<applet name="wordle" codebase="' + applethost + '" mayscript="mayscript" code="wordle.WordleApplet.class" '
		+ ' archive="/j/v837/wordle.jar" width="100%" height="600">'
		+ param
		+ '</applet>');
	$('#appletpanel').show();		
};
Wordle.d = function (applethost) {
	var user = $('#deliuser').val();
	var url = 'http://feeds.delicious.com/feeds/json/tags/' + encodeURIComponent(user);
	var ah = applethost;
	$.getJSON(url + "?callback=?", function (data) {
		var s = "";
		for (k in data) {
			if (s.length > 0) {
				s += ",";
			}
			s += k.replace(/[,:"]/g,'-') + ":" + data[k];
		}
		if (s.length == 0)
		{
			alert("No tags found for " + user);
			return false;
		}
		Wordle.a('wordcounts',s,ah);
	}); 
};
Wordle.f = function (applethost) {
	var url = $('#feedurl').val();
	google.feeds.lookupFeed(url, function(result) {
		if (result.error) {
			alert(result.error.message);
			return;
		}
		var feed = new google.feeds.Feed(result.url);
		feed.load(function(result) {
			if (result.error) {
				alert(result.error.message);
				return;
			}
			var container = document.getElementById("feed");
			var div = document.createElement("div");
			var t = "";
			for (var i = 0; i < result.feed.entries.length; i++) {
				var entry = result.feed.entries[i];
	           	div.innerHTML = entry.content;
				t += div.innerText || div.textContent;
			}
			Wordle.a('text', t.replace(/["\u0020\t\r\n\v]+/gm, ' '),applethost);
		});
	});
};
Wordle.debugstr = function(s) {
	for (var i = 0; i < s.length; i++)
		console.log(s.charAt(i), s.charCodeAt(i));
}
Wordle.t = function (applethost) {
	var text = $('#paste').val();
	var replaced = text.replace(/["\u0020\t\r\n\v]+/gm, ' ');
	Wordle.a('text', replaced,applethost);
};