/*
jquery.nc.lightBox.js
 */
(function ($) {
   var methods = {
      sizeTranslucentLayer : function () {
         var $this = $(this);
         $($this.data('opts').translucentLayer).height($(document).height())
      },
      close : function(){
         var $this = $(this);
         $('body').append( $($this.data('opts').content ).hide() ) ;
         $($this.data('opts').translucentLayer).hide();
         $($this.data('opts').lightBoxFrame).hide();
      },
      sizeBox : function(){
         var $this = $(this);
         $($this.data('opts').lightBoxFrame).width( $($this.data('opts').content).outerWidth() )
         $($this.data('opts').lightBoxFrame).height($($this.data('opts').content).outerHeight() )
      },
      positionBox : function(){
         var $this = $(this);
         var leftPos = ($(window).width() / 2) - ($($this.data('opts').lightBoxFrame).outerWidth() / 2)
         var topPos = ($(window).height() / 2) - ($($this.data('opts').lightBoxFrame).outerHeight() / 2) + $(window).scrollTop()
         $($this.data('opts').lightBoxFrame).css('left', leftPos)
         $($this.data('opts').lightBoxFrame).css('top', topPos)
      },
      init : function (options) {

         return this.each(function () {
            var $this = $(this);
            $this.data('opts', options);
            $(window).bind('scroll', function(){
               methods.positionBox.call($this);
            })
            .bind('resize', function(){
               methods.positionBox.call($this);
            });

            $this.bind('click', function(){
               $this.trigger('show');
               // trans layer
               methods.sizeTranslucentLayer.call($this);
               $(options.translucentLayer).show();
               // box
               $(options.lightBoxFrame).show();
               methods.sizeBox.call($this);
               methods.positionBox.call($this);

               // populate w. content
               $(options.content).show();
               $($this.data('opts').lightBoxFrame).append($(options.content));

               return false;
            });
            $(options.translucentLayer).bind('click', function(){
               methods.close.call($this);

            });
         });
      }
   };

   $.fn.lightBox = function (method) {
      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 lightBox');
      }
   };
})(jQuery);
