Here's the basics of defining and using a JQuery plugin within the context of Drupal.
This is definitely not the best and highest use of a plugin, and may in fact be a downright abomination-- this isn't a plugin that really works outside the context of the code it is written for, and certainly not outside Drupal. But it was a clean way to abstract out 42 lines of code, so here it is, for the enjoyment of all.
The first part here is simply how to call JQuery from Drupal, the second part is the plugin.
Agaric makes powerful web sites for people who do things. As a collective of skilled workers, Agaric collaborates with you and open source free software communities to develop tools and build platforms that meet your needs.
How we can help you? Please ask Agaric!