/* イベント初期化 */
$(function() {
	$(".showMylist").click(function(){ window.location.href = "/mypage/" ; return false;});
	$(".showNairan").click(function(){ window.location.href = "/search/request.html"; return false; });
	$(".showRequest").click(function(){ window.location.href = "/search/request.html";  return false;});
	$(".changeCondition").click(function(){ window.location.href = "/search/condition.html";  return false;});
	$("#clearCondition").click(onClearClick);

	// 詳細条件ウィンドウを閉じる（閉じるボタン押下）
	$('#dialog .close').click(function (e) {
		e.preventDefault();
		$('#mask').hide();
		$('#dialog').hide();
		$('#saodialog').hide();
		doSearch();
	});

	// 詳細条件ウィンドウを閉じる（枠外をクリック）
	$('#mask').click(function () {
		$(this).hide();
		setCondition();
		$('#dialog').hide();
		$('#saodialog').hide();
		doSearch();
	});

	// 検索ボタンイベントハンドラ（キーワード検索で使用）
	$("#frmCondition").submit(function(e) {
		e.preventDefault();
		doSearch();
	});

 	// 詳細を全てクリアするイベントハンドラ
	$('#dialog .clear').click(onClearClick);

	doSearch();
});

function onSearchClick()
{
	doSearch();
}

// 詳細条件ウィンドウを開く際のイベントハンドラ
function onOptionClick()
{
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	$('#mask').css({'width':maskWidth,'height':maskHeight});
	$('#mask').fadeTo("fast", 0.5);
	var winH = $(window).height();
	var winW = $(window).width();
	$("#dialog").css('top',  winH/2-$('#dialog').height()/2);
	$("#dialog").css('left', winW/2-$('#dialog').width()/2);
	$("#dialog").css('position', 'fixed');
	$("#dialog").fadeTo("fast", 1.0);
}


/******************************************************************************
 *検索詳細条件の可視化
 */
function setCondition()
{
	var d = zen_make_params('#frmCondition');
	d["cmd-condition"] = 1;
	$.post(
		$("#frmCondition").attr("action"),
		d,
		function (ret)
		{
			if (ret.status == "SUCCESS")
			{
				$("#condview").html(ret.html);
			}
			else alert(ret.msg);
		},
		"json"
	);
}


/******************************************************************************
 *検索処理（検索ボタン以外）
 */
function doSearch()
{
	$.blockUI({message: $('div#displayBox')});

	var d = zen_make_params('#frmCondition');
	d["cmd-search"] = 1;
	$.post(
		location.href,
		d,
		function (ret)
		{
			$.unblockUI();
			if (ret.status == "SUCCESS")
			{
				$('#mask').hide();
				$('#dialog').hide();
				$("#list").html(ret.list);
				$("#condview").html(ret.cond);
				if (ret.cnt <= 20) getRecommendPanel();
			}
			else alert(ret.msg);
		},
		"json"
	);
}

function getRecommendPanel()
{
	$("#listfooter").html('<div style="padding: 10px; border: solid 1px #7f7f7f; text-align: center;"><p>おすすめ物件を検索しています。<br />しばらくお待ちください...</p><img src="/search/images/searching.gif" /></div>');
	var d = zen_make_params("#frmCondition", "getRecommend");
	$.post(
		location.href,
		d,
		function (ret) {
			if (ret.status == "SUCCESS") $("#listfooter").html(ret.html);
			else alert(ret.msg);
		},
		"json"
	);
}


/******************************************************************************
 * エリア変更イベントハンドラ
 * @param li
 * @returns {Boolean}
 */
function onAreaTabClick(code) {
	$.post(
		location.href,
		{ "cmd-changeArea": 1, "code": code },
		function (ret) {
			if (ret.status == "SUCCESS") {
				location.href = ret.url;
			}
			else {
				alert(ret.msg);
			}
		},
		"json"
	);
	return false;
}

/******************************************************************************
 * 広さのClickイベントハンドラ
 * @param li
 * @returns {Boolean}
 */
function onSpaceClick(li) {
	var newfrom = parseInt($(li).attr("from"));
	var newto   = parseInt($(li).attr("to"));
	var oldfrom = parseInt($("#spacelist input[name=spfrom]").val());
	var oldto   = parseInt($("#spacelist input[name=spto]").val());
	if (oldfrom == -1 && oldto == -1) {
		$(li).addClass("on");
		$("#spacelist input[name=spfrom]").val(newfrom);
		$("#spacelist input[name=spto]").val(newto);
	}
	else {
		if (oldto < newto) {
			// 範囲の右
			$("#spacelist li").each(function () {
				var from = parseInt($(this).attr("from"));
				if (from > oldfrom && from <= newfrom) $(this).addClass("on");;
			});
		}
		else if (oldto == newto) {
			// 右端
			$(li).removeClass("on");
		}
		else if (oldfrom == newfrom) {
			// 左端
			$(li).removeClass("on");
		}
		else if (oldfrom > newfrom) {
			// 範囲の左
			$("#spacelist li").each(function () {
				var from = parseInt($(this).attr("from"));
				if (newfrom <= from && from < oldfrom) $(this).addClass("on");
			});
		}
		else {
			// 範囲内
			$("#spacelist li").each(function () {
				var from = parseInt($(this).attr("from"));
				if (from == newfrom) $(this).addClass("on"); else $(this).removeClass("on");
			});
		}
	}

	var min = 9999; max = 0;
	$("#spacelist li").each(function(){
		if ($(this).attr("class") == "on") {
			min = Math.min(min, parseInt($(this).attr("from")));
			max = Math.max(max, parseInt($(this).attr("to")));
		}
	});

	if (min == 9999) min = -1;
	if (max == 0) max = -1;
	$("#spacelist input[name=spfrom]").val(min);
	$("#spacelist input[name=spto]").val(max);

	doSearch();
	return false;
}


/******************************************************************************
 * 賃料タイプの切り替えイベントハンドラ
 * @param li
 * @returns {Boolean}
 */
function onFeeTypeClick(type) {
	// 坪単価の場合
	var d = { "type": type, "cmd-feeType": 1 };
	$.post(
		location.href,
		d,
		function (ret) {
			if (ret.status == "SUCCESS") {
				$("#feelist input[name=ftype]").val(type);
				$("#feelist").html(ret.html);
			}
			else {
				alert(ret.msg);
			}
		},
		"json"
	);
	return false;
}


/******************************************************************************
 * 賃料のClickイベントハンドラ
 * @param li
 * @returns {Boolean}
 */
function onFeesClick(li) {
	var newfrom = parseInt($(li).attr("from"));
	var newto   = parseInt($(li).attr("to"));
	var oldfrom = parseInt($("#feelist input[name=ffrom]").val());
	var oldto   = parseInt($("#feelist input[name=fto]").val());
	if (oldfrom == -1 && oldto == -1) {
		$(li).addClass("on");
		$("#feelist input[name=ffrom]").val(newfrom);
		$("#feelist input[name=fto]").val(newto);
	} else {
		if (oldto < newto) {
			// 範囲の右
			$("#feelist li").each(function () {
				var from = parseInt($(this).attr("from"));
				if (from > oldfrom && from <= newfrom) $(this).addClass("on");;
			});
		}
		else if (oldto == newto) {
			// 右端
			$(li).removeClass("on");
		}
		else if (oldfrom == newfrom) {
			// 左端
			$(li).removeClass("on");
		}
		else if (oldfrom > newfrom) {
			// 範囲の左
			$("#feelist li").each(function () {
				var from = parseInt($(this).attr("from"));
				if (newfrom <= from && from < oldfrom) $(this).addClass("on");
			});
		}
		else {
			// 範囲内
			$("#feelist li").each(function () {
				var from = parseInt($(this).attr("from"));
				if (from == newfrom) $(this).addClass("on"); else $(this).removeClass("on");
			});
		}
	}

	var min = 999999999; max = 0;
	$("#feelist li").each(function(){
		if ($(this).attr("class") == "on") {
			min = Math.min(min, parseInt($(this).attr("from")));
			max = Math.max(max, parseInt($(this).attr("to")));
		}
	});

	if (min == 999999999) min = -1;
	if (max == 0) max = -1;
	$("#feelist input[name=ffrom]").val(min);
	$("#feelist input[name=fto]").val(max);


	doSearch();
	return false;
}


/* ボタン型選択のイベントハンドラー */
function onBtnClick(o) {
	var id	= $(o).attr("id");
	var d = { "id" : id , 'href':$(o).attr("href") };
	var cmd  = "cmd-" + $(o).attr("cmd");
	d[cmd] = 1;
	$.post(
		'/search/ui/',
		d,
		function (ret) {
			if (ret.status == "SUCCESS") {
				if(ret.msg) alert(ret.msg);

				// コントロールの更新
				if (ret.html) {
					$.each(ret.html, function(cid, body) {
						if($(cid)&&$(cid).html) $(cid).html(''+ body);
						if (cid == 'action') $('#frmCondition').attr('action', ''+body);// フォームの飛び先を変更 （地域・路線変更時
					});
				}
			} else {
				alert(ret.msg);
			}
		},
		"json"
	);
	return false;
}

function onShowDetailSearchDialog()
{
}

/******************************************************************************
 * 詳細条件をクリアする
 */
function onClearClick()
{
	// select
	$('#cmb_dpfrom').attr('selectedIndex', 0);
	$('#cmb_dpto').attr('selectedIndex', 12);

	// radio
	$('#rad_time').filter(function(){return ($(this).val() == 0)}).attr("checked", true);
	$('#rad_dist').filter(function(){return ($(this).val() == 0)}).attr("checked", true);
	$('#rad_old').filter(function(){return ($(this).val() == 0)}).attr("checked", true);
	$('#rad_biglevel').filter(function(){return ($(this).val() == 1)}).attr("checked", true);
	$('#rad_evlevel').filter(function(){return ($(this).val() == 1)}).attr("checked", true);
	$('#rad_heightlevel').filter(function(){return ($(this).val() == 2500)}).attr("checked", true);
	$('#rad_tenant').filter(function(){return ($(this).val() == 0)}).attr("checked", true);
	$('#rad_floor').filter(function(){return ($(this).val() == 0)}).attr("checked", true);
	$('#rad_pub').filter(function(){return ($(this).val() == 0)}).attr("checked", true);

	// check
	$('#chk_reikin').attr('checked', false);
	$('#chk_renewal').attr('checked', false);
	$('#chk_tower').attr('checked', false);
	$('#chk_taishin').attr('checked', false);
	$('#chk_big').attr('checked', false);
	$('#chk_ev').attr('checked', false);
	$('#chk_air').attr('checked', false);
	$('#chk_sec').attr('checked', false);
	$('#chk_24h').attr('checked', false);
	$('#chk_toilet1').attr('checked', false);
	$('#chk_toilet2').attr('checked', false);
	$('#chk_oa').attr('checked', false);
	$('#chk_height').attr('checked', false);
	$('#chk_sep').attr('checked', false);

	return false;
}

/******************************************************************************
 * 坪数をクリアする
 */
function onClearSpaceCilck()
{
	$("#spacelist li").each(function () { $(this).removeClass("on"); });
	$("#spacelist input[name=spfrom]").val(-1);
	$("#spacelist input[name=spto]").val(-1);
	doSearch();
	return false;
}

/******************************************************************************
 * 賃料をクリアする
 */
function onClearFeeCilck()
{
	$("#feelist li").each(function () { $(this).removeClass("on"); });
	$("#feelist input[name=ffrom]").val(-1);
	$("#feelist input[name=fto]").val(-1);
	doSearch();
	return false;
}

