$(function() {
	//$("#search_area").hide();
	
	$("#tool").click(function () {
		$(this).toggleClass('opened');
		$("#search_area").slideToggle("fast");
	});
	
	if ($('#footer')) {
		
		 //$("body").css({"overflow":"auto"});
		fitFooter();
		$(window).bind('resize', function() {
			$("body").css({"overflow":"hidden"});
			fitFooter();
	   		/*var timer = setInterval(function () {
			$("body").css({"overflow":"auto"});
        	}
  		  , 3000);*/
		});

	}
});

//fitFooter

function fitFooter() {
	var contentHeight = $('#contents').outerHeight() + $('#footer_contents').height()-35;
	if ($(window).height() > contentHeight){
		 $('#footer').css({'height':$(window).height() - contentHeight});
		 //$("body").css({"overflow":"hidden"});
		 }
	if ($(window).height() < contentHeight+100){
		 $('#footer').css({'height':"100px"});
		 $("body").css({"overflow":"auto"});
	 }
}

//var clickFlag = false;

//$.fx.interval = 8;//framerate

/*function toolbtn(){
	
	var posi = $("#tool").css("backgroundPosition");
		
	if(clickFlag){
		clickFlag = false;
		$("#tool").css({"background-position":"0px 0px"});
	} else {
		clickFlag = true;
		$("#tool").css({"background-position":"0px -29px"});
	}
}*/

/*--------------------------------------------------------------
* 検索エリア
*-------------------------------------------------------------*/
/*function showhideToolBar(){
	$("#search_area").slideToggle("fast");
}*/

/*--------------------------------------------------------------
* 作品集の表示きりかえ
*-------------------------------------------------------------*/
function switchView(num){
	switch(num){
		case 0:
			$(".select_mn ul").removeClass("worklist_2").addClass("worklist_1");
			
			$("#chview #view1 a").addClass('selected');
			//$("#chview #view1 a").css({"background-position":"0px -19px"});
			$("#chview #view2 a").removeClass('selected');
			//$("#chview #view2 a").css({"background-position":"0px 0px"});
		break;
		case 1:
			$(".select_mn ul").removeClass("worklist_1").addClass("worklist_2");
			
			$("#chview #view1 a").removeClass('selected');
			//$("#chview #view1 a").css({"background-position":"0px 0px"});
			$("#chview #view2 a").addClass('selected');
			//$("#chview #view2 a").css({"background-position":"0px -19px"});
		break;
	}
	
	prd_box_resize();//リストの高さ調整
	
	$('.select_mn ul li a').hide();										 
	startFadelist(".select_mn ul li a");
}


/*--------------------------------------------------------------
* ナビスクロール
*-------------------------------------------------------------*/

function naviScroll(){
	var l_h = $(".cont_body .lf").height();
	var r_h = $(".cont_body .ri").height();
	//gttyalert($("body").height() - 170 - getScrollPosition() - $(".cont_body .ri").height())
	
	if(l_h > r_h && (getScrollPosition() + r_h) < (l_h + 200)){	
		if(getScrollPosition() > 200){
			$(".cont_body .ri").stop().animate({"top":getScrollPosition() - 150}, { duration: "eaesInOutQuart" });
		}else{
			$(".cont_body .ri").stop().animate({"top":0}, { duration: "eaesInOutQuart" });
		}
	}
}

function setScrollMn(){
	$(window).scroll(function() {				 
		//サブナビゲーションスクロール
		naviScroll();
	});
}

function setScrollAyumi(){
	$(window).scroll(function() {
		moveAyumi();
	});
}

/*--------------------------------------------------------------
* スクロール位置取得
*-------------------------------------------------------------*/

function getScrollPosition() {
	return (document.documentElement.scrollTop || document.body.scrollTop);
}

/*--------------------------------------------------------------
* 歩み年表
*-------------------------------------------------------------*/
var offsetArr = new Array();
var cflag = false;

function slectAyumi(){
	setoffsetArr();
	
	//$("#navi_area").after("<p id='debug' style='padding:10px;background-color:#eee'></p>");
	
	moveAyumi();
	
	/*$("#sv1 ul li a").click(function(){
		var index = $(this).parent().index();
		cflag = true;
		activeTable(index);
	});*/
}

function setoffsetArr(){
	for(var i=0;i<$(".ayumi_table div table").length;i++){
		var tr = $(".ayumi_table div table:eq(" + i + ")");
		var offset = tr.offset();
		offsetArr[i] = Math.floor(offset.top);
		//console.log(offset.top);
	}
}

var tr = 0;
var scrolly = 0;
var term_now = 0;

function moveAyumi(){
	
	//setoffsetArr();
	
	var baseH = $(document).height() - $(window).height();
	var scH = getScrollPosition() + offsetArr[0];

	var historyH = $('.ayumi_box').height();
	
	var arrleng = offsetArr.length-1;
	var re = Math.floor(Number(scH/baseH)*arrleng);
	
	scrolly = getScrollPosition();
	//console.log(historyH);
		
	/*$("#debug").after().html("Debug Area ----------------------------------- "
							+ "<br />" + re + " / " + offsetArr
							+ "<br />" + scH + " / " + serchArr(scH)
							+ "<br /> ----------------------------------- Debug Area");*/
	
	if(getScrollPosition() >= offsetArr[0]){
		if(!cflag){
			new_term = serchArr(scH - historyH);
			//console.log(new_term + " | " + term_now);
			if (new_term != term_now) activeTable(new_term);
		}
	} else {
		activeTable(0);
	}
}


function serchArr(num){
	
	var result;
	
	for(var i in offsetArr){
		if(i != (offsetArr.length - 1)){
			if(offsetArr[i] < num && num < offsetArr[Number(i)+1]){
				result = i;
				break;
			}else if(offsetArr[i] > num){
				result = 0;
			}else{
				result = (offsetArr.length - 1);
			}
		}
	}
	
	return result;
}

/*^^ 歩み色付け用 ^^*/

function activeTable(num){
	//alert(num)
	
	term_now = num;
	
	for(var i in offsetArr){
		if(i == num ){
			$(".ayumi_table #a" + num)
				.removeClass("gray")
				.addClass("color")
				.stop().animate({"filter":"alpha(opacity=100)","-moz-opacity":1,"opacity":1});
		}else{
			$(".ayumi_table #a" + i)
				.addClass("gray")
				.removeClass("color")
				.stop().animate({"filter":"alpha(opacity=50)","-moz-opacity":0.5,"opacity":0.5});
		}
	}
}


/*^^ 歩み色付け用 ^^*/

function activeTable_click(num){
			
	for( var i in offsetArr){
		if(i == num ){
			$(".ayumi_table #a" + num ).removeClass("gray");
			$(".ayumi_table #a" + num ).addClass("color");
			$(".ayumi_table #a" + num ).stop().animate({"filter":"alpha(opacity=100)","-moz-opacity":1,"opacity":1},{duration:"slow",
				easing:"linear",
				complete:function(){cflag = false;}});
		}else{
			$(".ayumi_table #a" + i ).addClass("gray");
			$(".ayumi_table #a" + i ).removeClass("color");
			$(".ayumi_table #a" + i ).stop().animate({"filter":"alpha(opacity=50)","-moz-opacity":0.5,"opacity":0.5});
		}
	}
}



/*--------------------------------------------------------------
* トップアニメーション
*-------------------------------------------------------------*/
var timerID;

function topAnime(){
	var loadFlag = false;
	
	//ローダー
	$("body").css({"width":$(window).width(),
				   "height":$(window).height()});
					
					
	$(window).resize(function() {
		if(!loadFlag){
			$("body").css({"width":$(window).width(),
				   		   "height":$(window).height()});
		}
	});
	
	//$("#contents_top").hide();
	//$("#header").hide();
	//$("#header").css({"top":-67});
	//$("#footer_top").hide();
	//$("#news_area").hide();
	
	//情報エリア
	//$(".top_p_info_cont").css({"height":0, "overflow":"hidden"});
	$(".top_p_info_title").css({"padding-top":0,"padding-bottom":0,"border-bottom-width":"0px","backgroundPosition":"-7px -3px"});
	
	$(".top_p_info_ft").height(5);
	$(".top_p_info_hd").height(5);
	
	$("#contents_top .top_p_info").css({"margin-top":200});
	
	$(function() {
		$("#contents_top ul li img").ready(function() {
			//topContents();
			//表示送らせ。
			timerID = setTimeout("delayAnime()", 2500);
			
		});
	});
}

function delayAnime(){
	
	clearTimeout(timerID);
	
	loadFlag = true;
		
	$("body").css({"background-image":"none"});
	
	//コンテンツ表示
	$("#top #header").delay(500).animate({"top":0}, {duration:"eaesInOutQuart"});
	$("#news_area").delay(1000).fadeIn("eaesInOutQuart");
	$("#footer_top").delay(1000).fadeIn("eaesInOutQuart");
	
	$("#contents_top").delay(1000).fadeIn("eaesInOutQuart",function(){
		
		startFade();
		
		$(".top_p_info").hover(function(){
			//over event
			stopFade();
			
			var userAgent = window.navigator.userAgent.toLowerCase();
			if (userAgent.indexOf("msie") > -1) {
				$(".top_p_info_title").stop().animate({"padding-top":10,"padding-bottom":10,"border-bottom-width":"1px","backgroundPosition":"-7px -15px"},{duration: "eaesInOutQuart"});
				$(".top_p_info_cont").stop().animate({"height":160},{duration: "eaesInOutQuart"});
				$("#contents_top .top_p_info").stop().animate({"margin-top":15},{duration: "eaesInOutQuart"});
				$(".top_p_info_ft").stop().animate({"height":14},{duration: "eaesInOutQuart"});
				$(".top_p_info_hd").stop().animate({"height":8},{duration: "eaesInOutQuart"});
			}else{
				$(".top_p_info_title").stop().animate({"padding-top":10,"padding-bottom":10,"border-bottom-width":"1px","background-position":"-7px -15px"},{duration: "eaesInOutQuart"});
				$(".top_p_info_cont").stop().animate({"height":160},{duration: "eaesInOutQuart"});
				$("#contents_top .top_p_info").stop().animate({"margin-top":0},{duration: "eaesInOutQuart"});
				$(".top_p_info_ft").stop().animate({"height":14},{duration: "eaesInOutQuart"});
				$(".top_p_info_hd").stop().animate({"height":8},{duration: "eaesInOutQuart"});
			}
		},function(){
			//out event
			$(".top_p_info_title").stop().animate({"padding-top":0,"padding-bottom":0,"border-bottom-width":0,"backgroundPosition":"-7px -3px"},{duration: "eaesInOutQuart"});
			
			$("#contents_top .top_p_info").stop().animate({"margin-top":200},{duration: "eaesInOutQuart"});
			$(".top_p_info_cont").stop().animate({"height":0},{duration: "eaesInOutQuart"});
			$(".top_p_info_ft, .top_p_info_hd").stop().animate({"height":5},{duration: "eaesInOutQuart"});
			//$("").stop().animate({"height":5},{duration: "eaesInOutQuart"});
			
			startFade();
		});
	});
}

/*--------------------------------------------------------------
* リストフェード
*-------------------------------------------------------------*/
function startFadelist(trg){
	$(trg).each(function(i){
		$(this).delay(50*i+200).fadeIn("eaesInOutQuart");
	});
}

/*--------------------------------------------------------------
* ブラウザチェック
*-------------------------------------------------------------*/
function checkbrowsIE(){
	var userAgent = window.navigator.userAgent.toLowerCase();
	var appVersion = window.navigator.appVersion.toLowerCase();

	var result;

	if (userAgent.indexOf("msie") > -1) {
		if (appVersion.indexOf("msie 8.0") > -1) {
			//alert("IE8");
			result = true;
		}
		else if (appVersion.indexOf("msie 7.0") > -1) {
			result = true;
		}
		else if (appVersion.indexOf("msie 6.0") > -1) {
			//alert("IE6");
			result = false;
		}
		else {
			//alert("Unknown");
			result = true;
		}
	}else{
			result = true;
	}
	
	return result;
}

/*--------------------------------------------------------------
* ニュースプルダウン
*-------------------------------------------------------------*/
function pulldownlist(trg){
	
	var h = $(trg).css("height");
	
	$(trg).css({"height":"18px"});
	$(trg).hover(
		function(){$(this).stop().animate({"height":h}, { duration: "eaesInOutQuart" });}
		,
		function(){$(this).stop().animate({"height":"18px"}, { duration: "eaesInOutQuart" });}
		);
}

/*--------------------------------------------------------------
* カラーボックスアイコン設定
*-------------------------------------------------------------*/
$(document).ready(function(){
		var userAgent = window.navigator.userAgent.toLowerCase();
		var appVersion = window.navigator.appVersion.toLowerCase();
		
		$(".iframe img").after("<div class='cbicon'></div>");
		
		$(".iframe").each(function(i){
			//$(".iframe").width($(".iframe img").width());
			$(".iframe").eq(i).height($(".iframe:eq("+i+") > img").height());
			$(".iframe").eq(i).width($(".iframe:eq("+i+") > img").width());
			
			if(checkbrowsIE()){
				$(".cbicon").eq(i).css({"top":5, "left":$(".iframe:eq("+i+") > img").width() -22 });
				//$(".iframe img").width -22
			}else{
				/*表示調整中
				*/
					if (appVersion.indexOf("msie 7.0") > -1) {
						$(".cbicon").eq(i).css({"margin-top":-$(".iframe:eq("+i+") > img").height() + 5, "top":-$(".iframe:eq("+i+") > img").height() + 5, "left":$(".iframe:eq("+i+") > img").width() - 22 });
					}
					else if (appVersion.indexOf("msie 6.0") > -1) {
						//alert("IE6");
						$(".cbicon").eq(i).css({"top":-$(".iframe:eq("+i+") > img").height() + 5, "left":$(".iframe:eq("+i+") > img").width() - 22 });
					}
					else {
						$(".cbicon").eq(i).css({"top":-$(".iframe:eq("+i+") > img").height() + 5, "left":$(".iframe:eq("+i+") > img").width() - 22 });
					}
			}
		})
});


