(function($) {
  // Slide all images
  $.fn.Slideshow = function(options) {
    var slideshow = $(this);

    var config = $.extend({
      Speed: 1000, // slide speed, 0 = just show next foto
      Interval: 1000, // between slide interval, 0 = only manual movement
      Navigation: {
        Previous: slideshow.find('.previous'), // Navigational control previous
        Next: slideshow.find('.next'), // Navigational control next
        Toggle: slideshow.find('.toggle') // Navigational control toggle play
      },
      DisableNavigationOnSameDirection: true,
      Direction: 'horizontal', // Values are vertical or horizontal
      Start: true // Starts immediately when set to true, or next or previous for starting direction, false doesn't start the slideshow
    }, options || {});

    var container = slideshow.find("div.slides");
    var slides = container.find("> *");
    var currentSlide = null;
    var current = 1;
    var play = true;
    var isBusy = false;
    var timer = null;

    // Set navigation, if config.Navigation is a wrapped set
    if ($.isFunction(config.Navigation)) {
      config.Navigation = config.Navigation.apply(this);
    }

    if (config.Navigation['size'] >= null) {
      config.Navigation = {
        Next: config.Navigation.find('.next'),
        Toggle: config.Navigation.find('.toggle'),
        Previous: config.Navigation.find('.previous')
      };
    }

    // Hide navigation if only 1 slide
    if (slides.size() < 2) {
      config.Navigation.Next.remove();
      config.Navigation.Toggle.remove();
      config.Navigation.Previous.remove();
    }

    // Set css properties according to direction
    var setEndPoint = function(pos) {
      return config.Direction == 'vertical' ? { top: -pos} : { left: -pos };
    };

    var slideToPosition = function(fn) {
      if (play) {
        isBusy = true;
        currentSlide = slides.filter(format('*[data-slideshoworder={0}]', current));
        var position = currentSlide.attr('data-slideshowpos');
        container.stop().animate(setEndPoint(position), config.Speed, function() {
          if (play) {
            var gotoSlide = currentSlide.attr('data-slideshowgoto');
            if (gotoSlide != null) {
              moveTo(gotoSlide);
            }

            if (config.Interval > 0) {
              timer = setTimeout(fn, config.Interval);
            }
          }
          isBusy = false;
        });
      }
    };

    var moveTo = function(slideNumber) {
      current = slideNumber;
      currentSlide = slides.filter(format('*[data-slideshoworder={0}]', current));
      position = currentSlide.attr('data-slideshowpos');
      container.css(setEndPoint(position));
    };

    var setSelectedNavigation = function(selectedNavigation) {
      config.Navigation.Next.removeClass('selected');
      config.Navigation.Previous.removeClass('selected');
      config.Navigation.Toggle.removeClass('selected');
      selectedNavigation.addClass('selected');
    };

    var stop = function() {
      container.stop();
      clearTimeout(timer);
    };

    // Slide to previous slide
    var previous = function() {
      direction = previous;
      if (play || !config.DisableNavigationOnSameDirection) {
        stop();
        current--;
        setSelectedNavigation(config.Navigation.Previous);
        if (!config.DisableNavigationOnSameDirection) {
          config.DisableNavigationOnSameDirection = true;
          setTimeout(function() {
            config.DisableNavigationOnSameDirection = false;
          },
          config.Speed);
        }
        slideToPosition(direction);
      }
    };

    // Slide to next slide
    var next = function() {
      direction = next;
      if (play || !config.DisableNavigationOnSameDirection) {
        stop();
        current++;
        setSelectedNavigation(config.Navigation.Next);
        if (!config.DisableNavigationOnSameDirection) {
          config.DisableNavigationOnSameDirection = true;
          setTimeout(function() {
            config.DisableNavigationOnSameDirection = false;
          },
          config.Speed);
        }
        slideToPosition(direction, -1);
      }
    };

    var initialize = function() {
      var counter = 0;
      var pos = 0;
      // Add last slide before first slide for smooth transition
      container.prepend(slides.last().clone(false));

      // Add first slide before last slide for smooth transition
      container.append(slides.first().clone(false));

      // Set offset to real first slide
      if (config.Direction == 'vertical') {
        container.css('top', -slides.first().height());
      }
      else {
        container.css('left', -slides.first().width());
      }

      // Reset slides
      slides = container.find("> *");

      // Process each slide
      slides.each(function() {
        var slide = $(this);
        if (counter == 1) {
          // Set current slide to real first slide
          currentSlide = slide;
        }
        // Set position of slide absolutely, to avoid margin render faults
        slide.css({
          left: (config.Direction == 'vertical' ? 0 : pos),
          top: (config.Direction == 'vertical' ? pos : 0),
          position: 'absolute'
        });
        if (counter == 0) {
          // Set move to real last slide when sliding to slide 0
          slide.attr("data-slideshowgoto", slides.size() - 2);
        }
        else if (counter == (slides.size() - 1)) {
          // Set move to real first slide when sliding to last slide
          slide.attr("data-slideshowgoto", 1);
        }
        slide.attr("data-slideshoworder", counter++);
        slide.attr("data-slideshowpos", pos);
        pos += config.Direction == 'vertical' ? slide.height() : slide.width();
      });

      // Set container width or height to slides width or height
      if (config.Direction == 'vertical') {
        container.width(pos);
      }
      else {
        container.height(pos);
      }

      // Set navigational functions
      // Set previous button
      config.Navigation.Previous.click(function() {
        if (!isBusy) {
          play = true;
          if (direction != previous || !config.DisableNavigationOnSameDirection) {
            stop();
            previous();
          }
        }
        return false;
      });

      // Set next button
      config.Navigation.Next.click(function() {
        if (!isBusy) {
          play = true;
          if (direction != next || !config.DisableNavigationOnSameDirection) {
            stop();
            next();
          }
        }
        return false;
      });

      var currentDirection = null;
      // Set Toggle play button
      config.Navigation.Toggle.click(function() {
        if (play) {
          play = false;
          currentDirection = direction;
          direction = null;
          setSelectedNavigation(config.Navigation.Toggle);
        }
        else {
          play = true;
          currentDirection();
        }
        return false;
      });
    };

    var start = function() {
      play = true;

      var defaultDirection = next;
      if (config.Start == 'previous') {
        defaultDirection = previous;
      }

      setTimeout(defaultDirection, 5000);
    };

    // Sets direction
    var direction = null;
    // Initialize slideshow
    initialize();
    if (config.Start != false) {
      start();
    }
    else {
      setSelectedNavigation(config.Navigation.Toggle);
    }

    // Return wrappedset for further processing
    return slideshow;
  };

  $.fn.SetSlideshowHeight = function() {
    var container = $(this).find("div.slides");
    var slides = container.find("> *");
    var height = 0;
    slides.each(function() {
      var slideHeight = $(this).height();
      if (slideHeight > height) {
        heigth = slideHeight;
      }
    });
    container.parent('.container').height(heigth);

    return $(this);
  };

  /* Example call
  $(function() {
    $('.slideshow.horizontal').SetSlideshowHeight().Slideshow({
      DisableNavigationOnSameDirection: false,
      Speed: 1000,
      Interval: 0,
      Start: false,
      Navigation: function() {
        var slideshow = $(this);
        slideshow.find('.container').append('<div class="fotonavigatie vorige"><a href="javascript:void()"><img src="/views/shared/images/buttons/vorige.png" /></a></div><div class="fotonavigatie volgende"><a href="javascript:void()"><img src="/views/shared/images/buttons/volgende.png" /></a></div>');

        return {
          Next: slideshow.find('.fotonavigatie.volgende a'),
          Toggle: $('selectnothing'),
          Previous: slideshow.find('.fotonavigatie.vorige a')
        };
      }
    });
  });
  */
})(jQuery);

// Default string.Format function
function format() {
  var args = arguments.length == 1 ? arguments[0] : arguments;
  var str = args[0];
  for (var i = 1; i < args.length; i++) {
    str = str.replace('{' + (i - 1) + '}', args[i]);
  }
  return str;
}

