/*
 * jQuery Slideshow
 * author:     Johannes Wüller
 * created on: 26.10.2009
 *
 * This provides a slideshow which is capable of displaying images and
 * controlling them with some buttons.
 *
 * Usage:
 *    jQuery.slideshow(options);
 *
 * Options:
 *    [selector] mainContainer      element that contains the whole slideshow
 *    [selector] nextButton         switches to the next image on click.
 *    [selector] prevButton         switches to the previous image on click.
 *    [selector] titleContainer     title gets displayed here.
 *    [selector] descContainer      description gets displayed here.
 *    [selector] imagesContainer    contains the <img>-tags to be displayed.
 *    [selector] displayContainer   contains the currently displayed image.
 *    [selector] diashowButton      toggles diashow.
 *    [selector] numberContainer    containes number of active/total image.
 *    [bool]     continuous         jump to first/last if at the end/beginning.
 *    [bool]     diashow            automated displaying of the next image.
 *    [integer]  fadeInSpeed        how fast the fade-in-effect should be.
 *    [integer]  fadeOutSpeed       how fast the fade-out-effect should be.
 *    [integer]  hideControlsDelay  how long to wait before hiding controls
 *    [integer]  diashowDelay       how long to wait before next image.
 */
jQuery.slideshow = function(options) {

   /*
    * options
    */
   var defaults = {
      fadeInSpeed: 500,
      fadeOutSpeed: 500,
      continuous: false,
      hideControlsDelay: 1000,
      diashow: false,
      diashowDelay: 5000
   };
   options = jQuery.extend(defaults, options);

   /*
    * variables
    */
   var currentImage = 0;
   var title = '';
   var description = '';
   var hideControlsTimeout = null;
   var diashowInterval = null;

   /*
    * functions
    */
   var next = function () {
      if (currentImage < images().length - 1) {
         currentImage++;
      } else if (options.continuous) {
         currentImage = 0;
      }
      displayCurrentNumbers();
   };
   var prev = function () {
      if (currentImage > 0) {
         currentImage--;
      } else if (options.continuous) {
         currentImage = images().length - 1;
      }
      displayCurrentNumbers();
   };
   var displayCurrentNumbers = function () {
      jQuery(options.numberContainer).html((currentImage + 1)+'/'+images().length);
   };
   var images = function () {
      return $(options.imagesContainer).children();
   };
   var setTitle = function(value) {
      jQuery(options.titleContainer).html(value);
   };
   var setDescription = function(value) {
      jQuery(options.descContainer).html(value);
   };
   var displayImage = function () {
      if (images().length == 1) {
         jQuery(options.nextButton).addClass('next-disabled');
         jQuery(options.prevButton).addClass('prev-disabled');
      }
      var container = jQuery(options.displayContainer);
      var preloadImage = function () {
         var currImage = jQuery(images()[currentImage]);
         jQuery('<img src="'+currImage.find('.url').html()+'" alt="" />').hide().load(function () {
            setTitle(currImage.find('.title').html());
            setDescription(currImage.find('.description').html());
            displayCurrentNumbers();
            container.html(jQuery(this).stop().fadeIn(options.fadeInSpeed));
         });
      };
      if (container.children().length > 0) {
         container.children().stop().fadeOut(options.fadeOutSpeed, preloadImage);
      } else {
         preloadImage();
      }
   };
   var reset = function () {
      currentImage = 0;
   };
   var showControls = function () {
      jQuery(options.controlbar).slideDown();
   };
   var hideControls = function () {
      jQuery(options.controlbar).slideUp();
   };
   var toggleDiashow = function () {
      if (diashowInterval) {
         window.clearInterval(diashowInterval);
         diashowInterval = null;
      } else {
         diashowInterval = window.setInterval(function () {
            jQuery(options.nextButton).click();
         }, options.diashowDelay);
      }
   };

   /*
    * bindings
    */
   jQuery(options.nextButton).click(function () {
      var prevImage = currentImage;
      next();
      if (currentImage != prevImage) {
         displayImage();
      }
   });
   jQuery(options.prevButton).click(function () {
      var prevImage = currentImage;
      prev();
      if (currentImage != prevImage) {
         displayImage();
      }
   });
   jQuery(options.mainContainer).hover(function () {
      if (hideControlsTimeout) {
         window.clearTimeout(hideControlsTimeout);
      }
      showControls();
   }, function () {
      if (!$.browser.ipad) {
         hideControlsTimeout = window.setTimeout(function () {
            hideControls();
         }, options.hideControlsDelay);
      }
   });
   jQuery(options.diashowButton).click(function () {
      toggleDiashow();
      return false;
   });
   jQuery.slideshowDisplayImage = function () {
      reset();
      displayImage();
   };

   if ($.browser.ipad) {
      showControls();
   }

   /*
    * diashow
    */
   if (options.diashow) {
      jQuery(options.diashowButton).click();
   }

};

