
function Swipe(targetElement)
{
	/* !Init */
	touchStartX = 0;
	touchStartY = 0;
	swipeStartX = 0;
	swipeStartY = 0;
	touchEndX = 0;
	touchEndY = 0;
	swipeLengthX = 0;
	swipeLengthY = 0;
	moveAmountX = 0;
	moveAmountY = 0;
	
	/* !Config */
	minLength = 20;
	maxThresholdRatio = 3;
	
	dragCallback = spin;
	startCallback = spinStart;
	
	/* !Events */
	targetElement.ontouchstart = this.touchStart;
	targetElement.ontouchmove = this.touchMove;
	targetElement.ontouchend = this.touchEnd;
}

	
Swipe.prototype.touchStart = function(event)
{
	cube.style.webkitTransitionDuration = '0s';
	touchStartX = touchEndX = event.touches[0].screenX;
	swipeStartX = swipeLengthX;
	touchStartY = touchEndY = event.touches[0].screenY;
	swipeStartY = swipeLengthY;
	startCallback(touchStartX, touchStartY);
}

Swipe.prototype.touchMove = function(event)
{
	event.preventDefault();

	// difference between where we are in mid-move and where we started
	moveAmountX = event.touches[0].screenX - touchStartX;	
	moveAmountY = event.touches[0].screenY - touchStartY;
	// reset this in case we change direction in mid-move
	touchStartX += moveAmountX;
	touchEndX = event.touches[0].screenX;
	touchStartY += moveAmountY;
	touchEndY = event.touches[0].screenY;
	
	// Get actual Length
	swipeLengthX += moveAmountX;
	swipeLengthY += moveAmountY;
	
	dragCallback(touchEndX, touchEndY);
}

Swipe.prototype.touchEnd = function()
{
	// brake slowly
	if (moveAmountX > 5 || moveAmountY > 5)
	{
		cube.style.webkitTransitionDuration = '.5s';
		dragCallback(touchEndX + swipeLengthX + moveAmountX * 3, touchEndY + swipeLengthY + moveAmountY * 3);
	}

	setTimeout('\
	// remember translation\
	spinX += swipeLengthX;\
	spinY += swipeLengthY;\
	// reset all\
	touchStartX = 0;\
	touchStartY = 0;\
	swipeLengthX = 0;\
	swipeLengthY = 0;', 501);
}





