(function($){
 $.fn.flickr = function(options) {
  var defaults = {
   display_count: 3,     /* Default display count if none set upon initailization or in title attribute */
   image_size: 'square',  /* Choices: square, thumb, medium, large, currently not allowing for 'origianl' as it is not always available */
   sort_order: 'random'  /* Choices: random, nonrandom */
  };
  
  var options = $.extend(defaults, options);
  
  return this.each(function() {
      var container = $(this);
      var flickr_params = $(this).attr("title").split('|');


      /* Define flickr set url, also append required variable to url */
      var photoURL = flickr_params[0];
      if (photoURL.search("jsoncallback") < 0) { photoURL += "&lang=en-us&format=json&jsoncallback=?";}
      
      
      /* Define dispplayCount:  Looks first for a title atribute then for display_count default upon intitailization and finally if both those are null goes to the display_count deafult set above */
      if (flickr_params[1]) { var displayCount = flickr_params[1] - 1; }
      else {var displayCount = options.display_count - 1;}

      /* Check for explicit height && width paramaters present in title attribute */
      var width, height;
      if (flickr_params[3] && flickr_params[4]) { width = ' width="'+ flickr_params[3] + '"'; height = ' height="'+ flickr_params[4] + '"'; } // can explicitly define width and height, though use of the image_size option is preferred 
      else { width = ''; height = ''; }


      /* GET flickr set url and return json */
      $.getJSON(photoURL, function(data){
      
        /* return random set of items from array if sort_order == random */
        if (options.sort_order == 'random') {var data_items = data.items.sort(function() {return 0.5 - Math.random()})}
        else {var data_items = data.items;}
        var content = '';
        
        /* Check and account for null returned, also accounting for when # returned is less than displayCount */
        var result = data.items.length;
        if (result < (displayCount + 1)) {displayCount = result - 1;}
        if (result == 0) {content = 'Found 0 results';container.html(content);return false;}
	    
	    
	    /* Deal with each image returned and get desired image size */
	    $.each(data_items, function(i, item) {
	      var image_size = '';
	      if (options.image_size == 'square') { var image_size = item.media.m.replace("_m.","_s."); }
	      if (options.image_size == 'thumb') { var image_size = item.media.m.replace("_m.","_t."); }
	      if (options.image_size == 'medium') { var image_size = item.media.m; }
	      if (options.image_size == 'large') { var image_size = item.media.m.replace("_m.","."); }
   	      //if (options.image_size == 'original') { var image_size = item.media.m.replace("_m.","_o."); }
          container.append('<div class="flickr_group"><a href="' + item.link + '" target="_blank" title="' + item.title + '" ><img src="' + image_size + '" alt="' + item.title + '"' + width + height +' /></a></div>');
          if ( i == displayCount ) return false;
	  }); // each    
    }); // getJSON
  }); // return
 }; // $.fn.flickr
})(jQuery);
