// Copyright(C)2014 Cafi Net All Rights Reserved.
// 利用規約 http://japanism.info/free-template.html#template
$(function(){
	// スピード
	var title_speed = 700; // タイトル（700）	
	var text_speed = 700; // 内容（700）
	var footer_speed = 700; // フッター（700）
	var one_page = 500; // ワンページスクロール（500）	
	var one_page_touch = 400; //ワンページスクロール・iPhone/iPad（400）
	var nav_button = 500; // ナビボタンスクロール（500）
	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_height = $('#nav').height();
		var nav_top = (window_height - nav_height) / 2;
		$('#nav').css('top', nav_top+'px');
		if (window_height < nav_height || one_scroll == 0 || off_button == 'off' || android1_ua > 0 || android2_ua > 0) {
			$('#nav').css('display','none');	
		}
		$('#button1 a').html('<img src="images/nav-button-active.png"/>');
		var block_number_nav;
		$(window).scroll(function(){
			var scroll_value = $(window).scrollTop();
			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 img').attr('src', 'images/nav-button-active.png');
				} else {
					$('#button'+i+' a img').attr('src', 'images/nav-button.png');
				}
			}
			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');
				}
			});
			$("[class^='bg-text']").css('opacity', 0);
			$("#bg-text-footer").css('opacity', 0);
			$(window).scroll(function(){
				var scroll_value = $(window).scrollTop();
				if (scroll_value == 0) {
					$('#site_title').animate({'opacity':1}, title_speed, 'swing');
					$('#site_title span').delay(title_speed).animate({'opacity':1}, title_speed, 'swing');
				} else {
					$('#site_title').css('opacity', 0);
					$('#site_title span').css('opacity', 0);
				}
				for (var i = 1; i <= block_count-1; i++) {
					if (scroll_value == block_top_array[i]) {					
						$('.bg-text'+(i+1)).animate({'opacity':1}, text_speed, 'swing');
					} else {
						$("[class^='bg-text']").css('opacity', 0);
					}
				}
				if (scroll_value >= block_top_array[block_count-1]) {
					$("#bg-text-footer").animate({'opacity':1}, footer_speed, 'swing');
				} else {
					$("#bg-text-footer").css('opacity', 0);
				}
			});	
		}
		$('#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();
	});
});