(function($) {
  $.fn.galleryView = function(options) {
      var opts = $.extend({}, $.fn.galleryView.defaults, options);

      //Is this a webkit-browser? (like Safari or Chrome)
      if($.browser.webkit){
          //Yes it is, then we need to wait until document is loaded, otherwise it will look messy
          $tmp = this; //Otherwise it won't be recognized in window load-function
          $(window).load(function(){
              return $.fn.galleryView.initGallery($tmp);
          });
      } else {
          //No, that's good, just continue
          return $.fn.galleryView.initGallery(this);
      }
  };

  $.fn.galleryView.initGallery = function(){
    //Fix for resetting scroller on reload (otherwise it could start in the middle of a scroll)
    $(window).load(function(){
      $(".galleryHolder .itemWrapper").scrollLeft(0);
    });
    
      //How many items are there?
      $(".galleryHolder").each(function(){
        var noOfItems = $(this).find(".itemHolder .item").length;
        //How wide are they?
        $.fn.galleryView.params.itemWidth = $(this).find(".itemWrapper:first").width();

        //How wide should the holder be?
        var holderWidth = $.fn.galleryView.params.itemWidth * noOfItems;
        //Set holders width
        $("#itemHolder").css({ 'width' : holderWidth + 'px' });

        $.fn.galleryView.setInterval();

        //Bind the arrows at the sides to action
        //Show previous item
        $(this).find("a.previous").click(function(ev){
            ev.preventDefault();
            $.fn.galleryView.goPrevious();

            //Stop slideshow, temporary
            clearInterval($.fn.galleryView.defaults.intervalHolder);
            clearTimeout($.fn.galleryView.defaults.timeoutHolder);

            $.fn.galleryView.restart();
        });

        //Show next item
        $(this).find("a.next").click(function(ev){
            ev.preventDefault();
            $.fn.galleryView.goNext();

            //Stop slideshow, temporary
            clearInterval($.fn.galleryView.defaults.intervalHolder);
            clearTimeout($.fn.galleryView.defaults.timeoutHolder);

            $.fn.galleryView.restart();
        });

        //When hovering a link/item, stop slideshow
        $(this).find("a.item").live('mouseenter', function(){
            //Stop slideshow, temporary
            clearInterval($.fn.galleryView.defaults.intervalHolder);
            clearTimeout($.fn.galleryView.defaults.timeoutHolder);
        });

        //On mouseleave, restart slideshow
        $(this).find("a.item").live('mouseleave', function(){
            $.fn.galleryView.restart();
        });

        //Shorten text to what is suitable
        $(this).find("a.item span.paragraph").each(function(){
          if($(this).height() > 40){
            if($(this).length > 200){
                $(this).text($(this).text().substring(0, 200));
            }
            while($(this).height() > 40){
              $(this).text($(this).text().substring(0, $(this).text().length - 10) + '...');
            }
          }
        });
      });
  }

  $.fn.galleryView.setInterval = function(){
      $.fn.galleryView.defaults.intervalHolder = setInterval(function(){
          $.fn.galleryView.goNext();
      }, $.fn.galleryView.defaults.slideInterval);
  }

  $.fn.galleryView.goPrevious = function(){
      $("#topOpinion .itemWrapper").scrollTo({top:'+=0px', left:'-=' + $.fn.galleryView.params.itemWidth + 'px'}, $.fn.galleryView.defaults.duration);
  }

  $.fn.galleryView.goNext = function(){
      $.fn.galleryView.defaults.position = $("#topOpinion .itemWrapper").scrollLeft();
      $("#topOpinion .itemWrapper").scrollTo({top:'+=0px', left:'+=' + $.fn.galleryView.params.itemWidth + 'px'}, $.fn.galleryView.defaults.duration, {
        onAfter:function(){
            if($.fn.galleryView.defaults.position == $("#topOpinion .itemWrapper").scrollLeft()){
                clearInterval($.fn.galleryView.defaults.intervalHolder);
                clearTimeout($.fn.galleryView.defaults.timeoutHolder);
                $("#topOpinion .itemWrapper").scrollTo({top:'+=0px', left:'0px'}, $.fn.galleryView.defaults.duration*3);
                $.fn.galleryView.restart();
            }
        }
      });
      
  }

    $.fn.galleryView.restart = function(){
        //Restart it after X seconds of inactivity
        $.fn.galleryView.defaults.timeoutHolder = setTimeout(function(){
            $.fn.galleryView.setInterval();
        }, $.fn.galleryView.defaults.waitTimeOut);
    }

  $.fn.galleryView.defaults = {
    duration:             1500,   //Integer, how many milliseconds for animation for each slide
    slideInterval:        8500,   //Integer, how many milliseconds between each slide
    waitTimeOut:          6500,   //Integer, how many milliseconds from click to restart of slideshow
    timeoutHolder:        0,      //Just a holder of the timeout, needed so that we can control whether the slideshow should re-start or not
    position:             0,      //Just a value of current position, updated in goNext-function
    intervalHolder:       0       //Just a holder of the interval, needed so that we can cancel and re-start it when wanted
  }

  $.fn.galleryView.params = {
      itemWidth:            562,   //Integer, how wide are one item, or more accurate, how much do we have to scroll to see the next item correctly
      moves:                0   //Integer, how many times has the the items been scrolled?
  }

})(jQuery);
