/*
* jquery.lighttable.js - Full Window Slideshow
* By Damian Boune (dboune.at.gmail.com)
*/

(function($) {
  
  var methods = {
    
    init: function( options ) {
      this.each(function() {
        
        $(this).css('overflow', 'hidden').css('position', 'fixed');
        $(this).children('img').css('position', 'absolute').wrap('<div class="lighttable-image" style="position: fixed"/>');
        
        $(this).children('.lighttable-image').each(function() {
          $(this).height($(this).children('img').height());
          $(this).width($(this).children('img').width());
        });
        
        $(this).children('.lighttable-image').first().addClass('active');
        $(this).children('.lighttable-image').not('.active').hide();
        
        $(window).bind('resize.lighttable', this, function(e) {
            $(e.data).lighttable('resize');
        });
        $(this).lighttable('resize');
        
        var t = this;
        setInterval(function() {
          $(t).lighttable('next');
        }, 10000);
      });
      
      return this;
    },
    
    destroy: function() {
      return this.each(function() {
        $(window).unbind('.lighttable');
        //clearInterval()
      });
    },
    
    resize: function() {
      
      $(this).children('.lighttable-image.active').each(function() {
        var current_img = $(this).children('img');
        var win_width = $(window).width();
        var win_height = $(window).height();
        var ratio = $(this).height() / $(this).width();
        var offset;

        if (win_height / win_width > ratio) {
          $(this).height(win_height);
          $(this).width(win_height / ratio);
          current_img.height(win_height);
          current_img.width(win_height / ratio);
        } else {
          $(this).width(win_width);
          $(this).height(win_width * ratio);
          current_img.width(win_width);
          current_img.height(win_width * ratio);
        }

        if (1 == 1) {
          current_img.css('left', (win_width - current_img.width()) / 2)
            .css('top', (win_height - current_img.height()) / 2);
        }
      });
      return this;
    },
    
    next: function() {
      var current = $(this).children('.active');
      var next = current.next('.lighttable-image');
    
      if (!next.length) {
        next = $(this).children('.lighttable-image').first();
      }
      
      current.fadeOut(2000, function() {
        $(this).removeClass('active');
      });
      next.fadeIn(2000).addClass('active');
      $(this).lighttable('resize');
      return this;
    }
  };
  
  $.fn.lighttable = 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 in jQuery.lighttable');
    }
    
    return this;
  };
  
  $.fn.lighttable.defaults = {
    width: 4,
    height: 3,
    centered: 1
  };

  
})(jQuery);
