/* thanks to: 

ramiro at 
http://www.seo-expert-blog.com/blog/parsing-yahoo-pipes-json-feeds-with-jquery

and

scott
http://weblogs.asp.net/skillet/archive/2006/03/23/440940.aspx

*/

/* global variables */
var descList = new Array();
var itemList = "";
var SUPERFEED_MAX = 64;
var VISIBLE = new Object();
	VISIBLE['blip'] = 1;
	VISIBLE['flickr'] = 1;
	VISIBLE['tm'] = 1;
	VISIBLE['delicious'] = 1;
	VISIBLE['reader'] = 1;
var superfeed;

$(document).ready(function() {
  for (var key in VISIBLE) {
  	if (VISIBLE[key] == 1) {
  		$("#" + key + "box").attr("checked", true);
  	}	
  }

  var content = "";
  $.getJSON("pipes.php",
		function(jsondata) {
			superfeed = jsondata;
			updateSuperfeed();
		});

	$.get('twitter.php',function(data){
		updateTwitter(data);
	});


});

function parseForURL(tweet) {

/* from http://snippets.dzone.com/posts/show/452 
	var regexp_url = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gim
*/
	var regexp_url = /(ftp|http|httpsi):\/\/(.+?)( |$)/gim
	
	return tweet.replace(regexp_url, "<a href=\"$1://$2\">$1://$2</a>$3");
	
}

function updateTwitter(xml) {
	var htmlTwitter = '';
	
	$(xml).find("item").each(function()
	{

		htmlTwitter += '<div class="twitterpost">';
		htmlTwitter += '<h2 class="twitter">' + parseForURL($(this).find("title").text().slice(15)) + '</h2>';
		htmlTwitter += '<p class="twitter"><a href="' + $(this).find("guid").text() + '">' + $(this).find("pubDate").text().split("+",1)[0] + '</a></p></div>\n';
	});

	htmlTwitter += '<div class="twitterpost_final"><h2 class="twitter"><a href="http://twitter.com/kevindriscoll">follow</h2><p class="twitter">@kevindriscoll</p></div>\n';
	$("#twitter-updates").html(htmlTwitter);

}

function updateVisibility() {
	for (var key in VISIBLE) {
		VISIBLE[key] = $("#" + key + "box").attr("checked");
	}
	updateSuperfeed();
}

function updateSuperfeed() {

	var htmlSuperfeed = '';
	
	if (superfeed.count > 0) {
		htmlSuperfeed += trav_json(superfeed);
	}
	
	htmlSuperfeed += "<div class='feeditem_final'>\n";
	htmlSuperfeed += "<h2 class='feedtitle'><a href='http://pipes.yahoo.com/pipes/pipe.run?_id=5m_etFHu3RGfmcMlQBJ3AQ&_render=rss'>subscribe to the super feed</a></h2>\n";
	htmlSuperfeed += "<p class='feeddate'><a href='http://pipes.yahoo.com/kevindriscoll/superfeed'>or see the pipes</a></h2>\n";
	htmlSuperfeed += "</div>\n";
	$("#pipes-feed-content").html(htmlSuperfeed);

}


function findSource(item) {
	try {
  	if (item.link.match("believekevin") != null) {
  		// flickr
  		return new Array('flickr','http://flickr.com/photos/believekevin');
  	} else if (item.link.match("todomundo") != null) {
  			// todo mundo blog
  			return new Array('tm','http://todomundosound.com/');
  	} else if (item.link.match("blip") != null) {
  			// blip
  			return new Array('blip','http://todomundo.blip.tv/');
  	} else if ("comments" in item) {
  		// delicious
  		return new Array('delicious','http://del.icio.us/believekevin');
  	} else {
  		// google reader
  		return new Array('reader','http://www.google.com/reader/shared/13102408274774238777');
  	}
	} catch(err) {
	//	alert(err);
	}
}

function trav_json(json) {
			
	var i = 0;

	var count = 0;
	var html = '';

	var err = '';

	var max = (json.count > SUPERFEED_MAX) ? SUPERFEED_MAX : json.count;

	while ((i<json.count) && (count<max)) {
		source = findSource(json.value.items[i]);
		try {
		if (VISIBLE[source[0]]) {
  		html += "<div class='feeditem_" + source[0] + "'>\n";
  		html += "<h2 class='feedtitle'><a href='" + json.value.items[i].link + "'>" + json.value.items[i].title + "</a></h2>\n";
  		html += "<p class='feeddate'><a href='" + source[1] + "'>" + json.value.items[i].pubDate.slice(0,(json.value.items[i].pubDate.length-6)) + "</a></p>\n";
  		html += "</div>\n";
  		count++;
		}
		} catch(err) {
			err += ('count:'+count+', ');
			err += ('max:'+max)+', ';
			err += ('i:'+i+'\n');
		}
		i++;
	}
	if (err != '') {
		alert(err);
	}
	return html;

}


var MAX_DUMP_DEPTH = 10;
function dumpObj(obj, name, indent, depth) {
	if (depth > MAX_DUMP_DEPTH) {
		return indent + name + ": <Maximum Depth Reached>\n";
	}
	if (typeof obj == "object") {
		var child = null;
		var output = indent + name;
		var total = 0;
		if (obj instanceof Array)
		{
			total = obj.length;
			output += " (Array)\n";
		} else {
			for (var item in obj)
			{
				total++;
			}
			output += " (Object)\n";
		}
		output += indent + "Total item: " + total + "\n";
		indent += "\t";
		if (obj instanceof Array)
		{
			for (var i=0; i<obj.length; i++)
			{
				child = obj[i];
				output += dumpObj(child, i, indent, depth + 1);
			}
		} else {
  		for (var item in obj)
  		{
  			try {
  				child = obj[item];
  			} catch (e) {
  				child = "<Unable to Evaluate>";
  			}
  			if (typeof child == "object") {
  				output += dumpObj(child, item, indent, depth + 1);
  			} else {
  				output += indent + item + ": " + child + "\n";
  			}
  		}
		}
		return output;
	} else {
		return obj + " is not an object.";
	}
}



