// Copyright(C)2014 Cafi Net All Rights Reserved.
// 利用規約 http://japanism.info/free-template.html#template
$(function(){
	// 調整用 ここから	
	var nav_speed = 500;     // nav と nav_copy のスピード[500]
	var nav_copy_fade = 550; // nav_copy が fadeIn・fadeOut する位置[550]
	var nav_copy_state = 1;  // nav_copy の表示・非表示[1 = ON][0 = OFF]
	var content_state = 1;   // content の非表示・表示[1 = ON][0 = OFF]
	var content_speed = 700; // content の表示スピード[700]
	var min_width = 600;     // ブレークポイント[600]
	var main_video = '<video autoplay controls id="top_video"><source src="images/main-video.mp4" type="video/mp4"><source src="images/main-video.ogv" type="video/ogg"><source src="images/main-video.webm" type="video/webm"><img src="images/main-video.jpg" alt="ビデオが再生できません"/></video>';
	// 調整用 ここまで		

	var nav_copy = $('#nav_copy');
	var site_title = $('#site_title');
	var block1 = $('#block1');
	var title_block = $('#title_block');
	var menu_button = $('#menu_button');
	var nav = $('#nav');
	var top_video = $('#top_video');
	var play_button = $('#play_button');
	function window_control () {
		$('#nav ul li a').unbind();
		$('a[href^=#block]').unbind();
		menu_button.unbind();
		nav.unbind();
		nav_copy.unbind();
		title_block.unbind();
		var window_width = $(window).width()+15;
		var nav_copy_height = nav_copy.height();
		if (window_width >= min_width && nav_copy_state == 1) {
			$('a[href^=#block]').click(function(){
				var id = $($(this).attr('href')).offset().top;
				$('body,html').animate({scrollTop:id - nav_copy_height +'px'}, nav_speed, 'swing');
				return false;
			});
		} else {
			$('a[href^=#block]').click(function(){
				var id = $($(this).attr('href')).offset().top;
				$('body,html').animate({scrollTop:id +'px'}, nav_speed, 'swing');
				return false;
			});
		}
		var top_video_width = top_video.innerWidth();
		var top_video_width_pe = top_video_width / 1920;
		var top_video_height = 1080 * top_video_width_pe;
		top_video.css('height', top_video_height);
		var touch_start = ('ontouchstart' in window); 
		if (touch_start) {
			$('#video_wrap').html('<img src="images/top-video.jpg" alt="トップ画像"/>');
		}
		play_button.click(function(){
			$('#content1').html(main_video);
		});
		var block1_width = block1.innerWidth();
		var block1_height = block1.innerHeight();
		var site_title_width = site_title.innerWidth();
		var site_title_height = site_title.innerHeight();
		var title_block_width = title_block.innerWidth();
		var title_block_height = title_block.innerHeight();
		var play_button_width = play_button.innerWidth();
		if (window_width < min_width) {
			var title_top = (block1_height - site_title_height) / 2;
			var title_left = (block1_width - site_title_width) / 2;
			site_title.css({'top': title_top+'px', 'left': title_left+'px'});
			var play_button_left = (block1_width - play_button_width) / 2;
			play_button.css({'top': title_top+site_title_height+'px', 'left': play_button_left+'px'});
			if(nav.css('margin-left') == '0px') {
				nav.css('margin-left', '-200px');
			}
			menu_button.click(function(){
				if(nav.css('margin-left') == '-200px') {
					nav.animate({'margin-left': '0px'}, 'fast', 'linear');
					menu_button.animate({'left': '200px'}, 'fast', 'linear');
				} else {
					nav.animate({'margin-left': '-200px'}, 'fast', 'linear');
					menu_button.animate({'left': '0px'}, 'fast', 'linear');
				}
			});
			$('#nav ul li a').click(function(){	
				if(window_width < min_width && nav.css('margin-left') == '0px') {
					nav.animate({'margin-left': '-200px'}, 'fast', 'linear');
					menu_button.animate({'left': '0px'}, 'fast', 'linear');
				}			
			});
		} else {
			if(nav.css('margin-left') == '-200px') {
				nav.css('margin-left', '0px');
			}
			var title_block_top = (block1_height - title_block_height) / 2;
			var title_block_left = (block1_width - title_block_width) / 2;
			title_block.css({'top': title_block_top+'px', 'left': title_block_left+'px'});
			var play_button_left = (block1_width - play_button_width) / 2;
			play_button.css({'top': title_block_top+title_block_height+'px', 'left': play_button_left+'px'});					
		}
		if (window_width >= min_width) {
			if (nav_copy_state == 2) {
				nav_copy_state = 1;
			}
			$(window).scroll(function(){
				var scroll_value = $(window).scrollTop();
				if (nav_copy_state == 1) {
					if (nav_copy_fade < scroll_value) {
						nav_copy.fadeIn();		
					} else {
						nav_copy.fadeOut();
					}
				}
			});
		} else {
			nav_copy.fadeOut();
			nav_copy_state = 2
		}
		if (touch_start == false) {
			if ($(window).height() < $(document).height() && content_state ==1) {
				var block_count = $("[id^='block']").length;
				for (var i = 3; i <= block_count-1; i++) {
					$('#content'+i).css('opacity', 0);
				}
			}
			$(window).scroll(function(){
				var scroll_value2 = $(window).scrollTop();
				if (content_state == 1) {
					for (var i = 3; i <= block_count-1; i++) {
						var block_top = $('#block'+(i-1)).offset().top;
						if (scroll_value2 >= block_top) {
							$('#content'+i).animate({'opacity': 1}, content_speed, 'swing');
						}
					}
					if ($(window).height() + scroll_value2 >= $(document).height() - 100) {
						for (var i = 3; i <= block_count-1; i++) {
							$('#content'+i).animate({'opacity': 1}, content_speed, 'swing');
						}
					}
				}
			});
		}
	}
	$(window).load(function(){
		window_control();
	});
	$(window).resize(function(){
		window_control();
	});
});