/*================================
ファイル名：otemae.js
説明：サブナビゲーションの追加(全ページ共通)
各ページの設定など

エンコーディング : utf-8

作成者：ND&amp;I 徳田 ( tokuda@n-di.co.jp )
=================================*/

$(function() {
	
	/* ルートディレクトリを設定 - セキュアサーバならbiyou-hifukaへ */
	var secureFlag = location.href.match(/secure-n-di/);
	var root = secureFlag ? "http://www.biyou-hifuka.com/" : "/";
	
	// GA用 セキュアサーバも追跡
	if(!secureFlag){
		$("a[href*='inquiy']").each(function(){
			$(this).click(function(){
				pageTracker._link("https://mt3006.secure.jp/~mt3006036/inquiry/index.html");
				return false;
			});
		});
		$("a[href*='reserve']").each(function(){
			$(this).click(function(){
				pageTracker._link("https://mt3006.secure.jp/~mt3006036/reserve/index.html");
				return false;
			});
		});
	}
			  
	/* サブナビゲーション *//* 以下URIはルート参照 */
	var infoMenu = new Array();	/* 医院案内のサブメニューのタイトルとURI */
	infoMenu[0] = ["医院概要",root+"clinic/outline.html"];
	infoMenu[1] = ["治療までの流れ",root+"clinic/flow.html"];
	infoMenu[2] = ["院内風景",root+"clinic/sight.html"];
	infoMenu[3] = ["当院の考える美容治療",root+"clinic/concept.html"];
	infoMenu[4] = ["お肌の再生医療について",root+"clinic/about.html"];
	infoMenu[5] = ["メディカルコスメ",root+"clinic/cosme.html"];
	infoMenu[6] = ["クリニック所在地",root+"clinic/access.html"];
	
	var treatMenu = new Array();	/* 治療一覧のサブメニューのタイトルとURI */
	treatMenu[0] = ["フォトフェイシャル",root+"treat/photo.html"];
	treatMenu[1] = ["医療レーザー脱毛",root+"treat/laser.html"];
	treatMenu[2] = ["ケミカルピーリング",root+"treat/peeling.html"];
	treatMenu[3] = ["Qスイッチ付ヤグレーザー",root+"treat/q_switch.html"];
	treatMenu[4] = ["ナローバンド",root+"treat/narrowband.html"];
	treatMenu[5] = ["イオン導入",root+"treat/ion.html"];
	treatMenu[6] = ["プラセンタ治療",root+"treat/plasenta.html"];
	treatMenu[7] = ["シミ・ホクロ取りレーザー",root+"treat/shimi.html"];
	treatMenu[8] = ["シワ伸ばし注射・プチ整形",root+"treat/shiwa.html"];
	treatMenu[9] = ["ピアッシング",root+"treat/piercing.html"];
	treatMenu[10] = ["まつ毛育毛剤",root+"treat/matsuge.html"];
	
	var priceMenu = new Array();	/* 料金表のサブメニューのタイトルとURI */
	priceMenu[0]= ["キャンペーン",root+"price/campaign.html"];
	priceMenu[1]= ["フォトフェイシャル",root+"price/price00.html"];
	priceMenu[2]= ["医療レーザー脱毛",root+"price/price01.html"];
	priceMenu[3]= ["Qスイッチ付ヤグレーザー",root+"price/price11.html"];
	priceMenu[4] = ["ケミカルピーリング",root+"price/price02.html"];
	priceMenu[5]= ["イオン導入",root+"price/price03.html"];
	priceMenu[6]= ["プラセンタ治療",root+"price/price04.html"];
	priceMenu[7]= ["シミ・ホクロ取りレーザー",root+"price/price05.html"];
	priceMenu[8]= ["シワ伸ばし注射・プチ整形",root+"price/price06.html"];
	priceMenu[9]= ["ピアッシング",root+"price/price09.html"];
	priceMenu[10]= ["まつ毛育毛剤",root+"price/price10.html"];
	
	/* gNavアンカーにimgの幅と高さを追加 : 選択時の動作設定のため */
	$("a","#gNav").each(function() {
		var img = this.firstChild;
		this.style.display = "block";
		this.style.width = img.width + "px";
		this.style.height = img.height + "px";
	});
	
	/* gNavロケーション */
	function gHide(target){$("img",target).hide();}
	function gNavLocation() {
		var b = document.body.className;
		var lists = $("#gNav > li");
		if(b=="clinic") gHide(lists[0]);
		else if(b=="treat") gHide(lists[1]);
		else if(b=="price"||b=="campaign") gHide(lists[2]);
		else if(b=="column") gHide(lists[3]);
		else if(b=="faq") gHide(lists[4]);
		else if(b=="sitemap") gHide(lists[5]);
		else return false;
	}
	gNavLocation();
			  
	/* サブナビゲーションを生成する */
	function createSubMenu(targetArray,parent) {
		var subMenu = document.createElement("ul");
		for(var a=0; a<targetArray.length; a++) {
			var li = document.createElement("li");
			var anc = document.createElement("a");
			var txt = document.createTextNode(targetArray[a][0]);
			anc.href = targetArray[a][1];
			$(anc).append(txt);
			$(li).append(anc);
			$(subMenu).append(li);
		}
		$(parent).append(subMenu);
	}
	
	/* サブナビゲーションを追加 */
	createSubMenu(priceMenu,$("#gNav li:nth-child(3)").attr("id","price"));
	createSubMenu(infoMenu,$("#info"));
	createSubMenu(treatMenu,$("#treat"));
					  
	$("#gNav > li > ul").hide();
	
	/* gNav選択時の動作設定 */
	$("#info,#treat,#price").hover(function(){$("ul",this).fadeIn(150);},function(){$("ul:visible",this).fadeOut(250);});
	$("#gNav > li").hover(function(){$("img:visible",this).stop().fadeTo(150,0);},function(){$("img:visible",this).fadeTo(500,1);});
	
	/* アンカー画像選択時の動作設定 */
	var anchorImg = $("a > img").not("p.pagetop a img");
	var gNavAnchorImg = $("img","#gNav");
	$(anchorImg.not(gNavAnchorImg)).hover(function(){$(this).fadeTo("normal",.75);},function(){$(this).fadeTo("normal",1);});
	
	/* 美肌応援キャンペーン(下層)枠を挿入 */
	function insertCampaign(){
		if(document.body.className=="index") return false;
		var this_banner = document.createElement("p");
		var this_bannerImg = document.createElement("img");
		$(this_bannerImg).attr("src",root+"img/share/ban_campaign.png").attr("alt","美肌応援キャンペーン");
		$(this_banner).append(this_bannerImg);
		var this_box = document.createElement("div");
		$(this_box).addClass("sub_campaign");
		var this_box_p = document.createElement("p");
		$(this_box_p).text("おりた皮フ科ではお得なコース・セットキャンペーンをおこなっています。");
		var this_box_ul = document.createElement("ul");
		var this_box_link = new Array();
		this_box_link[0] = ["医療レーザー脱毛キャンペーン",root+"price/campaign.html#laser"];
		this_box_link[1] = ["フォトフェイシャルキャンペーン",root+"price/campaign.html#photo"];
		this_box_link[2] = ["マックストーニングキャンペーン",root+"treat/q_switch.html"];
		for(var i=0;i<this_box_link.length;i++){
			var li = document.createElement("li");
			var a = document.createElement("a");
			$(a).attr("href",this_box_link[i][1]).text(this_box_link[i][0]);
			$(li).append(a);
			$(this_box_ul).append(li);
		}
		$(this_box).append(this_box_p).append(this_box_ul);
		$(".clinicInfo").prev().prev().after(this_box).after(this_banner);
	}
	insertCampaign();
		
	/* Galleriaの設定 */
	function settingGalleria(){
		var galleriaBox = document.createElement("div");
		galleriaBox.id = "img";
		$(galleriaBox).insertBefore(".sight");
		$(".sight li:first-child").addClass("active");
		var info = document.createElement("p");
		$(info).addClass("info").css("clear","left").css("margin","0 0 0 1em");
		var infoTxt = document.createTextNode("*サムネイルをクリックすると上に拡大表示されます");
		info.appendChild(infoTxt);
		$(info).insertAfter(".sight");
		$(".sight").galleria({
			history   : true,
			clickNext : true,
			insert    : "#img",
			onImage   : function(image,caption,thumb) {
				image.css("display","none").fadeIn("normal");
				caption.css("display","none").fadeIn("normal");
				var _li = thumb.parents("li");
				_li.siblings().children("img.selected").fadeTo("normal",0.3);
				thumb.fadeTo("normal",1).addClass("selected");
				image.attr("title","次の写真へ");
			},
			onThumb : function(thumb) {
				var _li = thumb.parents("li");
				var _fadeTo = _li.is(".active") ? "1" : "0.3";
				thumb.css({display:"none",opacity:_fadeTo}).fadeIn("fast");
				thumb.hover(
					function() { thumb.fadeTo("fast",1); },
					function() { _li.not(".active").children("img").fadeTo("fast",0.3); }
				)
			}
		});
	}
	
	/* 診療時間カレンダーを読み込む */
	function loadCalendar(){
		var d_calendar = document.createElement("div");
		d_calendar.id = "calendar";
		$(d_calendar).insertBefore("dl.schedule");
		$(d_calendar).datepicker({defaultDate: new Date(thisYear, thisMonth - 1, 1)});
		$(".datepicker_header select").attr("disabled","disabled");
		$("#holiday").insertAfter(".datepicker");
		$(".datepicker_links").remove();
		$(".datepicker td").each(function(){
			var td_txt = $(this).text();
			for(var i=0;i<days_open.length;i++){
				if(td_txt==days_open[i]){
					$(this).addClass("days_open");
				}
			}
			for(var j=0;j<rlDay.length;j++){
				if(td_txt==rlDay[j]){
					$(this).addClass("rlDay");
				}
			}
		});
		var p = document.createElement("p");
		var span1 = document.createElement("span");
		var span2 = document.createElement("span");
		$(span1).addClass("month").text($(".datepicker_newMonth option:selected").text());
		$(span2).addClass("year").text($(".datepicker_newYear option:selected").text()+"年");
		$(".datepicker_header").html("").append(span1).append(span2);
	}
	
	/* subNavロケーションと各ページの設定 */
	function addLocation(target){$(target).addClass("location");}
	function pageSetting() {
		var b = document.body.className;
		if(b=="index") return false;
		var lists = $("#subNav > li");
		var u = location.href;
		//医院案内ページ設定
		if(b.match(/clinic/)) {
			if(u.match(/ind/)) loadCalendar();
			else if(u.match(/outline/)) addLocation(lists[0]);
			else if(u.match(/flow/)) addLocation(lists[1]);
			else if(u.match(/sight/)) {
				addLocation(lists[2]);
				settingGalleria();
			}
			else if(u.match(/concept/)) addLocation(lists[3]);
			//お肌の再生医療についてページ設定
			else if(u.match(/about/)) {
				addLocation(lists[4]);
				$("#about").pager("div", {	//pager.jsの設定
					height: "auto",
					prevText: "前のページへ",
					nextText: "次のページへ"
				});
				var dt = $("dt","#about");	//対談のクラスの追加
				for(var dtl=0; dtl<dt.length; dtl++) {
					var dtTxt = dt[dtl].firstChild.nodeValue;
					if(dtTxt.match(/織/)) $(dt[dtl]).addClass("orita");
					else if(dtTxt.match(/島/)) $(dt[dtl]).addClass("tajima");
				}				
			}
			else if(u.match(/cosme/)) addLocation(lists[5]);
			else if(u.match(/card/)) addLocation(lists[6]);
			else if(u.match(/access/)) addLocation(lists[7]);
		//治療案内ページ設定
		} else if(b.match(/treat/)) {
			if($("ol.flow")) {
				$("ol.flow").each(function(){
					var numItem = $(">li",this);
					for(var j=0;j<numItem.length;j++){
						$(numItem[j]).addClass("no"+(j+1));
					}	//診察の流れのliにclassを振る
				});
			}
			if(u.match(/photo/)) addLocation(lists[0]);
			else if(u.match(/laser/)) addLocation(lists[1]);
			else if(u.match(/peeling/)) addLocation(lists[2]);
			else if(u.match(/ion/)) addLocation(lists[3]);
			else if(u.match(/plasenta/)) addLocation(lists[4]);
			else if(u.match(/shimi/)) addLocation(lists[5]);
			else if(u.match(/shiwa/)) addLocation(lists[6]);
			else if(u.match(/stress/)) addLocation(lists[7]);
			else if(u.match(/cancer/)) addLocation(lists[8]);
			else if(u.match(/piercing/)) addLocation(lists[9]);
			else if(u.match(/matsuge/)) addLocation(lists[10]);
		//料金表ページ設定
		} else if(b.match(/price/)){
			$("#campaign").hide();
			$("#tables").pager("table", {	//pager.jsの設定
				height: "auto",
				navAttach: "prepend",
				prevText: "前の料金表",
				nextText: "次の料金表",
				linkText: ["キャンペーン中","部位1","部位2","男性"]
			});
			$("a","div#nav").click(function(){
				pageTracker._trackPageview("/track_e/price/laserTabs/"+this.rel);
			});
			$("#tables table").css("margin-top","0");
			//以下Gecko系にのみに適用
			if(navigator.userAgent.indexOf("Gecko/")!=-1) {
				$("#nav").css("margin-bottom","1px");
				$("#nav a:nth-child(2)").css("margin-left","-1px");
			}
		} else if(b.match(/form/)){
			if(u.match(/reser/)) $("input#datepicker").datePicker();
			$("input, textarea").not("#verify").hover(function(){$(this).addClass("hover");},function(){	$(this).removeClass("hover");});
			$("input, textarea").not("#verify").focus(function(){$(this).addClass("active");});
			$("input, textarea").not("#verify").blur(function(){$(this).removeClass("active");});
		} else if(b.match(/free/)){
			var printBtn = document.createElement("p");
			printBtn.id = "print";
			var btnImg = document.createElement("img");
			btnImg.src = "../img/free/printout.png";
			btnImg.alt = "このページを印刷する";
			printBtn.appendChild(btnImg);
			$(printBtn).insertBefore(".attention");
			$(btnImg).click(function(){print();});
		} else if(b.match(/biyou/)){
			$("td",".ultraviolet").each(function(){
				var value = $(this).text();
				if(value.match(/弱/)){
					$(this).addClass("weak");
				}else if(value.match(/中/)){
					$(this).addClass("normal");
				}else if(value.match(/強/)){
					$(this).addClass("strong");
				}
			});
		}
	}
	pageSetting();
	
	/* body以下の要素のfirstChild(そのクラスにaccessGuideがあれば次の要素)にクラス(first-child)を付加 */
	$("body *:first-child").not("br").each(function(){
		if(this.className.match(/Guide/)){
			$(this).next().addClass("first-child");
		}else{
			$(this).addClass("first-child");
		}
	});
				
	/* スムーズスクロール */
	function getScrollPosition(){
		var obj = new Object();
		obj.x = document.body.scrollLeft || document.documentElement.scrollLeft;
		obj.y = document.body.scrollTop || document.documentElement.scrollTop;
		return obj;
	}

	$("p.pagetop a").click(function(){
		var y = getScrollPosition().y;
		var x = getScrollPosition().x;
		var timer = setInterval(
			function(){
				y = y - Math.ceil(y/5);
				x = x - Math.ceil(x/5);
				window.scroll(x,y);
				if(y<1) clearInterval(timer);
			}
		,10);
		return false;
	});
});

$(window).load(function(){
	/* フラグメント参照fix */
	function flagment(){
		var targetId = $(location.hash);
		if(location.hash && targetId.length>0){
			var offset = targetId.offset();
			window.scroll(0,offset.top);
			return false;
		}
	}
	flagment();								
});

