﻿/*---------------------------------------------
	ユーザーエージェントチェック
  ---------------------------------------------*/

function ua(target) {
	var nut = navigator.userAgent.toLowerCase();
	var uaCheck = {
		"ie"		:nut.indexOf("msie") != -1,
		"ie6"		:nut.indexOf("msie 6") != -1,
		"ie7"		:nut.indexOf("msie 7") != -1,
		"ff"		:nut.indexOf("firefox") != -1,
		"safari"	:nut.indexOf("safari") != -1,
		"chrome"	:nut.indexOf("chrome") != -1,
		"opera"		:nut.indexOf("opera") != -1,
		"ipad"		:navigator.platform == "iPad",
		"win"		:navigator.appVersion.indexOf ("Win")
	};
	return uaCheck[target];
}



/*---------------------------------------------
	JavaScript On時の処理
  ---------------------------------------------*/

$("html").addClass("jsOn opening");
if(ua("win")) {$("html").addClass("win");}



/*---------------------------------------------
	ブラウザサイズの判定
  ---------------------------------------------*/

function getBrowserWidth() {
	return window.innerWidth || self.innerWidth || (document.documentElement&&document.documentElement.clientWidth) || document.body.clientWidth;
}

function getBrowserHeight() {
	return window.innerHeight || self.innerHeight || (document.documentElement&&document.documentElement.clientHeight) || document.body.clientHeight;
}



/*---------------------------------------------
	iPad用設定
  ---------------------------------------------*/

var myScroll;
function loaded() {
	if(ua("ipad")) {
		document.addEventListener('touchmove', function(e){ e.preventDefault(); });
		myScroll = new iScroll('scroller');
	}
}

if(ua("ipad")) {
	$("html").addClass("ipad");
}



/*---------------------------------------------
	横幅950px以下のときの処理
  ---------------------------------------------*/

var u950 = 0;

if(getBrowserWidth() < 950) {
	$("html").addClass("u950");
	u950 = 1;
	if(ua("ipad")) {
		$("html").addClass("u950ipad");
	}
}

function switchVH() {
	if(getBrowserWidth()<950 && !u950) {
		u950 = 1;
		$("html").addClass("u950");
		if(ua("ipad")) {$("html").addClass("u950ipad");}
	} else if(getBrowserWidth()>=950 && u950) {
		u950 = 0;
		$("html").removeClass("u950");
		if(ua("ipad")) {$("html").removeClass("u950ipad");}
	} else {
		return false;
	}
}



/*---------------------------------------------
	コンテンツの高さの設定
  ---------------------------------------------*/

function setHeight() {
	var contentsM = !u950 ? 118 : 128;
	var contentsH = ua("ie6") ? "height" : "min-height";
	$("#contents").css(contentsH, getBrowserHeight()-contentsM+"px");
}



/*---------------------------------------------
	画面リサイズ時の処理
  ---------------------------------------------*/

$(window).bind("resize", function() {
	switchVH();
	setHeight();
});



/*---------------------------------------------
	「戻る」ボタンの処理
  ---------------------------------------------*/

if(!ua("ipad") && (ua("opera") || ua("ff") || ua("safari"))) {
	window.onunload = function() {}
}



/*---------------------------------------------
	オープニングアニメーション
  ---------------------------------------------*/

function opening() {
	if(ua("ipad")){
		$("html").removeClass("opening");
	}
	$("#loader").fadeOut(1000);
	$("#contents").fadeIn(1000,function(){
		if(ua("ie")) {this.style.removeAttribute("filter");}
		if(!ua("ipad")){
			$("html").removeClass("opening");
		}
	});
	setHeight();
}



/*---------------------------------------------
	サイトマップの開閉
  ---------------------------------------------*/

function openSitemap() {
	var sitemapH = $("#btnSitemap p a").hasClass("on") ? 0 : $("#sitemap").height();
	if(!ua("ipad")) {
		$("#btnSitemap").animate({bottom:sitemapH+66+"px"}, 500,"easeInOutQuad");
		$(".pageTop").animate({bottom:sitemapH+96+"px"}, 500,"easeInOutQuad");
		$("#footerArea").animate({bottom:sitemapH-532+"px"},500,"easeInOutQuad",function() {
			$("#btnSitemap p a").toggleClass("on");
		});
	} else {
		$("#btnSitemap p a").toggleClass("on");
		$("#btnSitemap").css({
			webkitTransitionDuration:"500ms",
			webkitTransitionTimingFunction:"ease-in-out",
			webkitTransform:"translate3d(0px,-"+sitemapH+"px,0px)"
		});
		$("#footerArea").css({
			webkitTransitionDuration:"500ms",
			webkitTransitionTimingFunction:"ease-in-out",
			webkitTransform:"translate3d(0px,-"+sitemapH+"px,0px)"
		});
	}

}

function btnSitemap() {
	$("#btnSitemap p a").click(function() {
		openSitemap();
	});
}



/*---------------------------------------------
	プライバシーポリシーの開閉
  ---------------------------------------------*/

function btnPrivacy() {
	var $btnPrivacy = $("#btnPrivacy a");
	var $privacy = $("#privacy");
	var privacyH = -($("#privacyArea").height());
	$privacy.css("marginTop", privacyH);

	$btnPrivacy.click(function() {
		var privacyM = $btnPrivacy.hasClass("on") ? privacyH : 0;
		$privacy.animate({marginTop:privacyM+"px"},500,"easeInOutQuad",function() {
			$btnPrivacy.toggleClass("on");
		});
	});
}



/*---------------------------------------------
	ボックスマウスオンの処理
  ---------------------------------------------*/

function boxHover() {
	if(!ua("ipad")) {
		$(".listBox1 .link, .listBox1 .blank, .listBox1 .enlarge, .listBox1 .play, .listBox2 .link, .listBox2 .blank, .listBox2 .enlarge, .listBox2 .play").hover(function() {
			$(this).addClass("boxHover");
		},function() {
			$(this).removeClass("boxHover");
		});
	}
}



/*---------------------------------------------
	リンククリック
  ---------------------------------------------*/

function slide(lr, link) {
	naviFadeOut();
	if(ua("ie6") || ua("ie7")) {
		$("#map_canvas").css({position:"static"});
		$("#map_canvas div").css({display:"none"});
		$(".jspContainer, .jspPane").css({position:"static"});
		$(".jspVerticalBar").css({display:"none"});
	}
	var contentsLeft = (u950 && ua("ipad")) ? 768 : 720;
	var loaderLeft = (u950 && ua("ipad")) ? 374 : 345;
	var cl1 = (lr) ? contentsLeft : -contentsLeft;
	var cl2 = (lr) ? -contentsLeft : contentsLeft;
	var easingTime = 500;
	if($("#btnSitemap a").hasClass("on")) {openSitemap();}
	if(!ua("ipad")) {
		$("#contents").animate({marginLeft:cl2+"px"},500,"easeInOutQuad",function() {
			pageTransition(link);
		});
	} else {
		easingTime = 800;
		$("#contents").css({
			webkitTransitionDuration:"800ms",
			webkitTransitionTimingFunction:"ease-in-out",
			webkitTransform:"translate3d("+cl2+"px,0px,0px)"
		});
		timerID = setTimeout("pageTransition('"+link+"')", 800);
	}
	$("#loader").css("display","block");
	$("#loader img").css("marginLeft",loaderLeft+cl1+"px").animate({marginLeft:loaderLeft+"px"},easingTime,"easeInOutQuad");
}

function pageTransition(link) {
	if (link == "submit") {
		$("form").submit();
	} else if(link == "back") {
		history.back();
	} else {
		window.location.href = link;
	}
}

function checkHref(selector) {
	var $selector = $(selector);
	return $selector.attr("href") || $selector.find("a").attr("href");
}

function pageTop() {
	var topID = (ua("ie6")) ? "#container" : (ua("safari")) ? "body" : "html";
	$(topID).stop().animate({scrollTop: 0}, 800, "easeInOutCubic");
}

function clickLink() {

	$("a").click(function() {

		var $parent = $(this).parent().parent();
		var $this = $(this);
		$this.blur();

		if($this.hasClass("blank") || $this.hasClass("current") || $this.hasClass("nolink") || $this.hasClass("back") || $this.attr("href")== "#" || $parent.hasClass("enlarge") || $parent.hasClass("play")) {
		} else if($this.hasClass("parent") || $this.hasClass("prev") || $this.attr("href") == "http://www.climarks.com/" || $parent.hasClass("breadCrumb")) {
			slide(0, checkHref(this));
		} else if($parent.hasClass("blank")) {
			window.open(checkHref(this));
		} else if($parent.hasClass("pageTop")) {
			pageTop();
		} else {
			slide(1, checkHref(this));
		}
		return false;

	});

	$(".link").click(function() {

		var $this = $(this);
		if($this.hasClass("parent") || $this.hasClass("prev")) {
			slide(0, checkHref(this));
		} else {
			slide(1, checkHref(this));
		}
		return false;

	});

	$(".blank").click(function() {
		window.open(checkHref(this));
	});

	$(".print").click(function() {
		window.print();
	});

	$(".submit").click(function() {
		if(!$(this).hasClass("nolink")) {formCheck();}
	});

	$(".back").click(function() {
		slide(0, "back");
	});

	$(".contact").click(function() {
		eval(String.fromCharCode(108,111,99,97,116,105,111,110,46,104,114,101,102,61,39,109,97,105,108,116,111,58,105,110,102,111,64,99,108,105,109,97,114,107,115,46,99,111,109,39));
	});

}



/*---------------------------------------------
	フォームチェック
  ---------------------------------------------*/

function formCheck() {

	var mailText = /[!#-9A-~]+@+[a-z0-9]+.+[^.]$/i;

	var checkItem = [
		"required",
		"phone",
		"mail",
		"age"
	];

	var errorText = [
		"入力必須項目です。",
		"電話番号の記載に誤りがあります。",
		"メールアドレスの記載に誤りがあります。xxx&#64;xxx.xx.xxの形式で入力してください。",
		"年齢の記載に誤りがあります。"
	];

	$(".field1, .field2, .field3").css({borderColor:"#e0e4e6", backgroundColor:"#fff"});
	$("p.errorText").remove();
	$(".formError").removeClass("formError");
	$("label").css({color:"#666"});

	function errorCheck(obj, i) {
		var str = $(obj).val();
		if(i==0) {
			if(str == "") {
				return true;
			}
		} else if(i==1) {
			var num = "0123456789-";
			var tmp = [];
			for(j=0; j<str.length; j++){
				tmp[j] = str.substring(j, j+1);
				if(str !== "" && num.indexOf(tmp[j]) == -1) {
					return true;
				}
			}
		} else if(i==2) {
			if(str !== "" && !str.match(mailText)) {
				return true;
			}
		} else if(i==3) {
			var num = "0123456789";
			var tmp = [];
			for(j=0; j<str.length; j++){
				tmp[j] = str.substring(j, j+1);
				if(str !== "" && num.indexOf(tmp[j]) == -1 || str<18 || str>70) {
					return true;
				}
			}
		}
	}

	$(".field1, .field2, .field3").each(function() {
		for(i=0; i<checkItem.length; i++) {
			if($(this).hasClass(checkItem[i]) && errorCheck(this, i)) {
				$(this).addClass("formError").css({borderColor:"#ebbfd4", backgroundColor:"#fee7f2"}).parent().append("<p class='errorText'>"+errorText[i]+"</p>");
			}
		}
	});

	var radioRequired = [];
	var num = 0;

	$(".radio").each(function() {
		if($(this).hasClass(checkItem[0])) {
			radioRequired[num] = $(this).attr("name");
			num++;
		}
	});

	for(i=0; i<num; i++) {
		if($(".radio:checked[name='"+radioRequired[i]+"']").length == 0) {
			var obj = $(".radio[name='"+radioRequired[i]+"']").eq(0);
			obj.addClass("formError");
			var objParent = obj.parent().parent().hasClass("radioList") ? obj.parent().parent().parent() : obj.parent();
			objParent.find("label").css({color:"#d7277a"})
			objParent.append("<p class='errorText'>"+errorText[0]+"</p>");
		}
	}

	if($(".formError").length !== 0) {
		$(".formError").eq(0).focus();
	} else {
		slide(1, "submit");
	}
}

function checkPrivacy() {

	if($(".privacy:checkbox").attr("checked")) {
		$(".submit").removeClass("nolink");
	};

	$(".privacy:checkbox").click(function() {
		$(".submit").toggleClass("nolink");
	});

}



/*---------------------------------------------
	サイドバーナビゲーションのフェード
  ---------------------------------------------*/

function naviFadeOut() {
	$(".jsOn #sidebar .gNavi").fadeOut(500);
}

function naviFadeIn() {
	$(".jsOn #sidebar .gNavi").fadeIn(500,function() {
		if(ua("ie")){this.style.removeAttribute("filter");}
	});
}



/*---------------------------------------------
	ヘッダーの追加（950px以下用）
  ---------------------------------------------*/

var himg = [];

for(i=0; i<7; i++){
	himg[i] = new Image();
	if(i<5){himg[i].src = "/common/images/gnavi_"+(i+1)+"_v.gif";}
}
himg[5].src="/common/images/logo_v.gif";
himg[6].src="/common/images/copy_v.gif";

function addHeader() {
	var innerHeader = $("#sidebar").html();
	$("#container").after($("<div id='header'></div>"));
	$("#header").html(innerHeader);

	for(i=0; i<5; i++){$("#header .gNavi img").eq(i).attr("src", himg[i].src);}
	$("#header .siteID img").attr("src", himg[5].src); 
	$("#header .siteCopy img").attr("src", himg[6].src);
}



/*---------------------------------------------
	画像遅延ロード
  ---------------------------------------------*/

var delayImage = ".listBox2 .image img, .listBox1 .image img";
var original = [];

function imageUnload() {
	if(ua("ipad")) {return false;}
	$(delayImage).each(function(i) {
		original[i] = $(this).attr("src");
		$(this).removeAttr("src");
	});
};

function imageDelayLoad() {
	if(ua("ipad")) {return false;}
	$(delayImage).each(function(i) {
		$(this).bind("load", function() {
			$(this).fadeIn(500, function() {
				$(this).parent().css({backgroundImage: "none"});
			});
		})
		.attr("src", original[i]);
	});
};



/*---------------------------------------------
	ソーシャルリンク
  ---------------------------------------------*/

function socialLink() {

	var euURL = encodeURI(window.location.href);
	var euTitle = encodeURI(document.title);
	var euBlank = encodeURI(" ");
	var eucURL = encodeURIComponent(window.location.href);
	var eucTitle = encodeURIComponent(document.title);
	var eucBlank = encodeURIComponent(" ");

	var btnSocial = "<ul class='clearfix'>";
	btnSocial += "<li><a id='btnHatena' href='http://b.hatena.ne.jp/entry/"+euURL+"' rel='nofollow'><img src='/common/images/icon_hatena.gif' width='14' height='28' alt='はてなブックマーク' title='はてなブックマーク' /></a></li>";
	btnSocial += "<li><a id='btnDelicious' href='http://del.icio.us/post?url="+eucURL+"&title="+eucTitle+"' rel='nofollow'><img src='/common/images/icon_delicious.gif' width='14' height='28' alt='delicious' title='delicious' /></a></li>";
	btnSocial += "<li><a id='btnTwitter' href='http://twitter.com/?status="+euTitle+euBlank+euURL+"' rel='nofollow'><img src='/common/images/icon_twitter.gif' width='14' height='28' alt='Twitter' title='Twitter' /></a></li>";
	btnSocial += "<li><a id='btnFacebook' href='http://www.facebook.com/sharer.php?u="+euURL+"&t="+euTitle+"' rel='nofollow'><img src='/common/images/icon_facebook.gif' width='14' height='28' alt='Facebook' title='Facebook' /></a></li>";
	btnSocial += "</ul>";

	$("#btnSitemap p").before(btnSocial);
	var socialLinkW = 24*$("#btnSitemap ul li").length+22;
	$("#btnSitemap").css({width:(socialLinkW+97)+"px"});
	socialPosition();
	if(!ua("ie6")) {
		$("#btnSitemap ul").css({width:(socialLinkW-12)+"px"});
	} else {
		$("#btnSitemap ul").css({width:(socialLinkW-7)+"px"});
	}

	function socialPosition() {
		if(getBrowserWidth() < 950) {
			$("#btnSitemap").css({marginLeft:(248-socialLinkW)+"px"});
		} else {
			$("#btnSitemap").css({marginLeft:(368-socialLinkW)+"px"});
		}
	}

	$(window).bind("resize", socialPosition);

	$("#btnSitemap ul li a").click(function() {
		window.open($(this).attr("href"));
		return false;
	});

}



/*---------------------------------------------
	メインルーチン
  ---------------------------------------------*/

function commonReady() {
	imageUnload();
	addHeader();
	boxHover();
	clickLink();
	btnSitemap();
	checkPrivacy();
	socialLink();
}

function commonLoad() {
	naviFadeIn();
	opening();
	btnPrivacy();
	imageDelayLoad();
	loaded();
}

$(document).ready(commonReady);
$(window).load(commonLoad);


