var MIN_HORIZONTAL = 100; // The minimum number of pixels a gesture must traverse to be considered a swipe.
var MAX_VERTICAL = 100; // The maximum number of vertical pixels allowed for a gesture to be considered a swipe.

// The Gesture tracking class.
function Gesture() {
  this.isSingle = false;
  this.hasMoved = false;
  this.startX = 0;
  this.startY = 0;
  this.currentX = 0;
  this.currentY = 0;
  this.element = null;

  this.touchStart = function(e) {
    var numTouches = e.touches;
    if(numTouches.length === 1) {
      this.isSingle = true;
      this.startX = numTouches[0].pageX;
      this.startY = numTouches[0].pageY;
      this.currentX = this.startX;
      this.currentY = this.startY;
      this.element = e.target;
    }
  };

  this.touchMove = function(e) {
    var numTouches = e.touches;
    if(this.isSingle && numTouches.length === 1) {
      e.preventDefault();
      this.hasMoved = true;
      this.currentX = numTouches[0].pageX;
      this.currentY = numTouches[0].pageY;
    } else {
      this.isSingle = false;
      this.hasMoved = false;
    }
  };

  this.touchEnd = function(e, c) {
    if(this.isSingle) {
      e.preventDefault();
      c(this);
    }
  };
}

// Complete the animation to go to the next frame once the gesture is ended/the frame is "dropped."
function dropCarousel(g) {
  // Check if the swipe is valid.
  if((Math.abs(g.startY - g.currentY) < MAX_VERTICAL) && (Math.abs(g.startX - g.currentX) > MIN_HORIZONTAL)) {
    if((g.startX - g.currentX) > 0) {
      // We're scrolling right/forwards.
      $(".next").trigger("click");
    } else {
      // We're scrolling left/backwards.    
      $(".prev").trigger("click");
    }
  }
}
