// Copyright(C)2014 Cafi Net All Rights Reserved.
// 利用規約 http://japanism.info/free-template.html#template
$(function(){
	// スピード
	var title_speed = 700; // タイトル（700）	
	var h1_p_speed = 500; // 内容（500）
	var footer_speed = 700; // フッター（700）
	var one_page = 500; // ワンページスクロール（500）	
	var one_page_touch = 400; // ワンページスクロール・iPhone/iPad（400）
	var nav_button = 500; // ナビボタンスクロール（500）	
	var nav_open_close = 200; // ナビオープン（200）
	var on_off_hover = 300; // hover（300）

	$('a[href^=#block]').click(function(){
		var id = $($(this).attr('href')).offset().top;
		$('body,html').animate({scrollTop:id+'px'}, nav_button, 'swing');
		return false;
	});
	var url_get = location.href;
	var off_button;
	$('#off_button').click(function(){
		location.href = url_get+'?'+'scroll=off';
	});
	off_button = location.search.substring(8);
	if (off_button == 'off') {
		$('#on_off').html('<div id="on_button">scroll action on</div>');
	}
	$('#on_button').click(function(){
		history.back();
	});
	$('#on_off').hover(function(){
		$(this).animate({opacity: 0.7}, on_off_hover, 'swing');
	},
	function(){
		$(this).animate({opacity: 1}, on_off_hover, 'swing');	
	});
	var block_number;
	var block_top_array = new Array();
	var one_scroll = 1;
	var android1_ua = navigator.userAgent.indexOf('Android 1');
	var android2_ua = navigator.userAgent.indexOf('Android 2');
	function window_control () {
		var block_count = $("[id^='block']").length;
		var window_height = $(this).height();
		for (var i = 1; i <= block_count; i++) {
			var content_height = $('#content'+i).height();
			if (window_height <= content_height) {
				$('#max_width'+i).css('height', content_height+50+'px');
				$('#content'+i).css('padding-top', '25px');
				one_scroll = 0;
			} else {
				$('#max_width'+i).css('height', window_height+'px');
				$('#content'+i).css('padding-top', (window_height - content_height) / 2 +'px');
				var block_top = $('#block'+i).offset().top;
				block_top_array.push(block_top);
			}
		}
		block_top_array.reverse();
		block_top_array.length = block_count;
		block_top_array.reverse();
		var window_width = $(this).width()+15;
		var nav_state = 0;
		$('#nav_open img').click(function(){
			if (nav_state == 0) {
					$('#nav_open').animate({'bottom': '50px'}, nav_open_close, 'swing');
					$('#nav').animate({'bottom': '0px'}, nav_open_close, 'swing');
					$('#back_top').animate({'bottom': '50px'}, nav_open_close, 'swing');
					nav_state = 1;
			} else if (nav_state == 1) {
					$('#nav_open').animate({'bottom': '0px'}, nav_open_close, 'swing');
					$('#nav').animate({'bottom': '-50px'}, nav_open_close, 'swing');
					$('#back_top').animate({'bottom': '0px'}, nav_open_close, 'swing');
					nav_state = 0;
			}
		});
		if (one_scroll == 0 || off_button == 'off' || android1_ua > 0 || android2_ua > 0) {
			$('#up_button').css('display','none');		
			$('#down_button').css('display','none');	
		}
		var nav_length = $('#nav ul li').length;
		var nav_width = 32 * nav_length;
		if (one_scroll == 0 && window_width - nav_width < 0) {
			$('#nav_open').css('display','none');
			$('#nav').css('display','none');	
		}
		if (off_button == 'off' && window_width - nav_width < 0) {
			$('#nav_open').css('display','none');
			$('#nav').css('display','none');	
		}
		if (android1_ua > 0 || android2_ua > 0 && window_width - nav_width < 0) {
			$('#nav_open').css('display','none');
			$('#nav').css('display','none');	
		}		
		if (window_width - nav_width < 0) {
			var nav_length_1 = nav_length-1;
			$('#nav ul').find('li:lt(' + nav_length_1 + '):gt(1)').css('display','none');
			$('#nav ul li:eq(1)').html(1+'/'+(nav_length-2));
		}
		$('#button1 a').css('color', 'rgb(0,0,0)');
		var block_number_nav;
		$(window).scroll(function(){
			var scroll_value = $(window).scrollTop();
			if (scroll_value == 0) {
				$('#up_button').stop().animate({opacity: 0}, 'fast', 'swing').css('cursor', 'default');
			} else {
				$('#up_button').stop().animate({opacity: 1}, 'fast', 'swing').css('cursor', 'pointer');
			}
			if (scroll_value == block_top_array[block_count-1]) {
				$('#down_button').stop().animate({opacity: 0}, 'fast', 'swing').css('cursor', 'default');
			} else {
				$('#down_button').stop().animate({opacity: 1}, 'fast', 'swing').css('cursor', 'pointer');
			}
			for (var i = 1; i <= block_count; i++) {
				var block_top_value = $('#block'+i).offset().top;
				if (scroll_value == block_top_value) {
					$('#button'+i+' a').css('color', 'rgb(0,0,0)');
				} else {
					$('#button'+i+' a').css('color', 'rgb(255,255,255)');
				}
			}
			var array_number_nav = $.inArray(scroll_value, block_top_array);
			if (array_number_nav >= 0) {
				block_number_nav = array_number_nav+1;
			}
			if (block_number_nav == undefined) {
				block_number_nav = '-';
			}
			if (window_width - nav_width < 0) {
				$('#nav ul').find('li:lt(' + nav_length_1 + '):gt(1)').css('display','none');
				$('#nav ul li:eq(1)').html(block_number_nav+'/'+(nav_length-2));
			}
			if (scroll_value == block_top_array[block_count-1]) {
				$('#back_top').fadeIn();
			} else {
				$('#back_top').fadeOut();
			}
		});
		if (one_scroll == 0 || android1_ua > 0 || android2_ua > 0) {
			$('#on_off').css('display', 'none');
		}
		if (one_scroll == 1 && off_button != 'off' && android1_ua < 0 && android2_ua < 0) {
			$('body').css('overflow','hidden');
			block_number = 1;
			window.onmousewheel = function(e){
				if (one_scroll == 0) {
					location.reload();
				}				
				var scroll_value = $(window).scrollTop();
				var array_number = $.inArray(scroll_value, block_top_array);
				if (array_number >= 0) {
					block_number = array_number;
				}
				if (e.wheelDelta < 0) {
					$('body,html').not(':animated').animate({scrollTop:block_top_array[block_number+1]}, one_page, 'swing');
				} else if (e.wheelDelta > 0) {
					$('body,html').not(':animated').animate({scrollTop:block_top_array[block_number-1]}, one_page, 'swing');
				}
	     	}
			if (window.addEventListener) {
				window.addEventListener('DOMMouseScroll', function(e){
					if (one_scroll == 0) {
						location.reload();
					}	
					var scroll_value = $(window).scrollTop();
					var array_number = $.inArray(scroll_value, block_top_array);
					if (array_number >= 0) {
						block_number = array_number;
					}
					if (e.detail > 0) {
						$('body,html').not(':animated').animate({scrollTop:block_top_array[block_number+1]}, one_page, 'swing');
					} else if (e.detail < 0) {
						$('body,html').not(':animated').animate({scrollTop:block_top_array[block_number-1]}, one_page, 'swing');
					}
				}, false);
			}
			if (document.attachEvent) {
				document.attachEvent('onmousewheel', function(e){
					if (one_scroll == 0) {
						location.reload();
					}
					var scroll_value = $(window).scrollTop();
					var array_number = $.inArray(scroll_value, block_top_array);
					if (array_number >= 0) {
						block_number = array_number;
					}
					if (e.wheelDelta < 0) {
						$('body,html').not(':animated').animate({scrollTop:block_top_array[block_number+1]}, one_page, 'swing');
					} else if (e.wheelDelta > 0) {
						$('body,html').not(':animated').animate({scrollTop:block_top_array[block_number-1]}, one_page, 'swing');
					}
				});
			}
			var touch1,touch2;
			$('body').bind({
				'touchstart': function(e){
					touch1 = event.changedTouches[0].pageY;
				},
				'touchmove': function(e){
					e.preventDefault();
				},
				'touchend': function(e){
					touch2 = event.changedTouches[0].pageY;
					if (one_scroll == 0) {
						location.reload();
					}
					var scroll_value = $(window).scrollTop();
					var array_number = $.inArray(scroll_value, block_top_array);
					if (array_number >= 0) {
						block_number = array_number;
					}
					if (touch1-touch2 > 10) {
						$('body,html').not(':animated').animate({scrollTop:block_top_array[block_number+1]}, one_page_touch,'swing');
					} else if (touch1-touch2 < -10) {
						$('body,html').not(':animated').animate({scrollTop:block_top_array[block_number-1]}, one_page_touch,'swing');
					}
				}
			});
			$(window).keydown(function(e){
				if (one_scroll == 0) {
					location.reload();
				}
				var cafinet = e.keyCode;
				var scroll_value = $(window).scrollTop();
				var array_number = $.inArray(scroll_value, block_top_array);
				if (array_number >= 0) {
					block_number = array_number;
				}
				if (cafinet == 40) {
					$('body,html').not(':animated').animate({scrollTop:block_top_array[block_number+1]}, one_page,'swing');
				} else if (cafinet == 38) {
					$('body,html').not(':animated').animate({scrollTop:block_top_array[block_number-1]}, one_page,'swing');
				}
			});
			$('#down_button').click(function(){
				if (one_scroll == 0) {
					location.reload();
				}
				var scroll_value = $(window).scrollTop();
				var array_number = $.inArray(scroll_value, block_top_array);
				if (array_number >= 0) {
					block_number = array_number;
				}
				$('body,html').not(':animated').animate({scrollTop:block_top_array[block_number+1]}, one_page,'swing');
			});
			$('#up_button').click(function(){
				if (one_scroll == 0) {
					location.reload();
				}
				var scroll_value = $(window).scrollTop();
				var array_number = $.inArray(scroll_value, block_top_array);
				if (array_number >= 0) {
					block_number = array_number;
				}
				$('body,html').not(':animated').animate({scrollTop:block_top_array[block_number-1]}, one_page,'swing');
			});	
			$("[class^='bg-text'] h1").css('opacity', 0);
			$("[class^='bg-text'] p").css('opacity', 0);
			$("#bg-text-footer p").css('opacity', 0);
			$(window).scroll(function(){
				var scroll_value = $(window).scrollTop();
				for (var i = 1; i <= block_count-1; i++) {
					if (scroll_value == block_top_array[i]) {					
						var content_h1 = '.bg-text'+(i+1)+' h1';
						var content_p = '.bg-text'+(i+1)+' p';
						$(content_h1).animate({'opacity':1}, h1_p_speed, 'swing');
						$(content_p).delay(h1_p_speed).animate({'opacity':1}, h1_p_speed, 'swing');
					} else {
						$("[class^='bg-text'] h1").css('opacity', 0);
						$("[class^='bg-text'] p").css('opacity', 0);
						$("#bg-text-footer p").css('opacity', 0);
					}
				}
				if (scroll_value >= block_top_array[block_count-1]) {
					$("#bg-text-footer p").animate({'opacity':1}, footer_speed, 'swing');
				}
			});	
		}
		$('#site_title').animate({'opacity':1}, title_speed, 'swing');
		$('#site_title span').delay(title_speed).animate({'opacity':1}, title_speed, 'swing');
	}
	$('#site_title').css('opacity', 0);
	$('#site_title span').css('opacity', 0);
	$(window).load(function(){
		window_control();
	});
	$(window).resize(function(){
		window_control();
	});
});