$(function(){

	//年齢検索
	$("[rel=category05]").live("click",function(){
		$("#dialogWindow").dialog({
			buttons: {
				"取消": function() {
					$(".ui-dialog-overlay").remove();
					$("[role=dialog]").remove();
				},
				"検索": function() {
					var age = $("#age").val();

					if(!age.match("[1-9][0-9]*")){
						$("#errMsg").html("半角数字をご入力ください。");
						return false;
					}

					if(age != "" && age >= 18){
						var area = $("#copyright").attr("rel");//document.URL.match(/hokkaido|tohoku|hokuriku|kanto|tokai|kansai|chugoku|kyushu/);
						if(area == "kansai" || area == ""){
							window.location.href = "/search/age/-/-/10/"+age+"/";
						}else{
							window.location.href = "/"+area+"/search/age/-/-/10/"+age+"/";
						}
						$(this).dialog('close');
					}else{
						if(age == ""){
							$("#errMsg").html("入力内容が空です。");
							return false;
						}
						if(age < 18){
							$("#errMsg").html("18歳以上の年齢をご入力ください。");
							return false;
						}
					}
				}
			},
			width: 260,
			height: 150,
			autoOpen: true,
			closeOnEscape: false,
			modal: true,
			title: 'あなたの年齢を入力してください。',
			open: function(){
				$("#dialogWindow").html('<span id="errMsg"></span><br /><input type="text" name="age" id="age" size="3" maxlength="2" />歳');
			},
			overlay: {
				opacity: 0.5,
				background: "black"
			}
		});
		return false;
	});

	
	if($("#keywordsearchtext").val() == ""){
		$("#keywordsearchtext").val("お好きなキーワードを入力してください。");
	}
	
	$("#keywordsearchtext").focus(function(){
		if($(this).val() == "お好きなキーワードを入力してください。"){
			$(this).val('');
		}
	})
	.blur(function(){
		if($(this).val() == ''){ 
			$(this).val("お好きなキーワードを入力してください。");
		}
	});

	//rel=windowは別ウィンドウで
	$("a[rel='window']").live("click",function(){
		window.open($(this).attr('href'),'','');
		return false;
	});

	//登録フォーム
	$("#registForm").submit(function(){
		var msg = "";
		if(!$("#kiyakuCheck").attr("checked")){
			msg += "規約に同意される方はチェックをつけてください。\n";
		}

		if($("#shopName").val() == ""){
			msg += "店舗名をご入力ください。\n";
		}
		if($("#shopNameRuby").val() == ""){
			msg += "フリガナをご入力ください。\n";
		}
		if($("#shopEmail").val() == ""){
			msg += "メールアドレスご入力ください。\n";
		}
		if($("#shopCheckEmail").val() == ""){
			msg += "確認用メールアドレスをご入力ください。\n";
		}
		if($("#shopPhone").val() == ""){
			msg += "お電話番号をご入力ください。\n";
		}
		if(msg != ""){
			alert(msg);
			return false;
		}
	});

	//モバイル転送フォーム
	$("#mobilesend_other").css("display","none");
	$("#mobilesend_domain").change(function(){
		if($(this).val() == "other"){
			$("#mobilesend_other").css("display","block");
		}else{
			$("#mobilesend_other").css("display","none");
		}
	});

	//メールアドレスのチェック
	$("#mobilesendForm").submit(function(){
		if($("#mobilesend_account").val() == "" || $("#mobilesend_domain").val() == "" || ($("#mobilesend_domain").val() == "other" && $("#mobilesend_other").val() == "")){
			$("#dialogWindow").dialog({
				buttons: {
					"確認": function() {
						$(".ui-dialog-overlay").remove();
						$("[role=dialog]").remove();
					}
				},
				width: 260,
				height: 150,
				autoOpen: true,
				closeOnEscape: false,
				modal: true,
				title: 'メッセージ',
				open: function(){
					$("#dialogWindow").html('メールアドレスを正確にご記入ください。');
				},
				overlay: {
					opacity: 0.5,
					background: "black"
				}
			});
			return false;
		}
	});


	var utmp = $("#detailLoginContainer").html();
	$("#detailLoginContainer").html("");
	$("body").prepend("<div id='dialogWindowWait'></div>")
	$("#dialogWindowWait").hide();
	$("#detailLoginBtn").click(function(){
		var loginFlag = 0;
		$("#dialogWindow").dialog({
			buttons: {
				"取消": function() {
					$(".ui-dialog-overlay").remove();
					$("[role=dialog]").remove();
				},
				"ログイン": function() {
					var username = $("#username").val();
					var password = $("#password").val();
					var shopid = $("#shopid").val();
					var area = $("#area").val();
					
					if(username == "" || password == ""){
						$("#errMsg").html("空の入力項目があります。");
						return false;
					}
					
					formurl = $("#loginForm").attr("action");
					
					$.ajax({
						url: formurl,
						data: "data[login][username]="+username+"&data[login][password]="+password,
						cache: false,
						type: "post",
						beforeSend: function(){
							scrollFigure = document.documentElement.scrollTop || document.body.scrollTop;
							scrollTop = document.documentElement.clientHeight+(scrollFigure*2) || document.body.scrollHeight+(scrollFigure*2);
							scrollLeft = document.documentElement.clientWidth || window.innerWidth;

							var defaultTop = (scrollTop-64)/2;
							var defaultLeft = (scrollLeft-64)/2;

							$("#dialogWindowWait").show().css("z-index","1500").css("position","absolute").html("<img src='/img/loading.gif' />").css("top","0px").css("left","0px").css("width","100%").css("height",scrollTop).css("background-color","#eee").css("opacity","0.6");
							$("img",$("#dialogWindowWait")).css("position","absolute").css("left",defaultLeft+"px").css("top",defaultTop+"px").css("opacity","0.6");
						},
						success: function(data, dataType){
							if(data == 0){
								$("#dialogWindowWait").hide();
								$("#errMsg").html("ログインに失敗しました！");
							}else{
								loginFlag = 1;
							}
							
						},
						complete: function(XMLHttpRequest, textStatus){
							if(loginFlag != 0){
								if(area != ""){
									location.href = "/"+area+"/detail/"+shopid+"/contact/";
								}else{
									location.href = "/detail/"+shopid+"/contact/";
								}
							}
						}
					});
					
				}
			},
			width: 400,
			height: 300,
			autoOpen: true,
			closeOnEscape: false,
			modal: true,
			title: 'ログインフォーム',
			open: function(){
				$("#dialogWindow").html(utmp);
			},
			overlay: {
				opacity: 0.5,
				background: "black"
			}
		});
		return false;
	});

	$("#checkBtn").click(function(){
		pageTracker._trackPageview('/G1/contact_check');
	});
	
});