(function ($) {
	var methods = {
		build: function  (idx, entry) {
			var summ = $(entry.summary);
			console.log(summ);
			var img = $(summ[0]);
			console.log(img);
			var cap = summ.slice(3);
			$(img).appendTo(figure);
			$.each(cap, function (i, o) { $(o).appendTo(figcaption); });
			figcaption.appendTo(figure);
			figure.appendTo(container);
		},
		display: function (data) {
   			console.log(data);
    		$.each(data, build);
    		console.log(container);
			$(this).each(function (i, o) {
    			$(container).appendTo($(o));
			});
		},
		fetch: function () {
			$.getJSON(settings['url'], display);
		},
		show: function (options) { 
			var settings = {
				url: "/json/posts.json",
				container: $('<ul class="portfolio"></ul>'),
				post: $('<li class="post"></li>'),
				figure: $('<figure></figure>'),
				figcaption: $('<figcaption></figcaption>')
			};
			return this.each(function() {        
			      // If options exist, lets merge them
			      // with our default settings
			      if ( options ) { 
			        $.extend( settings, options );
			 	  }
				  fetch();
			});
		}
	};	
	$.fn.portfolio = function (method) {
		
		// Method calling logic
		    if ( methods[method] ) {
		      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		    } else if ( typeof method === 'object' || ! method ) {
		      return methods.init.apply( this, arguments );
		    } else {
		      $.error( 'Method ' +  method + ' does not exist on jQuery.portfolio' );
		   	}
		
			
	}
})(jQuery);
$(document).ready($('#stage').portfolio('show'));

