/*

  ###############################################
  #  Feed Visualizer			v0.3.0  #
  #  Created By:		Semmy Purewal,  #
  #				Clay McCauley   #
  #  Last Modified:		     04/21/2009 #
  ###############################################

  @licstart

  This software is free software as defined by the GNU GPL:
	  http://www.gnu.org/licenses/gpl.html

  @licend

*/


var searchString = "pkchs";
var holdTime = 6000;
var fadeInTime = 3000;
var fadeOutTime = 1000;	
var data;
var doneCount;
var doneCheckCount;


function getFeeds() {
	doneCount = 0;
	doneCheckCount = 0;
	data = new Array();
	$("head").append($("<script></script>").attr('src','http://search.twitter.com/search.json?callback=handleTweets&q='+searchString));
	$("head").append($("<script></script>").attr('src','http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=867d7e82cd6b196e83e27d308d97a7f0&format=json&jsoncallback=handleFlickr&tags='+searchString));
	$("head").append($("<script></script>").attr('src','http://www.cs.cofc.edu/~pkchs/wp-json.php?callback=handleBlog'));
	checkIfDone();
}

function checkIfDone() {
	// check to see if a feed is stalled and if so wait a few seconds
	if (doneCount < 2) {
		// increment the counter to tell how many times we've checked
		doneCheckCount++;
		setTimeout('notDoneYet()', 5000);
	}
	else {
		// clear debug text before displaying content
		$(".debug").text("");
		preprocessData();
	}
}


function notDoneYet() {
	// one of the feeds hasn't responded, we'll give it a few more chances...
	if (doneCheckCount < 3) {
		checkIfDone();
	}
	else {
		// notify the user about the delay and try again...
		$(".debug").text("feed stalled, retrying...");
		getFeeds();
	}
}


function handleTweets(results)  {
	var tweets = results.results;
	for(i=0;i < Math.min(tweets.length,10); i++)  {
		var t = jQuery("<div class=\"tweet\">\"" + tweets[i].text.toString() +"\" <span class=\"user\">-- "+tweets[i].from_user+ "</span></div>");
		data.push(t);
	}
	doneCount++;

 
}


function handleFlickr(results)  {
	var photos = results.photos.photo;
	for(i=0; i < Math.min(photos.length,10);i++)  {
		imgURL = "http://farm" + photos[i].farm + ".static.flickr.com/"+photos[i].server+"/"+ photos[i].id + "_" + photos[i].secret + ".jpg";
                var t = jQuery("<div class=\"image\"><p><img class=\"instant pic\" src=\""+imgURL+"\"//></div>"); 
		data.push(t);
	}
	doneCount++;

}


function handleBlog(results)  {
	var posts = results.item;
	for (i=0;i < Math.min(posts.length,20); i++) {
		var t = jQuery("<div class=\"post_title\">" + posts[i].title.toString() + "<br><span class=\"post_desc\">"+posts[i].description.toString()+"</span></div>");
		data.push(t);
	}
	doneCount++;

}


function showData(index)  {
	//$(".debug").text((index+1) + " of " + data.length);
	$(".pkchs").empty();
	data[index].hide();
	$(".pkchs").append(data[index]);
	if(index >= data.length)
		alert(index + " " + data.length);
	data[index].fadeIn(fadeInTime, holdData(index))
}

function holdData(index)  {
	setTimeout('hideData('+index+')', holdTime);
}

function hideData(index)  {
	data[index].fadeOut(fadeOutTime);
	index = (index+1)%data.length;
	if(index == 0)
		setTimeout('getFeeds()', fadeOutTime);
	else
		setTimeout('showData('+index+')', fadeOutTime);	
}


function preprocessData()  {
	//shuffle the data
	var swapIndex;
	var temp;
	for(i = 0; i < data.length; i++)  {
		swapIndex = Math.floor((data.length-i)*Math.random() + i);
		temp = data[swapIndex];
		data[swapIndex] = data[i];
		data[i] = temp;
	}
	showData(0);
}

