(function($) {
 
  $.fn.blog = function(o){
	var s = {
      query: "get_recent_posts",    // [string]   required, posts search query. Expects one of the following values: 'get_recent_posts' 'get_post' 'get_page' 'get_date_posts' 'get_category_posts' 'get_tag_posts' 'get_author_posts' 'get_date_index' 'get_category_index' 'get_tag_index' 'get_author_index' 'get_search_results' 'submit_comment'
      custom_fields: null,			// [string]   includes values from posts' Custom Fields. Expects a comma-separated list of custom field keys
	  category_slug: null,			// [string]   used by get_category_posts API method. Expects one of the following category values: 'my-learning-space', 'pedagogue', 'people-places', 'events', 
	  list: null,                   // [string]   name of blog list element
      default_thumbnail: null,		// [string]   required, blog post image thumbnail path, if not present in the post custom thumbnail field
	  thumbnail_size: null,         // [integer]  height and width of post thumbnail if displayed (71px max)
      count: 3,                     // [integer]  how many tweets to display?      
      loading_text: null            // [string]   optional loading text, displayed while blogs load      
    };
    
    if(o) $.extend(s, o);
    
    // Takes an ISO time and returns a formatted date string
	function simplifyDate(time){
		var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," "));
		if ( isNaN(date) )
			return;
		var f_date = date.getDate();
		var f_month = date.getMonth();
		f_month++;
		f_year = date.getFullYear();
		var fd = f_date + "/" + f_month + "/" + f_year;
		return fd;
	}

    return this.each(function(i, widget){
		var list = $('<ul class="blogs_list">').appendTo(widget);
		var loading = $('<p class="loading">'+s.loading_text+'</p>');
		var q = (s.query == 'get_category_posts') ? s.query + '&category_slug=' + s.category_slug : s.query;	
		var url = 'http://cdsmteamblog.co.uk/wpmu/?json='+ q +'&custom_fields='+ s.custom_fields +'&count='+ s.count +'&callback=?';
		if (s.loading_text) $(widget).append(loading);
	  
		$.getJSON(url, function(data){
			if (s.loading_text) loading.remove();
			$.each(data, function(data){
				var posts = data;
				if (posts=="posts"){
					$.each(this, function(i,item){
						var custom_thumbnail = (typeof(item.custom_fields.Thumbnail) !== 'undefined' && item.custom_fields.Thumbnail != null) ? item.custom_fields.Thumbnail[0] : s.default_thumbnail;
						var custom_thumbnail_alt = (typeof(item.custom_fields.ThumbnailAlt) !== 'undefined' && item.custom_fields.ThumbnailAlt != null) ? item.custom_fields.ThumbnailAlt[0] :  item.title;
											
						var thumbnail = '<a href="'+ item.url +'"><img src="'+ custom_thumbnail +'" alt="'+ custom_thumbnail_alt +'" />';
						var blogtext = '<span class="BlogText">' + item.title + '</span>';
						var blogdate = '<span class="BlogDate">' + simplifyDate(item.date) + '</span>';
						var content = thumbnail +'<span class="BlogTextHold">' + blogtext + blogdate + '</span></a>';
								
						list.append('<li>'+ content +'</li>');														
					});	
					
					list.children('li:first').addClass('New');
					var new_blog = list.children('li:first').children('a');
					var new_icon = $('<span class="NewIcon">New</span>').appendTo(new_blog);
					new_blog.append(new_blog.children($('.BlogTextHold')).children().get(1));
					new_blog.append(new_blog.children().get(0));
					new_blog.append(new_blog.children().get(0));										
				}
			});
		});		
	});
  };
})(jQuery);
