/*================================
 setting.js
 tokuda@n-di.co.jp
=================================*/

$(function() {
			  	
	/* 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");
		}
	});
	$("body *:last-child").addClass("last-child");
	
	/* table, ul, olの子要素にodd, evenを追加 */
	$("table tr,ul>li,ol>li").filter(":odd").addClass("even").end()
	.filter(":even").addClass("odd");
	
	/* Ranking 以外のページ(検索結果を除く)はデザイン用のエレメントを追加 */
	if($("body").hasClass("search") || !$("body").attr("class").match(/ranking/)){
		$("#cont").after("<div id='cont-bottom'></div>");
	}
	
	/* input:radio, input:checkbox にクラスを追加 */
	$("input:radio").addClass("radio");
	$("input:checkbox").addClass("checkbox");
	
	/* ロールオーバー */
	function rollOver(img){
		var src = img.attr("src");
		var overSrc = src.replace("_off","_on");
		var over = new Image();
		over.src = overSrc;
		img.hover(function(){
			img.attr("src",overSrc);
		},function(){
			img.attr("src",src);		
		});
	}
	//$("img[src*='_off']").each(function(){rollOver($(this))});
	
	/* 外部へのリンク */
	function extLink(){
		var loc = location.href.replace(/(http\:\/\/[^\/]+\/?)[\#\-\_\/\w\.\d]*/,"$1");
		$("a[href*='http://']").each(function(){
			if(this.firstChild.nodeName!="IMG"){
				if(!$(this).attr("href").match(new RegExp(loc))){
					$(this).after("<a href='"+$(this).attr("href")+"' class='extLink' title='"+$(this).text()+"のリンクを別ウィンドウで開く'>"+$(this).html()+"</a>");
				}
			}
		});
		$("a.extLink").click(function(){
			window.open(this.href,"","");
			return false;
		});
	}
	extLink();
	
	/* /rank-\w+/ 以下のページへのリンクは #/rank-\w+/ に置き換え */
	function hrefReplace(){
		$(" a[href*='rank-']","div#sub ul.cat,div#cont ul.sitemap").each(function(){
			$(this).attr("href",$(this).attr("href").replace(/(rank\-\w+)/,"#$1"));
		}).click(function(){
			if($("body").attr("id") && $("body").attr("id").match(/index/)){
				if(/*@cc_on!@*/false){
					 $.historyLoad(this.hash.replace(/\#/,""));
				}
				window.scroll(0,0);
			}
		});
	}
	hrefReplace();
	
	/* ご案内ページ ご注文までの流れ */
	function flow(){
		var chart = $("#flow>li");
		for(var i=0,j=1;i<chart.length;i++,j++){
			if($(chart[i]).attr("id") && $(chart[i]).attr("id").match(/point/)){
				$(chart[i]).addClass("point");
				j--;
			}else{
				$("span:first-child",chart[i]).before("<span class='num'>"+j+".</span>");
			}
		}
	}
	flow();
	
	function sampleForm(){
		if($("p.more").length<1) return false;
		$($("p.more a").attr("href")).hide();
		$("p.more a").click(function(){
			$($(this).attr("href")).show();
			$(this).parent().remove();
			return false;
		});
	}
	sampleForm();
				
	/* スムーズスクロール */
	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();								
});
