//これは債務専門司法書士事務所のスクリプトです
//jquery-1.2.6.min.jsをフレームワークに使っています

$(init);


////////////////////////////////////////////////////////////////////
//初期化処理
////////////////////////////////////////////////////////////////////
function init()
{
	//
	//javascriptがON時にbodyにクラスを加えてスタイルをコントロール
	//
	$("body").addClass("onscript");

	//ヘッダーのロールオーバー
	$("#header-contact img").rollover();
	$("#header-address-map area").hover(
		function(){$("#header-address").addClass("hover");},
		function(){$("#header-address").removeClass("hover");}
	);

	//サイドバーのロールオーバー
	$("#sidebar a img").rollover();

	//フッターのロールオーバー
	$("#footer-contact-mail img").rollover();
	$("#footer-contact-form img").rollover();

	//
	//借金問題Ｑ＆Ａ
	//
	var saimu_faq = $("#main dl.saimu-faq dt");
	saimu_faq.hover(
		function(){ $(this).css("cursor","pointer"); },
		function(){ $(this).css("cursor","default"); }
	);
	
	saimu_faq.toggle(
		function(){ $(this).next().slideDown("fast");},
		function(){ $(this).next().slideUp("fast"); }
	);

	//
	//あなたの依頼する事務所は大丈夫ですか？
	//
	$("#topics02-q div.submit a")
		.click(
			function(){
				var checkedradio = $("#topics02-q input[type='radio']:checked");
				var response;
				if(checkedradio.length==4){
					var total
						= Number(checkedradio.get(0).value)
						+ Number(checkedradio.get(1).value)
						+ Number(checkedradio.get(2).value)
						+ Number(checkedradio.get(3).value);
					if		(total==20){response = "迷わず依頼しましょう";}
					else if(total>=18){response = "依頼しても大丈夫でしょう";}
					else if(total>=16){response = "よく考えて依頼しましょう";}
					else if(total>=12){response = "他も探してみては";}
					else			   {response = "お勧めしません";}
				}else{
					response = "全ての質問にお答えください";
				}
				$("#topics02-response div").fadeOut("fast",function(){
					$(this).text(response).fadeIn("slow");
				});
				return false;
			}
		)
		.children()
			.rollover();
	
	
	
	//
	//借金問題YES NO
	//
	$("#yesorno p.select a").one( "click", procYesOrNo );
	
	
	//
	//もう一度やるの処理
	//
	$("#yesorno div.yn-result button").click( procReset );	

	//
	//過払金返還シミュレーターのフォーム
	//
	$("#calcu dl input")
		.focus(function(){ $(this).css("backgroundColor","#FFFFFF"); } )
		.blur (function(){ $(this).css("backgroundColor","#E6F0E7"); } )
	$("#calcu div.submit input").rollover();
	$("#calcu table tbody tr:odd *").css("backgroundColor","#F4F9F4");
}


////////////////////////////////////////////////////////////////////
//YesとNo画像をクリックした場合の処理
////////////////////////////////////////////////////////////////////
function procYesOrNo()
{
	//
	//対象エレメントを取得
	//
	var ynshowid = $(this).attr("href");
	ynshowid = ynshowid.split("#");
	ynshowid = "#" + ynshowid[1];
	var ynshowyn = "yn-" + $(this).attr("class");
	
	
	//
	//次の質問及び結果の表示
	//
	$(this).attr("href","javascript:void(0)").append("<span></span>");
	$(this).hover(
		function(){$(this).css("cursor","default");},
		function(){$(this).css("cursor","default");}
	)
	$(this).next().fadeOut("fast");
	$(this).prev().fadeOut("fast");
	$(ynshowid).addClass(ynshowyn).fadeIn("slow");
	
	//
	//スクロール制御
	//現状のスクロールバーの位置を取得して、次に表示される内容の高さを加えたところまでスクロール
	//させる。
	//その際にマージンを25pxとる。
	//
	var margin    = 25;
	var myscrolly = document.documentElement.scrollTop || document.body.scrollTop;
	var height    = $(ynshowid).height();
	
	//スクロール位置の計算
	myscrolly += ( height + margin );
	
	//アニメーション的にスクロールさせる。
	$('html,body').animate({ scrollTop: myscrolly }, 'slow');
}


////////////////////////////////////////////////////////////////////
//もう一度やるをクリックした場合の処理
////////////////////////////////////////////////////////////////////
function procReset()
{
	$('html,body').animate({ scrollTop: 200},'slow' );
	
	$("#yesorno").fadeOut(
		"slow", 
		function() {
			//一度、表示した要素を全て消す。
			$("#yesorno > *").remove();
			
			//その後、消した要素をajexにて再度取り込む。
			//読み込み完了後、再度、各イベントを再登録する。
			var html = $("#yesorno");
			html.load( 
				"dgns4.html #yesorno > *",
				null,
				function(){
					$("#yesorno p.select a").one( "click", procYesOrNo ); 
					$("#yesorno div.yn-result button").click( procReset );
					
					//再度読み込んだ内容をフェードイン的に表示させる。
					html.fadeIn( "slow" );
				}
			);
		}
	);
}

////////////////////////////////////////////////////////////////////
//ロールオーバー
// 画像srcの後に?hsrc=でホバー用の画像ファイルを指定、
// ただし通常画像とホバー画像のファイルは同じフォルダ階層で
////////////////////////////////////////////////////////////////////

$.fn.rollover = function(){
	var images = this;
	images
		.each(function(){
			
			//通常時のイメージソース
			var nSrc = $(this).attr("src");

			// hover時のイメージソース

			// img要素にhsrc属性があれば
			if($(this).attr("hsrc")){
				var hSrc = $(this).attr("hsrc");
			}
			// img要素にsrcに文字列"?hsrc="があれば
			else if(nSrc.indexOf("?hsrc=")>=0){
				var hSrc = (
					function(){
						var array = nSrc.split("/");
						array[array.length-1] = nSrc.split("?hsrc=")[1];
						return array.join("/");
					}
				)();
			}
			// img要素にsrcに文字列"_n."があれば
			else if(nSrc.indexOf("_n.")>=0){
				var hSrc = (
					function(){
						var array = nSrc.split("/");
						array[array.length-1] = array[array.length-1].replace("_n.","_o.");
						return array.join("/");
					}
				)();
			}

			// hover時のイメージソースがあれば実行
			if(hSrc){
				//イメージのキャッシュ
				$(this).get(0).normalImage = {};
				$(this).get(0).hoverImage  = new Image();
				$(this).get(0).normalImage.src = nSrc;
				$(this).get(0).hoverImage.src  = hSrc;

				//イベントハンドラ
				$(this).hover(
					function(){$(this).attr("src",this.hoverImage.src);},
					function(){$(this).attr("src",this.normalImage.src);}
				);

			}

		})

	return images;
}
