// JavaScript Document

var st=0;
var h;
var wH;
var secH = [0];
var down = true;
var topicIndex = 0;
var topicFlIndex;
var isMSIE = /*@cc_on!@*/false;

$(function(){
	getSecH();
	var topicLen = $("#topic_index_wrap li").length;
	$("#topic_index_wrap ul").width(230*topicLen);
	
	if(!isMSIE) {
		$("body").addClass("noie");
	}
	
	setSize();
	$(window).bind("resize", function(){
		setSize();
	});
	
	var naviTimer;
	
	$(window).scroll(function(){
		pst = st;
		st = $(window).scrollTop();
		
		if(st>pst){
			down = true;
		}else {
			down = false;
		}
		
		clearTimeout(naviTimer);
		naviTimer = setTimeout(function(){
			$("header").stop();
		$("header").animate({
			"top": (st-39)+"px"
		}, 500, "easeInOutCubic");
		}, 500);
		
		
		if(st > 760 && st < 982){
			$("#arrow1").height((st-760)*0.5);
		}
		if(st > 850 && st < 1226){
			$("#aboutline").width(st-850);
		}
		if(st > 1350 && st < 1572){
			$("#arrow2").height((st-1350)*0.5);
		}
		
		if(down){
			if(st > 600 && pst <= 600){
				$("#greet p").animate({
					"height": "300px"
				}, 500);
			}
			if(st > 760 && pst <= 760 && !isMSIE){
				$("#what h3, #hatena1").animate({
					"opacity": "1"
				}, 500);
			}
			if(st > 982 && pst <= 982){
				$("#arrow1").height(111);
			}
			
			if(st > 980 && pst <= 980 && !isMSIE){
				$("#abouttxt").animate({
					"opacity": "1"
				}, 500);
			}
			if(st > 1150 && pst <= 1150 && !isMSIE){
				$("#aboutdetail").animate({
					"opacity": "1"
				}, 500);
			}
			if(st > 1226 && pst <= 1226){
				$("#aboutline").width(376);
			}
			
			if(st > 1350 && pst <= 1350 && !isMSIE){
				$("#example h3, #hatena2").animate({
					"opacity": "1"
				}, 500);
			}
			if(st > 1572 && pst <= 1572){
				$("#arrow2").height(111);
			}
			
			if(st > 1580 && pst <= 1580){
				if (!isMSIE) {
					$("#ex_part").animate({
						"top": "205px",
						"opacity": "1"
					}, 1000);
				}
				
			}
			if(st > 1680 && pst <= 1680){
				if (!isMSIE) {
					$("#ex_anime").animate({
						"top": "400px",
						"opacity": "1"
					}, 1000);
				}
				
			}
			if(st > 1780 && pst <= 1780){
				if (!isMSIE) {
					$("#ex_copy").animate({
						"top": "570px",
						"opacity": "1"
					}, 1000);
				}
				
			}
			
			for(var i = 0; i < 5; i++){
				if(st >= secH[i] && pst < secH[i]){
					$("header ul li").removeClass("selected");	
					$("header ul li").eq(i).addClass("selected");	
				}else if(st+h >= wH){
					$("header ul li").removeClass("selected");	
					$("header ul li").eq(4).addClass("selected");	
				}
			}
			
			/* if(st+h >= wH){
				$("header ul li").removeClass("selected");	
				$("header ul li").eq(4).addClass("selected");	
			} */
		}else {	
			if(st < 600 && pst >= 600){
				$("#greet p").animate({
					"height": "0px"
				}, 500);
			}
		
			if(st < 760 && pst >= 760){
				$("#arrow1").height(0);
				if (!isMSIE) {
					$("#what h3, #hatena1").animate({
						"opacity": "0"
					}, 500);
				}
			}
			
			if(st < 980 && pst >= 980){
				if (!isMSIE) {
					$("#abouttxt").animate({
						"opacity": "0"
					}, 500);
				}
				$("#aboutline").width();
			}
			if(st < 1150 && pst >= 1150 && !isMSIE){
				if (!isMSIE) {
					$("#aboutdetail").animate({
						"opacity": "0"
					}, 500);
				}
			}
			if(st < 1350 && pst >= 1350){
				$("#arrow2").height(0);
				if (!isMSIE) {
					$("#example h3, #hatena2").animate({
						"opacity": "0"
					}, 500);
				}
			}
			
			if(st < 1580 && pst >= 1580){
				if (!isMSIE) {
					$("#ex_part").animate({
						"top": "305px",
						"opacity": "0"
					}, 1000);
				}
			}
			if(st < 1680 && pst >= 1680){
				if (!isMSIE) {
					$("#ex_anime").animate({
						"top": "500px",
						"opacity": "0"
					}, 1000);
				}
			}
			if(st < 1780 && pst >= 1780){
				if (!isMSIE) {
					$("#ex_copy").animate({
						"top": "670px",
						"opacity": "0"
					}, 1000);
				}
			}
			
			for(var i = 0; i < 5; i++){
				if(st <= secH[i] && pst > secH[i]){
					$("header ul li").removeClass("selected");	
					$("header ul li").eq(i).addClass("selected");		
				}
			}
		}
		
	});
	
	$("header nav li").click(function(){
		var index = $("header nav li").index(this);
		if(index != 5){
			scrollSection(index);
		}
	});
	
	$("h1").click(function(){
		scrollSection(0);
	});
	
	$("#ccp_about").click(function(){
		scrollSection(1);
	});
	
	$("#ccp_works").click(function(){
		scrollSection(2);
	});
	
	$("#works_nav li").click(function(){
		$("#works_nav li").removeClass("selected");
		$(this).addClass("selected");
		var index = $("#works_nav li").index(this);
			$(".work_section").css("display", "none");
			$(".work_section").eq(index).css("display", "block");
		getSecH();
	});
	
	$("#cp_nav li").click(function(){
		$("#cp_nav li").removeClass("selected");
		$(this).addClass("selected");
		var index = $("#cp_nav li").index(this);
		$(".cp_section").css("display", "none");
		$(".cp_section").eq(index).css("display", "block");
		if(index == 2){
			var rel = $("iframe").attr("rel");
			$("iframe").attr("src",rel);
		}
		getSecH();
	});
	
	$("#topic_index_left").click(function(){
		topicIndex--;
		if(topicIndex == 0){
			$("#topic_index_left").css("display", "none");
		}
		if(topicIndex == topicLen-2){
			$("#topic_index_right").css("display", "block");
		}
		$("#topic_index_wrap ul").animate({
			"margin-left": (-230*topicIndex)+"px"
		}, 300);
	});
	
	$("#topic_index_right").click(function(){
		topicIndex++;
		if(topicIndex == topicLen-1){
			$("#topic_index_right").css("display", "none");
		}
		if(topicIndex == 1){
			$("#topic_index_left").css("display", "block");
		}
		$("#topic_index_wrap ul").animate({
			"margin-left": (-230*topicIndex)+"px"
		}, 300);
	});
	
	$("#nav_contact, #nav_ct").click(function(){
		$("#floatbox").css("display", "block");
		$("#contact").css("display", "block");
		setPopSize3();
		$(window).bind("resize", function(){
			setPopSize3();
		});
		$("#contact").animate({
			height: "398px"
		},300);
	});
	
	$("#topic_index_more").click(function(){
		$("#floatbox").css("display", "block");
		$("#topic_box article").eq(topicIndex).css("display", "block");
		topicFlIndex = topicIndex;
		if(topicIndex == 0){
			$("#topic_back").css("display", "none");
		}else if (topicIndex == topicLen-1){
			$("#topic_next").css("display", "none");
		}else if (topicIndex == 1){
			$("#topic_back").css("display", "block");
		}else if (topicIndex == topicLen){
			$("#topic_next").css("display", "block");
		}
		$("#topic_cont").css("display", "block");
		setPopSize();
		$(window).bind("resize", function(){
			setPopSize();
		});
		$("#topic_cont").animate({
			height: $("#topic_cont_wrap").height()
		},300);
	});
	
	$(".close img, #float_bg, .close2 img").click(function(){
		$("#topic_cont, #anim_cont, #contact").animate({
			height: 0
		}, 300, function(){
			$("#floatbox").css("display", "none");
			$("#topic_box article").css("display", "none");
			$("#topic_cont").css("display", "none");
			$(".moresection").css("display", "none");
			$("#anim_cont").css("display", "none");
			$("#contact").css("display", "none");
		});
	});
	
	$("#topic_back").click(function(){
		topicFlIndex--;
		if(topicFlIndex == 0){
			$("#topic_back").css("display", "none");
		}
		if(topicFlIndex == topicLen-2){
			$("#topic_next").css("display", "block");
		}
		$("#topic_box article").eq(topicFlIndex+1).css("display", "none");
		$("#topic_box article").eq(topicFlIndex).css("display", "block");
		$("#topic_cont").animate({
			height: $("#topic_cont_wrap").height()
		}, 200);
	});
	
	$("#topic_next").click(function(){
		topicFlIndex++;
		if(topicFlIndex == topicLen-1){
			$("#topic_next").css("display", "none");
		}
		if(topicFlIndex == 1){
			$("#topic_back").css("display", "block");
		}
		$("#topic_box article").eq(topicFlIndex-1).css("display", "none");
		$("#topic_box article").eq(topicFlIndex).css("display", "block");
		$("#topic_cont").animate({
			height: $("#topic_cont_wrap").height()
		}, 200);
	});
	
	$(".more a").click(function(){
		var index = $(".more a").index(this);
		
		$("#floatbox").css("display", "block");
		$(".moresection").eq(index).css("display", "block");
		$("#anim_cont").css("display", "block");
		setPopSize2();
		$(window).bind("resize", function(){
			setPopSize2();
		});		
		$("#anim_cont").animate({
			height: $("#anime_cont_wrap").height()
		},300);
	});
	
	/* オンマウスアニメ */
	if (!isMSIE) { 
		$(".hover").hover(function(){
			$(this).stop();
			$(this).animate({
				opacity: 0.7
			}, 200);
		}, function(){
			$(this).stop();
			$(this).animate({
				opacity: 1
			}, 200);
		});
	}
	
	$(window).keydown(function(e){
		if(e.keyCode == 87){
			$("h1").addClass("secretAnime");
		}
	})
})

function getSecH(){
	var $scrollpoint = $(".scrollpoint");
	var scrlen = $scrollpoint.length
	for(i = 0; i < scrlen; i++){
		secH[i+1] = $scrollpoint.eq(i).offset().top;
	}
	wH = $("#wrapper").height()+39;
}

function scrollSection(num){
	var postop = secH[num];
	$($.browser.webkit ? 'body' : 'html').stop();
	$($.browser.webkit ? 'body' : 'html').animate({
        scrollTop: postop
    }, 1000, "easeInOutCubic", function(){
	});
}

function setSize(){
	h = $(window).height();
}

function setPopSize(){
	w = $(window).width();
	
	var pw = (w < 1000) ? 1000 : w;
	$("#floatbox, #float_bg").width(w);
	$("#floatbox, #float_bg").height(wH);
	var top = (h>$("#topic_cont_wrap").height()) ? ((h-$("#topic_cont_wrap").height()) / 2) : 0;
	var left = (w>763) ? (w-763) / 2 : 0;
	$("#topic_cont").css({
		"top": top+st,
		"left": left
	});
}

function setPopSize2(){
	w = $(window).width();
	
	var pw = (w < 1000) ? 1000 : w;
	$("#floatbox, #float_bg").width(w);
	$("#floatbox, #float_bg").height(wH);
	var top = (h>$("#anime_cont_wrap").height()) ? ((h-$("#anime_cont_wrap").height()) / 2) : 0;
	var left = (w>763) ? (w-763) / 2 : 0;
	$("#anim_cont").css({
		"top": top+st,
		"left": left
	});
}

function setPopSize3(){

	w = $(window).width();
	
	var pw = (w < 1000) ? 1000 : w;
	$("#floatbox, #float_bg").width(w);
	$("#floatbox, #float_bg").height(wH);
	var top = (h>398) ? ((h-398) / 2) : 0;
	var left = (w>398) ? (w-398) / 2 : 0;
	$("#contact").css({
		"top": top+st,
		"left": left
	});
}
