(function($){
  $.mw = $.extend($.mw?$.mw:{}, {
    binders: [],
    registerBinder: function(binder) {
      this.binders.push(binder);
    },
    // bind(element) - binds editors to element.
    bind: function(elem) {  
      if (elem == undefined) elem = document;
      var $elem = $(elem); 
      $.each(this.binders, function(k, v) {
        v.bind($elem);
      });
    },
    debug: function(data) {
      if (console != undefined) console.debug(data);
    }           
  });
  // MW_Form_Control class:
  $.mw.binder = function(opts) {
    $.extend(this, opts);
    $.mw.registerBinder(this);
  };
  
  $.mw.binder.prototype.bind = function($elem) {
    alert('Unbound!');
  };
  
  // $().hoverClass('classname');
  $.fn.hoverClass = function(className) {
    return this.hover(function() { $(this).addClass(className); }, function() { $(this).removeClass(className); });
  };
  
  // $().mwbind(function);
  // checks EACH element that is not already been bound, then calls the function.
  // the function should return something that evaluates to true if it bound.
  
  $.fn.mwbind = function(key, callback) {
    if (typeof key == 'function' || !callback) {
      callback=key;
      key = '';
    }
    if (typeof key != 'string') key = '' + key;
    return this.each(function() {
      if ($.data(this,'mwbind_'+key)) return;
      $.data(this,'mwbind_'+key, callback.call(this));
    });
  };
  
  // 'a.new_window' links
  $.mw.new_window = new $.mw.binder({
    bind: function(elem) {
      $('a.new_window').mwbind('newwindow', function() {
        $(this).click(function(e) {
          window.open(this.href);
          e.stopPropagation();
          return e.preventDefault();
        });
        return true;
      });
    }
  });
  
  $.mw.confirm = new $.mw.binder({
    bind: function(elem) {
      $('.mw_confirm, .zend_form .delete').mwbind('confirm', function() {
        $(this).click(function(e) {
          if (!confirm('Are you sure?'))
          {
            e.stopPropagation();
            return e.preventDefault();
          }
        });
      });
    }
  });

  $(function() {
    $.mw.bind();
  });
})(jQuery);
