var regionDpt=new Array();
var typeDpt=new Array();

function DptType (dpt, type) {
	if (type=='all' || dpt=='all') return true;
	else {
		for (var keyDpt in typeDpt[type]) {
			if (typeDpt[type][keyDpt]==dpt) {
				return true;
			}
		}
	}
	return false;
}
function RegionDpt (region, dpt) {
	if (region=='all' || dpt=='all') return true;
	else {
		for (var keyDpt in regionDpt[region]) {
			if (regionDpt[region][keyDpt]==dpt) {
				return true;
			}
		}
	}
	return false;
}
function TypeRegion (type, region) {
	if (region=='all' || type=='all') return true;
	else {
		for (var keyDptType in typeDpt[type]) {
			for (var keyDptRegion in regionDpt[region]) {
				if (regionDpt[region][keyDptRegion]==typeDpt[type][keyDptType]) {
					return true;
				}
			}
		}
	}
	return false;
}

function filterRegion () {
	var selectDpt=$("#dpt").val();
	var selectType=$("#type").val();
	
	if (selectDpt=='all' && selectType=='all') $("#region option").show();
	else {
		$('#region option').each(function () {
			if (RegionDpt($(this).val(),selectDpt) && TypeRegion(selectType,$(this).val())) $(this).show();
			else $(this).hide();
		});
	}
}

function filterDpt () {
	var selectType=$("#type").val();
	var selectRegion=$("#region").val();
	
	if (selectType=='all' && selectRegion=='all') $("#dpt option").show();
	else {
		$('#dpt option').each(function () {
			if (RegionDpt(selectRegion,$(this).val()) && DptType($(this).val(),selectType)) $(this).show();
			else $(this).hide();
		});
	}
}

function filterType () {
	var selectDpt=$("#dpt").val();
	var selectRegion=$("#region").val();
	
	if (selectDpt=="all" && selectRegion=="all") $("#type option").show();
	else if (selectDpt=="all") {
		$('#type option').each(function () {
			if (TypeRegion($(this).val(),selectRegion)) $(this).show();
			else $(this).hide();
		});
	}
	else {
		$('#type option').each(function () {
			if (DptType(selectDpt,$(this).val())) $(this).show();
			else $(this).hide();
		});
	}
}

$(function () {
	$("#quickMap map area").hover(function () {
		$("#quickMapHover").css("backgroundPosition","center "+(-$("#quickMapImage").height()*parseInt($(this).attr("id").replace(/^area/,"")))+"px");
	},function () {
		if ($("#quickMap map area.active").size()==1) $("#quickMapHover").css("backgroundPosition","center "+(-$("#quickMapImage").height()*parseInt($("#quickMap map area.active").attr("id").replace(/^area/,"")))+"px");
		else $("#quickMapHover").css("backgroundPosition","center "+$("#quickMapImage").height()+"px");
	});
	
	$("#region").change(function () {
		filterDpt();
		filterType();
	});
	
	$("#dpt").change(function () {
		filterRegion();
		filterType();
	});
	
	$("#type").change(function () {
		filterRegion();
		filterDpt();
	});
				
	$("#region").change();
	if ($("#dpt[value='"+$("#dpt").val()+"']:visible").size()==0) $("#dpt").val('all');
	if ($("#type[value='"+$("#type").val()+"']:visible").size()==0) $("#type").val('all');
	
	$("#dpt").change();
	if ($("#region[value='"+$("#region").val()+"']:visible").size()==0) $("#region").val('all');
	if ($("#type[value='"+$("#type").val()+"']:visible").size()==0) $("#type").val('all');
	
	$("#type").change();
	if ($("#region[value='"+$("#region").val()+"']:visible").size()==0) $("#region").val('all');
	if ($("#dpt[value='"+$("#dpt").val()+"']:visible").size()==0) $("#dpt").val('all');
});
