﻿/*
    jQuery plugin to detect flicks on elements
		http://plugins.jquery.com/project/jFlick
    Steve Gough 12/04/2010
		
		Modificaciones realizadas debido a que el sistema
		no parece estar guardando correctamente las
		variables cuando se referencian con "this."
		También se realizaron modificaciones para permitir
		que se sigan detectando otros eventos por defecto
		aparte del flick en una misma zona de la pantalla.
		Juan Capristán 11/06/2010
		
		Versión 1.0.0 - Modificada
*/


(function($) {
    $.fn.detectFlicks = function(options) {
        
        //for reference only
        var LeftToRight = 'I>D',
            RightToLeft = 'D>I',
            UpToDown = 'AR>AB',
            DownToUp = 'AB>AR';
        
        var flickController = {
            direction: '',
            isFlick: false
        };

        var defaults = {
            threshold: 15,
            axis: 'x',
            flickEvent: function() { return true; }
        };
				
				var inicioX, inicioY, desplazamiento; //Juan

        var options = $.extend(defaults, options);

        flickController.touchStart = function(e) {
            var $el = $(e.target);
            // this is where the touch was first detected
            this.isFlick = false;
            //this.startX = event.targetTouches[0].clientX;
            //this.startY = event.targetTouches[0].clientY;
						inicioX = event.targetTouches[0].clientX;
						inicioY = event.targetTouches[0].clientY;
            if (options.axis == 'y') {
                $el.bind('touchmove', flickController.touchMoveY);
            }
            else {
                $el.bind('touchmove', flickController.touchMoveX);
            }

            $el.bind('touchend', flickController.touchEnd);
        };

        flickController.touchMoveX = function(e) {
            //event.preventDefault(); //permito que se sigan detectando otros eventos (scroll, zoom, etc.)
            this.movedX = event.targetTouches[0].clientX;
            //if (Math.abs(Math.abs(this.movedX) - Math.abs(this.startX)) > options.threshold) {
						desplazamiento = Math.abs(Math.abs(this.movedX) - Math.abs(inicioX)); //Juan
						if (desplazamiento > options.threshold) { //Juan
                this.isFlick = true;
                //if (this.movedX > this.startX) {
								if (this.movedX > inicioX) { //Juan
                    flickController.direction = LeftToRight;
                }
                else {
                    flickController.direction = RightToLeft;
                }
            }
        };

        flickController.touchMoveY = function(e) {
            //event.preventDefault(); //permito que se sigan detectando otros eventos (scroll, zoom, etc.)
            this.movedY = event.targetTouches[0].clientY;
            //if (Math.abs(Math.abs(this.movedY) - Math.abs(this.startY)) > options.threshold) {
						desplazamiento = Math.abs(Math.abs(this.movedY) - Math.abs(inicioY)); //Juan
						if (desplazamiento > options.threshold) { //Juan
                this.isFlick = true;
                //if (this.movedY > this.startY) {
								if (this.movedY > inicioY) { //Juan
                    flickController.direction = UpToDown;
                }
                else {
                    flickController.direction = DownToUp;
                }
            }
        };

        // Evaluate the custom code if a flick was detected
        flickController.touchEnd = function(e) {
            var $el = $(e.target);
            if (this.isFlick) {
                options.flickEvent({ direction: flickController.direction });
								this.isFlick = false; //Juan
            }
            $el.unbind('touchmove touchend');
        };

        obj = $(this);
        obj.bind('touchstart', flickController.touchStart);

        return flickController;
    };
})(jQuery);