/*

Modified version of Ryan Scherf's jSwipe plugin to detect touch support

*/

(function($) {
	$.fn.swipe = function(options) {
	
		// set default options
		var defaults = {
			swipeX:true,
			swipeY:false,
			enableMouse:false,
			threshold: {
				x: 10,
				y: 50
			},
			swipeLeft: function() { alert('swiped left'); },
			swipeRight: function(){ alert('swiped right'); }
		};
		var options = $.extend(defaults, options);
		
		
		if (!this) return false;
		
		// loop through instances
		return $(this).each(function(){
		
			var me = $(this);
			var touchable = ('ontouchstart' in document.documentElement) ? true : false;
			var single = false;
			var mousing = false;
			
			// Private variables for each element
			var originalCoord = { x: 0, y: 0 }
			var finalCoord = { x: 0, y: 0 }
			
			// touch functions
			
			// Screen touched, store the original coordinate
			function touchStart(event) {
				//alert(event.targetTouches.length);
				if(event.targetTouches.length == 1)
				{
					single = true;
					originalCoord.x = event.targetTouches[0].pageX
					originalCoord.y = event.targetTouches[0].pageY
				}
			}
			
			// Store coordinates as finger is swiping
			function touchMove(event) {
			    if(single) {
			    	event.preventDefault();
					finalCoord.x = event.targetTouches[0].pageX // Updated X,Y coordinates
					finalCoord.y = event.targetTouches[0].pageY
				}
			}
			
			// Done Swiping
			// Swipe should only be on X axis, ignore if swipe on Y axis
			// Calculate if the swipe was left or right
			function touchEnd(event) {
				changeX = originalCoord.x - finalCoord.x
				var changeY = originalCoord.y - finalCoord.y
				if(changeY < defaults.threshold.y && changeY > (defaults.threshold.y*-1)) {
					changeX = originalCoord.x - finalCoord.x
					
					if(changeX > defaults.threshold.x) {
						defaults.swipeLeft()
					}
					if(changeX < (defaults.threshold.x*-1)) {
						defaults.swipeRight()
					}
				}
				single = false;
			}
			
			// Swipe was canceled
			function touchCancel(event) { 
				//console.log('Canceling swipe gesture...')
			}
			
			// mouse functions
			
			function mouseStart(event)
			{
				//alert('click');
				mousing = true;
				originalCoord.x = event.pageX;
				originalCoord.y = event.pageY;
				//alert(originalCoord.x);
			}
			
			function mouseMove(event)
			{
				if(mousing) event.preventDefault();
			}
			
			function mouseStop(event)
			{
				finalCoord.x = event.pageX;
				finalCoord.y = event.pageY;
				
				changeX = originalCoord.x - finalCoord.x
				var changeY = originalCoord.y - finalCoord.y
				if(changeY < defaults.threshold.y && changeY > (defaults.threshold.y*-1)) {
					changeX = originalCoord.x - finalCoord.x
					
					if(changeX > defaults.threshold.x) {
						defaults.swipeLeft()
					}
					if(changeX < (defaults.threshold.x*-1)) {
						defaults.swipeRight()
					}
				}
				mousing = false;
			}
			
			if ('ontouchstart' in document.documentElement) {
				
				// Add gestures to all swipable areas
				this.addEventListener("touchstart", touchStart, false);
				this.addEventListener("touchmove", touchMove, false);
				this.addEventListener("touchend", touchEnd, false);
				this.addEventListener("touchcancel", touchCancel, false);
				
			} else {
				if(defaults.enableMouse) {
					$(this).children('*').mousemove(function(e)
					{
						e.preventDefault();
					});
					$(this).mousedown(function(e) { mouseStart(e); });
					$(this).mousemove(function(e) { mouseMove(e); });
					$(this).mouseup(function(e) { mouseStop(e); });
				}
			}
			
		});
		
		
	};
})(jQuery);
