﻿var _taihiFrom;
var _taihiTo;

/**
 * HTML LOAD
 */
$(function(){	
    // Firefoxでヘッダー幅が変化するのを抑制する
    $(".joken[@summary=簡易条件] th").width(40);

	_taihiFrom = $("#rentfrom > option");
	_taihiTo = $("#rentto > option");

	// 選択状態復元
	restoreList_base();

	// 賃料条件Fromの変更時
	$("#rentfrom").change(function(){	
/*
 		var selIdx = $("#rentto > option[@selected]").val();

	
		$("#rentto").children().remove();
		$.each(_taihiTo, function(key, data) {
			$("#rentto").append($("<option>").html(this.innerHTML).attr({ value: this.value }));
		});	
		$("#rentto > option[@value=" + selIdx + "]").attr("selected", "selected");
		$("#rentto > option:gt(0)[@value=" + this.value + "]").remove();
*/	
		disabledInput(true);
		if (window.updateList) {updateList();}
		disabledInput(false);
	});
	
	// 賃料条件Toの変更時
	$("#rentto").change(function(){
/*
 		var selIdx = $("#rentfrom > option[@selected]").val();

	
		$("#rentfrom").children().remove();
		$.each(_taihiTo, function(key, data) {
			$("#rentfrom").append($("<option>").html(this.innerHTML).attr({ value: this.value }));
		});	
	
		$("#rentfrom > option[@value=" + selIdx + "]").attr("selected", "selected");
		$("#rentfrom > option:gt(0)[@value=" + this.value + "]").remove();
*/	
		disabledInput(true);
		if (window.updateList) {updateList();}
		disabledInput(false);
	});
	// 賃料条件komiの変更時
	$("#rentkomi").change(function(){
		disabledInput(true);
		if (window.updateList) {updateList();}
		disabledInput(false);
	});
	// 間取り条件の追加/削除時
	$("input[@name='roomlayout[]']").click(function(){
		disabledInput(true);
		if (window.updateList) {updateList();}
		disabledInput(false);
	});
	
	// 他画面から戻ってきた場合の選択状態復元
	var arr = getQueryParams();
	if(arr!=null){
		if (arr['rentfrom']) {
			$("#rentfrom > option[@value=" + arr['rentfrom'] + "]").attr("selected", "selected");
		}
		if (arr['rentto']) {
			$("#rentto > option[@value=" + arr['rentto'] + "]").attr("selected", "selected");
		}
		if (arr['rentkomi']) {
			$("#rentkomi").attr("checked", "checked")
		}
		if (arr['roomlayout']) {
			var arr = arr['roomlayout'].split(',');
			for (var i = 0; i < arr.length; i ++) {
	 			$(".joken input[@type=checkbox][@value=" + arr[i] + "]").attr("checked", "checked");
			}
		}
	}
		
	$(window).unload(function() {
		saveList_base();
	});
});

/**
 * SELECTボックスの状態をクッキーに保存する。
 */
function saveList_base()
{
	
	// 賃料
	var rentFrom = $("#rentfrom > option[@selected]").val();
	var rentTo = $("#rentto > option[@selected]").val();
	// 下限値のほうが値が大きいときは、上限と下限を入れ替える。
	if (parseInt(rentFrom) > parseInt(rentTo)) {
		tmp = rentFrom;
		rentFrom = rentTo;
		rentTo = tmp;
	}
	var rentKomi = $("#rentkomi").attr("checked") ? $("#rentkomi").val() : '';
	setCookieValue("bs_rent", rentFrom + "," + rentTo + "," + rentKomi);
	
	// 間取り
	madori = '';
	$("input[@name='roomlayout[]'][@checked]").each(function() {
		madori += this.value + ",";
	});	
	if (madori.length > 0) {madori= madori.substring(0, madori.length - 1);}

	setCookieValue("bs_roomlayout", madori);

	// 詳細条件の内容を保存する。
	if ($("form[@name=detailcondition]").size() > 0) {
		setCookieValue("bs_detail", $("form[@name=detailcondition]").formSerializeCustom());
	}

}

/**
 * クッキーに保存しているSELECTボックスの状態を復元する。
 */
function restoreList_base()
{
	var qs = getQueryParams();
	var mode = (qs!=null)?qs.action:"index";

	if(mode != 'index'){
		// 賃料の選択状態を復元
		data = getCookieValue('bs_rent');
		if (data) {
			split = data.split(",");
			var rentFrom = split[0];
			var rentTo = split[1];
			var rentKomi = split[2];
			
			$("#rentfrom > option[@value=" + rentFrom + "]").attr("selected", "selected");
			$("#rentto   > option[@value=" + rentTo + "]").attr("selected", "selected");
			if (rentKomi) {
			    $("#rentkomi").attr("checked", "checked");
			}
		} else {
			$("#rentfrom").children().attr("selected","");
			$("#rentto").children().attr("selected","");
			$("#rentkomi").attr("checked", "")
		}
		
		// 間取りの選択状態を復元
		data = getCookieValue('bs_roomlayout');
		if (data) {
			split = data.split(",");
			$.each(split, function() {
				$("input[@name='roomlayout[]'][@value=" + this + "]").attr("checked", "checked");
			});
		} else {
		}
	}
	
	// 詳細条件の復元
	data = getCookieValue('bs_detail');
	formObj = $("form[@name=detailcondition]");
	if (data && formObj.size() > 0) {
		splitpara = data.split("&");
		formObj = new Array();
		for (i = 0; i < splitpara.length; i++) {
		    tmp = splitpara[i].split('=');
		    formObj[i] = new Object();
		    formObj[i].key = tmp[0];
		    formObj[i].value= unescape(tmp[1]).split(',');
		}
		
		for (i = 0; i < formObj.length; i++) {
		    key = formObj[i].key;
		    value = formObj[i].value;
		    for (j = 0; j < value.length; j++) {
		    	obj = $("form[@name=detailcondition] [@name=" + key + "]");
				if (obj.size() > 0) { // keyが見つかったもののみ復元する
			    	type = obj.get(0).type;
			    	switch (type) {
				    	case 'radio':
				    	case 'checkbox':
				        	$("form[@name=detailcondition] [@name=" + key + "][@value=" + value[j] + "]").attr('checked', 'checked');
				        	break;
				        case 'select-one':
							obj.val(value[j]);
							break;
						default:
					
					}
				}
		    }
		}	
	}
	
	removeCookieValue(new Array('bs_rent', 'bs_roomlayout', 'bs_detail'));

}
