/*
 * jMultiswipe - jQuery Plugin
 * http://plugins.jquery.com/project/multiswipe
 * http://www.prettydang.com/demos/multiswipe/
 *
 * Copyright (c) 2010 Ryan Laughlin (www.prettydang.com)
 * based off of jSwipe (c) 2009 Ryan Scherf (www.ryanscherf.com)
 * Licensed under the MIT license
 *
 * $Date: 2010-02-06 (Sat, 06 Feb 2010) $
 * $version: 0.1
 * 
 * This jQuery plugin will only run on devices running Mobile Safari
 * on iPhone or iPod Touch devices running iPhone OS 2.0 or later. 
 * http://developer.apple.com/iphone/library/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW5
 */
(function($) {
  $.fn.multiswipe = function(options) {
    
    // Default thresholds & swipe functions
    var defaults = {
      threshold: 30,
      fingers: 1,
      swipeLeft: function() {},
      swipeRight: function() {},
      swipeUp: function() {},
      swipeDown: function() {},
      moveLeft: function() {},
      moveRight: function() {},
      moveUp: function() {},
      moveDown: function() {}
    };
    
    var options = $.extend(defaults, options);
    
    if (!this) return false;
    
    return this.each(function() {
      var me = $(this);
      
      if (! me.data('multiswipe')) {
        me.data('multiswipe', true);
        
        // Private variables for each element
        var originalCoord = { x: 0, y: 0 },
            finalCoord = { x: 0, y: 0 },
            bool_touchMove = false;
        
        function touchStart(event) {
          if(event.targetTouches.length == defaults.fingers) {
            originalCoord.x = event.targetTouches[defaults.fingers-1].screenX;
            originalCoord.y = event.targetTouches[defaults.fingers-1].screenY;
          }
        }
        
        // Store coordinates as finger is swiping
        function touchMove(event) {
          event.preventDefault();
          
          if(event.targetTouches.length == defaults.fingers) {
            bool_touchMove = true;
            
            finalCoord.x = event.targetTouches[defaults.fingers-1].screenX;
            finalCoord.y = event.targetTouches[defaults.fingers-1].screenY;
            
            var changeY = originalCoord.y - finalCoord.y,
                changeX = originalCoord.x - finalCoord.x;
                
            if(changeY > defaults.threshold) {
              defaults.moveUp(changeY);
            }
            if(changeY < (defaults.threshold*-1)) {
              defaults.moveDown(changeY);
            }
            if(changeX > defaults.threshold) {
              defaults.moveLeft(changeX);
            }
            if(changeX < (defaults.threshold*-1)) {
              defaults.moveRight(changeX);
            }
          }
        }
        
        // Done Swiping
        // Trigger all relevant multiswipe events
        function touchEnd(event) {
          if (bool_touchMove) {
            var changeY = originalCoord.y - finalCoord.y,
                changeX = originalCoord.x - finalCoord.x;
            
            if(changeY > defaults.threshold) {
              defaults.swipeUp();
            }
            if(changeY < (defaults.threshold*-1)) {
              defaults.swipeDown();
            }
            if(changeX > defaults.threshold) {
              defaults.swipeLeft();
            }
            if(changeX < (defaults.threshold*-1)) {
              defaults.swipeRight();
            }
          }
          
          originalCoord = { x: 0, y: 0 };
          finalCoord = { x: 0, y: 0 };
          bool_touchMove = false;
        }
        
        // Add gestures to all swipable areas
        this.addEventListener("touchstart", touchStart, false);
        this.addEventListener("touchmove", touchMove, false);
        this.addEventListener("touchend", touchEnd, false);
      }
    });
  };
})(jQuery);
