jQuery plugins

(shamelessly stolen from: http://docs.jquery.com/Plugins/Authoring)

Getting started

Simple structure
jQuery.fn.myPlugin = function() {

  // Do your awesome plugin stuff here

};
Scaffolding we will use (Why is it better?)
(function( $ ){
  $.fn.myPlugin = function() {
  
    // Do your awesome plugin stuff here
	
	//inside of here, we don't do $(this) since it was sent in
	//From this context - $(this) === $($('#id'))
	
	this.somejQueryMethod();
  };
})( jQuery );

//To use above:
$('#element').myPlugin();

Maintain Chain

(function( $ ){

  $.fn.lockDimensions = function( type ) {  

    return this.each(function() {
	  //note the this above unwrapps from jQuery collection, so need factory function
      var $this = $(this);

      if ( !type || type == 'width' ) {
        $this.width( $this.width() );
      }

      if ( !type || type == 'height' ) {
        $this.height( $this.height() );
      }

    });

  };
})( jQuery );

//To use above:
$('div').lockDimensions('width').css('color', 'red');

Putting it all together

First, define your plugin
(function( $ ){

  $.fn.tooltip = function( method ) {
    var methods = {
      init : function( options ) { // code here to init },
      show : function( ) { // code here to show   },
      hide : function( ) { // code here to hide },
      update : function( content ) { // code here to change/alter }
    };
    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }    
  
  };

})( jQuery );

After above is loaded, we can now use!

$('div').tooltip(); // calls the init method
$('div').tooltip({  // calls the init method
  foo : 'bar'
});
$('div').tooltip('hide'); // calls the hide method
$('div').tooltip('update', 'This is the new tooltip content!'); // calls the update method