/**
 * jQuery Plugin to obtain touch gestures from iPhone, iPod Touch and iPad, should also work with Android mobile phones (not tested yet!)
 * Common usage: wipe images (left and right to show the previous or next image)
 * 
 * @author Andreas Waltl, netCU Internetagentur (http://www.netcu.de)
 * @version 1.1.1 (9th December 2010) - fix bug (older IE's had problems)
 * @version 1.1 (1st September 2010) - support wipe up and wipe down
 * @version 1.0 (15th July 2010)
 */
 
(function($) { 	
	$.fn.touchwipe = function(settings) {
	var config = {
		min_move_x: 20,
		min_move_y: 20,

 		wipeLeft: 	function() { },
 		wipeRight: 	function() { },
 		wipeUp: 	function() { },
 		wipeDown: 	function() { },

 		noPageWipeLeft: 	function() { },
 		noPageWipeRight: 	function() { },
 		noPageWipeUp: 		function() { },
 		noPageWipeDown: 	function() { },

 		moveXY: 	function(dx, dy) { },

		preventDefaultEvents: true
	};
	 
	if (settings) $.extend(config, settings);
 
	this.each(function() {
		var startX;
		var startY;

		var startTextX;
		var startTextY;

		var isMoving = false;

		function cancelTouch() {
			this.removeEventListener('touchmove', onTouchMove);
			this.removeEventListener('touchmove', onTouchMoveNoPage);
			this.removeEventListener('touchmove', onTouchMoveScroll);
			startX = null;
			startY = null;
			isMoving = false;
		}	
		
		function onTouchMove(e) {
		
			if(config.preventDefaultEvents) {
				e.preventDefault();
			}
		
			if(isMoving) {
				var x = e.touches[0].pageX;
				var y = e.touches[0].pageY;
				var dx = startX - x;
				var dy = startY - y;
				if(Math.abs(dx) >= config.min_move_x) {
					cancelTouch();
					if(dx > 0) {
						//document.title = "default left";
						config.wipeLeft();
					} else {
						//document.title = "default right";
						config.wipeRight();
					}
				} else if (Math.abs(dy) >= config.min_move_y) {
					cancelTouch();
					if(dy > 0) {
						//document.title = "default down";
						config.wipeDown();
					} else {
						//document.title = "default up";
						config.wipeUp();
					}
				}
			}
		}
		

		function onTouchMoveNoPage(e) {
			if(config.preventDefaultEvents) {
				e.preventDefault();
			}
		
			if(isMoving) {
				var x = e.touches[0].pageX;
				var y = e.touches[0].pageY;
				var dx = startX - x;
				var dy = startY - y;
				if(Math.abs(dx) >= config.min_move_x) {
					cancelTouch();
					if(dx > 0) {
						//document.title = "noPage left";
						config.noPageWipeLeft();
					} else {
						//document.title = "noPage right";
						config.noPageWipeRight();
					}
				} else if (Math.abs(dy) >= config.min_move_y) {
					cancelTouch();
					if(dy > 0) {
						//document.title = "noPage down";
						config.noPageWipeDown();
					} else {
						//document.title = "noPage up";
						config.noPageWipeUp();
					}
				}
			}
		}

		function onTouchMoveScroll(e) {
			if(config.preventDefaultEvents) {
				e.preventDefault();
			}
		
			if(isMoving) {
				var x = e.touches[0].pageX;
				var y = e.touches[0].pageY;
				var dx = startTextX - x;
				var dy = startTextY - y;
				if(Math.abs(dx) >= config.min_move_x || Math.abs(dy) >= config.min_move_y) {
					startTextX = x;
					startTextY = y;
					config.moveXY(dx,dy);
				}
			}
		}

		function onTouchStart(e) {
			if (e.touches.length == 1) {

				var obj = $(e.target)
				var objPath = '';
				var nopage = false;
				var textScroll = false;

				while(1) {
					objPath = obj[0].tagName + "|" + obj.attr('id') + '|' + obj.attr('class') + ' > ' + objPath;

					if (objPath.indexOf('nopage') > -1){
						nopage = true;
						break;
					}else if(objPath.indexOf('job_details') > -1) {
						textScroll = true;
						break;
					}

					obj = obj.parent();
					if (!obj[0]) break;
					if (obj[0].tagName.toLowerCase() == 'body') break;
					
				}
				

				startX = e.touches[0].pageX;
				startY = e.touches[0].pageY;
				startTextX = e.touches[0].pageX;
				startTextY = e.touches[0].pageY;

				isMoving = true;
				
				if (nopage) {
					//document.title = 'no page';
					this.addEventListener('touchmove', onTouchMoveNoPage, false);
				}else if (textScroll) {
					//document.title = 'textscroll';
					this.addEventListener('touchmove', onTouchMoveScroll, false);
				}else{
					this.addEventListener('touchmove', onTouchMove, false);
				}
				this.addEventListener('touchend', cancelTouch, false);

				

			}
		}

		if ('ontouchstart' in document.documentElement) {
			this.addEventListener('touchstart', onTouchStart, false);
		}
	});
 
	return this;

	};
})(jQuery);

