PMO = {};

PMO.ReadService = function() {
    
};

PMO.ReadService.connectSection = function(section_id) {
    var section = jQuery('#'+section_id)[0];
    if ( ! section ) {
        return;
    }
    var source = section.getAttribute('data-source');
    var reader = PMO.ReadService.factory(source);
    var datasource = jQuery('#'+section_id+' a')[0];
    
    reader.setDataSource(datasource.href);
    reader.targetElement = section;
    reader.callback = function() {
        datasource.style.display = 'none';
    };
    return reader;
};

PMO.ReadService.factory = function(source) {
    if (source == 'lastfm') {
        return new PMO.ReadLastFm();
    }
    if (source == 'blogger') {
        return new PMO.ReadBlogger();
    }
    if (source == 'twitter') {
        return new PMO.ReadTwitter();
    }
    if (source == 'flickr') {
        return new PMO.ReadFlickr();
    }
    if (source == 'tumblr') {
        return new PMO.ReadTumblr();
    }
};

PMO.ReadService.prototype.retrieve = function(max_items) {
    var self = this;
    jQuery.getJSON(this.getURL(),function(data) {
        self.parseData(data);
        if (max_items) {
            self.items.splice(max_items,(self.items.length - max_items));
        }
        if (self.targetElement) {
            var targ = self.targetElement;
            jQuery(self.items).each(function() {
                jQuery(targ).append(self.convertItem(this));
            });
            if (self.callback) {
                self.callback.apply();
            }
        }
    })
};

PMO.ReadService.prototype.convertItem = function(item) {
    if (item['url']) {
        return this.convertURL(item);
    }
    if (item['img']) {
        return this.convertImg(item);
    }
    if (item['quote']) {
        return this.convertQuote(item);
    }
};

PMO.ReadService.prototype.convertURL = function(item) {
    return jQuery('<article class="link"><a href="'+item['url']+'">'+item['title']+'</a></article>')[0];
}

PMO.ReadService.prototype.convertImg = function(item) {
    var img = jQuery('<article class="image"><img src="'+item['img']+'"/></article>');
    if (item['caption']) {
        img.append('<div class="caption">'+item['caption']+'</div>');
    }
    return img[0];
}

PMO.ReadService.prototype.convertQuote = function(item) {
    var quote = jQuery('<article class="quote"><p class="quote">'+item['quote']+'</p></article>');
    if (item['caption']) {
        quote.append('<div class="caption">'+item['caption']+'</div>');
    }
    return quote[0];    
}

PMO.ReadService.prototype.setDataSource = function(dataSource) {
    this.dataSource = dataSource.replace(/\/$/,'');
};

PMO.ReadBlogger = function() {};

PMO.ReadBlogger.prototype = new PMO.ReadService();

PMO.ReadBlogger.prototype.getURL = function() {
    return this.dataSource + "/feeds/posts/default?alt=json-in-script&callback=?";
};

PMO.ReadBlogger.prototype.parseData = function(data) {
    var titles = [];
    if (data.feed && data.feed.entry && data.feed.entry.length > 0) {
        jQuery(data.feed.entry).each(function(i) {
            var result = {};
            result['title'] = this.title.$t;
            result['date'] = this.updated.$t;
            for (var j = 0; j < this.link.length; j++ ) {
                if (this.link[j].rel == 'alternate') {
                    result['url'] = this.link[j].href;
                }
            }
            titles.push(result);
        });
    }
    this.items = titles;
};

PMO.ReadFlickr = function() {};

PMO.ReadFlickr.prototype = new PMO.ReadService();

PMO.ReadFlickr.prototype.getURL = function() {
    var basic_url = 'http://api.flickr.com/services/feeds/photos_public.gne?id=';
    var user_id = this.dataSource.split('/').reverse().shift();
    return basic_url+user_id+'&format=json&jsoncallback=?';
};

PMO.ReadFlickr.prototype.parseData = function(data) {
    var items = [];
    if (data.items) {
        jQuery(data.items).each(function(i) {
            items.push({'img' : this.media.m});
        });
    }
    this.items = items;
}

PMO.ReadLastFm = function() {};

PMO.ReadLastFm.prototype = new PMO.ReadService();

PMO.ReadLastFm.prototype.getURL = function() {
    var basic_url = 'http://ws.audioscrobbler.com/2.0/?method=user.gettopalbums&api_key=cb59f7f3111f3517d29337bffca93764&format=json&period=3month&callback=?&user=';
    var user_id = this.dataSource.split('/').reverse().shift();
    return basic_url+user_id;
};

PMO.ReadLastFm.prototype.parseData = function(data) {
    var items = [];
    if (data.topalbums && data.topalbums.album.length > 0) {
        jQuery(data.topalbums.album).each(function(i) {
            for (var j = 0; j < this.image.length; j++ ) {
                if (this.image[j].size == 'medium') {
                    items.push({ 'img' : this.image[j]['#text']});
                }
            }
        });
    }
    this.items = items;
}

PMO.ReadTumblr = function() {};

PMO.ReadTumblr.prototype = new PMO.ReadService();

PMO.ReadTumblr.prototype.getURL = function() {
    return this.dataSource + '/api/read/json?callback=?';
};

PMO.ReadTumblr.prototype.parseData = function(data) {
    var items = [];
    if (data.posts) {
        jQuery(data.posts).each(function(i) {
            var result = null;
            if (this.type == 'photo') {
                result = { 'img' : this['photo-url-500'], 'caption' : this['photo-caption']};
            }
            if (this.type == 'link') {
                result = { 'url' : this['link-url'], 'title' : this['link-text']};
            }
            if (this.type == 'quote') {
                result = { 'quote' : this['quote-text'], 'caption' : this['quote-source']};
            }
            if (result) {
                items.push(result);
            }
        });
    }
    this.items = items;
};

PMO.ReadTwitter = function() {};

PMO.ReadTwitter.prototype = new PMO.ReadService();

PMO.ReadTwitter.prototype.getURL = function() {
  var username = this.dataSource.split('/').reverse().shift();
  return 'http://twitter.com/status/user_timeline/'+username+'.json?&count=1&callback=?';  
};

PMO.ReadTwitter.prototype.parseData = function(data) {
    var items = [];
    items.push({'quote' : data[0].text });
    this.items = items;
};


