$().ready(function() {	
    var url = "/wp-content/plugins/tweet-fetchr/tweet-fetchr-ajax.php";
    $.post(url,
		'',
		tf_cb,
		"json");
});

function tf_cb(data) {
	var hasTweets = false;
    var one_hour = 1000 * 60 * 60;

    var tpl = $('.tf-tweet-template');
    $('#tf-tweets').empty();

    var item = "";
    var alt = 0;
    for (var i = 0; i < data.length; i++) {
		hasTweets = true;
        item = tpl.clone();
		var nameLink = data[i].username.replace(" ","");
        item.find(".tf-name").html("<a href=\"http://twitter.com/"+nameLink+"\" target='_blank'>"+data[i].name+"</a>");
        item.find(".tf-text").html(data[i].status);
        item.find(".tf-picture").attr("src", data[i].profileImageUrl);

        today = new Date();
        today = today.getTime();
        createdAt = data[i].createdAt;
        // HACK ALERT
        // To make javascript notice the date through parsing...
        createdAt = createdAt.replace("+", "UTC+");
        createdAt = Date.parse(createdAt);
        hours = Math.ceil((today - createdAt) / (one_hour));		
		days = Math.ceil(hours / 24);

		if(hours>48)
		{
	        item.find(".tf-age").html(days + " days ago");			
		} else 
		{
	        item.find(".tf-age").html(hours + " hours ago");			
		}


        if (alt == 1) {
            item.addClass("color1");
        }

        item.appendTo("#tf-tweets")
            .slideDown();

        if (alt == 0) { alt = 1; } else {alt = 0; };

    }
	if(hasTweets){
		$(".twitter-box").show();
	}
	
}

