var Dashboard = new Class({
    initialize: function(url) {
        this.url = url;
        // Load lastfm
        new Request.JSON({
            url: [this.url, 'lastfm/'].join(''),
            method: 'get',
            onSuccess: function(resp) {
                if (resp.code == 1) {
                    this.insert(resp.tracks, 'lastfm');
                }
            }.bind(this)
        }).send();

        // Twitter
        new Request.Twitter('xintron', {
            data: {
                count: 5
            },
            onSuccess: function(resp) {
                this.insert(resp, 'twitter');
            }.bind(this)
        }).send();
    },

    insert: function(data, source) {
        var ol = document.body.getElement('div.'+source+' ol');
        document.body.getElement('div.'+source+' p').dispose();

        if (source == 'lastfm') {
            data.each(function(item) {
                new Element('li', {
                    'html': '<p>'+item.artist+' - '+item.name+'</p>'+(item.date ? '<p class="info">'+item.date+'</p>' : (item.playing ? '<p class="info playing">Now playing</p>' : ''))
                }).inject(ol);
            });
        }
        else if (source == 'twitter') {
            data.each(function(item) {
                new Element('li', {
                    'html': '<p>'+item.text+'</p>'
                }).inject(ol);
            });
        }
    }
});
