/*
    jquery.showPosterous.js

    Author: Mike Haugland
    Date: Dec 5th, 2011
    Version: 1.0.0
*/

(function( $ ){

  $.fn.showPosterous = function( options ) {
    var settings = $.extend( {
      'siteid'         : '2230884',
      'amount' : 2,
      'hidetitlevalue' : '',
      'linktitles' : true,
      'linktoblog' : true,
      'linktoblogtext' : 'See more...',
      'apikey' : ''
    }, options);

    return this.each(function(){
      var node = $(this);
      var endpoint = "http://posterous.com/api/2/sites/"+settings.siteid+"/posts/public?callback=?";

      $.getJSON(endpoint, function(data) {
        node.empty();

        $.each(data, function(i, post) {
          var postHTML = '<div class="blog_post">';
          if (settings.hidetitlevalue != post.title) {
            postHTML += '<h3 class="title"><span>';
            postHTML += settings.linktitles ? '<a href="'+post.full_url+'">'+post.title+'</a>' : post.title;
            postHTML += '</span></h3>';
          }
          postHTML += '<div class="body">'+post.body_full+'</div>';
          postHTML += '</div>';
          node.append(postHTML);
        });
        if (settings.linktoblog) {
          node.append('<div class="blog_link"><a href="http://'+data[0].site.full_hostname+'">'+settings.linktoblogtext+'</a></div>');
        }
      });
    });
  };

})( jQuery )

