/**
 * jQuery Slideshow Plugin
 *
 * Author: Johannes Wüller
 * Created On: 20.04.2010
 */
(function() {

   var window = this;

   jQuery.fn.slideshow = function(options) {
      // merge options with defaults
      options = jQuery.extend({
         interval:      3000,
         slideDuration: 1000,
         minItems:      2
      }, options || {});

      // loop through all slideshows
      jQuery(this).each(function() {
         var me = jQuery(this);

         // determine content dimensions
         var contentDimensions = {
            width:  0,
            height: 0
         };
         me.find(".slideshow-item").each(function() {
            contentDimensions.width += jQuery(this).outerWidth();
            var height = jQuery(this).outerHeight();
            if (height > contentDimensions.height) {
               contentDimensions.height = height;
            }
         });
         me.find(".slideshow-wrap").width(contentDimensions.width).height(contentDimensions.height);
         
         // functions
         var slide = function() {
            var firstItem = me.find(".slideshow-item").eq(0);
            me.find(".slideshow-wrap").animate({
               left: '-'+firstItem.outerWidth()+'px'
            }, options.slideDuration, function() {
               firstItem = firstItem.detach();
               jQuery(this).css({
                  left: '0px'
               }).append(firstItem);
            });
         };

         // bind interval
         if (me.find(".slideshow-item").length >= options.minItems) {
            window.setInterval(slide, options.interval);
         }
      });
   };

}());
