(function(){
  $.mn.mainMenu = {
  };

  $.mn.personalMenu = {
  };

  $.mn.contextualMenu = {
    noteId: null,
    touched: false,
    timeoutId: null,
    init: function() {
      $('#contextual_menu').hoverIntent({
        over: function(ev){
          $.mn.contextualMenu.touched = true;
        }, 
        out: function(ev){
          $.mn.contextualMenu.hide();
        },
        timeout: 2000
      });
    },
    shown: function(noteId) {
      return (this.noteId == noteId);
    },
    show: function(noteId) {
      var n;
      if (this.noteId != null) {
        n = noteId;
        this.noteId = null;
        $('#contextual_menu').hide('fast', function(){ $.mn.contextualMenu.show(n) });
      } else {
        var $j = $('#note_' + noteId);
        var p = $j.position();
        p.top += $j.height() + 1;
        $('#contextual_menu').css({ top: p.top, left: p.left }).show('fast');
        this.noteId = noteId;
      }
      this.touched = false;
      if (this.timeoutId) {
        window.clearTimeout(this.timeoutId);
        this.timeoutId = null;
      }
      this.timeoutId = window.setTimeout(function(){
        if (!$.mn.contextualMenu.touched) {
          $.mn.contextualMenu.hide();
        }
        $.mn.contextualMenu.timeoutId = null;
      }, 3000);
    },
    hide: function() {
      if (this.timeoutId) {
        window.clearTimeout(this.timeoutId);
        this.timeoutId = null;
      }
      $('#contextual_menu').hide('fast');
      this.noteId = null;
      this.touched = false;
    },
    toggle: function(noteId) {
      if (this.noteId == noteId) {
        this.hide();
      } else {
        this.show(noteId);
      }
    },
    move: function(noteId) {
      var $j = $('#note_' + noteId);
      var p = $j.position();
      p.top += $j.height() + 1;
      $('#contextual_menu').css({ top: p.top, left: p.left });
    }
  };

})();
