$(function() {
  
  $('h1').fadeIn(1000, function() {
    $('nav').fadeIn(1000);
  });
  
  $('nav ul li').hover(
    function() {
      var offset;
      var section = $('section.' + $(this).attr('class'));
      $('section').not(section).stop().hide();
      if ($.trim(section.children(":not(h2)").text()) != "" || section.has('img').length) {
        section.not(":animated").css('opacity', 1);
        section.fadeIn();
      }
      offset = 2.5 - $(this).index();
      $('#rotation').css('-webkit-transform', 'rotateY(' + offset + 'deg) scaleX(' + (1 - Math.abs(offset) / 24) + ')');
    }
  );
  
  function relative_created_at(time_value) {
     var created_at_time = Date.parse(time_value.replace(" +0000",""));
     var relative_time = ( arguments.length > 1 ) ? arguments[1] : new Date();
     var wordy_time = parseInt(( relative_time.getTime() - created_at_time ) / 1000) + (relative_time.getTimezoneOffset()*60);
 
     if ( wordy_time < 59 ) {
       return 'less than a minute ago';
     } 
     else if ( wordy_time < 89) {
       return 'about a minute ago';
     } 
     else if ( wordy_time < 3000 ) {         // < 50 minutes ago
       return ( parseInt( wordy_time / 60 )).toString() + ' minutes ago';
     } 
     else if ( wordy_time < 5340 ) {         // < 89 minutes ago
       return 'about an hour ago';
     } 
     else if ( wordy_time < 9000 ) {          // < 150 minutes ago
       return 'a couple of hours ago';  
     }
     else if ( wordy_time < 82800 ) {         // < 23 hours ago
       return 'about ' + ( parseInt( wordy_time / 3600 )).toString() + ' hours ago';
     } 
     else if ( wordy_time < 129600 ) {       //  < 36 hours
       return 'a day ago';
     }
     else if ( wordy_time < 172800 ) {       // < 48 hours
       return 'almost 2 days ago';
     }
     else {
       return ( parseInt(wordy_time / 86400)).toString() + ' days ago';
     }
  }

  if (typeof Twitter != "undefined") {
    var count = 0;
    for (i in Twitter.posts) {
	  if (count < 5) {
	    var post = Twitter.posts[i]; 
	    if (post.text.substr(0, 1) != "@") {
	      var li = $('<li>');
	      li.html(post.text + ' '); 
	      var a = $('<a>');
	      a.attr('href', 'http://twitter.com/#!/JeffreyATW/' + 'status/' + post.id_str);
	      a.attr('title', 'Permalink to this twitter (id ' + post.id_str + ') at Twitter.com');
	      a.text(relative_created_at(post.created_at));
	      li.append(a); 
	      $('section.twitter ul').append(li);
	      count++;
	    }
	  }
    }
  }

  $.getJSON('http://pipes.yahoo.com/pipes/pipe.run?_id=3615cdd72b5a2c5e762feca9631d0e79&_render=json&_callback=?', function(data) {
    $.each(data.value.items, function(i, item) {
      var li = $('<li>');
      var h3 = $('<h3>');
      var a = $('<a>');
      a.attr('href', item.link);
      a.text(item.title);
      h3.append(a);
      var p = $('<p>');
      var text = item.description.replace(/<\/?[^>]+(>|$)/g, " ");
      text = text.substring(0, 200);
      text = text.replace(/\w+$/, '');
      text = text + "...";
      p.html(text);
      li.append(h3);
      li.append(p);
      $('section.journal ul').append(li);
    });
  });

});

