jQuery.fn.autolink = function () {
  return this.each(function(){
    var re = /((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g;
    $(this).html($(this).html().replace(re, '<a href="$1">$1</a>'));
  });
};

Twitter = $.klass({
  initialize: function(user_id, count) {
    var container = $(this.element);
    $.getJSON("http://twitter.com/status/user_timeline/"+user_id+".json?count=" + count + "&callback=?", function(data) {
      $.each(data, function(i, item) {
        container.html('<span>Twitter status:</span> ' + item.text).autolink();
      });
    });
  }
});

var original_msg, message;

function showMessage(menu_item) {
  message = (menu_item) ? $(menu_item).html() : original_msg;
  $('#messages').html(message).addClass('focus');
}

$(document).ready(function() {
  
  $twitter = $('<div id="latest_tweet" style="display: none"></div>');
  $('body').append($twitter);
  $twitter.html('Loading latest tweet...').slideDown(750, function() {
    $(this).attach(Twitter, 'attilagyorffy', 1);
  });
  
  original_msg = $('#messages').html();

  $('#main a').each(function(index, menu_item) {
    $(menu_item).hover(
      function() {
        showMessage(menu_item);
        // $(this).expose({api: true}).load();
        // $(this).expose({
        //   // custom mask settings with CSS
        //   maskId: 'mask',
        //   // when exposing is done, change form's background color
        //   onLoad: function() {
        //     this.getExposed().css({backgroundColor: '#c7f8ff'});
        //   },
        //   // when "unexposed", return to original background color
        //   onClose: function() {
        //     this.getExposed().css({backgroundColor: null});
        //   },
        //   api: true
        // }).load();
      },
      function() { showMessage(); }
    );
  });
    
});

function hide_notification() {
  $('#top').fadeOut(1000);
}