$(document).ready(function(){
	$("input[type='checkbox']").bind('click', function(){
		filter_category(this);
	})
	$(".plus").bind('click', function(){
		show_selector(this);
	})	
	
	$("a.more").bind('click', function() {
		document.location=$(this).parent().find('a:eq(0)').attr('href');
	});
	
	if (location.hash!=''){
		list=location.hash.split('|');
		if (list.length>0){
			for(i=0; i<list.length; i++){
				if (list[i].match(/(-c)\d+(\,\d+){0,}/g)){
					listOfCats=list[i].split('-c')[1].split(',');
					if (listOfCats.length>0){
						for(j=0; j < listOfCats.length ; j++)
							$('#categ_'+listOfCats[j]).attr('checked' ,true);
					}else{
						$("input[type='checkbox'][name='cat_id[]']:eq(0)").attr('checked', true);
					}
					filter_category();
				}
				if (list[i].match(/(-r)\d+(\,\d+){0,}/g)){
					listOfRegs=list[i].split('-r')[1].split(',');
					if (listOfRegs.length>0){
						for(j=0; j < listOfRegs.length ; j++)
							$('#reg_'+listOfRegs[j]).attr('checked' ,true);
					}else{
						$("input[type='checkbox'][name='reg_id[]']:eq(0)").attr('checked', true);
					}
					filter_category();
				}
				if (list[i].match(/(-ct)\d+(\,\d+){0,}/g)){
					listOfCountry=list[i].split('-ct')[1].split(',');
					if (listOfCountry.length>0){
						$.ajax({
							type: "POST",
							url: "/category_filter.php",
							data : "action=find_region&int_ctrid="+listOfCountry[0],
							success: function (d){
								show_selector($("#reg_"+d).next(), 'country', listOfCountry);
							}
						});
					}else{
						$("input[type='checkbox'][name='reg_id[]']:eq(0)").attr('checked', true);
					}
				}
				if (list[i].match(/(-st)\d+(\,\d+){0,}/g)){
					listOfCountry=list[i].split('-st')[1].split(',');
					if (listOfCountry.length>0){
						$.ajax({
							type: "POST",
							url: "/category_filter.php",
							data : "action=find_region&int_stid="+listOfCountry[0],
							success: function (d){
								show_selector($("#reg_"+d).next(), 'state', listOfCountry);
							}
						});
					}else{
						$("input[type='checkbox'][name='reg_id[]']:eq(0)").attr('checked', true);
					}
				}
				if (list[i].match(/(-cy)\d+(\,\d+){0,}/g)){
					listOfCountry=list[i].split('-cy')[1].split(',');
					if (listOfCountry.length>0){
						$.ajax({
							type: "POST",
							url: "/category_filter.php",
							data : "action=find_country&city_id="+listOfCountry,
							dataType: "json",
							success: function (dat){
								show_selector($("#reg_"+dat.r).next(), 'city', dat);
							}
						});
					}else{
						$("input[type='checkbox'][name='reg_id[]']:eq(0)").attr('checked', true);
					}
				}
			}
		}
		
		/*cats = location.hash.split('-c')[1];
		listOfCats = cats.split(',');
		$("#all_categ").attr('checked','');
		if (listOfCats.length>0){
			for(i=0; i < listOfCats.length ; i++)
				$('#categ_'+listOfCats[i]).attr('checked' ,true);
		}else{
			$("input[type='checkbox'][name='cat_id[]']:eq(0)").attr('checked', true);
		}*/
		//filter_category();
	}
})


function filter_category(obj){
	/*category checkbox*/
	if(obj == $("input[type='checkbox'][name='cat_id[]']").get(0)){
		$("input[type='checkbox'][name='cat_id[]']:gt(0)").attr('checked', false);
	} else {
		$("input[type='checkbox'][name='cat_id[]']:eq(0)").attr('checked', false);
	}
	if(!$("input[type='checkbox'][name='cat_id[]']:checked").length) $("input[type='checkbox'][name='cat_id[]']:eq(0)").attr('checked', true);
	/*region checkbox*/
	if(obj == $("input[type='checkbox'][name='reg_id[]']").get(0)){
		$("input[type='checkbox'][name='reg_id[]']:gt(0)").attr('checked', false);
	} else {
		$("input[type='checkbox'][name='reg_id[]']:eq(0)").attr('checked', false);
	}
	if(!$("input[type='checkbox'][name='reg_id[]']:checked").length) $("input[type='checkbox'][name='reg_id[]']:eq(0)").attr('checked', true);
	
	if($("input[type='checkbox'][name='cat_id[]']:eq(0)").attr('checked')){
		type_cat='all';
	} else {
		type_cat='not_all';
	}
	
	page=1;
	if($(obj).attr('class')=='page_number'){
		page=$(obj).attr('rel');
	}
	
	cat_select=new Array();
	cat_select_hash=new Array();
	cat_name=new Array();
	$("input[type='checkbox'][name='cat_id[]']").each(function(){
		if ($(this).attr('checked')){
			cat_select.push($(this).val());
			if ($(this).val()!=$("#category_id").val()){
				cat_select_hash.push($(this).val());
				cat_name.push($(this).next().html().replace(' ','-'));
			}
		}
	})
	
	reg_select=new Array();
	reg_select_hash=new Array();
	reg_name=new Array();
	$("input[type='checkbox'][name='reg_id[]']").each(function(){
		if ($(this).attr('checked')){
			reg_select.push($(this).val());
			if ($(this).val()!='0'){
				reg_select_hash.push($(this).val());
				reg_name.push($(this).next().next().html().replace(' ','-'));
			}
		}
	})
	
	data={"cat_id":cat_select.join(","), "reg_id":reg_select.join(","), "type_cat":type_cat, 
					   "category_id": $("#category_id").val(), "int_regid": $("#int_regid").val(),
					   "int_ctrid": $("#int_ctrid").val(), "int_stid": $("#int_stid").val(),
					   "city_id":$("#city_id").val()};
	
	
	$("#content").load("/category_filter.php",
					   data,
					   function(){
							$(".tips").each(function(){
								//console.log($(this).attr("id"));
								$("#div_"+$(this).attr("id")).hide();
								$("#"+$(this).attr("id")).tooltip({
									bodyHandler: function() {
										//return $("#div_"+$(this).attr("id")).attr("id");
										return $("#div_"+$(this).attr("id")).html();
									},
									track: true,
									delay: 0,
									showURL: false
								});
							})
							$("input").bind('click', function(){
								filter_category(this);
							})
							$(".plus").bind('click', function(){
								show_selector(this);
							})
							hash();
	});
}
function show_selector(obj, type, list){
	if ($($(obj).prevAll("input").get(0)).attr('name')=='reg_id[]'){
		var int_regid=$($(obj).prevAll("input").get(0)).val();
		$("input[type='checkbox'][name='city_id[]']").attr('checked', false);
		$("div[class='filter_check filter_selector']").attr('class','filter_check');
		$($(obj).parents().get(0)).attr('class','filter_check filter_selector');
		
		$(".clear").hide();
		
		cat_select=new Array();
		$("input[type='checkbox'][name='cat_id[]']").each(function(){
			if ($(this).attr('checked'))
				cat_select.push($(this).val());
		})
		temp = $(obj).attr('class').split(' ')[1];
		countryHolderId = temp.split('_')[1];
		$("#country_"+countryHolderId).load("/category_filter.php",
							   {"action": "show_country", "int_regid":int_regid, "cat_id":cat_select.join(","), "category_id": $("#category_id").val()},
							   function(){
									/*$("#country_"+countryHolderId).css("border-bottom", "solid 1px #B6B6B4");
									$("#country_"+countryHolderId).css("background-color", "#D8DEF0");
									$("#country_"+countryHolderId).css("margin-right", "10px");*/
									$("#country_"+countryHolderId).show();
									$("input[name='country_id[]']").bind('click', function(){
										filter_by_country(this);
									})
									$("input[name='state_id[]']").bind('click', function(){
										filter_by_state(this);
									})
									$(".plus").bind('click', function(){
										show_selector(this);
									})
									if (type=='country'){
										if (list.length>0){
											for(j=0; j < list.length ; j++)
												$('#country_'+list[j]).attr('checked', true);
											$("input[type='checkbox'][name='reg_id[]']").attr('checked', false);
											$("input[type='checkbox'][name='city_id[]']").attr('checked', false);
											filter_by_country(this);
										}
									}
									if (type=='state'){
										if (list.length>0){
											for(j=0; j < list.length ; j++)
												$('#state_'+list[j]).attr('checked', true);
											$("input[type='checkbox'][name='reg_id[]']").attr('checked', false);
											$("input[type='checkbox'][name='city_id[]']").attr('checked', false);
											filter_by_state(this);
										}
									}
									if (type=='city'){
										if (list.s=='')
											show_selector($("#country_"+list.c).next(), 'city', list);
										else show_selector($("#state_"+list.s).next(), 'city', list);
										$("input[type='checkbox'][name='reg_id[]']").attr('checked', false);
										$("input[type='checkbox'][name='city_id[]']").attr('checked', false);
									}
								}
		);
	} else if($($(obj).prevAll("input").get(0)).attr('name')=='country_id[]') {
		var int_ctrid=($($(obj).prevAll("input").get(0)).val());
		$("#country_show_"+int_ctrid).load("/category_filter.php",
											{"action": "show_city_by_country", "int_ctrid":int_ctrid, "cat_id":cat_select.join(","), "category_id": $("#category_id").val()},
											function(){
												$("#country_show_"+int_ctrid).show();
												//$("input[type='checkbox'][name='city_id[]']").attr('checked', false);
												$("input[name='city_id[]']").bind('click', function(){
													filter_by_city(this);
												})
												if (type=='city'){
													ls=list.city.split(',');
													if (ls.length>0){
														for(j=0; j < ls.length ; j++){
															$('#city_'+ls[j]).attr('checked', true);
														}
														filter_by_city(this);
													}
												}
											}
										  );
	} else if($($(obj).prevAll("input").get(0)).attr('name')=='state_id[]'){
		var int_stid=($($(obj).prevAll("input").get(0)).val());
		$("#state_show_"+int_stid).load("/category_filter.php",
											{"action": "show_city_by_state", "int_stid":int_stid, "cat_id":cat_select.join(","), "category_id": $("#category_id").val()},
											function(){
												$("#state_show_"+int_stid).show();
												//$("input[type='checkbox'][name='city_id[]']").attr('checked', false);
												$("input[name='city_id[]']").bind('click', function(){
													filter_by_city(this);
												})
												if (type=='city'){
													ls=list.city.split(',');
													if (ls.length>0){
														for(j=0; j < ls.length ; j++){
															$('#city_'+ls[j]).attr('checked', true);
														}
														filter_by_city(this);
													}
												}
											}
										  );
	}
}
function filter_by_country(obj){
	/*category checkbox*/
	if(obj == $("input[type='checkbox'][name='cat_id[]']").get(0)){
		$("input[type='checkbox'][name='cat_id[]']:gt(0)").attr('checked', false);
	} else {
		$("input[type='checkbox'][name='cat_id[]']:eq(0)").attr('checked', false);
	}
	if(!$("input[type='checkbox'][name='cat_id[]']:checked").length) $("input[type='checkbox'][name='cat_id[]']:eq(0)").attr('checked', true);
	$("input[type='checkbox'][name='reg_id[]']").attr('checked', false);
	$("input[type='checkbox'][name='city_id[]']").attr('checked', false);
	if(!$("input[type='checkbox'][name='country_id[]']:checked").length) $("input[type='checkbox'][name='reg_id[]']:eq(0)").attr('checked', true);
	
	cat_select=new Array();
	$("input[type='checkbox'][name='cat_id[]']").each(function(){
		if ($(this).attr('checked')){
			cat_select.push($(this).val());
		}
	})
	ct_select=new Array();
	$("input[type='checkbox'][name='country_id[]']").each(function(){
		if ($(this).attr('checked')){
			ct_select.push($(this).val());
		}
	})
	$("#property_content").load("/category_filter.php",{"action": "property_by_country", "cat_id":cat_select.join(","), "ct_id":ct_select.join(","), "category_id": $("#category_id").val()},
								function(){
									$(".plus").bind('click', function(){
										show_selector(this);
									})
									$(".tips").each(function(){
										//console.log($(this).attr("id"));
										$("#div_"+$(this).attr("id")).hide();
										$("#"+$(this).attr("id")).tooltip({
											bodyHandler: function() {
												//return $("#div_"+$(this).attr("id")).attr("id");
												return $("#div_"+$(this).attr("id")).html();
											},
											track: true,
											delay: 0,
											showURL: false
										});
									})
								}
							);
	hash();
}
function filter_by_state(obj){
	$("input[type='checkbox'][name='reg_id[]']").attr('checked', false);
	$("input[type='checkbox'][name='city_id[]']").attr('checked', false);
	if(!$("input[type='checkbox'][name='state_id[]']:checked").length) $("input[type='checkbox'][name='reg_id[]']:eq(0)").attr('checked', true);
	
	cat_select=new Array();
	$("input[type='checkbox'][name='cat_id[]']").each(function(){
		if ($(this).attr('checked')){
			cat_select.push($(this).val());
		}
	})
	st_select=new Array();
	$("input[type='checkbox'][name='state_id[]']").each(function(){
		if ($(this).attr('checked')){
			st_select.push($(this).val());
		}
	})
	$("#property_content").load("/category_filter.php",{"action": "property_by_country", "cat_id":cat_select.join(","), "st_id":st_select.join(","), "category_id": $("#category_id").val()},
								function(){
									$(".plus").bind('click', function(){
										show_selector(this);
									})
									$(".tips").each(function(){
										//console.log($(this).attr("id"));
										$("#div_"+$(this).attr("id")).hide();
										$("#"+$(this).attr("id")).tooltip({
											bodyHandler: function() {
												//return $("#div_"+$(this).attr("id")).attr("id");
												return $("#div_"+$(this).attr("id")).html();
											},
											track: true,
											delay: 0,
											showURL: false
										});
									})
								}
							);
	hash();
}

function filter_by_city(obj){
	$("input[type='checkbox'][name='reg_id[]']").attr('checked', false);
	$("input[type='checkbox'][name='country_id[]']").attr('checked', false);
	$("input[type='checkbox'][name='state_id[]']").attr('checked', false);
	if(!$("input[type='checkbox'][name='city_id[]']:checked").length) $("input[type='checkbox'][name='reg_id[]']:eq(0)").attr('checked', true);
	
	cat_select=new Array();
	$("input[type='checkbox'][name='cat_id[]']").each(function(){
		if ($(this).attr('checked')){
			cat_select.push($(this).val());
		}
	})
	city_select=new Array();
	$("input[type='checkbox'][name='city_id[]']").each(function(){
		if ($(this).attr('checked')){
			city_select.push($(this).val());
		}
	})
	$("#property_content").load("/category_filter.php",{"action": "property_by_city", "cat_id":cat_select.join(","), "city_id":city_select.join(","), "category_id": $("#category_id").val()},
								function(){
									$(".tips").each(function(){
										//console.log($(this).attr("id"));
										$("#div_"+$(this).attr("id")).hide();
										$("#"+$(this).attr("id")).tooltip({
											bodyHandler: function() {
												//return $("#div_"+$(this).attr("id")).attr("id");
												return $("#div_"+$(this).attr("id")).html();
											},
											track: true,
											delay: 0,
											showURL: false
										});
									})
								}
							);
	
	hash();
}

function hash(){
	cat_select=new Array();
	cat_select_hash=new Array();
	cat_name=new Array();
	$("input[type='checkbox'][name='cat_id[]']").each(function(){
		if ($(this).attr('checked')){
			cat_select.push($(this).val());
			if ($(this).val()!=$("#category_id").val()){
				cat_select_hash.push($(this).val());
				cat_name.push($(this).next().html().replace(' ','-'));
			}
		}
	})
	reg_select=new Array();
	reg_select_hash=new Array();
	reg_name=new Array();
	$("input[type='checkbox'][name='reg_id[]']").each(function(){
		if ($(this).attr('checked')){
			reg_select.push($(this).val());
			if ($(this).val()!='0'){
				reg_select_hash.push($(this).val());
				reg_name.push($(this).next().next().html().replace(' ','-'));
			}
		}
	})
	ct_select=new Array();
	ct_select_hash=new Array();
	ct_name=new Array();
	$("input[type='checkbox'][name='country_id[]']").each(function(){
		if ($(this).attr('checked')){
			ct_select.push($(this).val());
			if ($(this).val()!='0'){
				ct_select_hash.push($(this).val());
				ct_name.push($(this).next().next().html().replace(' ','-'));
			}
		}
	})
	st_select=new Array();
	st_select_hash=new Array();
	st_name=new Array();
	$("input[type='checkbox'][name='state_id[]']").each(function(){
		if ($(this).attr('checked')){
			st_select.push($(this).val());
			if ($(this).val()!='0'){
				st_select_hash.push($(this).val());
				st_name.push($(this).next().next().html().replace(' ','-'));
			}
		}
	})
	ci_select=new Array();
	ci_select_hash=new Array();
	ci_name=new Array();
	$("input[type='checkbox'][name='city_id[]']").each(function(){
		if ($(this).attr('checked')){
			ci_select.push($(this).val());
			if ($(this).val()!='0'){
				ci_select_hash.push($(this).val());
				ci_name.push($(this).next().html().replace(' ','-'));
			}
		}
	})
	url=document.location.toString().split('#')[0];
	if ((cat_name!='') || (reg_name!='') || (ct_name!='') || (st_name!='') || (ci_name!='')){
		
		if (cat_name!=''){
			if (url==document.location.toString().split('#')[0])
				url+="#"+cat_name.join(',')+'-c'+cat_select_hash.join(",")
			else url+="|"+cat_name.join(',')+'-c'+cat_select_hash.join(",")
		}
		if (reg_name!=''){
			if (url==document.location.toString().split('#')[0])
				url+="#"+reg_name.join(',')+"-r"+reg_select_hash.join(",");
			else url+="|"+reg_name.join(',')+"-r"+reg_select_hash.join(",");
		}
		if (ct_name!=''){
			if (url==document.location.toString().split('#')[0])
				url+="#"+ct_name.join(',')+"-ct"+ct_select_hash.join(",");
			else url+="|"+ct_name.join(',')+"-ct"+ct_select_hash.join(",");
		}
		if (st_name!=''){
			if (url==document.location.toString().split('#')[0])
				url+="#"+st_name.join(',')+"-st"+st_select_hash.join(",");
			else url+="|"+st_name.join(',')+"-st"+st_select_hash.join(",");
		}
		if (ci_name!=''){
			if (url==document.location.toString().split('#')[0])
				url+="#"+ci_name.join(',')+"-cy"+ci_select_hash.join(",");
			else url+="|"+ci_name.join(',')+"-cy"+ci_select_hash.join(",");
		}
	}
	//alert(url);
	document.location=url;
}