// JavaScript Document

/*=========================================================
	
	毎日更新ブログ
	
=========================================================*/
var blogAreaN = 0;
function blogArea(){
	var blogLen = $("#blog-area ul li").length;
	if(blogLen>1){
		if(blogAreaN<blogLen-1){
			$("#blog-area li.blog-now").attr("class","").fadeOut(500).next().attr("class","blog-now").fadeIn(500);
			blogAreaN++;
		}else{
			$("#blog-area li.blog-now").fadeOut(500).attr("class","");
			$("#blog-area li:first").fadeIn(500).attr("class","blog-now");
			blogAreaN = 0;
		};
	};
};



/*=========================================================
	
時計とIEのアラート
	
=========================================================*/

function init(){
  var canvas = document.getElementById('clockArea');
  /*
    次のif条件式を書くことにより、
    canvas要素をサポートしていないブラウザからの、
    スクリプト実行を防ぐことができます。
  */
  
  if(canvas.getContext) {
    clock();
  //1秒おきにclock関数を実行するようタイマーを設定
    setInterval('clock()',1000);
  }
  //canvas要素未サポートのブラウザの場合は、アラートウィンドウが立ち上がります。 
  else {
	var ieFlag = true;
	//$('html').css({overflow:'hidden'});
	if(ieFlag){
	  if(!$.cookie('core-tech')){
		$('body').prepend('<div id="ieAlert-back"></div>');
		var ieAlertBack = $('#ieAlert-back');
		$('body','html').css({height: '100%', width: '100%'});
		ieAlertBack.css({'height':$(document).height()});
		$('body').prepend(
			'<div id="ieAlert-window">' +
			'<p><strong>コアテックのコーポレートサイトへようこそ!!</strong><br />このページをより快適に、より楽しんでいただく為に、<br />「Firefox」「Google Chrome」<br />いずれかのブラウザでご覧いただく事をお勧めします。</p>' +
			'<p id="ieAlert-btn"><a href="javascript:void(0);">ENTER</a></p>' +
			'<ul><li><a href="http://mozilla.jp/firefox/">Firefoxをダウンロードする</a></li><li><a href="http://www.google.co.jp/chrome/intl/ja/landing_ch.html">Google Chromeをダウンロードする</a></li</ul>'+
			'</div>'
		);
		var ieAlertWindow = $('#ieAlert-window');
		ieAlertWindow.css('top',($(document).scrollTop() + $(window).height()/2) + "px");
		$.cookie('core-tech',true, { expires: 1 });
    	ieAlertBack.show();
    	ieAlertWindow.fadeIn('slow');
	  }
	}//if ieFlag
	$('#ieAlert-btn a').click(function () {
		ieAlertEliminate();
		ieFlag = false;
	});
	function ieAlertEliminate() {
		$('html').css({overflow:''});
		ieAlertBack.fadeOut('fast');
		ieAlertWindow.fadeOut('fast');
		//$("body","html").css({height: "auto", width: "auto"});
		setTimeout('ieAlertRemove()',500);
	};
	function ieAlertRemove() {
		ieAlertBack.remove();
		ieAlertWindow.remove();
	};
	
	var staffLink =[];
	staffLink["staffLink"] = $(".staffLink a")
	staffLink["staffLink"].each(function(i){
		staffLink["staffLink"].eq(i).attr("href","staff-list-forOld.html");
	});
  }
}
function clock(){
  //Dateオブジェクトを生成し変数nowへ代入
  var now = new Date(); 
  var ctx = document.getElementById('clockArea').getContext('2d');
  ctx.save();
  ctx.clearRect(0,0,100,100);
  ctx.translate(30,30);
  ctx.scale(1.5,1.5);
  ctx.rotate(-Math.PI/2);
  ctx.strokeStyle = "#fff";
  ctx.fillStyle = "#fff";
  ctx.lineCap = "round";

  //時間を示すマークの描画処理
  ctx.save();
  ctx.beginPath();
  for (var i=0;i<12;i++){
    ctx.rotate(Math.PI/6);
	ctx.lineWidth = 1;
    ctx.moveTo(14,0);
    ctx.lineTo(15,0);
  }
  ctx.stroke();
  ctx.restore();

  var sec = now.getSeconds();
  var min = now.getMinutes();
  var hr  = now.getHours();
  hr = hr>=12 ? hr-12 : hr;
  ctx.fillStyle = "#fff";

  //時針を描画する処理
  ctx.save();
  ctx.rotate( hr*(Math.PI/6) + (Math.PI/360)*min + (Math.PI/21600)*sec )
  ctx.lineWidth = 1.8;
  ctx.beginPath();
  ctx.moveTo(-2,0);
  ctx.lineTo(8,0);
  ctx.stroke();
  ctx.restore();

  //分針を描画する処理
  ctx.save();
  ctx.rotate( (Math.PI/30)*min + (Math.PI/1800)*sec )
  ctx.lineWidth = 1.5;
  ctx.beginPath();
  ctx.moveTo(-3,0);
  ctx.lineTo(12,0);
  ctx.stroke();
  ctx.restore();
  
  //秒針を描画する処理 
  ctx.save();
  ctx.rotate(sec * Math.PI/30);
  ctx.strokeStyle = "#fff";
  ctx.fillStyle = "#fff";
  ctx.lineWidth = 1;
  ctx.beginPath();
  ctx.moveTo(-3,0);
  ctx.lineTo(10,0);
  ctx.stroke();
  ctx.restore();

  ctx.restore();
}

function yearDate(){
	var yD= new Date();
	y = yD.getFullYear();
	m = yD.getMonth()+1;
	d = yD.getDate();
	$(".date-time p .year").text(y+"年");
	$(".date-time p .date").text(m+"月"+d+"日");
};

/*=========================================================
	
グラフ
	
=========================================================*/

function graph(){
	//分母=denominator　分子=numerator
	g = $("#graph dl").size();
	var graphItem = new Array(g);
	var numerator = new Array();
	var ratio = new Array();
	var ratioAll = 0;
	var graphDl = new Array();
	var thisLen;
	
	for(i=0;i<=g;i++){
		$("#graph dl").eq(i).attr("class","graph-dl"+i);
		thisLen = $("#graph dl").eq(i).children("dd").length-1;
		//alert(thisLen);
		graphItem['graphItem'] = $("#graph dl.graph-dl"+i+" dd.graph-item");
		var denominator = 0;
		graphItem['graphItem'].each(function(i){
			numerator[i] = Number(graphItem['graphItem'].eq(i).text());
			denominator += numerator[i];
		});
		graphItem['graphItem'].each(function(i){
			ratio[i] = Math.round(numerator[i]/denominator*100);
			ratioAll += ratio[i];
			
			/*ratioAllが101以上かつratio[i]が0でない時
			---------------------------------------------------*/
			if(ratioAll > 100 && ratio[i]!=0){
				ratio[i] -= 1;
			}
			/*最後のddかつratioAllが100未満の時
			---------------------------------------------------*/
			else if(thisLen == i && ratioAll < 100){
				if(ratio[i] == 0){
					graphItem['graphItem'].eq(i-1).css("width",ratio[i-1]+1+"%");
					graphItem['graphItem'].eq(i-1).text(ratio[i-1]+1+"%");
				}else{
					ratio[i] += 1;
				}
			}
			graphItem['graphItem'].eq(i).css("width",ratio[i]+"%");
			graphItem['graphItem'].eq(i).text(ratio[i]+"%");
		});
	}
}





$(function(){
	
	/*=========================================================
	
	dt dd 高さ揃え
	
	=========================================================*/
	var eachDt = [];
	var eachDd = [];
	var eachDdH = [];
	eachDt["ta"] = $(".hUniform dt");
	eachDd["ta"] = $(".hUniform dt").next("dd");
	eachDt["ta"].each(function(i){
		eachDdH[i] = eachDd["ta"].eq(i).height();
		if(eachDdH[i]>eachDt["ta"].eq(i).height()){
			eachDt["ta"].eq(i).height(eachDdH[i]);
		}
	});
	
	/*=========================================================
	
	のびーるマウスオーバー
	
	=========================================================*/
	var extend = [];
	extendWidth = [];
	extend["extend"] = $("a.extend");
	
	extend["extend"].each(function(i){
		extendWidth[i] = extend["extend"].eq(i).width();
	});
	
	extend["extend"].find("span").css("display","none");
	
	$("a.extend").hover(function(){
		n = extend["extend"].index(this);
		thisWidth = $(this).css("width");
		$(this).stop().animate({"width":extendWidth[n]+5},{duration:200,complete:function(){$(this).find("span").css("display","inline").animate({"opacity":1},{duration:100})}})
	},function(){
		$(this).find("span").css("display","none");
		$(this).stop().animate({"width":thisWidth},{duration:100,complete:function(){$(this).attr("style","");}});
	});
	
	
	/*=========================================================
	
	ツールチップ
	
	=========================================================*/
	var thistip = [];
	var thistitle = [];
	thistip['thistip'] = $(".toolTip");
	thistip['thistip'].each(function(i){
		thistitle[i] = thistip['thistip'].eq(i).attr("title");
		thistip['thistip'].eq(i).attr("title","");
	});
	thistip['thistip'].hover(function(e) {
		mouseCoordinateX = e.pageX;
		mouseCoordinateY = e.pageY;
		windowCoordinateY = window.innerHeight-300;
		//alert(mouseCoordinateX);
		if(mouseCoordinateY-windowCoordinateY<0){
			if(mouseCoordinateX>300){
				n = thistip['thistip'].index(this);
				$(this).after(
				'<div class="toolTipWrapperBottomLeft"><div class="toolTipMid">'
				+thistitle[n]+'</div></div>');
				$(this).parent().css("position","relative");
				thisHeight = $(this).next('.toolTipWrapperBottomLeft').height();
				$(this).next('.toolTipWrapperBottomLeft').css({"bottom":-thisHeight,"left":"-60px"}).fadeIn(200);
			}else{
				n = thistip['thistip'].index(this);
				$(this).after(
				'<div class="toolTipWrapperBottomRight"><div class="toolTipMid">'
				+thistitle[n]+'</div></div>');
				$(this).parent().css("position","relative");
				thisHeight = $(this).next('.toolTipWrapperBottomRight').height();
				$(this).next('.toolTipWrapperBottomRight').css({"bottom":-thisHeight,"right":"-30px"}).fadeIn(200);
			}
		}else{
			if(mouseCoordinateX>300){
				n = thistip['thistip'].index(this);
				$(this).after(
				'<div class="toolTipWrapperTopLeft"><div class="toolTipMid">'
				+thistitle[n]+'</div></div>');
				$(this).parent().css("position","relative");
				thisHeight = $(this).next('.toolTipWrapperTopLeft').height();
				$(this).next('.toolTipWrapperTopLeft').css({"top":-thisHeight,"left":"-60px"}).fadeIn(200);
			}else{
				n = thistip['thistip'].index(this);
				$(this).after(
				'<div class="toolTipWrapperTopRight"><div class="toolTipMid">'
				+thistitle[n]+'</div></div>');
				$(this).parent().css("position","relative");
				thisHeight = $(this).next('.toolTipWrapperTopRight').height();
				$(this).next('.toolTipWrapperTopRight').css({"top":-thisHeight,"right":"-80px"}).fadeIn(200);				
			}
		}
	},function() {
		$('.toolTipWrapperBottomRight').remove();
		$('.toolTipWrapperBottomLeft').remove();
		$('.toolTipWrapperTopLeft').remove();
		$('.toolTipWrapperTopRight').remove();
	});
	
	/*=========================================================
	
	スライド
	
	=========================================================*/
	
	var page = new Array();
	var pageH = new Array();
	var pageOuterHeight = new Array();
	var pageHeightHigher = 0;
	var maxH = new Array();
	var h = $($.browser.safari ? 'body' : 'html')
	page['page'] = $(".sec-center .category");
	page['page'].each(function(i){
		page['page'].eq(i).children(".page").each(function(){
			pageOuterHeight = $(this).outerHeight(true);
			if(pageOuterHeight > pageHeightHigher){
				pageHeightHigher = pageOuterHeight;
			};});
			maxH[i] = pageHeightHigher;
			pageHeightHigher=0;
		});
	//logo部分のリンク
	$("header h1").stop().click(function(){
		$(".sec-center-inner-inner").animate({"left":0,"top":0},{duration:400});
		$(".page").removeClass("now");
		$("#default section.page").addClass("now");
		secHeight();
		$("#right-area nav[class!='default']").hide();
		$("#right-area .default").fadeIn(400);
		h.animate({scrollTop:0}, 300, 'swing');
	});
	
	//HOMEのみ分けてある
	$("#gnav li.home a").attr("href","javascript:void(0);");
	$("#gnav li.home a").stop().click(function(){
		$(".sec-center-inner-inner").animate({"left":0,"top":0},{duration:400});
		$(".page").removeClass("now");
		$("#default section.page").addClass("now");
		secHeight();
		$("#right-area nav[class!='default']").hide();
		$("#right-area .default").fadeIn(400);
		h.animate({scrollTop:0}, 300, 'swing');
	});
	
	//class="slide"のリンク
	var slideHref = new Array();
	var thisId;
	var category = $("div.category");
	var h = $('html, body');
	slideA = $("body a.slide");
	slideA.each(function(i){
		slideHref[i] = slideA.eq(i).attr("href");
		slideA.eq(i).attr("href","javascript:void(0);");
	});
	$("body a.confirm").removeClass("slide");
	$("body a.slide").click(function(){
		n = slideA.index(this);
		categoryNum = $(slideHref[n]).parents(".category").index();
		o = $(slideHref[n]).closest(".category").children(".page").index($(slideHref[n]));
		var distance = 0;
		for(var aNum = 0; aNum<categoryNum; aNum++){
			distance += maxH[aNum];
		};
		$(".sec-center-inner-inner").animate({"left":-558*o,"top":-(distance-1)},{duration:400});
		$(".page").removeClass("now");
		$(slideHref[n]).addClass("now");
		secHeight();
		thisId = $(slideHref[n]).closest("div.category").attr("id");
		$("#right-area nav[class!='"+thisId+"']").hide();
		$("#right-area ."+thisId).fadeIn(400);
		// スクロール位置を先頭／左端に移動
		//h.scrollTop(0).scrollLeft(0);
		h.animate({scrollTop:100}, 300, 'swing');
	});
	
	//高さのアニメーション//
	var firstLoad = true;
	function secHeight(){
		if(firstLoad){//初回でアニメーションしないように
			page =[];
			pageH = [];
			page['now'] = $(".sec-center .now");
			pageH['now'] = page['now'].outerHeight(true)-1;
			$(".sec-center-inner").css("height",pageH['now']);
			firstLoad = false;
		}else{
			page =[];
			pageH = [];
			page['now'] = $(".sec-center .now");
			pageH['now'] = page['now'].outerHeight(true)-1;
			$(".sec-center-inner").animate({"height":pageH['now']},{duration:100});
		}
	}
	
	/*=========================================================
	
	ブログリンク
	
	=========================================================*/
	$("#footer-other a.blogLink").attr("href","javascript:void(0);");
	$("#footer-other a.blogLink").click(function(){
		var thisImg = $(this).find("img");
		var wW = $(window).width();
		var wH = $(window).height();
		thisImg.attr("src","images/blog-ikedasan-ani.gif");
		$(this).css({"position":"relative","z-index":"100"});
		dora = wH/2-85;
		$("#blogLinkEffect").css({"display":"block","top":$(window).scrollTop(),"left":-(1230-wW/2),"z-index":"100"}).animate({"opacity":1},2500);
		$(this).animate({top:"-"+dora+"px",left:"-245px",opacity:"0"},{duration:2000,complete:
		function(){
			location.href ="blog/";
			$(this).animate({"opacity":"1","top":"0px","left":"0px"},{dulation:2000,complte:function(){$("#footer-other a.blogLink").css({"top":"0px","left":"0px"});}});
			$("#blogLinkEffect").animate({"opacity":"0"},{dulation:2000,complete:function(){$("#blogLinkEffect").css({"display":"none","left":"0","z-index":"-100"})}});
			thisImg.attr("src","images/blog-ikedasan.png");
			}});
	});
	
	/*=========================================================
	
	ブログリンク
	
	=========================================================*/
	/*$("#footer-other a.blogLink").attr("href","javascript:void(0);");
	$("#footer-other a.blogLink").click(function(){
		var thisImg = $(this).find("img");
		var wW = $(window).width();
		var wH = $(window).height();
		thisImg.attr("src","images/blog-ikedasan-ani.gif");
		$(this).css({"position":"relative","z-index":"100"});
		dora = wH/2-85;
		$("#blogLinkEffect").css({"top":$(window).scrollTop(),"left":-(1230-wW/2),"z-index":"100"}).animate({"opacity":1},2500);
		$(this).animate({top:"-"+dora+"px",left:"-245px",opacity:"0"},{duration:2000,complete:
		function(){
			location.href ="blog/";
			$(this).animate({"opacity":"1"},{dulation:2000,complte:function(){$(this).css({"top":"0px","left":"0px"})}});
			$("#blogLinkEffect").animate({"opacity":"0"},{dulation:2000,complete:function(){$("#blogLinkEffect").css({"left":"0","z-index":"-100"})}});
			thisImg.attr("src","images/blog-ikedasan.png");
			}});
	});*/
	
	
	/*=========================================================
	
	フォーム
	
	=========================================================*/
	$(".entry-button a").click(function(){
		$("#contact-page1 select option:selected").removeAttr("selected");
		$("#contact-page1 select option").eq(3).attr("selected","selected");
	});
	//お問い合わせ入力→確認
	$(".confirm").click(function(e) {
		if(isvalid()) {
			//エラーがない場合
			$("#contact-page2 #confirm_name").text($("#contact-page1 input[name='contact_name']").attr("value"));
			$("#contact-page2 #confirm_company").text($("#contact-page1 input[name='contact_company']").attr("value"));
			$("#contact-page2 #confirm_list").text($("#contact-page1 select[name='contact_list'] option:selected").attr("value"));
			$("#contact-page2 #confirm_body p").text($("#contact-page1 textarea[name='contact_body']").val());
			$("#contact-page2 #confirm_email").text($("#contact-page1 input[name='contact_email']").attr("value"));
			$("#contact-page2 #confirm_email2").text($("#contact-page1 input[name='contact_email2']").attr("value"));
			$("#contact-page2 #confirm_tel").text($("#contact-page1 input[name='contact_tel']").attr("value"));
			$("#contact-page2 #confirm_way").text($("#contact-page1 input[name='contact_way']:checked").attr("value"));
			$("#contact-page2 #confirm_postal").text($("#contact-page1 input[name='contact_postal']").attr("value"));
			$("#contact-page2 #confirm_address1").text($("#contact-page1 input[name='contact_address1']").attr("value"));
			$("#contact-page2 #confirm_address2").text($("#contact-page1 input[name='contact_address2']").attr("value"));
			$("#contact-page2 #confirm_address3").text($("#contact-page1 input[name='contact_address3']").attr("value"));

			//画面遷移
			categoryNum = $("#contact-page2").parents(".category").index();
			var distance = 0;
			for(var aNum = 0; aNum<categoryNum; aNum++){
				distance += maxH[aNum];
			};
			$(".sec-center-inner-inner").animate({"left":-558,"top":-(distance-1)},{duration:400});
			$(".page").removeClass("now");
			$("#contact-page2").addClass("now");
			secHeight();
			h.animate({scrollTop:100}, 300, 'swing');
		}else{
			//エラーがある場合
			$("#form1").validate().focusInvalid();
		}
	});

	//お問い合わせ確認→完了
	$(".sendmail").click(function(e) {
		$.ajax({
			url:"sendmail.php",
			daataType:"json",
			data:{
				contact_name:$("#contact-page2 #confirm_name").text(),
				contact_company:$("#contact-page2 #confirm_company").text(),
				contact_list:$("#contact-page2 #confirm_list").text(),
				contact_body:$("#contact-page2 #confirm_body p").text(),
				contact_email:$("#contact-page2 #confirm_email").text(),
				contact_tel:$("#contact-page2 #confirm_tel").text(),
				contact_way:$("#contact-page2 #confirm_way").text(),
				contact_postal:$("#contact-page2 #confirm_postal").text(),
				contact_address1:$("#contact-page2 #confirm_address1").text(),
				contact_address2:$("#contact-page2 #confirm_address2").text(),
				contact_address3:$("#contact-page2 #confirm_address3").text()
			},
			type:"post",
			success:function(rst){
				if(rst == "NG"){
					$('#contact-page3 .thanks').html('メール送信できませんでした。');
				}
				//画面遷移
				categoryNum = $("#contact-page3").parents(".category").index();
				o = $("#contact-page3").closest(".category").children(".page").index($("#contact-page3"));
				var distance = 0;
				for(var aNum = 0; aNum<categoryNum; aNum++){
					distance += maxH[aNum];
				};
				$(".sec-center-inner-inner").animate({"left":-558*o,"top":-(distance-1)},{duration:400});
				$(".page").removeClass("now");
				$("#contact-page3").addClass("now");
				secHeight();
				h.animate({scrollTop:100}, 300, 'swing');
			},
			error:function(){
				$('#contact-page3 .thanks').html('メール送信できませんでした。');
				//画面遷移
				categoryNum = $("#contact-page3").parents(".category").index();
				o = $("#contact-page3").closest(".category").children(".page").index($("#contact-page3"));
				var distance = 0;
				for(var aNum = 0; aNum<categoryNum; aNum++){
					distance += maxH[aNum];
				};
				$(".sec-center-inner-inner").animate({"left":-558*o,"top":-(distance-1)},{duration:400});
				$(".page").removeClass("now");
				$("#contact-page3").addClass("now");
				secHeight();
				h.animate({scrollTop:100}, 300, 'swing');
			}
		});
		return false;
	});

	//フォームエラー定義
	//電話番号
	jQuery.validator.addMethod("phone",
		function(value, element) {
			return this.optional(element) || /^[0-9-]{10,15}$/.test(value);
		},
		"電話番号を入力して下さい。"
	);
	//郵便番号
	jQuery.validator.addMethod("postnum",
		function(value, element) {
			return this.optional(element) || /^\d{3}(\-|)\d{4}$/.test(value);
		},
		"郵便番号を入力して下さい。"
	);
	$('#form1').validate({
		errorElement: 'p',
		errorClass: 'error-message',
		rules: {
			contact_email2: {
				equalTo: "#contact_email"
			}
		}
	});

	//フォームエラーチェック
	function isvalid() {
		z =$("#form1").validate({}).form();
		return z;
	}
	
	/*=========================================================
	
	ソーシャルボタン
	
	=========================================================*/
	$("aside#social").hover(function(){
		$(this).stop().animate({"right":"-50px"},{duration:300});
	},function(){
		$(this).stop().animate({"right":"-184px"},{duration:200});
	});
	
	graph();
	secHeight();//高さ
	yearDate();//日付の挿入
	init();//時計の実行

	setInterval('blogArea()',5000);	
	
	
	
});
