$(function()
{
	// 新着物件初期化
	$('#newcommer-list').scrollShow({
		view:'#newcommer-view',
		itemSize:{
			height:'auto',
			width:'auto'
		},
		elements:'img.dmy',
		content:'#newcommer-images',
		wrappers:'simple',
		navigators:'a.arrow',
		navigationMode:'sr',
		circular:true,
		start:0
	});

	$("#clearCondition").click(function(){
		$.post(
			'/search/ui/',
			{"cmd-clearCondition": 1},
			function (ret)
			{
				if (ret.status == "SUCCESS")
				{
					// 検索条件一覧を更新
					if(ret.url)
					{
						window.location.href= ret.url;
					}
					else
					{
						window.location.href= "./";
					}
				}
				else
				{
					alert(ret.msg);
				}
			},
			"json"
		);
		return false;
	});

	$("#searchbox #dialog .control").click(onSearchClick);

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

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

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


function onSearchClick()
{
	var d = zen_make_params("#frmCondition");
	d["cmd-search"] = 1;
	$.post(
		location.href,
		d,
		function (ret) { if (ret.status == "SUCCESS") location.href = ret.url; else alert(ret.msg); },
		"json"
	);
	return false;
}

// 詳細条件ウィンドウを開く際のイベントハンドラ
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")
			{
				$("#search_condition").html(ret.html);
			}
			else alert(ret.msg);
		},
		"json"
	);
}


/******************************************************************************
 * エリア変更イベントハンドラ
 * @param li
 * @returns {Boolean}
 */
function onAreaTabClick(code) {
	var d = { "code": code, "cmd-changeArea": 1 };
	$.post(
		location.href,
		d,
		function (ret) {
			if (ret.status == "SUCCESS") {
				$("#searchbox").html(ret.html);
			}
			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);
	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") {
				$("#searchbox").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 onMylistClick(o){

	var code = $(o).attr("code");
	var seq  = $(o).attr("seq");
	var d = { "code" : code, "seq" : seq };
	var cmd  = "cmd-" + $(o).attr("cmd") + "MyList";	// add or del
	d[cmd] = 1;
	$("#body").css('cursor','wait');
	$.post(
		'/search/',
		d,
		function (ret)
		{
			$("#body").css('cursor','auto');

			if (ret.status == "SUCCESS")
			{
				// 一覧の更新
				$("#list_" + code + "_" + seq + "_1").html(ret.list1);
				$("#list_" + code + "_" + seq + "_2").html(ret.list2);

				$(".showMylist").attr('src','/search/images/btn_mylist_blink.gif');

				if(ret.msg)
				{
					alert(ret.msg);
				}
			}
			else
			{
				alert(ret.msg);
			}
		},
		"json"
	);
}


/* ボタン型選択のイベントハンドラー */
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 onClearCilck()
{
	// 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);
	return false;
}


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

