$(document).ready(function(){
	makeSelect();
	checkDetails();	
	$("#region").change(function(){
		makeSelect();
	});	
	$("#city").autocomplete(getCityUrl(),{
		 max: 10,
		 multiple: false,
		 cacheLength: 0, 
		 extraParams: {
			region: function() { return $("#region").val();	},
			powiat: function() { return $("#powiat").val();	}
		 }
	});
	$("#district").autocomplete(getDistrictUrl(),{
		max: 10,
		multiple: true,
		cacheLength: 0, 
		extraParams: {
			region: function() { return $("#region").val();	},
			powiat: function() { return $("#powiat").val();	},
			city: function() { return $("#city").val();	}
		}
	});
	$("#showMore").click(function(){
		showDetails();
		return false; });
	
	$("#hideHref").click(function(){
		hideDetails();
		return false; });
	
});
function checkDetails(){ 
	if($("#details").val()=="yes")
	showDetails(); 
}
function showDetails(){ 		
	$("#rommArea").show('slow');
	$("#floorArea").show('slow');
	$("#showHref").hide();
	$("#hideHref").show('slow'); 
}
function hideDetails(){
	$("#rommArea").hide();
	$("#floorArea").hide();
	$("#hideHref").hide();
	$("#showHref").show();
}
function makeSelect()
{	
	var region=getRegion();
	if(region!=0)
	{
		unBlockSelect();
		getNewSelect();
		var query=URItest('county');
		if(query!=null) { 
			selectSelected(query);
		}
	}
	else blockSelect(); 
}
function getCityUrl()
{
	var host=window.location.hostname;
	return "http://"+host+"/helper/city/";
}
function getDistrictUrl()
{
	var host=window.location.hostname;
	return "http://"+host+"/helper/district/";
}
function selectSelected(query)
{
	$("#powiat").selectOptions(query, true);
}
function blockSelect(){	
	$("#powiat").val("Wybierz");
	$("#powiat").attr("disabled", "disabled");
}

function unBlockSelect() {$("#powiat").removeAttr("disabled").val("jest");}
function getRegion() { return $("#region").val();}
function getCity() { return $("#city").val();}

function getAdress(){	
	zmienna=getConstant()+"region/"+getRegion();
	return zmienna; 
}
function getNewSelect(){	
	var url=getAdress();
	$('#powiat').load(url);
}
function getConstant(){
	var host=window.location.hostname;
	return "http://"+host+"/helper/county/chosen/"+URItest()+"/";
	//return "http://"+host+"/face/ebonface/"+$("#currLang").val()+"/helper/county/";
}
function URItest(){
var $_GET = {};
document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
    function decode(s) {
        return decodeURIComponent(s).replace(/\+/g, " ");
    }
    $_GET[decode(arguments[1])] = decode(arguments[2]);
});
return $_GET['county'];
}
