/**
 * Created by JetBrains PhpStorm.
 * User: michael
 * Date: 01.04.11
 * Time: 10:58
 * To change this template use File | Settings | File Templates.
 */

(function($) {
	jQuery.fn.touchController = function(callback) {
		$(this).each(function() {
			new TouchController($(this), callback);
		});
		return $(this);
	};
})(jQuery);

TouchController = function(element, callback) {
	this.__construct(element, callback);
}

$.extend(TouchController.prototype, {
	element: null,

	x_start: null,
	y_start: null,
	x_end: null,
	y_end: null,

	touch_start_time: null,
	touch_duration: null,

	callback: null,

	is_capturing: null,

	rotation : 0,

	/**
	 * Constructor: initialization, event handlers binding
	 * @param element
	 * @param callback
	 */
	__construct: function(element, callback) {
		this.element = element;
		this.callback = callback;

		var __instance = this;

		this.element.parent()[0].ontouchstart = function(e) {
			return __instance.onTouchStart(e)
		};
		this.element.parent()[0].ontouchmove = function(e) {
			return __instance.onTouchMove(e)
		};

		this.element.parent()[0].ongesturechange= function(e) {
			return __instance.onGestureChange(e)
		};

		/* Binding to body, cause "release outside" */

		$("body")[0].ontouchend = function(e) {
			return __instance.onTouchEnd(e)
		};

		$("body")[0].ongestureend= function(e) {
			return __instance.onGestureEnd(e)
		};

	},

	/**
	 * Touch star event handler
	 * @param e
	 * @param is_mouse
	 */
	onTouchStart:  function(e, is_mouse) {
		(this.callback)({
			'type': 'touchstart'
		})
		this.is_capturing = true;
		this.touch_start_time = new Date().getTime();
		var pageX = is_mouse ? e.pageX : e.touches[0].pageX;
		var pageY = is_mouse ? e.pageY : e.touches[0].pageY;
		this.x_start = pageX;
		this.y_start = pageY;
		this.x_end = pageX;
		this.y_end= pageY;
	},

	/**
	 * Moving finger event
	 * @param e
	 * @param is_mouse
	 */
	onTouchMove:  function(e, is_mouse) {
		var pageX = is_mouse ? e.pageX : e.touches[0].pageX;
		var pageY = is_mouse ? e.pageY : e.touches[0].pageY;
		if (this.is_capturing) {
			(this.callback)({
				'type': 'touchmove',
				'duration': new Date().getTime() - this.touch_start_time,
				'x': pageX - this.x_end,
				'y': pageY - this.y_end
			})
		}
		this.touch_start_time = new Date().getTime();
		this.x_start = this.x_end;
		this.y_start = this.y_end;
		this.x_end = pageX;
		this.y_end = pageY;
		return false;
	},

	/**
	 * Finger up, touch end
	 * @param e
	 * @param is_mouse
	 */
	onTouchEnd:  function(e, is_mouse) {
		if (this.is_capturing) {
			(this.callback)({
				'type': 'touchend',
				'duration': new Date().getTime() - this.touch_start_time,
				'x': this.x_end - this.x_start,
				'y': this.y_end - this.y_start
			})
		}
		this.is_capturing = false;
	},

	/**
	 * On gesture: scale, rotate.
	 * Only rotation at this moment
	 * @param e
	 * @param is_mouse
	 */
	onGestureChange:  function(e, is_mouse) {
		(this.callback)({
			'type': 'rotate',
			'rotate': e.rotation,
			'prev_rotate': this.rotation
		})
		this.rotation = e.rotation
	},

	/**
	 * Finish gesture
	 * @param e
	 * @param is_mouse
	 */
	onGestureEnd:  function(e, is_mouse) {
		(this.callback)({
			'type': 'rotateend'
		})
		this.rotation = 0
	}
});


