// Grabs from json basic.
(function (jQuery) {
  /** public methods **/
  
  // url = 'http://search.twitter.com/search.json'
  jQuery.fn.jsonWidget = function (url) {
    try {
      var opts = jQuery.extend({}, jQuery.fn.jsonWidget.defaults);
      var c = jQuery.isFunction(opts.callback) ? opts.callback : _build;
        
      params = {};

      return this.each(function (i, e) {
        jQuery.ajax({
          url: url,
          data: params,
          dataType: 'jsonp',
          jsonpCallback: 'callback',
          success: function (o) {
            c.apply(this, [(o.results) ? o.results : o, e, opts]);
          }
        });
      });
    } catch (e) {}
  };
  /** defaults **/
  jQuery.fn.jsonWidget.defaults = {
    callback: null,
    maximum: 10,
    ulClass: 'json-widget',
    oddClass: 'odd'
  };

  /** private methods **/
  
  var _build = function (object, element, opts) {
    var html = '<ul class="' + opts.ulClass + '">';
    for (var i = 0; i < object.length; i++) {
      current = object[i];
      html += '<li' + ((opts.odd && i % 2) ? ' class="' + opts.oddClass + '"' : '') + '>' + 
      '<img class="article_image" src="' + current.article_photo_url + '">' + '<a href="' + current.url+ '"><strong>' + current.headline + '</strong></a>';
    }
    html += '</ul>';
    $(element).html(html);
  };
  
  $("img.article_image").live('click', function() {
     location.href = $(this).next('a').attr('href');
     return false;
    });

})(jQuery);

