Help turning my modal window script into a plugin

Help turning my modal window script into a plugin

Hi All

I've been learning jQuery for the last while now and really love it. I've written a few simple scripts, and I'm now trying to learn how to make them into plugins so I can re-use them in different projects.

I made a modal window script, which I'm trying to make into a plugin so I can pass different 'triggers' and 'modal contents' to it. However, the close function doesn't work anymore now. Can anyone see what the problem is? Pretty sure it's something simple I'm missing.


         
  1. <script type="text/javascript">
  2. (function($){
  3. $.fn.extend({
  4.     mynewmodal: function(options) {
  5.    var defaults = {
  6.                    container: "#modalcontainer"
  7.           };
  8.         var options = $.extend(defaults, options);
  9.         return this.each(function() {
  10.             var o =options;
  11.             // Add our click OPEN event
  12.             $(this).click(function (e) {
  13.                 e.preventDefault();
  14.                 // Add our overlay div
  15.                 $('body').append('<div id="overlay" />');
  16.                 // Fade in overlay
  17.                 $('#overlay').css({"display":"block","opacity":"0"}).animate({"opacity":"0.2"}, 300),
  18.                 // Animate our modal window into view
  19.                 $(o.container).css({"top":"43%"}).pause(200).css({"opacity":"0"}).show().animate({"top": "50%", "opacity": "1"}, 550),
  20.                 // Add our close image
  21.                 $(o.container).append('<div id="modal-vid-close" title="Close window" />');
  22.                 // Add our click CLOSE event
  23.                 $('#overlay', '#modal-vid-close').click(function () {
  24.                     //Animate our modal window out of view
  25.                     $(o.container).animate({"top": "55%", "opacity": "0"}, 350).fadeOut(200),
  26.                     // Fade out and remove our overlay
  27.                     $('#overlay').pause(500).fadeOut(200, function () { $(this).remove(); $('#modal-vid-close').remove()} )
  28.                 });
  29.             });
  30.         });
  31.     }
  32. });
  33. })(jQuery);
  34. </script>

and then the options being passed on the page:


  1. <script type="text/javascript">
  2. var $j = jQuery.noConflict();
  3. $j(document).ready( function() {
  4. $j('.video_container a').mynewmodal({ container: "#video" });
  5. });
  6. </script>