var m2brLocalizacao = function( sel_estados, sel_cidades, sel_bairros, tem_bairro )
{

	sel_bairros = typeof(sel_bairros) != 'undefined' ? sel_bairros : null;
 	tem_bairro 	= typeof(tem_bairro) != 'undefined' ? tem_bairro : false;


	// estado -> cidade
	$('select#'+sel_estados).change(function(){

		if ( $(this).val() ) {
			$('<img src="'+raiz+'_img/loading.gif" id="loading-'+sel_cidades+'" />').insertBefore($("select#"+sel_cidades));
			$("select#"+sel_cidades).hide();
			$.getJSON(raiz+"_inc/cidades.inc.php?search=",{cod_estados: $(this).val(), ajax: 'true'}, function(j){
				var options = '<option value=""></option>';
				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].cod_cidades + '">' + j[i].nome + '</option>';
				}
				$("select#"+sel_cidades).html(options);
				$('#loading-'+sel_cidades).remove();
				$("select#"+sel_cidades).show();
			});
		} else {
			$("select#"+sel_cidades).html( '<option value="">-- escolha um estado --</option>' );
			if( tem_bairro ) $("select#"+sel_bairros).html( '<option value="">-- escolha uma cidade --</option>' );
		}

	});


	if( tem_bairro ) {
		$('select#'+sel_cidades).change(function(){

			if ( $(this).val() ) {
				$('<img src="'+raiz+'_img/loading.gif" id="loading-'+sel_bairros+'" />').insertBefore($("select#"+sel_bairros));
				$("select#"+sel_bairros).hide();
				$.getJSON(raiz+"_inc/bairros.inc.php?search=",{cod_cidades: $(this).val(), ajax: 'true'}, function(j){
					var options = '<option value=""></option>';
					for (var i = 0; i < j.length; i++) {
						options += '<option value="' + j[i].cod_bairros + '">' + j[i].nome + '</option>';
					}
					$("select#"+sel_bairros).html(options);
					$('#loading-'+sel_bairros).remove();
					$("select#"+sel_bairros).show();
				});
			} else {
				$("select#"+sel_bairros).html( '<option value="">-- escolha uma cidade --</option>' );
			}

		});
	}

};
