Multiple instances of plugin

Multiple instances of plugin

Hey there,

I'm trying to figger out how to make a simple plugin, and make it work for multiple sections on my current project.
 
Let's say, I've got two sets of images that I want to horizontally scroll when I click on there own navigation-arrows. The markup could look something like this:

  1. <div id="container_1" class="container">
  2. <div class="nav">
  3. <a href="#" class="prev">previous</a>
  4. <a href="#" class="prev">next</a>
  5. </div>
  6. <div class="item"><img src="" alt="" /></div>
  7. <div class="item"><img src="" alt="" /></div>
  8. <div class="item"><img src="" alt="" /></div>
  9. <div class="item"><img src="" alt="" /></div>
  10. <div class="item"><img src="" alt="" /></div>
  11. </div>

  12. <div id="container_2" class="container">
  13. <div class="nav">
  14. <a href="#" class="prev">previous</a>
  15. <a href="#" class="prev">next</a>
  16. </div>
  17. <div class="item"><img src="" alt="" /></div>
  18. <div class="item"><img src="" alt="" /></div>
  19. <div class="item"><img src="" alt="" /></div>
  20. <div class="item"><img src="" alt="" /></div>
  21. <div class="item"><img src="" alt="" /></div>
  22. </div>

Now.. Using jQuery, I want to call something like:

  1. $("#container_1").scroller();
  2. $("#container_2").scroller();

or even better:
  1. $(".container").scroller();

I've kinda got this working, but there's defiantly something wrong. Here's what I got (simplified):

  1. (function($)
  2. {
  3. $.fn.scroller = function(options) {
  4. var defaults = {
  5.    container_width: 900,
  6. container_height: 400,
  7. item_width: 900,
  8. items_per_view: 1,
  9. duration: 200
  10.  };
  11. if (options) { $settings = $.extend(defaults, options) };
  12. return this.each(function() {
  13. $inner = $(this).find('.inner');
  14. $nav = $(this).find('.nav');
  15. $items = $(this).find('.item');
  16. $sliding = false;
  17. $(nav).find('.prev').click(function() {
  18. if(sliding === false && $.fn.scroller.currentItem() > 0) {
  19. $.fn.scroller.toItem('prev');
  20. }
  21. this.blur();
  22. return false;
  23. });
  24. $(nav).find('.next').click(function() {
  25. if(sliding === false && $.fn.scroller.currentItem() < (items.length-options.items_per_view)) {
  26. $.fn.scroller.toItem('next');
  27. }
  28. this.blur();
  29. return false;
  30. });
  31. });
  32. };
  33. $.fn.scroller.currentItem = function() {
  34. return 0-(parseFloat($($inner).css('left')) / $settings.item_width);
  35. }

  36. $.fn.scroller.toItem = function(direction) {
  37. var target_item;
  38. if(direction=='next') {
  39. target_item = $.fn.scroller.currentItem() + 1;
  40. } else {
  41. target_item = $.fn.scroller.currentItem() - 1;
  42. }
  43. var target_margin = 0-(target_item*$settings.item_width);
  44. $sliding = true;

  45. $($inner).stop().animate(
  46. { "left": target_margin },
  47. {
  48. duration: $settings.duration, 
  49. complete: function() {
  50. $sliding = false;
  51. }
  52. }
  53. );
  54. }
  55. })(jQuery);

This kinda works, but the problem is that the click-event are overwritten, so both navigations will control the second container (instead of their own). What should be the right approach to make this work for each instance specifically? And what should be the way to set variables for each instance, for starters? Thanks!