var WindowOptions = {modal:true, draggable:false, autoOpen: false,width:720,resizable:false,title:"",position:"top",buttons:{}};
buttons = function(obj){obj.set(); $(this).dialog('close'); $('html,body').animate({ scrollTop: 0 });}

WindowOptions.title = 'カテゴリを追加';
WindowOptions.buttons = {"追加":function () {
  Category.set(); $(this).dialog('close'); $('html,body').animate({ scrollTop: 0 });},
"キャンセル":function(){  $(this).dialog('close') }}
$("#CategoryWindow").dialog(WindowOptions);


WindowOptions.title = 'エリアを追加';
WindowOptions.buttons = {"追加":function () {
  Area.set();$(this).dialog('close');$('html,body').animate({ scrollTop: 0 });
  },"キャンセル":function(){  $(this).dialog('close') }}
$("#AreaWindow").dialog(WindowOptions);


WindowOptions.title = 'サービスを追加';
WindowOptions.position = 'center';
WindowOptions.buttons = {"追加":function (){
  Service.set();$(this).dialog('close');$('html,body').animate({ scrollTop: 0 });
  },"キャンセル":function(){  $(this).dialog('close') }}
$("#ServiceWindow").dialog(WindowOptions);

WindowOptions.title = 'さらに条件を追加';
WindowOptions.buttons = {"追加":function (){
	More.set();$(this).dialog('close');$('html,body').animate({ scrollTop: 0 });
},"キャンセル":function(){  $(this).dialog('close') }}
$("#MoreWindow").dialog(WindowOptions);

function html_hidden(name,value) {
	return '<input type="hidden" name="'+name+'" value="'+value+'">';
}
var Category = {
	'load': function () {
		if (!$("#CategoryWindow").data('loaded')) {
			$("#tabs").tabs();
			$("#CategoryWindow").dialog('open');
			_self = this;
			$.getJSON("/shop/js_search.shtml?category=all",function (data,status){
				if (status === "success"){
					_self.build(data);
					_self.checked();
				}
			})
			$("#CategoryWindow").data('loaded',true);
		}else{
			this.checked();
			$("#CategoryWindow").dialog('open');
		}

	},
	'checked':function () {
		$("input[name=category1[]]:checked").attr('checked',false);
		$("input[name=category2[]]:checked").attr('checked',false);
		$("input[name=category3[]]:checked").attr('checked',false);
		var t = $("input[name=search_category1[]]");
		for (var i=0; i < t.length; i++) {
			$("#category1_"+t[i].value).attr("checked",true);
			this.clicked($("#category1_"+t[i].value).get(0));
		}
		var t = $("input[name=search_category2[]]");
		for (var i=0; i < t.length; i++) {
			$("#category2_"+t[i].value).attr("checked",true);
			this.clicked($("#category2_"+t[i].value).get(0));
		}
		var t = $("input[name=search_category3[]]");
		for (var i=0; i < t.length; i++) {
			$("#category3_"+t[i].value).attr("checked",true);
		}
	},
	'build': function(categories) {
		for (cat1 in categories) {
			html = '<form id="CategorySelectForm">';
			html += '<table class="tbl-lists">';
			var cat2_array = categories[cat1].cat2_array
			for (cat2 in cat2_array) {
				var id = cat2_array[cat2].id,
				name = cat2_array[cat2].name,
				count = cat2_array[cat2].count;
				html += '<tr><th>';
				html += '<input type="checkbox" name="category2[]" value="'+id+'" id="category2_'+id+'" class="category2 parent_category_id1_'+cat1+' category2_'+id+'">';
				html += '<label for="category2_'+id+'">' + name + '</label>';
				html += ' <span>(' + count + ')</span>';
				html += '</th>';
				var cat3_array = cat2_array[cat2].cat3_array,
				parent_id = id;
				html += '<td><ul class="roll">';
				for (cat3 in cat3_array){
					var id = cat3_array[cat3].id,
					name = cat3_array[cat3].name,
					count = cat3_array[cat3].count;
					if (id) {
						html += '<li class="item-input">';
						html += '<input type="checkbox" name="category3[]" value="'+id+'" id="category3_'+id+'" class="parent_category_id'+parent_id+' category3 category3_'+id+'">';
						html += '<label for="category3_'+id+'">' + name + '</label>';
						html += ' <span>(' + count + ')</span>';
						html += '</li>';
					}
				}
				html += '</ul>';
				html += '</td></tr>';
			}

			html += '<tr>';
			html += '<th>';
			html += '<input type="checkbox" name="category1[]" value="'+cat1+'" id="category1_'+cat1+'" class="category1 category1_'+cat1+'">';
			html += '<label for="category1_'+cat1+'">'+$("#category"+cat1+"_name").text()+'</label>全て';
			html +='</th>';
			html += '</tr>';
			
			html += "</table>";
			
			
			html += '</form>';
			$("#category"+cat1).html(html);

			for (cat2 in cat2_array) {
				var id = cat2_array[cat2].id;
				$("#category2_"+id).click(function (a){
					el = a.currentTarget;
					Category.clicked(el);
				});
			}

			$("#category1_"+cat1).click(function (a){
				Category.clicked(a.currentTarget);
			});
		}
		
		html = categories = cat2_array = cat3_array = null;
	},
	'clicked':function (el) {
		var parent_id = el.value, checked = el.checked;
		if ($(el).hasClass("category2")) {
			$(".parent_category_id"+parent_id).attr({'checked': checked});
			if (checked == true){
				$(".parent_category_id"+parent_id).attr({'disabled': true});
			}else{
				$(".parent_category_id"+parent_id).attr({'disabled': false});
			}
		}else if ($(el).hasClass("category1")){
			if (checked == true){
				$(".parent_category_id1_"+parent_id).attr({'checked': checked});
			}else{
				$(".parent_category_id1_"+parent_id).attr({'checked': checked});
			}
			_self=this;
			$(".parent_category_id1_"+parent_id).each(function (i,e){
				_self.clicked(e);
			});
			if (checked == true){
				$(".parent_category_id1_"+parent_id).attr({'disabled': checked});
			}else{
				$(".parent_category_id1_"+parent_id).attr({'disabled': checked});
			}
			
		}
	},
	'set': function () {
		var cat1 = $("input[name=category1[]]:checked"),
		cat2 = $("input[name=category2[]]:checked"),
		cat3 = $("input[name=category3[]]:checked"),
		html = input = "",
		f = $("#f2");

		count = cat1.length;
		for (i=0; i < count ; i++) {
			val = $("label[for="+cat1[i].id+"]").html();
			html += ''+val+' ';
			input += html_hidden("search_category1[]", cat1[i].value);
		}
		count = cat2.length;
		for (i=0; i < count ; i++) {
			if (!cat2[i].disabled) {
				val = $("label[for="+cat2[i].id+"]").html();
				html += ''+val+' ';
				input += html_hidden("search_category2[]", cat2[i].value);
			}
		}
		count = cat3.length;
		for (i=0; i < count ; i++) {
			if (!cat3[i].disabled) {
				val = $("label[for="+cat3[i].id+"]").html();
				html += ''+val+' ';
				input += html_hidden("search_category3[]", cat3[i].value);
			}
		}
		$("input[name=search_category1[]]").remove();
		$("input[name=search_category2[]]").remove();
		$("input[name=search_category3[]]").remove();
		if (html) {
			html = ' '+html+' ';
		}
		$("#categoryContext").html( html );
		$(f).append(input);
	}
};
var Area = {
	'load':function(){
		if (!$("#AreaWindow").data('loaded')) {
			$("#AreaWindow").dialog('open');
			$.getJSON("/shop/js_search.shtml?area=all",function (data,status){
				if (status === "success"){
					Area.build(data);
					Area.checked();
				}
			})
			$("#AreaWindow").data('loaded',true);
		}else{
			this.checked();
			$("#AreaWindow").dialog('open');
		}
	},
	'checked':function() {
		$("input[name=area1[]]:checked").attr('checked',false);
		$("input[name=area2[]]:checked").attr('checked',false);
		var t = $("input[name=search_area1[]]");
		for (var i=0; i < t.length; i++) {
			$("#area1_"+t[i].value).attr("checked",true);
			this.clicked($("#area1_"+t[i].value).get(0));
		}
		var t = $("input[name=search_area2[]]");
		for (var i=0; i < t.length; i++) {
			$("#area2_"+t[i].value).attr("checked",true);
		}
	},
	'build':function(areas) {
		html = '<form id="areaSelectForm">';
		html += '<table class="tbl-lists">';
		for (area1 in areas) {
			var id = areas[area1].area1_id,
			name = areas[area1].area1_name,
			count = areas[area1].count;
			html += '<tr>';
			html += '<th>';
			html += '<input type="checkbox" name="area1[]" value="'+id+'" id="area1_'+id+'" class="area1_'+id+'">';
			html += '<label for="area1_'+id+'">' + name + '</label>';
			html += ' <span>(' + count + ')</span>';
			html += '</th>';

			var area2_array = areas[area1].area2_array
			html += '<td>';
			html += '<ul class="roll">';
			var parent_id = id;
			for (area2 in area2_array) {
				var id = area2_array[area2].area2_id,
				name = area2_array[area2].area2_name,
				count = area2_array[area2].count;
				if (id) {
					html += '<li class="item-input">';
					html += '<input type="checkbox" name="area2[]" value="'+id+'" id="area2_'+id+'" class="parent_area_id'+parent_id+' area2_'+id+'">';
					html += '<label for="area2_'+id+'">' + name + '</label>';
					html += ' <span>(' + count + ')</span>';
					html += '</li>';
				}
			}
			html += "</ul>";
			html += "</td>";
			html += "</tr>";
		}
		html += "</table>";
		html += '</form>';
		$("#AreaWindow").html(html);

		for (area1 in areas) {
			var id = areas[area1].area1_id;
			$("#area1_"+id).click(function (a){
				el = a.currentTarget;
				Area.clicked(el);
			});
		}
		html = areas = area2_array = area3_array = null;
	},
	'clicked':function (el) {
		var parent_id = el.value;
		checked = el.checked;
		$(".parent_area_id"+parent_id).attr({'checked': checked});
		if (checked == true){
			$(".parent_area_id"+parent_id).attr({'disabled': true});
		}else{
			$(".parent_area_id"+parent_id).attr({'disabled': false});
		}
	},
	'set': function() {
		var area1 = $("input[name=area1[]]:checked"), area2 = $("input[name=area2[]]:checked"),
		html = input = "",
		f = $("#f2");

		count = area1.length;
		for (i=0; i < count ; i++) {
			val = $("label[for="+area1[i].id+"]").html();
			html += ''+val+' ';
			input += html_hidden("search_area1[]", area1[i].value);
		}
		count = area2.length;
		for (i=0; i < count ; i++) {
			if (!area2[i].disabled) {
				val = $("label[for="+area2[i].id+"]").html();
				html += ''+val+' ';
				input += html_hidden("search_area2[]", area2[i].value);
			}
		}
		$("input[name=search_area1[]], input[name=search_area2[]]").remove();
		if (html) {
			html = ' '+html+' ';
		}
		$("#areaContext").html(html);
		$(f).append(input);
	}
};
var Service = {
	'load':function(){
		if (!$("#ServiceWindow").data('loaded')) {
			$.getJSON("/shop/js_search.shtml?service=all",function (data,status){
				if (status === "success"){
					Service.build(data);
					Service.checked();
					$("#ServiceWindow").dialog('open');
				}
			})
			$("#ServiceWindow").data('loaded',true);
		}else{
			Service.checked();
			$("#ServiceWindow").dialog('open');
		}
	},
	'checked':function(){
		$("input[name=service[]]:checked").attr('checked',false);
		var t = $("input[name=search_service[]]");
		for (var i=0; i < t.length; i++) {
			$("#service_"+t[i].value).attr("checked",true);
		}
	},
	'build':function(services){
		html = '<form id="serviceSelectForm">';
		html += '<table class="tbl-lists">';
		for (service in services){
			var srvs = services[service];
			html += '<tr>';
			html += '<th>';
			if (service == 1){html += 'サービス';}
			else if (service == 2){html += 'シチュエーション';}
			html += '</th>';
			html += '<td>';
			html += '<ul class="roll">';
			for (srv in srvs){
				var id = srvs[srv].svc_id,
				name = srvs[srv].svc_name;
				html += '<li class="item-input">';
				html += '<input type="checkbox" name="service[]" value="'+id+'" id="service_'+id+'" class="service_'+id+'">';
				html += '<label for="service_'+id+'">' + name + '</label>';
				html += '</li>';
			}
			html += '</ul>';
			html += '</td>';
		}
		html += "</table>";
		html += '</form>';
		$("#ServiceWindow").html(html);
		html = service = services = srvs = null;
	},
	'set':function(){
		var service = $("input[name=service[]]:checked"),
		html = input = "",
		f = $("#f2");
		count = service.length;
		for (i=0; i < count ; i++) {
			val = $("label[for="+service[i].id+"]").html();
			html += ''+val+' ';
			input += html_hidden("search_service[]",service[i].value);
		}

		$("input[name=search_service[]]").remove();
		if (html) {
			html = ' '+html+' ';
		}
		$("#serviceContext").html(html);
		$(f).append(input);
	}
};
var More = {
	'load':function(){
		More.checked();
		$("#MoreWindow").dialog('open');
	},
	'checked':function(){
		active_time = $("input[name=search_active_time]:checked");
		$("input[name=shop_hour[]]").attr('checked',false);
		$("#blockTime,#blockTimeSelect").hide();
		if (!active_time.length) {
			$("#blockTime,#blockTimeSelect").show();
			var t = $("input[name=search_time[]]");
			for (var i=0; i < t.length; i++) {
				$("#shop_hour_"+t[i].value).attr("checked",true);
			}
			/*
			var t = $("input[name=search_open_time]").val();
			if (t) {
				t = t.split(/:/);
				$("select[name=open_time_hour]").val( t[0] );
				$("select[name=open_time_minute]").val( t[1] );
			}
			var t = $("input[name=search_close_time]").val();
			if (t) {
				t = t.split(/:/);
				$("select[name=close_time_hour]").val( t[0] );
				$("select[name=close_time_minute]").val( t[1] );
			}
			*/
			var t = $("input[name=search_over_time]").val();
			if (t) {
				t = t.split(/:/);
				$("select[name=over_time_hour]").val( t[0] );
			}
			
			
		}else{
			this.resetTime();
		}

		t = $("input[name=search_grapevine_num]").val();
		$("input[name=grapevine_num]").val( t );

		t = $("input[name=search_pricemin]").val();
		$("select[name=pricemin]").val( t );
		t = $("input[name=search_pricemax]").val();
		$("select[name=pricemax]").val( t );

		var list = $("input[name=search_parking_type[]]");
		for (var i=0;i < list.length; i++) {
			$("#parking_type_" + list[i].value).attr("checked",true);
		}
	},
	'build':function(){},
	'set':function() {
		var html = input = "";
		var area1 = $(".pricemax").val();
		f = $("#f2");
		grapevine_num = $("input[name=grapevine_num]").val();
		pricemin = $("#pricemin").val();
		pricemax = $("#pricemax").val();
		parking = $(".input_parking_type:checked");
		active_time = $("input[name=search_active_time]:checked");
		shop_hour = $("input[name=shop_hour[]]:checked");
		//open_time_hour = $("select[name=open_time_hour]").val();
		//open_time_minute = $("select[name=open_time_minute]").val();
		//close_time_hour = $("select[name=close_time_hour]").val();
		//close_time_minute = $("select[name=close_time_minute]").val();
		over_time_hour = $("select[name=over_time_hour]").val();
		if (pricemin) { input += html_hidden("search_pricemin",pricemin);}
		if (pricemax) { input += html_hidden("search_pricemax",pricemax);}
		if (pricemin && pricemax) {
			html += '<div>□予算帯：' + pricemin+'円 ～'+pricemax + '円<div>';
		} else if(pricemin) {
			html += '<div>□予算帯：' + pricemin + '円 ～ <div>';
		} else if (pricemax) {
			html += '<div>□予算帯： ～' + pricemax + '円<div>';
		}

		if (!active_time.length) {
			var t = shop_hour.length
			for (var i=0; i < t; ++i) {
				if (shop_hour[i].value == 1) {
					input += html_hidden("search_time[]", shop_hour[i].value);
					html += '<div>営業時間:24時間営業<div>';
				}
			}
			if (!t){
				if (over_time_hour) {
					input += html_hidden("search_over_time", over_time_hour + ':00');
					if (24 < over_time_hour){
						over_time = '翌'+(over_time_hour-24) + ':00';
					}else{
						over_time = over_time_hour + ':00';
					}
					html += '<div>営業時間:' + over_time+"以降も営業している</div>";
				}
			}
		}else{
			this.resetTime();
		}

		if (0 < grapevine_num) {
			input += html_hidden("search_grapevine_num",grapevine_num);
			html += '<div>' + grapevine_num + '件以上クチコミのあるお店<div>';
		}
		var length = parking.length;tmp="";
		for (var i=0; i < length; i++) {
			var val = parking[i].value;
			label = $("label[for="+parking[i].id+"]").html();
			input += html_hidden("search_parking_type[]",val);
			tmp += '' + label + ' ';
		}
		if (tmp) {
			html += "駐車場:" + tmp;
		}

		$("input[name=search_pricemin], input[name=search_pricemax]").remove();
		$("input[name=search_grapevine_num]").remove();
		$("input[name=search_parking_type[]]").remove();
		$("input[name=search_time[]]").remove();
		$("input[name=search_open_time],input[name=search_close_time],input[name=search_over_time]").remove();
		if (html) {
			html = '<ul>'+html+'</ul>';
		}

		$("#moreContext").html(html);
		$(f).append(input);
	},
	'resetTime': function() {
		$("input[name=shop_hour[]]").attr('checked',false);
		//$("select[name=open_time_hour]").val(0);
		//$("select[name=open_time_minute]").val(0);
		//$("select[name=close_time_hour]").val(0);
		//$("select[name=close_time_minute]").val(0);
		$("select[name=over_time_hour]").val(0);
	}
};

function go_sort(v) {
  if (!v) return;document.f2.sort.value = v;document.f2.submit();
}
