// -----------------------------------------------------------------------------

// Calculating relative time
function relativeTime(time) {
  var values = time.split(' ');
  time = values[1] + ' ' + values[2] + ', ' + values[5] + ' ' + values[3];
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var diff = parseInt((relative_to.getTime() - Date.parse(time)) / 1000) + (relative_to.getTimezoneOffset() * 60);
  if (diff < 60)
    return 'less than a minute ago';
  else if (diff < 120)
    return 'about a minute ago';
  else if (diff < (60*60))
    return (parseInt(diff / 60)).toString() + ' minutes ago';
  else if (diff < (120*60))
    return 'about an hour ago';
  else if (diff < (24*60*60))
    return 'about ' + (parseInt(diff / 3600)).toString() + ' hours ago';
  else if (diff < (48*60*60))
    return '1 day ago';
  else
    return (parseInt(diff / 86400)).toString() + ' days ago';
}

//-----------------------------------------------------------------------------

// Twitter callback
function twitterCallback(tweets) {
  for (var i in tweets) {
    var screen_name = tweets[i].user.screen_name;
    var text = tweets[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'/">'+reply.substring(1)+'</a>';
    });
    jQuery('#twitter').append
    (
      '<div class="tweet">'+
        '<div class="date">'+
          '<a href="http://twitter.com/'+screen_name+'/statuses/'+tweets[i].id+'">'+
            relativeTime(tweets[i].created_at)+
          '</a>'+
        '</div>'+
        '<p>'+text+'</p>'+
      '</div>'
    );
  }
}

// -----------------------------------------------------------------------------

jQuery(document).ready(function(){
  
  // Binding Navigate button action
  jQuery('#navigate').click(function(){
    if ( ! jQuery('#menu').is(':animated'))
      jQuery('#menu').slideToggle(300);
  });
  
  // Configuring search edit
  jQuery('#s').focus(function(){
    if (jQuery(this).val() == 'search')
    {
      jQuery(this).val('');
    }
  });
  jQuery('#s').blur(function(){
    if (jQuery(this).val() == '')
    {
      jQuery(this).val('search');
    }
  });
  
  // Binding Submit Comment button action
  jQuery('#commentsubmit').click(function(){
    jQuery(this).parent().parent().submit();
  });
  
  // Centering .center class button
  jQuery('.button.center').each(function(){
    jQuery(this).css('width', jQuery(this).width()).css('float', 'none');
  });
  
});
