;(function() {
  if (typeof window.MO == 'undefined') { window.MO = {}; }

  $(document).ready(function() {
    MO.setupUIEffects();
  });

  MO.setupUIEffects = function() {
    MO.setupSidebarEffects();
    MO.setupSearchFormEffects();
    MO.setupContactMeEffects();
  };

  MO.effects = {
    fadeIn: function(e) {
      e.stop();
      e.fadeTo('fast', 1.0);
    },
    fadeOut: function(e) {
      e.stop();
      e.fadeTo('slow', 0.3);
    }
  };

  MO.setupSidebarEffects = function() {
    $('#sidebar, #footer').fadeTo(3000, 0.3);
    $('#sidebar, #footer').hover(function() {
      MO.effects.fadeIn($(this));
    }, function() {
      if (MO.isSearching) { return; }
      MO.effects.fadeOut($(this));
    });
  };

  MO.setupSearchFormEffects = function() {
    $('.searchform #s').focus(function() {
      MO.isSearching = true;
      MO.effects.fadeIn($('#sidebar'));
    });
    $('.searchform #s').blur(function() {
      MO.isSearching = false;
      MO.effects.fadeOut($('#sidebar'));
    });
  };

  MO.setupContactMeEffects = function() {
    $('.contact-me-link').hover(function() {
      $('#header-contact-me').html('<span class="contact-me-email">' +
                                   ["michael","michaelobrien.info"].join("@") +
                                  '</span><br /><span style="font-size: 75%;">irc: notbrien</span>');
      MO.effects.fadeIn($('#header-contact-me'));
    }, function () {
      $('#header-contact-me').stop();
      $('#header-contact-me').fadeTo('slow', 0.0);
    });

    $('#header-contact-me').hover(function() {
      if($(this).css('opacity') > 0) {
        MO.effects.fadeIn($(this));
      }
    }, function () {
      $(this).stop();
      $(this).fadeTo('slow', 0.0);
    });
  };
})();
