(function($){
  $.fn.slideshow = function(){
    return $(this).each(function(){
      var $slideshow  = $(this);
      var $ul         = $slideshow.find('ul');
      var $slides     = $slideshow.find('ul li');
      var $prev       = $slideshow.find('span.prev');
      var $next       = $slideshow.find('span.next');
      var $inView     = $ul.find('li:first');

      var posLeft     = -1;
      var animating   = false;
      var skipPrevOut = false;
      var skipNextOut = false;
      var isOver      = '';
      var opacitymin  = 0.6;
      var opacitymax  = 1;
      var slideindex  = 1;
      var slidelength = $slides.length;

      var atFirst = function () {
            return $inView.get(0) == $ul.find('li:first').get(0);
          };

      var atLast = function () {
            return $inView.get(0) == $ul.find('li:last').get(0);
          };

      var indicator = function(){
        $next.text( slideindex+'/'+slidelength );
      }

      var switchSlide = function () {
          indicator();
          $ul.stop(true);
          //$slideshow.find('div.buttons span').fadeTo('fast', 0.4);
          $ul.animate(
            { left: posLeft },
              500,
              'swing',
              function(){
                switch (true) {
                  case atFirst():
                    animating = false;
                    $prev.attr('title', '').addClass('inactive');
                    $next.removeClass('inactive');
                    break;
                  case atLast():
                    animating = false;
                    $next.attr('title', '').addClass('inactive');
                    $prev.removeClass('inactive');
                    break;
                  default:
                    switch (isOver) {
                      case 'next':
                        $next.fadeTo('fast', opacitymax, function () {
                          animating = false;
                        });
                        break;
                      case 'prev':
                        $prev.fadeTo('fast', opacitymax, function () {
                          animating = false;
                        });
                        break;
                      default:
                        animating = false;
                        break;
                    }
                    $next.attr('title', 'Neste bilde').removeClass('inactive');
                    $prev.attr('title', 'Forrige bilde').removeClass('inactive');
                 }
               }
          );
        };
      var showNext = function () {
          if (!atLast() && !animating) {
            animating = true;
            $inView   = $inView.next();
            posLeft   = parseInt($ul.css('left')) - parseInt($inView.width());
            slideindex++;            
            switchSlide();            
          }
        };

      var showPrev = function () {
          if (!atFirst() && !animating) {
            animating = true;
            $inView   = $inView.prev();
            posLeft   = parseInt($ul.css('left')) + parseInt($inView.width());
            slideindex--;            
            switchSlide();
          }
        };
      
      // Klikk og hoverevents på next
      $next.click(function (e) {
        e.preventDefault();
        showNext();
      });
      $next.hover(
        function () {
          isOver = 'next';
          $next.stop(false, true);
          if (!atLast()) {
            $next.fadeTo('normal', opacitymax);
          } else {
            skipNextOut = true;
          }
        },
        function () {
          isOver = '';
          $next.stop(false, true);
          if (!skipNextOut) {
            $next.fadeTo('normal', opacitymin);
          }
          skipNextOut = false;
        }
      );

      // Klikk og hoverevents på prev
      $prev.click(function (e) {
        e.preventDefault();
        showPrev();
      });
      
      $prev.hover(
        function () {
          isOver = 'prev';
          $prev.stop(false, true);
          if (!atFirst()) {
            $prev.fadeTo('normal', opacitymax);
          } else {
            skipPrevOut = true;
          }
        },
        function () {
          isOver = '';
          $prev.stop(false, true);
          if (!skipPrevOut) {
            $prev.fadeTo('normal', opacitymin);
          }
          skipPrevOut = false;
        }
      );
        
      var init = function () {
        if ($ul.find('li').length > 1) {
          $prev.text('').css('opacity', 0).show().fadeTo('normal', opacitymin).addClass('inactive');
          $next.css('opacity', 0).show().fadeTo('normal', opacitymin);
          indicator();
        }else{
          $prev.hide();
          $next.hide();
        }
      }();
      
    });
  }
})(jQuery);


