var loaderImage = "loaderImage";
var form;
var params = lastParams = new Object();
params.type = "huur";
$(document).ready(function(){
	form = $("#woning-zoeken");
	$("input[name=type]",form).click(function() {
		params.type = form.find("input[name=type]:checked").val();
		params.country = null;
		
		loadCountry();
		
		if (params.type == "koop")
		{
			$("select[name=people]").attr("disabled","disabled");
			$("select[name=people]").css("display","none");
			$("select[name=price]").attr("disabled","");
			$("select[name=price]").css("display","block");
		}
		else
		{
			$("select[name=people]").attr("disabled","");
			$("select[name=people]").css("display","block");
			$("select[name=price]").attr("disabled","disabled");
			$("select[name=price]").css("display","none");
		}
		
		emptyList("select[name=region]");
		emptyList("select[name=object_type]");
		emptyList("select[name=people]");
		emptyList("select[name=price]");
	});
	form.delegate("select[name=country]","change",function() {
		params.type		= form.find("input:radio:checked").val();
		params.country	= $(this).val();
		lastParams = params;
		
		loadRegion();
		emptyList("select[name=object_type]");
		emptyList("select[name=people]");
	});
	form.delegate("select[name=region]","change",function() {
		params.type		= form.find("input:radio:checked").val();
		params.country	= form.find("select[name=country]").val();
		params.region	= $(this).val();
		lastParams = params;
		
		loadObjectType();
		emptyList("select[name=people]");
	});

	form.delegate("select[name=object_type]","change",function() {
		params.type		= form.find("input:radio:checked").val();
		params.country	= form.find("select[name=country]").val();
		params.region	= form.find("select[name=region]").val();
		params.object_type= $(this).val();
		lastParams = params;
		
		if (params.type == "huur")
		{
			loadPeople();
			emptyList("select[name=people]");
		}
		else if (params.type == "koop")
		{
			loadPrice();
			emptyList("select[name=price]");
		}
	});
});

/**
 * Load country list
 * @return
 */
function loadCountry()
{
	params.action = "getcountry";
	showLoaderAt(form.find("select[name=country]"));
	$.get("pages/ajax/woning_zoeken.php", params, function(data) {
		params.action = lastParams.action = null;
		for (var i in lastParams)
			if (lastParams[i] != params[i])
				return;
		$("#"+loaderImage).replaceWith(data);
	});
}

/**
 * Load region list
 * @return
 */
function loadRegion()
{
	params.action = "getregion";
	showLoaderAt(form.find("select[name=region]"));
	$.get("pages/ajax/woning_zoeken.php", params, function(data) {
		params.action = lastParams.action = null;
		for (var i in lastParams)
			if (lastParams[i] != params[i])
				return;
		$("#"+loaderImage).replaceWith(data);
	});
}

/**
 * Load object type list
 * @return
 */
function loadObjectType()
{
	params.action = "getobjecttype";
	showLoaderAt(form.find("select[name=object_type]"));
	$.get("pages/ajax/woning_zoeken.php", params, function(data) {
		params.action = lastParams.action = null;
		for (var i in lastParams)
			if (lastParams[i] != params[i])
				return;
		$("#"+loaderImage).replaceWith(data);
	});
}

/**
 * Load people type list
 * @return
 */
function loadPeople()
{
	params.action = "getpeople";
	showLoaderAt(form.find("select[name=people]"));
	$.get("pages/ajax/woning_zoeken.php", params, function(data) {
		params.action = lastParams.action = null;
		for (var i in lastParams)
			if (lastParams[i] != params[i])
				return;
		$("#"+loaderImage).replaceWith(data);
	});
}

/**
 * Load price type list
 * @return
 */
function loadPrice()
{
	params.action = "getprice";
	showLoaderAt(form.find("select[name=price]"));
	$.get("pages/ajax/woning_zoeken.php", params, function(data) {
		params.action = lastParams.action = null;
		for (var i in lastParams)
			if (lastParams[i] != params[i])
				return;
		$("#"+loaderImage).replaceWith(data);
	});
}

////////////////////////////////////
function showLoaderAt(targetElement)
{
	$(targetElement).replaceWith("<div class='clear left stretch' id='"+loaderImage+"'></div>");
}
function emptyList(obj)
{
	var item = $(obj).children("option").first().html();
	$(obj).children("option:not(:first)").remove();
	var arr = item.split("(");
	item = arr[0] + "(0)";
	$(obj).children("option:first").attr("selected","selected").html(item);
}
