var hideUrl = function(viewport) {
	// hide url on iphone
	setTimeout(function () {
		if (window.orientation == 0) {
			var height = screen.height;
			if (screen.width < viewport)
			{
				height = viewport * 1.5;
			}
			
			
			$('body').css('min-height', (height + 60) + 'px');
		}
		if (Math.abs(window.orientation) == 90) {
			var width = screen.width;
			if (screen.height < viewport)
			{
				width = viewport * 1.5;
			}
		
			$('body').css('min-height', (width + 60) + 'px');
		}		
		window.scrollTo(0, 1);	
	}, 1000);
}

$(function() {
	var count = $('#food li').size();
	var itemWidth = 110;
	$('#food ul').css('width', count*itemWidth);
	
	$('#food li').click(function(e) {
		e.preventDefault();
		
		if (!$(this).hasClass('nodetail'))
		{
			var name = $(this).data('name');	
			var type = $(this).data('type');				
			$('#detail').attr('class', name + ' ' + type);				
		
			$('#detail > .img').attr('src', $(this).data('detail-img'));
			$('#detail > .info').html($(this).find('.info').html());
		}
	});

	if (/mobile/i.test(navigator.userAgent))
	{
		var easeInQuad = function (t, b, c, d) {
			t /= d;
			return -c * t*(t-2) + b;
		};
	
		$('body').addClass('touch');
		
		var swiping = false;
		var swipeX;
		var swipeLeft = 0;
		var swipeElement;
		var swipeKineticX = 0;
		var swipeKineticEnergy = 0;
		var swipeKineticTime = null;
		var swipeKineticTimeout = null;
		var swipeKineticFriction = 0.95;
		
		$('#food')[0].ontouchstart = function(e) {
//			e.touches = [{pageX: e.pageX}]
		
			if (!swiping)
			{
				swipeX = e.touches[0].pageX;
				swipeKineticX = e.touches[0].pageX;
				swipeKineticEnergy = 0;
				swipeKineticTime = new Date().getTime();				
				swiping = true;
				clearTimeout(swipeKineticTimeout);
			}
			e.preventDefault();
		}
	
		$('#food li').bind('touchstart', function(e) {
			swipeElement = this;
			e.preventDefault();		
		});
		
		$('#food')[0].ontouchmove = function(e) {
//			e.touches = [{pageX: e.pageX}];
			
			if (swiping)
			{
				var delta = e.touches[0].pageX - swipeKineticX;
				if (delta > 0) 
				{
					if (swipeKineticEnergy < 0) 
					{
						swipeKineticEnergy = 0;
						swipeKineticTime = new Date().getTime();
					}
				}
				else if (delta < 0)
				{
					if (swipeKineticEnergy > 0) 
					{
						swipeKineticEnergy = 0;
						swipeKineticTime = new Date().getTime();
					}
				}
				
				swipeKineticEnergy += delta;								
				swipeKineticX = e.touches[0].pageX;
			
				var left = swipeLeft - (e.touches[0].pageX - swipeX);
				var food_width = $('#food').width();
				var scroll_max = count*itemWidth - food_width;		
				if (left < 0 || left > scroll_max) swipeKineticEnergy = 0;
				$('#food ul').css('margin-left', -left);
			}
		}
	
		$('#food')[0].ontouchend = function(e) {
//			e.changedTouches = [{pageX: e.pageX}];
			
			if (swiping)
			{
				var delta = (e.changedTouches[0].pageX - swipeX);
				if (Math.abs(delta) < 20)
				{
					$(swipeElement).click();
					swipeLeft -= delta;
				}
				else
				{
					swipeLeft -= delta;
					
					var speed = swipeKineticEnergy / (new Date().getTime() - swipeKineticTime) * 15;
					var tick = function() {
						var inRange = true;
					
						var food_width = $('#food').width();					
						var scroll_max = count*itemWidth - food_width;
						if (swipeLeft < 0) 
						{
							inRange = false;
							
							if (speed < 0) {
								var t = (new Date().getTime() - swipeKineticTime) / 500;
								var c = swipeKineticX;
								var x = easeInQuad(t, 0, -c, 1) + c;
								speed = swipeLeft - x;
								swipeLeft = x;
							}
							else
							{
								swipeKineticX = swipeLeft;
								swipeKineticTime = new Date().getTime();
								swipeLeft -= speed;								
								speed -= 1;
								speed *= swipeKineticFriction;
							}
						}
						else if (swipeLeft > scroll_max) 
						{
							inRange = false;
							
							if (speed > 0) 
							{
								var t = (new Date().getTime() - swipeKineticTime) / 500;
								var c = swipeKineticX - scroll_max;
								var x = scroll_max + (c - easeInQuad(t, 0, c, 1));
								speed = swipeLeft - x;
								swipeLeft = x;
							}
							else
							{
								swipeKineticX = swipeLeft;
								swipeKineticTime = new Date().getTime();
								swipeLeft -= speed;								
								speed += 1;
								speed *= swipeKineticFriction;
							}
						}
						else
						{
							swipeLeft -= speed;
							speed *= swipeKineticFriction;
						}
					
						$('#food ul').css('margin-left', -swipeLeft);
					
						if (Math.abs(speed) > 0.1 || !inRange)
						{
							swipeKineticTimeout = setTimeout(tick, 10);
						}
					};
				
					tick();
					
				}
				swiping = false;
			}
		}
	
		hideUrl(630);
	}
	else
	{
		$('body').mousemove(function(e) {
			var food_left = $('#food').offset().left;
			var food_width = $('#food').width();
			var scroll_max = count*itemWidth - food_width;	
	
			var left = e.pageX - food_left - 50;
			var scroll = left/(food_width - 100);
			if (scroll < 0) scroll = 0;
			if (scroll > 1) scroll = 1;
			$('#food ul').css('margin-left', -scroll_max*scroll);
		});
	}
});

