﻿$(document).ready(function() {
	// Referencia: http://icode4you.net/creating-a-custom-twitter-feed-with-search-results
    // json call to twitter to request tweets containing our keyword, in this case 


    $.getJSON("http://search.twitter.com/search.json?q=from:kwtelevision&callback=?", function(data) 

{
        // loop around the result
        $.each(data.results, function() {
            var text = this.text;
    
            if(text.charAt(0) != '@') {
                // construct tweet and add append to our #tweets div
                var tweet = $("<div></div>").addClass('tweet').html(text);
                // analyse our tweet text and turn urls into working links, hashtags into search links, and @replies into profile links.
                tweet.html('<div>' +
                    tweet.html()
                    .replace(/((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi,'<a href="$1" target="_blank">$1</a>')
                    .replace(/(^|\s)#(\w+)/g,'$1<a href="http://search.twitter.com/search?q=%23$2" target="_blank">#$2</a>')
                    .replace(/(^|\s)@(\w+)/g,'$1<a href="http://twitter.com/$2" target="_blank">@$2</a>')
                    + '<br /><a href="http://www.twitter.com/' + this.from_user + '/status/' + this.id_str + '" target="_blank">' + $.timeSinceTweet(this.created_at) + '</a></div>')
                    //.prepend('<a href="http://www.twitter.com/' + this.from_user + '" target="_blank"><img src="' + this.profile_image_url + '" width="48" height="48" /></a>')
                    .appendTo('#tweets')
                    .fadeIn();
            }
        });
    });
});

(function($) {
    $.timeSinceTweet = function(time) {
        var date = new Date(time);
        var diff = ((new Date()).getTime() - date.getTime()) / 1000;
        var day_diff = Math.floor(diff / 86400);
        
        if (day_diff < 0 || day_diff >= 31 || isNaN(day_diff)) {
            return "View tweet";
        }
        
        if(day_diff == 0) {
            if(diff < 60) {
                return "hace" + Math.ceil(diff) + " segundos";
            }
            else if(diff < 120) {
                return "hace 1 minuto";
            }
            else if(diff < 3600) {
                return "hace " + Math.floor( diff / 60 ) + " minutos";
            }
            else if(diff < 7200) {
                return "hace 1 hora";
            }
            else if(diff < 86400) {
                return "hace " + Math.floor( diff / 3600 ) + " horas";
            }
        }
        
        if(day_diff == 1) {
            return "ayer";
        }
        else if(day_diff < 7) {
            return "hace " + day_diff + " días";
        }
        else if(day_diff < 31) {
            return "hace " + Math.ceil( day_diff / 7 ) + " semanas";
        }
        else {
            return "ver tweet";
        }    
    }
    


})(jQuery);

