// bodyがonloadしてから
function initIPhoneGestures(){
	if (isAndroidWebkit()) {
		return;
	}
	initIPhoneTripleTap();
}


gScrollTimerId = false;
function iphonePageScroll(dist)
{
	if (gScrollTimerId) {
		clearInterval(gScrollTimerId);
	}
	gScrollTimerId = setInterval('iphonePageScrollDo(' + dist + ')', 5);
}

function iphonePageScrollDo(dist)
{
	if (typeof(gBaseScrollTop) == 'number') {
		if (Math.abs(gBaseScrollTop - document.body.scrollTop) < 1) {
			clearInterval(gScrollTimerId);
			gBaseScrollTop = false;
			return;
		}
	}
	
	gBaseScrollTop = document.body.scrollTop;
	window.scrollBy(0, (dist - gBaseScrollTop)/26);
}

function inactiveFlick()
{
	gFlickActive = false;
	gFlickPoint = 0;
}
function initIPhoneTripleTap()
{
	gTapped = new Object();
	gTapped.times = 0;
	gTapped.startDate = new Date();
	gTapped.touchendact = 0;
	gTapped.dbldiff = 0;
	gTapped.startY = false;
	
	document.body.ontouchstart  = function() {
		var e = event.touches[0];
		//alert(e.clientY);
		//alert(window.innerHeight);
		var nowDate = new Date();
		//alert(nowDate.getTime());
		var diff = nowDate.getTime() - gTapped.startDate.getTime();
		gTapped.startDate = nowDate;
		gTapped.startY = e.clientY;
		if (diff < 400) {
			gTapped.times = gTapped.times + 1;
			if (gTapped.times == 2) {
				//alert('ダブル！');
				gTapped.dbldiff = diff;
				if (e.clientY < window.innerHeight/2*0.8) {
					gTapped.touchendact = 3
				} else if (e.clientY > window.innerHeight/2*1.2) {
					gTapped.touchendact = 4;
				}
				return;
			}
			if (gTapped.times > 2 && diff > gTapped.dbldiff*0.7 && diff < gTapped.dbldiff*1.07) {
				//alert('トリプル！');
				if (e.clientY < window.innerHeight/2*0.8) {
					gTapped.touchendact = 1
				} else if (e.clientY > window.innerHeight/2*1.2) {
					gTapped.touchendact = 2;
				}
				return;
			}
		} else {
			gTapped.times = 1;
			if (gScrollTimerId) {
				clearInterval(gScrollTimerId);
			}
		}
		gTapped.touchendact = 0;
	};

	gFlickPoint = 0;
	gFlickActive = false;
	gFlickTimerID = false;
	
	document.body.ontouchmove  = function() {
		gTapped.times = 0;

		return;
		
		if (gFlickActive) {
			return;
		}
		var nowDate = new Date();
		var diff = nowDate.getTime() - gTapped.startDate.getTime();
		if (diff < 400) {
			var e = event.touches[0];
			if (Math.abs(e.clientY - gTapped.startY) > 200) {
				gFlickActive = true;
				gFlickPoint = gFlickPoint + 1;
				if (gFlickPoint > 1) {
					if (e.clientY > gTapped.startY) {
						iphonePageScroll(0);
					} else {
						iphonePageScroll(document.height - window.innerHeight + 14);
					}
				}
				if (gFlickTimerID) clearTimeout(gFlickTimerID);
				gFlickTimerID = setTimeout('inactiveFlick()', 600);
			}

		} else {
			inactiveFlick();
		}
		
		/*
		var dp = document.getElementById('debugpanel');
		var e = event.touches[0];
		dp.innerHTML = e.clientX + '.' + e.clientY;
		if (e.clientX > 200) {
			alert(e.clientX);
		}
		*/
	}

	document.body.ontouchend  = function() {
		//var zoomer = document.body.clientWidth / window.innerWidth;
		gFlickActive = false;
		if (gTapped.touchendact) {
			event.preventDefault();
			var nowDate = new Date();
			var diff = nowDate.getTime() - gTapped.startDate.getTime();
			if (diff < 400) {
				if (gTapped.touchendact == 1) {
					iphonePageScroll(0);
				} else if (gTapped.touchendact == 2) {
					iphonePageScroll(document.height - window.innerHeight + 14);
				} else if (gTapped.touchendact == 3) {
					iphonePageScroll(document.body.scrollTop - window.innerHeight*0.85);
				} else if (gTapped.touchendact == 4) {
					iphonePageScroll(document.body.scrollTop + window.innerHeight*0.85);
				}
			}
		}
	}
}