$(document).ready(function (){

	/* ダイアログの開閉*/
	var mask_id = "overlay";
	var on_flg = "dialog-usable";
	var open_dialog_id = null;
	$("#nav a").click(function(){
		var hash = $(this).attr("href");
		var menu_height = $("#menu").outerHeight();
		var header_height = $("#header").outerHeight();
		var overlay_height = $("html").height();

		if($("."+mask_id).length == 0){	//ダイアログが閉じている場合
			$("body").append("<div id='#"+mask_id+"' class='"+mask_id+"' style='height:"+overlay_height+"px;'></div>");
			$(hash).animate({top: menu_height+header_height},500).addClass(on_flg);
		}
		else{
			open_dialog_id = "#"+$("."+on_flg).attr("id");
			//ハッシュが開いているダイアログと同じ場合
			if(open_dialog_id == hash){
				$("."+mask_id).remove();
				$(hash).removeClass(on_flg).animate({top: 0},100);
			}
			//ハッシュが開いているダイアログと違う場合
			else{
				$(open_dialog_id).removeClass(on_flg).animate({top: 0},100);
				$(hash).animate({top: menu_height+header_height},500).addClass(on_flg);
			}
		}
		return false;
	});

	$("#menu .close, ."+mask_id).live("click", function(){
		$("."+mask_id).remove();
		$("."+on_flg).animate({top: 0},100).removeClass(on_flg);
	});

	/* 要素の開閉処理 */
	$(".accordion").css("cursor", "pointer").live("click", function(){
		var ele = $(this);
		ele.nextAll().slideToggle("fast", function(){
			//必要ならアイコンの切り替え
		});
	});

	$(".scrollup").click(function(){
		$("html,body").animate({scrollTop: 0}, 1000);
		return false;
	});

	$("p.toggle a").click(function(){
		$(this).parent("p").prev("p").toggleClass("close");
		if($(this).text() == "▼続きを読む"){
			$(this).text("▲記事を隠す");
		}
		else{
			$(this).text("▼続きを読む");
		}
		return false;
	});

	$("div#next a").click(function(){
		var post_url = $(this).attr("href");
		var list_num = $("#next").prevAll("div").length;
		$.post(post_url, {data_number: list_num}, function(e){
			if(e){
				$("#next").before(e);
			}
			else {
				window.alert("これ以上データはありません。");
			}
		}, "html");
		return false;
	});

	$("form#search h3").click(function(){
		$(this).nextUntil("h3,p.attention").slideToggle("fast");
	});
	
	
	$("form#search input.btn").click(function(){
		var baseform = $("form#search");
		var post_url = baseform.attr("action");
		var params = baseform.serialize();
		if(!params){
			window.alert("項目を最低1つチェックして下さい。");
			return false;
		}

		$.post(post_url, {params: params}, function(e){
			if(e){
				$("div#result").removeClass("hide").children().remove("div").end().append(e);
				baseform.children().not("h3,input,p.attention").slideUp("fast", function(){
					var HashOffset = $("div#result").offset().top;
					$("html,body").animate({scrollTop: HashOffset}, 1000);
				});
			}
			else {
				window.alert("該当する女性はいません。");
			}
		}, "html");
		return false;
	});
	
	/* 要素の開閉処理() */
	$(".opener").css("cursor", "pointer").live("click", function(){
		var open = $(this);
		var bros = open.parent().siblings();
		open.nextAll().slideToggle("fast");
		open.toggleClass("down-icon").toggleClass("up-icon");
		bros.children().not(".opener").slideUp("fast", function(){
			bros.children(".opener").removeClass("up-icon").addClass("down-icon");
		});
	});


}); 
