/*================================
[share_jquery.js]
[jQuery1.2.3,jquery.cookie.js,jquery.styleswichter.js]
[euc-jp][tokuda@n-di.co.jp]
=================================*/

$(function() {
			  
	/* Style Switcher */
	var options = {
		linkTitle: "styleswichter",
		cookieName: "selected-style"
	};
	$.fn.StyleSwichter(options);
			  	
	/* 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");
		}
	});
	
	$("p.error").each(function(){
		if($(this).children().length<1) $(this).addClass("empty");
	});
	
	$("table.inquiry").each(function(){
		if($(this).prev().hasClass("empty")){
			$(this).addClass("mtMinus");
		}
	});
	
	/* body以下の要素のlastChildにクラス(last-child)を付加 */
	$("body *:last-child").not("br").each(function(){
		$(this).addClass("last-child");
	});
	
	function register(){
		if($("body").hasClass("index")) return false;
		$("img[src*='header_login']").parent().parent().remove();
		var loc = location.href;
		var selectedMenu = function(index){
			$("li:nth-child("+index+")","div#mypage ul").addClass("selected")
		}
		if(loc.match(/member\/scout.php/)) selectedMenu(3);
		else if(loc.match(/m_member.php\?action\=revision/)) selectedMenu(2);
		else if(loc.match(/m_member.php\?action\=exit/)) selectedMenu(1);
		else if(loc.match(/m_member.php/)) selectedMenu(2);
	}
	register();
	
	$("input.freeword").each(function(){
		var defVal = $(this).val();
		$(this).focus(function(){
			if($(this).val().match(new RegExp(defVal))) $(this).val("");
		}).blur(function(){
			if($(this).val().length<1) $(this).val(defVal);
		});
	});
	
	/* よくあるご質問の強調 */
	$(".helpNav li a").filter("[href*='faq.php']").addClass("strong");
	
	if($("#pMap").length>0){
		$("area","map").mouseover(function(event){
			$(".pd").removeClass("display").hide();				  
			$("#"+$(this).attr("id")+"_p").show().hover(function(){
				$(this).addClass("display");
			},function(){
				$(this).removeClass("display").hide();
			});
			event.stopPropagation();
		});
		$("#pMap img").mouseover(function(event){
			$(".pd").hide();
		});
	}

	/* スムーズスクロール */
	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;
	});
	
	/* ul, ol, table の子要素にodd, evenを付加 */
	$("ul, ol").each(
		function(){
			$(this).find(">li:odd").addClass("even");
			$(this).find(">li:even").addClass("odd");
		}
	);
	$("table tbody").each(
		function(){
			$(this).find("tr:odd").addClass("even");
			$(this).find("tr:even").addClass("odd");
		}
	);
	
	$("tr","table.checker tbody").hover(
		function(){
			$(this).addClass("hover");
		},
		function(){
			$(this).removeClass("hover");
		}
	);
	
	$("a","table.checker tbody td").each(function(){
		if($(this).css("fontSize")=="10px") $(this).parent().parent().addClass("visited");
	});
	
	function addLabel(){
		var preLabel = $("label","#prefecture");
		var preChk = $("input","#prefecture");
		for(var i=0;i<preLabel.length;i++){
			$(preLabel[i]).attr("for","ec"+i);
			$(preChk[i]).attr("id","ec"+i);
		}
	}
	addLabel();
	
	$("input[@type='submit'],input[@type='reset']").css("cursor","pointer");
	
	/* fontSizeボタンを生成、関連付け */
	function fontSize(){
	}
	fontSize();
	
	/* タブ */
	var tabMenu = {
		tabClass : ".tab",
		area : ".tabArea",
		pageLink : true,
		hide : function(e,j){
			if(j){
				$(e).each(function(){
					$(this.hash).hide();
				});
			}else{
				if($(e).hasClass("active")){
					$(e).each(function(){
						if(!$(this).hasClass("active"))
							$(this.hash).hide();
					});
				}else{
					for(var i=0;i<e.length;i++){
						if(i==0) $(e[i]).addClass("active");
						else $(e[i].hash).hide();
					}
				}
			}
		},
		set : function(s){
			if($(s).length<1) return false;
			$(s).each(function(){
				var anchor = $(tabMenu.tabClass+" a",this);
				for(var i=0;i<anchor.length;i++){
					if(!anchor[i].href.match(/#/)){
						delete anchor[i];
						anchor.length = 7;
					}
				}
				for(var j=0;j<anchor.length;j++){
					if(anchor[i]=="undefined") return false;
					tabMenu.hide(anchor,false);
					$(anchor[j]).click(
						function(){
							tabMenu.hide(anchor,true);
							$(anchor).not(this).removeClass("active");
							$(this.hash).show();
							$(this).addClass("active");
							return false;
						}
					);
				}
				if(location.hash && $(location.hash,this).length>0){
					tabMenu.hide(anchor,true);
					$(location.hash).show();
					window.scroll(0,0);
				}
			});
		}
	}
	tabMenu.set($(tabMenu.area));
	
	/*$("#header").before("<div id='entryBg'></div>");
	$("div#entryBg").css({
		marginLeft: "-479px",
		padding: "0 21px",
		width: "914px",
		height: document.documentElement.scrollHeight+"px",
		position: "absolute",
		left: "50%",
		background: "transparent url(http://www.e-iryou.net/img/share/bg.png) repeat-y center center",
		zIndex: "-1"
	});
	
	$(window).resize(function(){
		$("div#entryBg").css("height",document.documentElement.scrollHeight+"px");
	});*/
	
});

$(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();	
	
});

/*--------------------------------------------------------------------------*
 *  
 *  footerFixed.js
 *  
 *  MIT-style license. 
 *  
 *  2007 Kazuma Nishihata [to-R]
 *  http://blog.webcreativepark.net
 *  
 *--------------------------------------------------------------------------*/

new function(){
	
	var footerId = "footer";
	//メイン
	function footerFixed(){
		//ドキュメントの高さ
		var dh = document.getElementsByTagName("body")[0].clientHeight;
		//フッターのtopからの位置
		document.getElementById(footerId).style.top = "0px";
		var ft = document.getElementById(footerId).offsetTop;
		//フッターの高さ
		var fh = document.getElementById(footerId).offsetHeight;
		//ウィンドウの高さ
		if (window.innerHeight){
			var wh = window.innerHeight;
		}else if(document.documentElement && document.documentElement.clientHeight != 0){
			var wh = document.documentElement.clientHeight;
		}
		if(ft+fh<wh){
			document.getElementById(footerId).style.position = "relative";
			document.getElementById(footerId).style.top = (wh-fh-ft-1)+"px";
		}
	}
	
	//文字サイズ
	function checkFontSize(func){
	
		//判定要素の追加	
		var e = document.createElement("div");
		var s = document.createTextNode("S");
		e.appendChild(s);
		e.style.visibility="hidden"
		e.style.position="absolute"
		e.style.top="0"
		document.body.appendChild(e);
		var defHeight = e.offsetHeight;
		
		//判定関数
		function checkBoxSize(){
			if(defHeight != e.offsetHeight){
				func();
				defHeight= e.offsetHeight;
			}
		}
		setInterval(checkBoxSize,1000)
	}
	
	//イベントリスナー
	function addEvent(elm,listener,fn){
		try{
			elm.addEventListener(listener,fn,false);
		}catch(e){
			elm.attachEvent("on"+listener,fn);
		}
	}

	//addEvent(window,"load",footerFixed);
	/*addEvent(window,"load",function(){
		checkFontSize(footerFixed);
	});*/
	//addEvent(window,"resize",footerFixed);
	
}