$(function() {

	/*$('.event-container').hover(function(){
		$('.event-bubble').hide();
		$(this).children(".event-bubble").show();
	},
	function(){
		if (oldie) $('.event-bubble').hide();
		else $('.event-bubble').fadeOut('400');
	});*/
	
	var lTopHeight = $('#l-top p').height();
	var rTopHeight = $('#r-top p').height();
	var lBotHeight = $('#l-bot p').height();
	var rBotHeight = $('#r-bot p').height();
	var tHeight = (lTopHeight >= rTopHeight) ? lTopHeight : rTopHeight;
	var bHeight = (lBotHeight >= rBotHeight) ? lBotHeight : rBotHeight;
	$('#l-top p, #r-top p').height(tHeight);
	$('#l-bot p, #r-bot p').height(bHeight);
	
	
	
	$('#body-middle .sub-section p:first-child').siblings('p').css({'position':'relative', 'top':'-13px'});

    /** Facebook JSON **/

    $.getJSON('https://graph.facebook.com/tapahtumaravintolat/posts?access_token=AAADqYZAYZAQHcBAJ7zc4KKZC3pJ9KrTQtBytGxrcDQKSZBfVftYNLT88iKorrSZBKZAbHacdmNZBkhrOVB0unmSDbAVsZCF8Ld0ZD&limit=1&format=json&callback=?', function(data) {
     
      var msg = data.data[0].message;
      var timest = data.data[0].created_time;
      var relativetime = fuzzyFacebookTime(timest.replace(/-/g,'/'));
      $('<span class="fb-head">Tapahtumaravintolat</span><br><span class="fb-msg">'+msg+'</span><br><span class="fb-time">'+relativetime+'</span>').appendTo('#facebook-feed');
      
    });
	
});

var fuzzyFacebookTime = (function(){
	 
	  fuzzyTime.defaultOptions={
	    // time display options
	    relativeTime : 48,
	    // language options
	    monthNames : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
	    amPm : ['AM', 'PM'],
	    ordinalSuffix : function(n) {return ['th','st','nd','rd'][n<4 || (n>20 && n % 10<4) ? n % 10 : 0]}
	  }
	 
	  function fuzzyTime (timeValue, options) {
	 
	    var options=options||fuzzyTime.defaultOptions,
	        date=parseDate(timeValue),
	        delta=parseInt(((new Date()).getTime()-date.getTime())/1000),
	        relative=options.relativeTime,
	        cutoff=+relative===relative ? relative*60*60 : Infinity;
	 
	    if (relative===false || delta>cutoff)
	      return formatTime(date, options)+' '+formatDate(date, options);
	 
	    if (delta<60) return 'alle minuutti sitten';
	    var minutes=parseInt(delta/60 +0.5);
	    if (minutes <= 1) return 'noin minuutti sitten';
	    var hours=parseInt(minutes/60 +0.5);
	    if (hours<1) return minutes+' minuuttia sitten';
	    if (hours==1) return 'noin tunti sitten';
	    var days=parseInt(hours/24 +0.5);
	    if (days<1) return hours+' tuntia sitten';
	    if (days==1) return 'eilen';
	    var weeks=parseInt(days/7 +0.5);
	    if (weeks<2) return days+' päivää sitten';
	    var months=parseInt(weeks/4.34812141 +0.5);
	    if (months<2) return weeks+' viikkoa sitten';
	    var years=parseInt(months/12 +0.5);
	    if (years<2) return months+' kuukautta sitten';
	    return years+' vuotta sitten';
	  }
	 
	  function parseDate (str) {
	    var v=str.replace(/[T\+]/g,' ').split(' ');
	    return new Date(Date.parse(v[0] + " " + v[1] + " UTC"));
	  }
	 
	  function formatTime (date, options) {
	    var h=date.getHours(), m=''+date.getMinutes(), am=options.amPm;
	  //  return (h>12 ? h-12 : h)+':'+(m.length==1 ? '0' : '' )+m+' '+(h<12 ? am[0] : am[1]);
		  return h+':'+m;
	  }
	 
	  function formatDate (date, options) {
	    var mon=options.monthNames[date.getMonth()],
	        day=date.getDate(),
	        year=date.getFullYear(),
	        thisyear=(new Date()).getFullYear(),
	        suf=options.ordinalSuffix(day);
	 
	    return mon+' '+day+suf+(thisyear!=year ? ', '+year : '');
	  }
	 
	  return fuzzyTime;
	 
	}());



