/**
 * Give Me Stuff I Starred - gmsis
 *  by Bill Israel [http://cubicle17.com/]
 *
 * This plugin pulls the last X Instapaper articles you've starred.
 *
 * To use GMSIS, you'll need the URL for your Starred RSS feed, which
 * you can get from http://www.instapaper.com/starred if you're logged in.
 * NOTE: RSS feed URL _must_ start with "http://", not "feed://"
 *
 * Options:
 *  - count: (int) - the number of items to pull (5 by default, max is 25)
 *  - desc: (boolean) - whether to show item descriptions (false by default)
 *
 * Examples:
 *
 * To retrieve the 5 most recent starred articles:
 *  var url = "http://www.instapaper.com/rss/starred/2/asdfasdfasdf"
 *  $("#instapaper").gmsis(url);
 *
 * To retrieve the 2 most recent starred articles, and show descriptions:
 *  var url = "http://www.instapaper.com/rss/starred/2/asdfasdfasdf"
 *  $("#instapaper").gmsis(url, {count: 2, desc: true});
 *
 *
 * "Easter Egg":
 *  You can also use this script to pull your Unread list and your
 *  Archive list. To do this, just substitute the Unread or Archive RSS
 *  feed for the Starred feed. Everything else is the same.
 */
;(function($) {
$.fn.gmsis = function(url, options) {
    // If there's no URL to pull from, do nothing
    if(!url || url === '') return this;
    
    var opts = $.extend({}, $.fn.gmsis.defaults, options);
    return this.each(function() {
        var $this = $(this);
        
        // Load the RSS
        $.getJSON(url + "?jsonp=?", function(data) {
            var items = [];
            var li_html = '<li> \
                            <a class="instapaper-link" href="{url}">{title}</a> \
                            <span class="instapaper-desc">{desc}</span> \
                           </li>';
            
            $.each(data, function(index, item) {
                // Stop if we've gathered enough 
                if( index >= opts.count ) return false;
                
                var title = item.title, link = item.url; 
                var desc = opts.desc && item.selection ? item.selection : '';
                li = li_html.replace("{title}", title)
                            .replace("{url}", link)
                            .replace("{desc}", desc);
                
                items.push(li);
            });
            
            // Make UL from gathered items
            $this.html("<ul>" + items.join('') + "</ul>");
        });
    });
};

$.fn.gmsis.defaults = { count: 5, desc: false };
})(jQuery);
