$(document).ready(function() {
	setHeight();
	$(window).bind('resize', function(){setHeight()});
	var box = $(".select-box").selectbox({hoverClass: 'selected'});
	var types = ["area", "country", "year"];
	for(var i = 0; i<types.length; i++){
		var box = $("#"+types[i]+"-box").selectbox({hoverClass: 'selected'});
		box.change(function(){
			sendValues(this.name);
		})
	}
	$(".nospam").each(function() {
	   var $this = $(this);
	   var t = $this.text();
	   t = t.replace(/\|/g, ".");
	   t = t.replace(/#/g, "@");
	   $this.text(t);
	   $this.attr("href", "mailto:"+t);
	});
});
function updateBox(j){
	for(n in j){
		var boxname = n;
		var options = '<select id="'+boxname+'-box" name="'+boxname+'">';
		for (var i = 0; i < j[n].length; i++) {
			var selected = ($("#"+boxname+"-box").val()==j[n][i].optionValue)?" selected":"";
			options += '<option value="' + j[n][i].optionValue + '"' + selected + '>' + j[n][i].optionDisplay + '</option>';
		}
		$("#"+boxname).html(options+"</select>");
		var box = $("#"+boxname+"-box").selectbox({hoverClass: 'selected'});
		box.change(function(){
			sendValues(this.name);
		})
	}
	$("#quick-result").load("includes/ajax-result.php?country="+$("#country-box").val()+"&area="+$("#area-box").val()+"&year="+$("#year-box").val());
}
function sendValues(cName){
	$.getJSON("includes/ajax-select.php",{current:cName, area:$("#area-box").val(), country:$("#country-box").val(), year:$("#year-box").val()},function(j, textStatus){
	updateBox(j);
	});
}
function setHeight(){
	var docH = $(window).height();
	$("#main").height("100%");
	var contentH = $("#main").height()+20;
	if(contentH<docH){
		$("#footer").css("top", $(window).height()-20);
	}else{
		$("#footer").css("top", contentH);
	}
}