/**
 * jQuery plugin for www.varldenidag.se (sitespecific) (jquery.vidOpinion.js)
 *
 * Sets up opinion-box in the top
 *
 * CSS and XHTML required
 * Other plugin required: .scrollTo()
 *
 * @version 0.1
 * @Created: 2010-11-09
 * @Author: Oscar Engström, www.engstream.se
 *
 */
(function($) {
  $.fn.vidOpinion = function(options) {
      var opts = $.extend({}, $.fn.vidOpinion.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.vidOpinion.initTopOpinion($tmp);
          });
      } else {
          //No, that's good, just continue
          return $.fn.vidOpinion.initTopOpinion(this);
      }
  };

  $.fn.vidOpinion.initTopOpinion = function(){
    //Fix for resetting scroller on reload (otherwise it could start in the middle of a scroll)
    $(window).load(function(){
      $("#topOpinion .itemWrapper").scrollLeft(0);
    });
    
      //How many items are there?
      var noOfItems = $("#topOpinion .itemHolder .item").length;
      //How wide are they?
      $.fn.vidOpinion.params.itemWidth = $("#topOpinion .itemWrapper:first").width();

      //How wide should the holder be?
      var holderWidth = $.fn.vidOpinion.params.itemWidth * noOfItems;
      //Set holders width
      $("#itemHolder").css({ 'width' : holderWidth + 'px' });

      $.fn.vidOpinion.setInterval();

      //Bind the arrows at the sides to action
      //Show previous item
      $("#topOpinionPrevious").click(function(ev){
          ev.preventDefault();
          $.fn.vidOpinion.goPrevious();

          //Stop slideshow, temporary
          clearInterval($.fn.vidOpinion.defaults.intervalHolder);
          clearTimeout($.fn.vidOpinion.defaults.timeoutHolder);

          $.fn.vidOpinion.restart();
      });

      //Show next item
      $("#topOpinionNext").click(function(ev){
          ev.preventDefault();
          $.fn.vidOpinion.goNext();

          //Stop slideshow, temporary
          clearInterval($.fn.vidOpinion.defaults.intervalHolder);
          clearTimeout($.fn.vidOpinion.defaults.timeoutHolder);

          $.fn.vidOpinion.restart();
      });

      //When hovering a link/item, stop slideshow
      $("#topOpinion a.item").live('mouseenter', function(){
          //Stop slideshow, temporary
          clearInterval($.fn.vidOpinion.defaults.intervalHolder);
          clearTimeout($.fn.vidOpinion.defaults.timeoutHolder);
      });

      //On mouseleave, restart slideshow
      $("#topOpinion a.item").live('mouseleave', function(){
          $.fn.vidOpinion.restart();
      });

      //Shorten text to what is suitable
      $("#topOpinion a.item span.paragraph").each(function(){
        //var topOpinionShortTimeout = setTimeout(2000);
        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.vidOpinion.setInterval = function(){
      $.fn.vidOpinion.defaults.intervalHolder = setInterval(function(){
          $.fn.vidOpinion.goNext();
      }, $.fn.vidOpinion.defaults.slideInterval);
  }

  $.fn.vidOpinion.goPrevious = function(){
      $("#topOpinion .itemWrapper").scrollTo({top:'+=0px', left:'-=' + $.fn.vidOpinion.params.itemWidth + 'px'}, $.fn.vidOpinion.defaults.duration);
  }

  $.fn.vidOpinion.goNext = function(){
      $.fn.vidOpinion.defaults.position = $("#topOpinion .itemWrapper").scrollLeft();
      $("#topOpinion .itemWrapper").scrollTo({top:'+=0px', left:'+=' + $.fn.vidOpinion.params.itemWidth + 'px'}, $.fn.vidOpinion.defaults.duration, {
        onAfter:function(){
            if($.fn.vidOpinion.defaults.position == $("#topOpinion .itemWrapper").scrollLeft()){
                clearInterval($.fn.vidOpinion.defaults.intervalHolder);
                clearTimeout($.fn.vidOpinion.defaults.timeoutHolder);
                $("#topOpinion .itemWrapper").scrollTo({top:'+=0px', left:'0px'}, $.fn.vidOpinion.defaults.duration*3);
                $.fn.vidOpinion.restart();
            }
        }
      });
      
  }

    $.fn.vidOpinion.restart = function(){
        //Restart it after X seconds of inactivity
        $.fn.vidOpinion.defaults.timeoutHolder = setTimeout(function(){
            $.fn.vidOpinion.setInterval();
        }, $.fn.vidOpinion.defaults.waitTimeOut);
    }

  $.fn.vidOpinion.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.vidOpinion.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);
