/*
 * tweetable 1.3 - jQuery twitter feed generator plugin
 *
 * Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Revision: $Id: jquery.tweetable.js 2010-08-09 $ 
 *
 */
(function($){
		$.fn.tweetable=function(options){
			var defaults={
				limit:12,
				username:'disenchant_ch',
				time:false,
				replies:false
			};
			var options=$.extend(defaults,options);
			return this.each(function(options){
				var act=$(this);
				var api="http://api.twitter.com/1/statuses/user_timeline.json?include_rts=true&screen_name=";
				var count="&count=";
				$(act).prepend('<ul class="tweetList"><li>Loading ... <img src="/images/spinner.gif"/></li></ul>');
				// $.getJSON(api+defaults.username+count+defaults.limit+"&callback=?",act,function(data){
				$.getJSON(api+defaults.username+count+"40"+"&callback=?",act,function(data){
					$(act).empty();
					$(act).prepend('<ul class="tweetList">');
					
					// $.each(data,function(i,item){
					// 	if(item.in_reply_to_status_id === null){
					// 		$('.tweetList').append('<li class="tweet_content"><span class="tweet_link">'+item.text.replace(/#(.*?)(\s|$)/g,'<span class="hash">#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,'<a href="$&">$&</a>').replace(/@(.*?)(\s|:|\(|\)|$)/g,'<a href="http://twitter.com/$1">@$1</a>$2'));
					// 		
					// 		if ($(".tweet_content:last").text().substr(0,1) == "@") {
					// 			$(".tweet_content:last").remove();
					// 		}
					// 		
					// 		
					// 		if ($(".tweet_content:last").text().substr(0,4) == "RT @") {
					// 			$(".tweet_content:last").html($(".tweet_content:last").html().replace(/RT\s/, ''));
					// 			$(".tweet_content:last").removeClass("tweet_content").addClass("retweet");
					// 		}
					// 	}							
					// });
					
					
					// console.log($(".tweet_content, .retweet").length);
					console.log(data.length);
					
					for (i=0; i < data.length && $(".tweet_content, .retweet").length < defaults.limit;i++) {
						// console.log(data[i]);

						item = data[i];

						if(item.in_reply_to_status_id === null){
							$('.tweetList').append('<li class="tweet_content"><span class="tweet_link">'+item.text.replace(/#(.*?)(\s|$)/g,'<span class="hash">#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig,'<a href="$&">$&</a>').replace(/@(.*?)(\s|:|\(|\)|$)/g,'<a href="http://twitter.com/$1">@$1</a>$2'));
							
							if ($(".tweet_content:last").text().substr(0,1) == "@") {
								$(".tweet_content:last").remove();
							}
							
							
							if ($(".tweet_content:last").text().substr(0,4) == "RT @") {
								$(".tweet_content:last").html($(".tweet_content:last").html().replace(/RT\s/, ''));
								$(".tweet_content:last").removeClass("tweet_content").addClass("retweet");
							}
						}




					}
					
					// alert($(".tweet_content").text());
					
				});
			});
		}		
	})(jQuery);
