function urlify(text) {
  var urlRegex = /(https?:\/\/[^\s]+)/g;
  return text.replace(urlRegex, function(url) {
    return '<a href="' + url + '">' + url + '</a>';
  })
}

function parseTwitterDate($stamp) {   
  // convert to local string and remove seconds and year
  var date = new Date(Date.parse($stamp)).toLocaleString().substr(0, 16);
  var intDate = new Date(Date.parse($stamp));

  month = date.split(" ");

  return month[0]+" "+month[1]+' • '+intDate.getHours()+":"+intDate.getMinutes();
}

$(document).ready(function() {

  // foursquare
  $.getJSON("foursquare.json", function(data) {
    now = new Date();
    var tense = "I'm currently at";
    // Check if I was at this venue in the last three hours (10800 sec).
    if ((parseInt(now.getTime()/1000)-10800) > (data.time + 7200)) // +7200 to convert to SAST (server time)
      tense = "I was last at";

    place_details = ""
    if (typeof data.city !== "undefined" && data.city !== null) {
      place_details += ", "+data.city;
    }

    if (typeof data.state !== "undefined" && data.state !== null) {
      place_details += ", "+data.state;
    }
    place_details += ".<br /><br />";
    
    html  = "<img src='https://playfoursquare.s3.amazonaws.com/press/logo/icon-36x36.png' align='left' style='padding-right: 5px'>"+tense+" <strong>"+data.name+"</strong>"+place_details;
    html += "<div class='social fsq-checkins'>"+data.checkins+" check-ins</div>";
    html += "<div class='social fsq-mayor'>"+data.mayorships+" mayorships</div>";
    html += "<div class='social fsq-badges'>"+data.badges+" badges</div>";
    $("div#foursquare").html(html);
  });


  // instagram
  $.getJSON("instagram.json", function(data) {
    var html = "";

    $.each(data, function(idx, img) {
      html += "<a href='"+img.fullsize+"' title='"+img.caption+"'><img src='"+img.thumbnail+"' width='50' /></a>"
    });
    html += "<a href='http://instagrid.me/kishyr/'><img src='/images/instagram-28.png' align='left' /></a> <p>I'm <a href='http://instagrid.me/kishyr/'>@kishyr</a> on <a href='http://instagr.am'><em>instagram</em></a>.</p>";

    $("div#instagram").html(html);
    $("div#instagram a").fancybox();
  });


  // twitter
  $.getJSON("twitter.json", function(data) {
    var html = "";

    $.each(data, function(idx, tweet) {
      html += "<p class='tweet'>"+urlify(tweet.text)+"<br /><small>"+parseTwitterDate(tweet.created)+"</small></p>";
    });

    $("div#twitter").html(html);
  });


  // // entropylinks
  // $.getJSON("entropylinks.json", function(data) {
  //   var html = "<ul class='entropylinks'>";

  //   $.each(data, function(idx, link) {
  //     html += "<li><a href='http://"+link.url+"'>"+link.title+"</a><br ><small>"+link.desc+"</small></li>";
  //   });
  //   html += "</ul>";
  //   html += "All links are from <a href=''>entropy|links</a>, a daily newsletter that I curate containing interesting links. You should subscribe!";


  //   $("div#entropylinks").html(html);
  // });


  // last.fm
  $.getJSON("lastfm.json", function(data) {
    var html = "<ul class='lastfm'>";

    $.each(data, function(idx, track) {
      html += "<li style='background: url("+track.thumb+") no-repeat;'><a href='http://"+track.url+"'>"+track.name+"</a><br/><small>"+track.artist+"</small></li>";
    });
    html += "</ul>";
    html += "<a href='http://www.last.fm/user/kishyr'><img width='15' style='margin-right: 5px' src='/images/lastfm.png' align='left' /></a> <p>I'm <a href='http://www.last.fm/user/kishyr'>kishyr</a> on <a href='http://last.fm'><em>last.fm</em></a>.</p>";


    $("div#lastfm").html(html);
  });


  // entropythoughts
  $.getJSON("entropythoughts.json", function(data) {
    var html = "<ul class='entropythoughts'>";

    $.each(data, function(idx, post) {
      html += "<li><a href='"+post.url+"'>"+post.title+"</a> <small>"+post.date+"</small></li>";
    });
    html += "</ul>";

    $("div#entropythoughts").html(html);
  });

});


