// 

$(function(){
	
	$('html,body').animate({ scrollTop: 0 }, 0);
	$('body').css({ overflow: "hidden" });
	
	var counter = new Counter('#depthNum', 'img/numbers.gif', 5, 6, 20);


	var $window = $(window);
	var $wrap = $("#wrap");
	var totalH = $("#wrap").height();
	var winH = $(window).height()
 //var winH = $("body").height();
	var windowHeight = $window.height();
	var easeType = "easeInOutQuart";
	
	var $all = $('#top,#message,#service,#works,#recruit,#contact');
	
	var top = $("#top");
	var topOp = $("#topOp");
	var topH2 = $("#top h2");
	var topP = $("#top p");
	var cover1 = $("#cover1");
	var cover2 = $("#cover2");
	var cover3 = $("#cover3");
	var cover4 = $("#cover4");
	var catchBg = $("#catchBg");

	var message = $("#message");
	var messageH2 = $("#message h2");
	var messageP = $("#message p");

	var service = $("#service");
	var service2 = $("#service2In");
	var serviceH2 = $("#service h2");
	var serviceP = $("#service p");
	
	var works = $("#works");
	var worksH2 = $("#works h2");
	var worksP = $("#works p");
	
	var recruit = $("#recruit");
	var recruitH2 = $("#recruit h2");
	var recruitP = $("#recruit p");
	
	var contact = $("#contact");
	var contactH2 = $("#contact h2");
	var contactP = $("#contact p");

	var topH = $("#top").height();
 var newsH = $("#news").height();
	var messageH = $("#message").height();
	var serviceH = $("#service").height();
	var worksH = $("#works").height();
	var contactH = $("#contact").height();
	var recruitH = $("#recruit").height();

	var topH2S = $("#top h2 span");
	var messageH2S = $("#message h2 span");
	var serviceH2S = $("#service h2 span");
	var worksH2S = $("#works h2 span");
	var contactH2S = $("#contact h2 span");
	var recruitH2S = $("#recruit h2 span");

	var Flg1 = 0;
	var Flg2 = 0;
	var Flg3 = 0;
	var Flg4 = 0;
	var Flg5 = 0;
	var Flg6 = 0;
	var fScrl = 0;
	var newsOpen = 1;

	var hMenu = $("#hMenu li")
	var headSet = $("#headSet");
	var blogBox = $("#blogBox");

// OP
	if(!$.cookie('the_cookie')){
	//$("body").height(winH)
	$("body").height(winH)
	topOp.height(winH);
	
	cover1.delay(2000).animate({left:728},1300,easeType,function(){})
		cover2.delay(2500).animate({left:728},800,easeType,function(){
			cover3.delay(0).animate({height:0},800,easeType,function(){})
			cover4.delay(0).animate({height:0},800,easeType,function(){
				catchBg.delay(0).animate({width:728},500,easeType,function(){
					topOp.delay(300).animate({height:297},1000,easeType,function(){
						blogBox.animate({right:0},500,easeType);
						$("body").css("overflow","auto");
						$("header").fadeIn(1500,function(){
							if(fScrl == 0){
								NewsClose(500);
								
							}
						});
						})
				})
			})
		})
	
	 //$.cookie('the_cookie', 'the_value');
	}else{
			cover1.hide();
			cover2.hide();
			cover3.hide();
			cover4.hide();
			catchBg.css({width:728})
			topOp.css({height:297})
			$("header").show();
			$("body").css("overflow","auto");
	}
	
	// parallax effect
	
		$all.bind('inview', function (event, visible) {
			if (visible == true) {
			$(this).addClass("inview");
			} else {
			$(this).removeClass("inview");
			}
		});	
	
	function depth(){
		//var totalH = $("#wrap").height();
		var winH = $("body").height();
		var nowS = ($(this).scrollTop());
	
	if(newsOpen == 1){
		var depthNum = (nowS/(totalH-winH))*100;
	}else{
		var depthNum = (nowS/(totalH-winH-newsH))*100;
	}
	//	$("#depthNum").text(depthNum);
		
		var deptPer = Math.floor(depthNum)+"%";
		$("#depthM span").css({height:deptPer})
	}

/*
		x = horizontal position of background
		windowHeight = height of the viewport
		pos = position of the scrollbar
		adjuster = adjust the position of the background
		inertia = how fast the background moves in relation to scrolling
*/

	function newPos(x, windowHeight, pos, adjuster, inertia){
		return x + "% " + (-((windowHeight + pos) - adjuster) * inertia)  + "px";
	}
	
	function Move(){ 
		
/*
				message.css({'backgroundAttachment': scroll});
				service.css({'backgroundAttachment': scroll});
				service2.css({'backgroundAttachment': scroll}); 
				works.css({'backgroundAttachment': scroll}); 
				contact.css({'backgroundAttachment': scroll}); 
				recruit.css({'backgroundAttachment': scroll});
*/
		
		
			var pos = $window.scrollTop();
			// message
			
			
			if(message.hasClass("inview")){
				message.css({'backgroundPosition': newPos(50, windowHeight, pos, 1400, 0.2)}); 
			}
	
			// service
			if(service.hasClass("inview")){
				service.css({'backgroundPosition': newPos(50, windowHeight, pos, 2300, 0.2)}); 
				service2.css({'backgroundPosition': newPos(100, windowHeight, pos, 2000, 0.2)}); 
			}
	
			// works
			if(works.hasClass("inview")){
				works.css({'backgroundPosition': newPos(50, windowHeight, pos, 3600, 0.2)}); 
			}
	
			// contact
			if(contact.hasClass("inview")){
				contact.css({'backgroundPosition': newPos(50, windowHeight, pos,4800, 0.2)}); 
			}
	
			// recruit
			if(recruit.hasClass("inview")){
				recruit.css({'backgroundPosition': newPos(50, windowHeight, pos, 5750, 0.1)}); 
			}
		}
	
	/*
	$window.scroll(function(){
	});	
	*/
	function hSet(){
			var pos = $window.scrollTop();
			var topH = 297;
			var hFlg = 0;
		if(pos > topH){
			if(hFlg == 0){
				hFlg = 1;
				headSet.animate({right:0},800,easeType,function(){
		//			
					hFlg = 0;
				})
				
			}
		}else	{
			if(hFlg == 0){
				hFlg = 1;
				headSet.stop(true,false).animate({right:-426},400,easeType,function(){
					hFlg = 0;
	//				blogBox.stop(true,false).animate({right:-82},200,easeType);
				})
			}
		
		}
	}
	
	function nowCK(){
	var pos = $window.scrollTop();
	
	
	if(pos < topH){
		hMenu.removeClass("now");

	}else if(pos >= topH && pos < (topH+messageH)){
		hMenu.removeClass("now");
		hMenu.eq(1).addClass("now");
		if(Flg1 == 0){
			Flg1 = 1;
			messageH2S.stop(true,false).animate({width:"100%"},300,easeType)
		}
	
	}else if(pos >= (topH+messageH) && pos < (topH+messageH+serviceH)){
		hMenu.removeClass("now");
		hMenu.eq(2).addClass("now");
		if(Flg2 == 0){
			Flg2 = 1;
			serviceH2S.stop(true,false).animate({width:"100%"},300,easeType)
		}
	
	}else if(pos >= (topH+messageH+serviceH) && pos < (topH+messageH+serviceH+worksH)){
		hMenu.removeClass("now");
		hMenu.eq(3).addClass("now");
		if(Flg3 == 0){
			Flg3 = 1;
			worksH2S.stop(true,false).animate({width:"100%"},300,easeType)
		}
	
	}else if(pos >= (topH+messageH+serviceH+worksH) && pos < (topH+messageH+serviceH+worksH+contactH)){
		hMenu.removeClass("now");
		hMenu.eq(4).addClass("now");
		if(Flg4 == 0){
			Flg4 = 1;
			contactH2S.stop(true,false).animate({width:"100%"},300,easeType)
		}
	
	}else if(pos >= (topH+messageH+serviceH+worksH+contactH) && pos < (topH+messageH+serviceH+worksH+contactH+recruitH)){
		hMenu.removeClass("now");
		hMenu.eq(5).addClass("now");
		if(Flg5 == 0){
			Flg5 = 1;
			recruitH2S.stop(true,false).animate({width:"100%"},300,easeType)
		}
	
	}else{
		hMenu.removeClass("now");
	}


/*
	var messageH2S = $("#message h2 span");
	var serviceH2S = $("#service h2 span");
	var worksH2S = $("#works h2 span");
	var contactH2S = $("#contact h2 span");
	var recruitH2S = $("#recruit h2 span");
		*/	
	}
	
		
	$window.resize(function(){
		if(navigator.userAgent.indexOf('iPhone') != -1 || navigator.userAgent.indexOf('iPad') != -1){
			depth();
			nowCK();
			hSet();
			fScrl = 1;
		}else{
			nowCK()
			hSet();
			Move();
			depth();
			fScrl = 1;
		}
	
	});		
	
	$window.bind('scroll', function(){
		if(navigator.userAgent.indexOf('iPhone') != -1 || navigator.userAgent.indexOf('iPad') != -1){
			nowCK();
			depth();
			hSet();
			fScrl = 1;
	}else{
			nowCK()
			hSet();
			Move();
			depth();
			fScrl = 1;
		}
	});
	

// message
	
	var $btnNext = $("#messageIn .btn-next");
	var $btnPrev = $("#messageIn .btn-prev");
	var $mboxIn = $("#mboxIn");
	var MSGpos =0;
	var MSGlock = "off";
	
	$btnNext.click(function(){
		if(MSGlock = "off"){
		MSGlock = "on"
		MSGpos = MSGpos+300;
		var targetPos = "-"+MSGpos+"px";
		$mboxIn.animate({top:targetPos},1000,easeType,function(){
		MSGlock = "off"
		})
		}
	});
	
	$btnPrev.click(function(){
		if(MSGlock = "off"){
		MSGlock = "on"
		MSGpos = MSGpos-300;
		var targetPos = "-"+MSGpos+"px";
		$mboxIn.animate({top:targetPos},1000,easeType,function(){
		MSGlock = "off"
		})
		}
	});

// SERVICE


	var $sbox = $("#sbox");
	var sPos0 = 380;
	var $sboxBack = $("#sbox .back img");
	var $sboxDetail = $("#sbox .detail");
	var $popFigBox = $("#popFigBox div");
	var $service2In = $("#service2In");
	
 var sbox1H = "-"+($("#sbox1").height()/2)+"px";
 var sbox2H = "-"+($("#sbox2").height()/2)+"px";
 var sbox3H = "-"+($("#sbox3").height()/2)+"px";
 var sbox4H = "-"+($("#sbox4").height()/2)+"px";
 var sbox5H = "-"+($("#sbox5").height()/2)+"px";
 var sbox6H = "-"+($("#sbox6").height()/2)+"px";

	$("#sbox1").css("margin-top",sbox1H);
	$("#sbox2").css("margin-top",sbox2H);
	$("#sbox3").css("margin-top",sbox3H);
	$("#sbox4").css("margin-top",sbox4H);
	$("#sbox5").css("margin-top",sbox5H);
	$("#sbox6").css("margin-top",sbox6H);
	
	$(".snav li").click(function(){
		if(!$(this).hasClass("current")){
					foFig();
					var sPos = "-"+($(this).index()+1)*sPos0+"px";
					$sbox.animate({left:sPos},1000,easeType,function(){
					})
		}
	})

	$sboxBack.click(function(){
						foFig();
						$sbox.animate({left:0},1000,easeType,function(){
				})
	})

	$sboxDetail.click(function(){
				var num = $(this).attr('rel');

				$service2In.animate({opacity:0.2},500,function(){
					if(num == 1){
						$("#fig-1").delay(200).fadeIn(1000);
					}else if(num == 2){
						$("#fig-2").delay(200).fadeIn(1000);
					}else if(num == 3){
						$("#fig-3").delay(200).fadeIn(1000);
					}else if(num == 4){
						$("#fig-4").delay(200).fadeIn(1000);
					}
				})
	})

	$popFigBox.click(function(){
		foFig();
	})

	function foFig(){
		$popFigBox.fadeOut(500);
		$service2In.animate({opacity:1},500)
	}

// work
	

	var $workBox = $("#workBox");
	var $workAll = $("#workAll");
	var $workNext = $(".workNext");
	var $workPrev = $(".workPrev");
	var $moreInfo = $(".moreInfo");
	var $moreInfoImg = $(".moreInfo img");
	var moreFlg = 0;

	var wPos0 = 800;
	
 $workNext.click(function(){
		var num = $(this).attr('rel');
		var wPos = "-"+(num*wPos0)+"px";
		$workAll.animate({left:wPos},1000,easeType,function(){})
	})
 $workPrev.click(function(){
		var num = $(this).attr('rel');
		var wPos = "-"+((num*wPos0)-(wPos0*2))+"px";
		$workAll.animate({left:wPos},1000,easeType,function(){})
	})
	
 $moreInfo.click(function(){
		if(moreFlg == 0){
			$workBox.animate({height:644},1000,easeType,function(){
				$moreInfoImg.attr("src","img/works/workMore2.gif");
				moreFlg = 1;
			})
		}else{
			$workBox.animate({height:529},1000,easeType,function(){
				$moreInfoImg.attr("src","img/works/workMore.gif");
				moreFlg = 0;
			})
		}
	})
	

	
	//SET
	$("#linkedIn").setTube("6MYHkJbxXKs");
	$("#worldCruise").setTube("W9T4tfajkDw");
	$("#babytoi").setTube("hcXqvZa8yng");
	
	$(".can01").click(function(){
			$("#linkedIn").resetTube();
	});
	$(".can02").click(function(){
			$("#worldCruise").resetTube();
	});
	$(".can03").click(function(){
			$("#babytoi").resetTube();
	});
	

// contact


	var $cbox = $("#cbox");
	var cPos0 = 380;
	var $cboxBack = $("#cbox .back img");
	var $contact2In = $("#contact2In");

 var cbox1H = "-"+($("#cbox1").height()/2)+"px";
 var cbox2H = "-"+($("#cbox2").height()/2)+"px";
 var cbox3H = "-"+($("#cbox3").height()/2)+"px";

	$("#cbox1").css("margin-top",cbox1H);
	$("#cbox2").css("margin-top",cbox2H);
	$("#cbox3").css("margin-top",cbox3H);
	
	$(".cnav li.jump").click(function(){
		if(!$(this).hasClass("current")){
					var cPos = "-"+($(this).index()+1)*sPos0+"px";
						$cbox.animate({left:cPos},1000,easeType,function(){
					})
		}
	})

	$cboxBack.click(function(){
						$cbox.animate({left:0},1000,easeType,function(){
				})
	})

$(function(){
    $('#slideShow img:gt(0)').hide();
    $('#slideShow img').removeClass("hide")
    setInterval(function(){$('#slideShow :first-child').fadeOut("slow").next('img').fadeIn("slow").end().appendTo('#slideShow');}, 6500);
});


// スムーススクロール

   $('a[href^=#]').click(function() {
      var speed = 1000;
      var href= $(this).attr("href");
      var target = $(href == "#" || href == "" ? 'html' : href);
      var position = target.offset().top;
      $($.browser.safari ? 'body' : 'html').animate({scrollTop:position}, speed, easeType);
      return false;
   });

// NEWS

	$("#newsClose").click(function(){
		NewsClose(0);
	})
	
	
	var self = this;
	var NewsClose= function(delayT){
		
		$("#newsOpen").delay(delayT).fadeIn();
		$("#news").delay(delayT).animate({
			height:0,
			},1000,easeType,function(){
			newsOpen= 0;
			//alert(self)
			$(window).trigger("resize");
			})
	}
	
	$("#newsOpen").click(function(){
		$("#newsOpen").fadeOut();
		$("#news").animate({
			height:newsH,
			},1000,easeType,function(){
			newsOpen= 1;	
			$(window).trigger("resize");
			})
	})


// ------------------------- end of function ----------------------------
});


// setTube

$.fn.setTube = function(_movieid){
	var self = $(this);
	var imgTag = self.html();
	var	movTag = '<iframe width="800" height="450" src="http://www.youtube.com/embed/'+ _movieid + '?feature=player_embedded&autoplay=1" frameborder="0" allowfullscreen></iframe>'
	self.data({ 'img':imgTag, 'mov':movTag }).setTubeMouse();
}



$.fn.setTubeMouse = function(){
	var self = $(this);
	if(!self.data('mov')) return;
	self
	.find('img')
	.css({'cursor':'pointer'})
	.bind('click', function(){
		$(this).stop(true,false).fadeOut(500, function(){
			self.html(self.data('mov'));
		});
	});
}



$.fn.resetTube = function(){
	var self = $(this);
	var imgTag = self.data("img");
	var movTag = self.data("mov");
	if(imgTag && movTag && self.find('iframe').size()) 
	self
	.css({'display':'none'})
	.html(imgTag)
	.fadeIn(500)
	.setTubeMouse()
}


