// Twitter stream js:
jQuery.fn.reverse = Array.prototype.reverse;
String.prototype.linkify = function() {
  return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
  return m.link(m);
  });
};

String.prototype.linkuser = function() {
  return this.replace(/[@]+[A-Za-z0-9-_]+/, function(u) {
    var username = u.replace("@","")
    return u.link("http://twitter.com/"+username);
  });
};

String.prototype.linktag = function() {
  return this.replace(/[#]+[A-Za-z0-9-_]+/, function(t) {
    var tag = t.replace("#","%23")
    return t.link("http://summize.com/search?q="+tag);
  });

}; 
window.last_id = 0;
function fetch_tweets()
{
	var url = "http://search.twitter.com/search.json?q=django&lang=en&rpp=3&since_id="+window.last_id+"&callback=?";
	$.getJSON(url, function(json)
	{
  		$(json.results).reverse().each(function()
  		{
  			if($('#tw'+this.id).length == 0)
  			{
				var divstr = '<div id="tw'+this.id+'" class="tweet"><img width="48" height="48" src="'+this.profile_image_url+'" align="left"><p class="text">'+this.text.linkify().linkuser().linktag()+' -&nbsp;<b>'+this.from_user+'</b></p></div>';
			
				$('#tweetspace').prepend(divstr);
				$('#tw'+this.id).fadeIn('slow');
				
				window.last_id = this.id;
		 	}
  		});
  		
		setTimeout(fetch_tweets, 10000);
});}

// Makes the latest shows expand and contract
function expander_dls()
{
	var expanders = $('.expander');
	console.log(expanders);
	$('.expander').each(function()
	{
		//$(".expander dd:first",$(this).parent()).css("display", "block");
		$(".expander dt",$(this).parent()).mouseover(
		 function () 
		 {
			if ($(this).next().css("display") == "none") 
			{
				$(".expander dd",$(this).parent().parent()).slideUp("fast");
				$(this).next().slideDown("fast");
			};
		  });
      });
}

// Loads up the various functions we are going to use
$(document).ready(function(){
	expander_dls();	
	fetch_tweets();

});

// let's also implement our voting code


/*
Event.observe(window, 'load', function() {
	var article_blocks = $$('.article-preview');
	$$('div.blurb').map(Element.hide);
	for (var a = 0; a < article_blocks.length; a++)
	{
		var article = article_blocks[a];
		Event.observe(article, 'click', function (e){
			try
			{
				var previewblock = e.element().adjacent('div.blurb');
				previewblock = previewblock[0];
				previewblock.appear();
				
				Event.observe(e.element(), 'click', function (e){
				
					try
					{
						var previewblock = e.element().adjacent('div.blurb');
						previewblock = previewblock[0];
						Effect.Fade(previewblock);
					}
					catch(e)
					{
						console.log(e);
					}
					Event.stop(e);
				});

			}
			catch(e)
			{
				console.log(e);
			}
			Event.stop(e);
		});
			}
	return(false);
});

*/