/**
 * jQuery plugin for www.varldenidag.se (sitespecific) (jquery.vidWebshop.js)
 *
 * Handles the webshop for the site
 *
 * CSS and XHTML required
 * Other plugin required: .scrollTo()
 *
 * @version 0.1
 * @Created: 2010-11-15
 * @Author: Oscar Engström, www.engstream.se
 *
 */
(function($) {
  $.fn.vidWebshop = function(options) {
      var opts = $.extend({}, $.fn.vidWebshop.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.vidWebshop.initPostit($tmp);
          });
      } else {
          //No, that's good, just continue
          return $.fn.vidWebshop.initPostit(this);
      }
  };

  $.fn.vidWebshop.initPostit = function(){
      //How many items are there?
      var noOfItems = $("#topRight .itemHolder .item").length;
      //How wide are they?
      $.fn.vidWebshop.params.itemWidth = $("#topRight .itemHolder:first").width();

      //How wide should the holder be?
      var holderWidth = $.fn.vidWebshop.params.itemWidth * noOfItems;
      //Set holders width
      $("#topRight .itemHolder").css({ 'width' : holderWidth + 'px' });

      $.fn.vidWebshop.setInterval();

      //Bind the arrows at the sides to action
      //Show previous item
      $("#topRight .previous").click(function(ev){
          ev.preventDefault();
          $.fn.vidWebshop.goPrevious();

          //Stop slideshow, temporary
          clearInterval($.fn.vidWebshop.defaults.intervalHolder);
          clearTimeout($.fn.vidWebshop.defaults.timeoutHolder);

          $.fn.vidWebshop.restart();
      });

      //Show next item
      $("#topRight .next").click(function(ev){
          ev.preventDefault();
          $.fn.vidWebshop.goNext();

          //Stop slideshow, temporary
          clearInterval($.fn.vidWebshop.defaults.intervalHolder);
          clearTimeout($.fn.vidWebshop.defaults.timeoutHolder);

          $.fn.vidWebshop.restart();
      });

      //When hovering a link/item, stop slideshow
      $("#topRight a.item").live('mouseenter', function(){
          //Stop slideshow, temporary
          clearInterval($.fn.vidWebshop.defaults.intervalHolder);
          clearTimeout($.fn.vidWebshop.defaults.timeoutHolder);
      });

      //On mouseleave, restart slideshow
      $("#topRight a.item").live('mouseleave', function(){
          $.fn.vidWebshop.restart();
      });
  }

  $.fn.vidWebshop.setInterval = function(){
      $.fn.vidWebshop.defaults.intervalHolder = setInterval(function(){
          $.fn.vidWebshop.goNext();
      }, $.fn.vidWebshop.defaults.slideInterval);
  }

  $.fn.vidWebshop.goPrevious = function(){
      $("#topRight .itemWrapper").scrollTo({top:'+=0px', left:'-=' + $.fn.vidWebshop.params.itemWidth + 'px'}, $.fn.vidWebshop.defaults.duration);
  }

  $.fn.vidWebshop.goNext = function(){
      $.fn.vidWebshop.defaults.position = $("#topRight .itemWrapper").scrollLeft();
      $("#topRight .itemWrapper").scrollTo({top:'+=0px', left:'+=' + $.fn.vidWebshop.params.itemWidth + 'px'}, $.fn.vidWebshop.defaults.duration, {
        onAfter:function(){
            if($.fn.vidWebshop.defaults.position == $("#topRight .itemWrapper").scrollLeft()){
                clearInterval($.fn.vidWebshop.defaults.intervalHolder);
                clearTimeout($.fn.vidWebshop.defaults.timeoutHolder);
                $("#topRight .itemWrapper").scrollTo({top:'+=0px', left:'0px'}, $.fn.vidWebshop.defaults.duration*3);
                $.fn.vidWebshop.restart();
            }
        }
      });
      
  }

  $.fn.vidWebshop.restart = function(){
    //Restart it after X seconds of inactivity
    $.fn.vidWebshop.defaults.timeoutHolder = setTimeout(function(){
        $.fn.vidWebshop.setInterval();
    }, $.fn.vidWebshop.defaults.waitTimeOut);
  }

  $.fn.vidWebshop.defaults = {
    duration:             750,   //Integer, how many milliseconds for animation for each slide
    slideInterval:        13500,   //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.vidWebshop.params = {
      itemWidth:            170   //Integer, how wide are one item, or more accurate, how much do we have to scroll to see the next item correctly
  }

})(jQuery);
