﻿/// <reference path="jquery-1.4.1-vsdoc.js" />


$(document).ready(function () {

    var $panel = null;

    function _formatDate(utcDate) {
        try {
            var d = new Date(utcDate);
            var month_names = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];
            var day_names = ["Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado"]
            return day_names[d.getDay()] + ", " + d.getDate() + " de " + month_names[d.getMonth()] + " de " + d.getFullYear();
        }
        catch (e) {
            window.alert(e);
        }
    };

    function _callBack(data) {
        $panel.html("<span class='twitter-logo'></span>");
        $.each(data, function (i, tweets) {
            if (tweets.length != undefined) {
                if (tweets[0] != undefined) {
                    if (tweets[0].created_at != undefined) {
                        var $ul = $("<ul class='tweets'></ul>")                        
                        for (var i = 0; i < tweets.length; i++) {                            
                            $ul.append("<li class='tweet'>" + tweets[i].text + "<small>" + _formatDate(tweets[i].created_at) + "</small></li>");
                        }
                        $panel.append($ul);
                    }
                }
            }
        });
    }

    function _init() {
        $panel = $("#noticias #noticias-listado");
        $panel.html("Espere un momento")
        $.getJSON(
            'http://search.twitter.com/search.json?callback=?&rpp=25&q=from:pollamaxve',
             _callBack
        );
    };

    return _init();
})

