/*
 * TwitStream - A jQuery plugin for the Twitter Search API
 * Version 1.2
 * http://kjc-designs.com/TwitStream
 * Copyright (c) 2009 Noah Cooper
 * Licensed under the GNU General Public License <http://www.gnu.org/licenses/>
*/
String.prototype.linkify=function(){
	return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&;\?\/.=]+/g,function(m){
		return m.link(m);
	});
};
String.prototype.linkuser=function(){
	return this.replace(/[@]+[A-Za-z0-9-_]+/g,function(u){
		return u.link("http://twitter.com/"+u.replace("@",""));
	});
};
String.prototype.linktag=function(){
	return this.replace(/[]+[A-Za-z0-9-_]+/,function(t){
		return t;
	});
};
var showTweetLinks='none';

var currentID;

var lastShownID = 0;

function fetch_twitID(){
	var url="http://search.twitter.com/search.json?q="+keyword+"&rpp=1&callback=?";
	$.getJSON(url,function(json){
		$(json.results).each(function(){
			
			lastID=(this.id);
			alert('twitID:' + lastID);		
			
		});
	});
	return(false);
}

var myCount = 0;
var foreachCount = 0;

function check_for_failure()
{
	if(foreachCount == 0)
	{
		var fw = '<div align="center"><img src="images/failwhale.png"></div>';
		$("#tweets").prepend(fw);
		foreachCount++;
	}
}

function fetch_tweets(elem){	
	elem=$(elem);
	keyword=escape(elem.attr('title'));
	num=elem.attr('class').split(' ').slice(-1);	
	
	//foreachCount = 0;
	window.setInterval('check_for_failure()', 10000);
	
	var url="http://search.twitter.com/search.json?q="+keyword+"&rpp="+num+"&callback=?";
	$.getJSON(url,function(json){
		$(json.results).each(function(){
			foreachCount++;
			if(myCount >= 10 && lastShownID >= (this.id)) { /* skip this tweet, 'continue' not working here */ }
			else
			{
				myCount++;
				if((this.id) > lastShownID) lastShownID = (this.id);
				
				var tTime=new Date(Date.parse(this.created_at));
				var cTime=new Date();
				var sinceMin=Math.round((cTime-tTime)/60000);
				
	
				
				
				//var lastID = (this.id);
	
	
				/*alert(this.id +' + '+ lastID);
	
				if(this.id<lastID){
				
				alert('this < last'+ this.id + lastID)
				
				};
				*/
				
				if(sinceMin==0){
					var sinceSec=Math.round((cTime-tTime)/1000);
					if(sinceSec<10)
						var since='less than 10 seconds ago';
					else if(sinceSec<20)
						var since='less than 20 seconds ago';
					else
						var since='half a minute ago';
				}
				else if(sinceMin==1){
					var sinceSec=Math.round((cTime-tTime)/1000);
					if(sinceSec==30)
						var since='half a minute ago';
					else if(sinceSec<60)
						var since='less than a minute ago';
					else
						var since='1 minute ago';
				}
				else if(sinceMin<45)
					var since=sinceMin+' minutes ago';
				else if(sinceMin>44&&sinceMin<60)
					var since='about 1 hour ago';
				else if(sinceMin<1440){
					var sinceHr=Math.round(sinceMin/60);
					if(sinceHr==1)
						var since='about 1 hour ago';
					else
						var since='about '+sinceHr+' hours ago';
				}
				else if(sinceMin>1439&&sinceMin<2880)
					var since='1 day ago';
				else{
					var sinceDay=Math.round(sinceMin/1440);
					var since=sinceDay+' days ago';
				}
				
				since = tTime;
				
				var tweetBy='<a class="tweet-user" target="_blank" href="http://twitter.com/'+this.from_user+'">@'+this.from_user+'</a> <span class="tweet-time">'+since+'</span>';
				if(showTweetLinks.indexOf('reply')!=-1)
					tweetBy=tweetBy+' &middot; <a class="tweet-reply" target="_blank" href="http://twitter.com/?status=@'+this.from_user+' &in_reply_to_status_id='+this.id+'&in_reply_to='+this.from_user+'">Reply</a>';
				if(showTweetLinks.indexOf('view')!=-1)
					tweetBy=tweetBy+' &middot; <a class="tweet-view" target="_blank" href="http://twitter.com/'+this.from_user+'/statuses/'+this.id+'">View Tweet</a>';
				if(showTweetLinks.indexOf('rt')!=-1)
					tweetBy=tweetBy+' &middot; <a class="tweet-rt" target="_blank" href="http://twitter.com/?status=RT @'+this.from_user+' '+escape(this.text.replace(/&quot;/g,'"'))+'&in_reply_to_status_id='+this.id+'&in_reply_to='+this.from_user+'">RT</a>';
				var tweet='<div class="tweet" style="display:none" id="tweet_'+(this.id)+'"><div class="tweet-left"><a target="_blank" href="http://twitter.com/'+this.from_user+'"><img width="48" height="48" alt="'+this.from_user+' on Twitter" src="'+this.profile_image_url+'" /></a></div><div class="tweet-right"><p class="text"><a target="_blank" href="http://twitter.com/'+this.from_user+'">'+this.from_user+'</a> '+this.text.linkify().linkuser().linktag().replace(/<a/g,'<a target="_blank"')+'</p><div class="timestamp">'+since+'</div></div></div>';
				
				if(myCount > 10) elem.prepend(tweet);
				else elem.append(tweet);

				$("#tweet_"+(this.id)).fadeIn("slow");
			
			} // end else
		
		});
	});
	return(false);
}

//$(function(){
function go_fetch() {
	//alert("checking for and fetching any new tweets");
	showTweetLinks=showTweetLinks.toLowerCase();
	if(showTweetLinks.indexOf('all')!=-1)
		showTweetLinks='reply,view,rt';
	$('.twitStream').each(function(){
		fetch_tweets(this);
	});
}
//});
