$(document).ready(function(){
	$("#location").change(function(){
		var flash = document.getElementById("flashSelector");
		flash.chooseDistricts($(this).val());
	});
	$("#goal").change(function(){
		switch($(this).val()){
			case "all":
				$("#min_sale_price").removeAttr("disabled");
				$("#max_sale_price").removeAttr("disabled");
				$("#min_rent_fee").removeAttr("disabled");
				$("#max_rent_fee").removeAttr("disabled");
				$("#rentrow").css("color", "#000000");
				$("#salerow").css("color", "#000000");
				break;
			case "rent":
				$("#min_sale_price").val("");
				$("#max_sale_price").val("");
				$("#min_sale_price").attr("disabled", true);
				$("#max_sale_price").attr("disabled", true);
				$("#min_rent_fee").removeAttr("disabled");
				$("#max_rent_fee").removeAttr("disabled");
				$("#rentrow").css("color", "#000000");
				$("#salerow").css("color", "#CCCCCC");
				break;
			case "sale":
				$("#min_rent_fee").val("");
				$("#max_rent_fee").val("");
				$("#min_rent_fee").attr("disabled", true);
				$("#max_rent_fee").attr("disabled", true);
				$("#min_sale_price").removeAttr("disabled");
				$("#max_sale_price").removeAttr("disabled");
				$("#rentrow").css("color", "#CCCCCC");
				$("#salerow").css("color", "#000000");
				break;
		}
	});
	$('#euro').change(function(){
		switchCurrency();
	});
	$('#type').change(function(){
		switchType();
	});
	switchType();
	//open form
	$(".offer").click(function(){
		_gaq.push(['_trackEvent', formURL]);
		$.colorbox({
			href:formURL,
			scrolling: false,
			innerWidth: 690,
			innerHeight: 330,
		});
		return false;
	});
});

function switchType()
{
	switch($("#type").val()){
	case "labour":
		$("#rentrow").css("display", "none");
		$("#salerow").css("display", "none");
		$("#net").css("display", "none");
		break;
	case "flat":
		$("#rentrow").css("display", "block");
		$("#salerow").css("display", "block");
		$("#net").css("display", "block");
		break;
	default:
		$("#rentrow").css("display", "block");
		$("#salerow").css("display", "block");
		$("#net").css("display", "block");
	}
	switchCurrency();
}
function switchCurrency()
{
	if($("#euro").attr('checked')){
		if($("#type").val() == "flat"){
			$(".currency").html("€");
		}else{
			$(".currency").html("€/"+sqm);
		}
	}else{
		if($("#type").val() == "flat"){
			$(".currency").html(huf);
		}else{
			$(".currency").html(huf+"/"+sqm);
		}
	}
}
var formURL = null;
function selectLocations(locationsString)
{
	/*
	var locations = locationsString.split(",");
	$('#location option').each(function() {
		var selected = false;
		for(var s in locations){
		   	if($(this).val() == locations[s]){
		   		selected = true;
		   	}
		}
		if(selected){
			$(this).attr("selected", "selected");
		}else{
			$(this).removeAttr("selected");
		}
	});
	*/
	var locations = locationsString.split(",");
	$('#location option').each(function() {
		$(this).removeAttr("selected");
	});
	var found = false;
	$("#hiddenfields").html("");
	for(var s in locations){
		found = false;
		$('#location option').each(function() {
		   	if($(this).val() == locations[s]){
		   		$(this).attr("selected", "selected");
		   		found = true;
		   	}
		});
		if(found == false){
			if(locations[s] > 0 && locations[s]< 24)
			$("#hiddenfields").append('<input type="hidden" name="location[]" value="'+locations[s]+'"');
		}
	}
}
