$(document).ready(function(){

//  $.ajax({
//    url: 'https://api.twitter.com/1/statuses/user_timeline.json?screen_name=perfect_events&callback=?',
//    crossDomain: true,
//    success: function(data) {
//      $('.twitter').html(data);
//    }
//  });
  
  $.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=perfect_events&callback=?',
          { count: 1 },
          function(tweets) {
            createTweet(tweets[0]);
          }
  );

  if ($(".lightbox").length){
    $(".lightbox").lightBox();
  }
});

function createTweet(tweet){
  // i18n is bogus in javascript. Therefore this simple build-in translation
  var months = new Object();
  months = {
    'Jan': 'januari',
    'Feb': 'februari',
    'Mar': 'maart',
    'Apr': 'april',
    'May': 'mei',
    'Jun': 'juni',
    'Jul': 'juli',
    'Aug': 'augustus',
    'Sep': 'september',
    'Oct': 'oktober',
    'Nov': 'november',
    'Dec': 'december'
  };
  // extract the useful information from twitter`s created_at attribute
  var tweetdate = tweet.created_at.split(" ", 3);
  var tweetday = tweetdate[2];
  
  // transform the monthname to something pretty
  var tweetmonth = months[tweetdate[1]];

  // building the tweet box
  var container = $('<div class="twitter"></div>');
  var datum = $('<p class="date">' + tweetday + ' ' + tweetmonth + '</p>');
  var text = $('<p>' + tweet.text + '</p>');
  container.append(datum, text);
  $('#fotodiv').append(container);

  // clickevent
  $('#fotodiv .twitter').click(function(){
    window.open('http://twitter.com/#!/perfect_events');
    
  });
}
