/**
 * jQuery Plugin to obtain touch gestures from iPhone, iPod Touch and iPad,
 * Android-based tablet with webkit and Windows-based with IE
 * Common usage: wipe images (left and right to show the previous or next image)
 *
 * Inspired from jquery plugin (http://plugins.jquery.com/project/Touchwipe-iPhone-iPad-wipe-gesture)
 * and from the book Programming For the Mobile Web
 * @author Tinh Truong, Evolution Solutions Co., Ltd (www.evolus.vn)
 * @version 1.0 (01st September 2010)
 */
(function($) {
    $.fn.touchswipe = function(settings) {
        var config = {
            vertical: false,
	        gestureMinDelta: 20,
	        axisThreshold: 200,
	        wipeLeft: function() { alert("left"); },
	        wipeRight: function() { alert("right"); },
	        wipeUp: function() { alert("up"); },
	        wipeDown: function() { alert("down"); },
	        preventDefaultEvents: true
        };

        if (settings) $.extend(config, settings);

        this.each(function() {
            var startX = 0;
            var startY = 0;
            var isMoving = false;
            var $this = $(this);

            function cancelGesture() {
                if ($.browser.webkit) {
                    this.removeEventListener('touchmove', onMouseMove);
                } else {
                    $this.unbind("mousemove", onMouseMove);
                }
                startX = 0;
                startY = 0;
                isMoving = false;
            }

            function onMouseMove(event) {
                if (config.preventDefaultEvents) {
                    event.preventDefault();
                }
                var delta = 0;
                // Get coordinates using iPhone/iPad or standard technique
                var currentX = (event.touches) ? event.touches[0].pageX : event.pageX;
                var currentY = (event.touches) ? event.touches[0].pageY : event.pageY;

                // Check if the user is still in line with the axis
                if (isMoving) {
                    if (config.vertical) {
                        delta = Math.abs(currentX - startX);
                    } else {
                        delta = Math.abs(currentY - startY);
                    }
                    if (delta > config.axisThreshold) {
                        // Cancel the gesture, the user is moving in the other axis
                        //isMoving = false;
                        cancelGesture();
                    }
                }

                // Check if we can consider it a swipe
                if (isMoving) {
                    if (config.vertical == false) {
                        delta = Math.abs(currentX - startX);
                        if (currentX > startX) {
                            direction = 0;
                        } else {
                            direction = 1;
                        }
                    } else {
                        delta = Math.abs(currentY - startY);
                        if (currentY > startY) {
                            direction = 2;
                        } else {
                            direction = 3;
                        }
                    }
                    if (delta >= config.gestureMinDelta) {
                        // Gesture detected!
                        var handler = null;
                        switch(direction) {
                            case 0: handler = config.wipeRight; break;
                            case 1: handler = config.wipeLeft; break;
                            case 2: handler = config.wipeDown; break;
                            case 3: handler = config.wipeUp; break;
                        }
                        if (handler != null) {
                            // Call to the callback with the optional delta
                            handler(delta);
                        }
                        cancelGesture();
                    }
                }
        	}

            function onMouseDown(event)
            {
                isMoving = true;
                startX = (event.touches) ? event.touches[0].pageX : event.pageX;
                startY = (event.touches) ? event.touches[0].pageY : event.pageY;
                // Only for iPhone/iPad
                if (event.touches && event.touches.length != 1) { // Cancel gesture on multiple touch
                    isMoving = false;
                } else {
                    if ($.browser.webkit) {
                        this.addEventListener("touchmove", onMouseMove, false);
                    } else {
                        $this.bind("mousemove", onMouseMove);
                    }
                }
                return false;
            }
            if ($.browser.webkit) {
                this.addEventListener('touchstart', onMouseDown, false);
                this.addEventListener('touchend', cancelGesture, false);
            } else {
                $(this).bind("mousedown", onMouseDown);
                $(this).bind("mouseup", cancelGesture);
            }
        });
        return this;
    };
})(jQuery);

