/*
* Written by Christopher Pietsch
* Licensed under the GPL (http://www.gnu.org/licenses/gpl-3.0.html).
*/


var lastItem = "?";
var aktiv = window.setInterval("queue(lastItem)", 2000);
queue(lastItem);

function queue(sinceId){
	$.getJSON("http://search.twitter.com/search.json?q=twitpic&since_id="+sinceId+"&rpp=20&callback=?",
	function(data){
		//console.log(data);
		if(!data.error){
			$.each(data.results, function(i,item){
				//$("<img/>").attr("src", item.profile_image_url).appendTo("#images");
				
				var twitpicHash=item.text.match(/http:\/\/twitpic.com\/(\w*)/);
			
				if(twitpicHash){
					var img = new Image();
					$(img)
					.load(function () {
						var $self=$(this)
						.hide()
						.prependTo("#images")
						.wrap('<div style="float:left"><a target="_blank" href="'+twitpicHash[0]+'"></a></div>')

						$self.slideDown('fast');

					})
					.error(function () { })
					.attr("src", "http://twitpic.com/show/thumb/"+twitpicHash[1])
				}
			
							
				if ( i == 0 ) lastItem = item.id;
			});
			//$("#images").append("<br/>");
		}


	});
}



$(document).ready(function() {
	
	//var string= "T @vickytcobra: http://twitpic.com/n91lf - @gabrielsapor";
	//var search = /http:\/\/twitpic.com\/(\w*)/;
	//var res = string.match(/http:\/\/twitpic.com\/(\w*)/);
	
	
	//console.log(res);
	
	
	$("#stop").click(function(){
		window.clearInterval(aktiv);
	})

});

