/*! NOTE!!! I hacked this up pretty good, it is not swapplable with whatever version
 * is on their site. Has been customized for our use! :) Thanks for reading this!!
 *
 * Updated: 2011/12/26 jd - Gutted pretty much all of the code we aren't using. Refactored
 * a lot of the code related to determining what 'page' the user is on for the current set
 * of entries.
 */

/*!
 * Tiny Carousel 1.9
 * http://www.baijs.nl/tinycarousel
 *
 * Copyright 2010, Maarten Baijs
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/gpl-2.0.php
 *
 * Date: 01 / 06 / 2011
 * Depends on library: jQuery
 */

(function($){
  $.tiny = $.tiny || { };

  $.tiny.carousel = {
    options: {
      controls: true, // show left and right navigation buttons.
      duration: 1000, // how fast must the animation move in ms?
      callback: null // function that executes after every move.
    }
  };

  $.fn.tinycarousel = function(options) {
    var options = $.extend({}, $.tiny.carousel.options, options);
    this.each(function(){ $(this).data('tcl', new Carousel($(this), options)); });
    return this;
  };
  $.fn.tinycarousel_update = function(){ $(this).data('tcl').update(); };

  function Carousel(root, options){
    var oSelf = this;
    var oViewport = $('.viewport:first', root);
    var oContent = $('.overview:first', root);
    var oPages = oContent.children('li[data-shown*="true"]');
    var oBtnNext = $('.next:first', root);
    var oBtnPrev = $('.prev:first', root);
    var iPageSize, iSteps, iCurrent, oTimer, bPause, bForward = true

    function initialize(){
      // How wide one entry is (in pixels)
      iPageSize = $(oPages[0]).outerWidth(true);
      var maxPagesShown = Math.floor(oViewport.outerWidth() / iPageSize);
      // How many entries are left over
      var iLeftover = oPages.length - maxPagesShown;
      // How many steps to view the remaining entries
      iSteps = Math.max(1, iLeftover+1); // one step for each remaining, plus the current step
      // Current position (step) of left-most item
      iCurrent = 0;
      // Set overview width so our set will fit inside of it
      oContent.css('width', (iPageSize * oPages.length));
      // oSelf.move(1);
      setEvents();
      setButtons();
      setTimer();
      return oSelf;
    };
    function setEvents(){
      if(options.controls && oBtnPrev.length > 0 && oBtnNext.length > 0){
        oBtnPrev.click(function(){
          if ($(this).hasClass('ui-state-disabled')) {
            return false;
          }
          oSelf.move(-1);
          return false;
        });
        oBtnNext.click(function(){
          if ($(this).hasClass('ui-state-disabled')) {
            return false;
          }
          oSelf.move( 1);
          return false;
        });
      }
      if(options.interval){ root.hover(oSelf.stop,oSelf.start); }
    };
    function setButtons(){
      if(options.controls){
        if (iCurrent < 0) iCurrent = 0;
        // console.log("iCurrent: "+iCurrent);
        // console.log("iSteps: "+iSteps);
        oBtnPrev.toggleClass('ui-state-disabled', (iCurrent < 1));
        oBtnNext.toggleClass('ui-state-disabled', (iCurrent +1 >= iSteps));
      }
    };
    function setTimer(){
      if(options.interval && !bPause){
        clearTimeout(oTimer);
        oTimer = setTimeout(function(){
          iCurrent = iCurrent +1 == iSteps ? -1 : iCurrent;
          bForward = iCurrent +1 == iSteps ? false : iCurrent == 0 ? true : bForward;
          oSelf.move(bForward ? 1 : -1);
        }, options.intervaltime);
      }
    };
    this.stop = function(){ clearTimeout(oTimer); bPause = true; };
    this.start = function(){ bPause = false; setTimer(); };
    this.update = function() {
      oContent.css('left', 0);
      oPages = oContent.children('li[data-shown*="true"]');

      var maxPagesShown = Math.floor(oViewport.outerWidth() / iPageSize);
      var iLeftover = oPages.length - maxPagesShown;
      iSteps = Math.max(1, iLeftover+1);
      iCurrent = 0;

      oContent.css('width', (iPageSize * oPages.length));
      setButtons();
    }
    this.move = function(iDirection){
      iCurrent = iCurrent += iDirection;
      if(iCurrent > -1 && iCurrent < iSteps){
        var oPosition = {};
        oPosition['left'] = -(iCurrent * iPageSize);
        oContent.animate(oPosition,{
          queue: false,
          duration: options.duration,
          complete: function(){
            if(typeof options.callback == 'function') {
              options.callback.call(this, oPages[iCurrent], iCurrent);
            }
          }
        });
        setButtons();
        setTimer();
      }
    };
    return initialize();
  };
})(jQuery);
