(function() {
	var shortened = "";	
	String.prototype.truncate = function(length) {
  
	  if (this.length > length) {
	   shortened = this.slice(0, length - 3);
	   shortened = shortened.replace(/\w+$/, '');
	   return shortened + "..."
	  } else {
	    return this;
	  }
	};
}());


function loadFBFeeds() {
loadFBFeed(60778338566, 2, '#ps-bkk-feed', true);
loadFBFeed(365959455756, 2, '#ps-ks-feed', true);
loadFBFeed(2338459409, 2, '#ps-kt-feed');

}

function quickParse(dateString) {
var working = dateString.replace(/T.*$/, '');
working = working.split("-");
return  new Date(working[0], working[1], working[2]);
}

var months = [0,'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

function loadFBFeed(gid, numberToShow, targetElement, restrictToPostsFromGid) {
	var accessToken = "149877928372743|Qb5AAbpyufh1uPjVAWlVVDvKqNY."
	var numberShown = 0;
	var target = $(targetElement);
	$.ajax({ url: "https://graph.facebook.com/" + gid + "/feed",
		type: "GET",
		data: {access_token: accessToken},
		dataType: "jsonp",
		success: function(data) {
			$(data.data).each(function(idx) {
				if (numberShown < numberToShow) {
					if (!restrictToPostsFromGid || this.from.id == gid) {
						var ts = quickParse(this.created_time);
						target.append('<p><strong>' + this.message.truncate(100) + '</strong><br/>' +
							'<em>'+ ts.getDate() + " " + months[ts.getMonth()] + ', ' + ts.getFullYear() + '</em></p>')
						numberShown++;
				    }
				}
			})
			
		}
	});
	
}

//Flickr
$.ajax({url: "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
	type: "GET",
	data: {id: '47655160@N07', format: 'json'},
	dataType: "jsonp",
	success: function(data) {
		var target = $("#flickr-widget")
		$(data.items).slice(0,5).each(function(idx) {
			$("<img/>").attr("src", this.media.m.replace("_m.jpg", "_t.jpg")).appendTo(target).wrap('<a rel="external" href="' + this.link + '"></a>')
		});
	}
});



window.fbAsyncInit = function() {
  FB.init({
    appId  : '149877928372743',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true  // parse XFBML
  });
loadFBFeeds();
};

(function() {
  var e = document.createElement('script');
  e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
  e.async = true;
  document.getElementById('fb-root').appendChild(e);
}());
