if (typeof ABLE == "undefined" || !ABLE) {
    var ABLE = {};
}

ABLE.currentTouch = {};

ABLE.init = function(params){
  this.wrapper = document.getElementById(params.wrapper);
  this.itemID = this.wrapper.getElementsByTagName("li");
  this.gallery = document.getElementById('thumbnails');
  this.galleryItems = this.gallery.getElementsByTagName("li");
  this.itemWidth = parseInt($('#carousel img').css('width')); // this is mobile only

  $.extend(jQuery.browser,{SafariMobile : navigator.userAgent.toLowerCase().match(/iP(hone|ad)/i)});

  this.ablecarousel_moveCallback = function(carousel) {
    if($('#next').length){
      if(carousel.first == 1){
        $('#previous').css('visibility','hidden');
      } else {
        $('#previous').css('visibility','visible');
      }
    }
    $('body').removeClass('white');

    var galleryItemCount = ABLE.galleryItems.length;
    for ( var i=galleryItemCount-1; i>=0; --i ){
      ABLE.galleryItems[i].className = '';
    }
    var currentGalleryNode = ABLE.galleryItems[carousel.first-1];
    currentGalleryNode.className = 'gallerySelected';
    return false;
  }
  
  this.ablecarousel_initCallback = function(carousel) {
    $('#thumbnails a').bind('click', function() {
      carousel.scroll($.jcarousel.intval($(this).parent().index()+1));
      return false;
    });
    $('#next').bind('click', function() {
      carousel.next();
      return false;
    });
    $('#previous').bind('click', function() {
      carousel.prev();
      return false;
    });
    $('a.info').click(function(){
      $('.infobox').toggleClass('hide');
      return false;
    });

    $(document).keydown(function(e){
      // left
      if (e.keyCode == 37) { 
        carousel.prev();
        return false;
      }
      // right
      if (e.keyCode == 39) { 
        carousel.next();
        return false;
      }
    });
  }

  /* more touching! */
  
  this.currentPos = 0;
  this.movePos = 0;
  this.tempPos = 0;
  this.direction = 0;
  this.yMovement = false;
  this.xMovement = false;
  this.threshold = params.threshold;
  
  this.bulletColor = function(){
    var that = this;
    var page = Math.abs(that.currentPos/that.itemWidth)+1;
    var galleryItemCount = that.galleryItems.length;
    for ( var i=galleryItemCount-1; i>=0; --i ){
      that.galleryItems[i].className = '';
    }
    that.galleryItems[page-1].className = 'gallerySelected';
  }
  
  this.move = function(speed){
    var that = this;
    var itemLength = -1 * that.galleryItems.length * that.itemWidth;
    if(speed < 0){
      that.currentPos = that.currentPos + that.itemWidth;
      if(that.currentPos > 0){
        that.currentPos = 0;
      }
    }
    else {
      that.currentPos = that.currentPos - that.itemWidth;
      if(that.currentPos <= itemLength){
        that.currentPos = itemLength + that.itemWidth;
      }
    }
    $('#'+params.wrapper).css('-webkitTransitionDuration','0.4s');
    $('#'+params.wrapper).css('-webkitTransform', 'translate3d('+that.currentPos+'px,0,0)');
    that.bulletColor();
  };
  
  this.bindTouches = function(){
    var that = this;
    that.bulletColor();
    $('#thumbnails a').bind('click', function() {
      $('#'+params.wrapper).css('-webkitTransitionDuration','0.4s');
      $('#'+params.wrapper).css('webkitTransform', 'translate3d('+(that.itemWidth*$(this).parent().index())*-1+'px,0,0)');
      that.currentPos = (that.itemWidth*$(this).parent().index())*-1;
      that.bulletColor();
      return false;
    });
    
    that.wrapper.addEventListener('touchstart', function(e) {
      e.preventDefault();
      var touch = e.touches[0];
      ABLE.currentTouch.startX = touch.screenX;
      ABLE.currentTouch.startY = touch.screenY;
    },false);
    that.wrapper.addEventListener('touchmove', function(e) {
      var touch = e.touches[0];
      var begin = ABLE.currentTouch.startX;
      var beginY = ABLE.currentTouch.startY;
      that.movePos = that.currentPos + touch.screenX - begin;
          if(that.tempPos > that.movePos){
            that.direction = 1;
          }
          else {
            that.direction = -1;
          }
          $('#'+params.wrapper).css('-webkitTransitionDuration','0s');
          $('#'+params.wrapper).css('-webkitTransform','translate3d('+that.movePos+'px,0,0)');
        that.tempPos = that.movePos;
      },false);
    that.wrapper.addEventListener('touchend', function(e) {
      e.preventDefault();
      var touch = e.changedTouches[0];
      var leftright = Math.abs(touch.screenX - ABLE.currentTouch.startX);
        ABLE.currentTouch.endX = touch.screenX;
        ABLE.currentTouch.endY = touch.screenY;
        if(leftright >= that.threshold){
          that.move(that.direction);
        }
        else {
          $('#'+params.wrapper).css('-webkitTransitionDuration','0.4s');
          $('#'+params.wrapper).css('-webkitTransform','translate3d('+that.currentPos+'px,0,0)');
        }
    }, false);
  };
  $(function(){
    if($.browser.SafariMobile){
      $('#carousel').css('width',parseInt(ABLE.itemWidth) * ABLE.galleryItems.length + 'px');
      ABLE.bindTouches();
    } else {
       $('#carousel').jcarousel({
         visible: 1,
         scroll: 1,
         initCallback: ABLE.ablecarousel_initCallback,
         itemFirstInCallback: ABLE.ablecarousel_moveCallback,
         wrap: 'last',
         background: '#FFF',
         buttonNextHTML: null,
         buttonPrevHTML: null,
         itemFallbackDimension: 578
       });
      }
  });
}
