/**
 * jQuery plugin for www.varldenidag.se (sitespecific) (jquery.vidComment.js)
 *
 * Handles the comments for the site
 *
 * CSS and XHTML required
 * Other plugin required: .scrollTo()
 *
 * @version 0.1
 * @Created: 2010-11-16
 * @Author: Oscar Engström, www.engstream.se
 *
 */
(function($) {
  $.fn.vidComment = function(options) {
    var opts = $.extend({}, $.fn.vidComment.defaults, options);

    /* Call Init slide */
    //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.vidComment.initSideSlide($tmp);
        });
    } else {
        //No, that's good, just continue
        return $.fn.vidComment.initSideSlide(this);
    }
  };

  /**
   * Init the slideshow
   */
  $.fn.vidComment.initSideSlide = function(){
    //How many items are there?
    var noOfItems = $("#latestComments .itemHolder .item").length;
    //How wide are they?
    $.fn.vidComment.params.itemWidth = $("#latestComments .itemHolder .item:first").width();
    //How wide should the holder be?
    //At end, subtract width for a/one divider, since the loop never ends with a divider
    var holderWidth = $.fn.vidComment.params.itemWidth * noOfItems;
    $.fn.vidComment.params.endLeftScroll = $.fn.vidComment.params.itemWidth * (noOfItems-2);
    //Set holders width
    $("#latestComments .itemHolder").css({ 'width' : holderWidth + 'px' });

    $.fn.vidComment.setInterval();

    //Bind the arrows at the sides to action
    //Show previous item
    $("#latestComments .previous").click(function(ev){
        ev.preventDefault();
        $.fn.vidComment.goPrevious();

        //Stop slideshow, temporary
        clearInterval($.fn.vidComment.defaults.intervalHolder);
        clearTimeout($.fn.vidComment.defaults.timeoutHolder);

        $.fn.vidComment.restart();
    });

    //Show next item
    $("#latestComments .next").click(function(ev){
        ev.preventDefault();
        $.fn.vidComment.goNext();

        //Stop slideshow, temporary
        clearInterval($.fn.vidComment.defaults.intervalHolder);
        clearTimeout($.fn.vidComment.defaults.timeoutHolder);

        $.fn.vidComment.restart();
    });

    //When hovering a link/item, stop slideshow
    $("#latestComments a.item").live('mouseenter', function(){
        //Stop slideshow, temporary
        clearInterval($.fn.vidComment.defaults.intervalHolder);
        clearTimeout($.fn.vidComment.defaults.timeoutHolder);
    });

    //On mouseleave, restart slideshow
    $("#latestComments a.item").live('mouseleave', function(){
        $.fn.vidComment.restart();
    });
  }

  /**
   * For slideshow
   * Starts the interval for displaying next comment
   */
  $.fn.vidComment.setInterval = function(){
      $.fn.vidComment.defaults.intervalHolder = setInterval(function(){
          $.fn.vidComment.goNext();
      }, $.fn.vidComment.defaults.slideInterval);
  }

  /**
   * For slideshow
   * Go back and display previous comment
   */
  $.fn.vidComment.goPrevious = function(){
      $("#latestComments .itemWrapper").scrollTo({top:'+=0px', left:'-=' + $.fn.vidComment.params.itemWidth + 'px'}, $.fn.vidComment.defaults.duration);
  }

  /**
   * For slideshow
   * Go forward and show next comment
   */
  $.fn.vidComment.goNext = function(){
      $.fn.vidComment.defaults.position = $("#latestComments .itemWrapper").scrollLeft();

        if($("#latestComments .itemWrapper").scrollLeft() == $.fn.vidComment.params.endLeftScroll){
            clearInterval($.fn.vidComment.defaults.intervalHolder);
            clearTimeout($.fn.vidComment.defaults.timeoutHolder);

            $("#latestComments .itemWrapper").scrollTo({top:'+=0px', left:'0px'}, $.fn.vidComment.defaults.duration*3);
            $.fn.vidComment.restart();
        } else {
            $("#latestComments .itemWrapper").scrollTo({top:'+=0px', left:'+=' + ($.fn.vidComment.params.itemWidth) + 'px'}, $.fn.vidComment.defaults.duration);
        }

  }

  /**
   * For slideshow
   * Restart the slideshow (not from beginning but from where it is now, the slideshow stops when someone is hovering the slideshow)
   */
  $.fn.vidComment.restart = function(){
    //Restart it after X seconds of inactivity
    $.fn.vidComment.defaults.timeoutHolder = setTimeout(function(){
        $.fn.vidComment.setInterval();
    }, $.fn.vidComment.defaults.waitTimeOut);
  }

  /**
   * Default values
   */
  $.fn.vidComment.defaults = {
    duration:             750,   //Integer (for slideshow), how many milliseconds for animation for each slide
    slideInterval:        8500,   //Integer (for slideshow), how many milliseconds between each slide
    waitTimeOut:          6500,   //Integer (for slideshow), how many milliseconds from click to restart of slideshow
    timeoutHolder:        0,      //(for slideshow) Just a holder of the timeout, needed so that we can control whether the slideshow should re-start or not
    position:             0,      //(for slideshow) Just a value of current position, updated in goNext-function
    intervalHolder:       0       //(for slideshow) Just a holder of the interval, needed so that we can cancel and re-start it when wanted
  }

  $.fn.vidComment.params = {
      itemWidth:            300,   //Integer (for slideshow), how wide are one item, or more accurate, how much do we have to scroll to see the next item correctly
      endLeftScroll:        false  //(for slideshow)
  }

})(jQuery);
