$(document).ready(function() {
	$("#select_province_map area").mouseover(function(){
		title = $(this).attr("title");
		$("#select_province_img").attr("src", "/public/img/states/"+title+".png");

		$(this).click(function(){
			document.location.href = "/aanbod/Nederland/"+title;
		});

		$(this).mouseout(function(){
			$("#select_province_img").attr("src", "/public/img/states/Nederland.png");
		});
	});
	var border1 = RUZEE.ShadedBorder.create({ corner:8, border:1});
    border1.render($('.tabs-container'));

	$("#home-tab-container").tabs();
    
	emptyCell('#query'); 
    
    //alle form action met advanced form.
    if($("#u_country").val() != ""){
    	getProvinces($("#u_country").val());
		$("#country_parts").show();
    }else{
		$("#country_parts").hide();
    }

    if($("#u_inventory").val() != ""){
		if($("#u_inventory").val() == 'tenant_farming'){
			$("label[for='u_inventory_min']").text("Prijs per maand") 
		}else{
			$("label[for='u_inventory_min']").text("Prijsklasse") 
		}
		$("#inventory_parts").show();
	}else{
		$("#inventory_parts").hide();
    }
    

    if($("#u_estate").val() != ""){
		if($(this).val() == 'rent'){
			$("label[for='u_price_min']").text("Prijs per maand") 
		}else{
			$("label[for='u_price_min']").text("Prijsklasse") 
		}
		$("#estate_parts").show();
	}else{
		$("#estate_parts").hide();
    }

   	if($("#u_terras").val() != "" && $("#u_terras").val() != "0" ){
		$("#terras_parts").show();
	}else{
		$("#terras_parts").hide();
    }

    $("#u_country").change(function () {
    	if($(this).val() != ""){
    		getProvinces($("#u_country").val());
			if ($("#country_parts").is(":hidden")) {
		      	$("#country_parts").slideDown().show(function(){
					$(this).show();
				});
		     }
			return false;
      	}else{
	      	$("#country_parts").slideUp().show(function(){
					$(this).show();
				});
      	}
    });

    $("#u_inventory").change(function () {
    	if($(this).val() != ""){
			if($(this).val() == 'tenant_farming'){
				$("label[for='u_inventory_min']").text("Prijs per maand") 
			}else{
				$("label[for='u_inventory_min']").text("Prijsklasse") 
			}
			if ($("#inventory_parts").is(":hidden")) {
		      	$("#inventory_parts").slideDown().show(function(){
					$(this).show();
				});
		     }
      		return false;
		}else{
	      	$("#inventory_parts").slideUp();
      	}
    });

    $("#u_estate").change(function () {
    	if($(this).val() != ""){
			if($(this).val() == 'rent'){
				$("label[for='u_price_min']").text("Prijs per maand") 
			}else{
				$("label[for='u_price_min']").text("Prijsklasse") 
			}
			if ($("#estate_parts").is(":hidden")) {
		      	$("#estate_parts").slideDown().show(function(){
					$(this).show();
				});
		     }
      		return false;
      	}else{
	      	$("#estate_parts").slideUp();
      	}
    });


    $("#u_terras").change(function () {
    	if($(this).val() != "" && $(this).val() != "0" ){
			if ($("#terras_parts").is(":hidden")) {
		      	$("#terras_parts").slideDown().show(function(){
					$(this).show();
				});
		     }
      		return false;
      	}else{
	      	$("#terras_parts").slideUp();
      	}
    });
    
   	$("#product_type").change(function(){
		change_sub_types($(this).val());	
	});


});

//functie om de provincies van een land op te halen.
function getProvinces(country){
	if(country == 'undefined' || country == '')
		return;

	$.ajax({
 	   	data: "method=getProvinces&country="+country,
   		success: function(d){
			$('#u_province')
			.find('option')
		    .remove()
		    .end();

			$('#u_province').
			append($("<option></option>").
			attr("value","").
			text("Geen voorkeur"));

   			for(i in d){
				$('#u_province').
				append($("<option></option>").
				attr("value",d[i].province_id).
				text(d[i].province_name));
          }
   		}
   	});
	return true;
}

function change_sub_types(id){
	if(id == '' || typeof(id) == 'undefined')
		return;

	$.ajax({
 	   	data: "method=getSubTypes&id="+id,
   		success: function(d){
			$("#product_subtype").find('option').remove();

			$('#product_subtype').append($("<option></option>").attr("value","").text("Geen voorkeur"));
			for(i in d){
				$("#product_subtype").append('<option value="'+d[i].product_type_id+'">'+d[i].product_type_name+'</option>');
			}
        }
   	});
}
