$(function() {

    $('select#city').click(function() {
		
        var valor = $(this).val();

        if(valor!=85138){
        	$('.moresearch').slideDown();

        	return false;
		}
    });


    $("select#city").change(function(){

		var iniDiv1;
		
        var valor = $(this).val();

        if(valor!=85138){
        	$("div.results").html("Carregando...");

			$("div.results").html(null);

        		$.post('http://www.mirantte.com.br/Public/index.php?class=busca&method=getBairrosAjax&zona=1&cidade='+valor, null, function(data){
					$("div.results").html(data);
                });
		}
    });


    $("input[name='district[]']").change(function(){


        var tipos = [];

           $("input[name='district[]']:checked").each(function() {

                     tipos.push($(this).val());

            });

            if ( tipos.length > 0  ) {

        		$.post('http://www.mirantte.com.br/Public/index.php?class=busca&method=getBairrosAjax&zona=1&cidade='+valor, null, function(data){
					var titulo1 = "<label style='height:30px;'> <input type=\"checkbox\"name=\"district[]\" />Zona Oeste</label>";
                    if(data != null){

                        var size = data.length;

                        $("div.results").html(null);

                        for(var i = 0; i < size; i++){

                            var contCk = "<label for=\""+ data[i].EXTENSOBAI +"\" style='height:30px;'> <input type=\"checkbox\"name=\"district[]\" value=\"" + data[i].EXTENSOBAI + "\" id=\"" + data[i].EXTENSOBAI + "\" />" + data[i].EXTENSOBAI + "</label>";
                        }
                    }else{
                         alert("Preencha todos os parametros da busca");
                    }

                });


				$("div.results").append(titulo1+contCk);


            }else{
                $("div.results").html(null);
            }

    });



    $("select[name=finalidade]").change(function(){

        var finalidade = $(this).val();

        if(finalidade == 1){
            $("select#from").html('<option value="0" selected="selected">Indiferente</option>');
            $("select#from").append('<option value="50-100">50.000 ate 100.000</option>');
            $("select#from").append('<option value="100-200">100.000 ate 200.000</option>');
            $("select#from").append('<option value="200-300">200.000 ate 300.000</option>');
            $("select#from").append('<option value="300-400">300.000 ate 400.000</option>');
            $("select#from").append('<option value="400-500">400.000 ate 500.000</option>');
            $("select#from").append('<option value="500">Mais de 500.000</option>');
            $("select#from").removeAttr('disabled');
        }

        if(finalidade == 2){
                $("select#from").html('<option value="0" selected="selected">Indiferente</option>');
                $("select#from").append('<<option value="0.6-1">600 ate 1.000</option>');
                $("select#from").append('<option value="1-2">1.000 ate 2.000</option>');
                $("select#from").append('<option value="2-5">2.000 ate 5.000</option>');
                $("select#from").append('<option value="5-10">5.000 ate 10.000</option>');
                $("select#from").append('<option value="10-20">10.000 ate 20.000</option>');
                $("select#from").append('<option value="20">Mais de 20.000</option>');
                $("select#from").removeAttr('disabled');
        }


    });

    $("input[name='tipovenda[]']").change(function(){


        var tipos = [];

           $("input[name='tipovenda[]']:checked").each(function() {

                     tipos.push($(this).val());

            });

            if ( tipos.length > 0  ) {

                $.post('http://www.mirantte.com.br/Public/index.php?class=busca&method=getTipoAjax', { tipos : tipos } , function(data){

                    if(data != null){

                        var size = data.length;

                        $("div.results").html(null);

                        for(var i = 0; i < size; i++){

                            $("div.results").append("<label for=\""+ data[i].IDtipoImovel +"\"> <input type=\"checkbox\"name=\"tipoimovel[]\" value=\"" + data[i].IDtipoImovel + "\" id=\"" + data[i].IDtipoImovel + "\" />" + data[i].Descricao + "</label>");
                        }
                    }else{
                         alert("Preencha todos os parametros da busca");
                    }

                });

            }else{
                $("div.results").html(null);
            }

    });


});
