function busquedahome() {
    $('#frmBusqueda').submit();
}

function buscar_propiedades(pagina){
    //imgCargando
    $('#imgCargando').css('display','');
    if (existe_propiedad()) {//si existe la propiedad, pregunto por el path de la propiedad
        var path = get_path();
        window.parent.location = path;
    }else{//si no, muesto los resultados de búsqueda
        get_propiedades(pagina);
    }
}

function existe_propiedad(){
    var existe = false;
    if ($('#txtNumero').val() != "") {
        if (!document.sending) {
            document.sending = true;
            $.ajax({
                data: {
                    txtNumero : $('#txtNumero').val(),
                    tipo_propiedad : $("input[name='tipo_propiedad']:checked").val(),
                    existe : true
                },
                type: 'POST',
                url: WEB_PATH + 'app/ajax/existe_propiedad.php',
                async: false,
                success: function(response) {
                    document.sending = false;
                    if (response == 0) {
                        existe = false;
                    }else{
                        existe = true;
                    }
                }
            });
        }
    }
    return existe;
}

function get_path(){
    var path = "";
    if (!document.sending) {
        document.sending = true;
        $.ajax({
            data: {
                txtNumero : $('#txtNumero').val(),
                tipo_propiedad : $("input[name='tipo_propiedad']:checked").val(),
                get_path : true
            },
            type: 'POST',
            url: WEB_PATH + 'app/ajax/existe_propiedad.php',
            async : false,
            success: function(response) {
                document.sending = false;
                path =  response;
            }
        });
    }
    return path;
}

function get_propiedades(pagina){
    var data = get_parametros(pagina);
    console.log(data);
    if (!document.sending) {
        document.sending = true;
        $.ajax({
            data: data,
            type: 'POST',
            url: WEB_PATH + 'app/ajax/busqueda.php',
            success: function(response) {
                document.sending = false;
                $('#imgCargando').css('display','none');
                $('#resultado_propiedades').html(response);
            }
        });
    }
}

function get_titulo(data){
    if (!document.sending) {
        document.sending = true;
        $.ajax({
            data: data,
            type: 'POST',
            url: WEB_PATH + 'app/ajax/titulo_get.php',
            success: function(response) {
                document.sending = false;
                //$('.tituloResultados').style('url( '+ response + ' )');
                $('.tituloResultados').css('background', 'url(' + response + ')');
            //.css('background-image', 'url(images/curvedinputblue.gif)');
            }
        });
    }
}

function get_parametros(pagina){
    var servicio = 0;
    if($('#dservicios').attr('checked')){ servicio=1;}
    var data = {
        pagina : pagina,
        operacion : $("input[name='operacion']:checked").val(),
        tipo_propiedad : $("input[name='tipo_propiedad']:checked").val(),
        ddlZona : $('#ddlZona').val(),
        txtNumero : $('#txtNumero').val(),
        ddlDesde : $('#ddlDesde').val(),
        ddlHasta : $('#ddlHasta').val(),
        ddlPeriodo: $("#ddlPeriodo").val(),
        ddlDormitorios: $('#ddlDormitorios').val(),
        ddlBa: $('#ddlBa').val(),
        dservicio: servicio
    };
    return data;
}


function operacion_changed()
{
    var operacion = $("input[name='operacion']:checked").val();
    //alquier
    if (operacion == 1) {
        $("#spanPeriodo").css({
            'visibility' : 'visible'
        });
    //$("#spanPeriodo").show('slow');
    }else{
        $("#spanPeriodo").css({
            'visibility' : 'hidden'
        });
        //$("#spanPeriodo").hide('slow');
        operacion  = 2;
    }
    if (!document.sending) {
        document.sending = true;
        $.ajax({
            data: {
                operacion : operacion
            },
            type: 'POST',
            url: WEB_PATH + 'app/ajax/precios_busqueda_get.php',
            success: function(response) {
                document.sending = false;
                $('#ddlDesde').html(response);
                $('#ddlHasta').html(response);
            }
        });
    }
}
$('document').ready(function(){
	$('#chkAlquiler').click(function(){ operacion_changed() });
	$('#chkVenta').click(function(){ operacion_changed() });
});
function tipo_propiedad_changed(){
    var tipo_propiedad = $("input[name='tipo_propiedad']:checked").val();
    if (tipo_propiedad == 4) {
        $('#ddlPeriodo').html('');
        $('#spanPeriodo').css({
            'visibility' : 'hidden'
        });
        $('#chkVenta').attr("checked", true);
        operacion_changed();
        $('#chkAlquiler').attr('disabled', true);
    }else{
        $('#chkAlquiler').attr('disabled', false);
        if (!document.sending) {
            document.sending = true;
            $.ajax({
                data: {
                    tipo_propiedad : tipo_propiedad
                },
                type: 'POST',
                url: WEB_PATH + 'app/ajax/periodos_busqueda_get.php',
                success: function(response) {
                    document.sending = false;
                    //$('.tituloResultados').css('background', 'url(' + response + ')');
                    $('#ddlPeriodo').html(response);
                }
            });
        }
    }
}
