/*tweetable 1.6 - jQuery twitter feed generator plugin  (c) 2009 Philip Beel (http://www.theodin.co.uk/)*/
(function ($) {
    //define the tweetable plugin
    $.fn.tweetable = function (options) {
        //specify the plugins defauls
        var defaults = {
            limit: 5, 						//number of tweets to show
            username: '', 	//@username tweets to display
            time: true, 					//display date
            replies: true,				//filter out @replys
            position: 'append'			//append position
        };
        //overwrite the defaults
        var options = $.extend(defaults, options);
		//loop through each instance
        return this.each(function (options) {
			//assign our initial vars
            var act = $(this);
            var $tweetList;
            var tweetMonth = '';
            var api = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=";
            var count = "&count=";
            //do a JSON request to twitters API
            $.getJSON(api + defaults.username + count + defaults.limit + "&callback=?", act, function (data) {
				//loop through twitters response
                $.each(data, function (i, item) {
					//check for the first loop
                    if (i == 0) {
                    	//create an unordered list to store tweets in
                        $tweetList = $('<div>')[defaults.position.toLowerCase() + 'To'](act);
                    }
                    //handle @reply filtering if required
                    if (defaults.replies === false) {
                        if (item.in_reply_to_status_id === null) {
                            $tweetList.append('<p id="tweet_link_' + i + '">' + item.text.replace(/#(.*?)(\s|$)/g, '<span>#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a target="_blank" href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g, '<a target="_blank" href="http://twitter.com/$1">@$1 </a>$2')+'</p>');
                        }
                    } else {
                        $tweetList.append('<p id="tweet_link_' + i + '">' + item.text.replace(/#(.*?)(\s|$)/g, '<span>#$1 </span>').replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a target="_blank" href="$&">$&</a> ').replace(/@(.*?)(\s|\(|\)|$)/g, '<a target="_blank" href="http://twitter.com/$1">@$1 </a>$2') + '</p>');
                    }
                    //display the tiem of tweet if required
                    if (defaults.time == true) {
                    	
                        $('#tweet_link_' + i).append('<small> ' + relative_time(item.created_at) + '</small>');
                    }
                });
                //close the unordered list
               });
        });
    }
})(jQuery);


function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'ahora mismo';
  } else if(delta < 120) {
    return 'hace un minuto';
  } else if(delta < (60*60)) {
    return ('hace ' + parseInt(delta / 60)).toString() + ' minutos';
  } else if(delta < (120*60)) {
    return 'hace una hora';
  } else if(delta < (24*60*60)) {
    return 'hace unas ' + (parseInt(delta / 3600)).toString() + ' horas';
  } else if(delta < (48*60*60)) {
    return 'ayer';
  } else {
    return ('hace ' + parseInt(delta / 86400)).toString() + ' días';
  }
}

