function setupBuscador() {
    var $tab = $('#tab');
    $tab.click(function() {
       var $buscador = $('#buscador');
       if ($buscador.hasClass('desplegado')) {
           cerrarBuscador();
       }
       else {
           mostrarBuscador();
       }
       return false;
    });    
}
function cerrarBuscador() {    
    var $buscador = $('#buscador');
    var $interior = $('#interior');
    $buscador.css("height",$buscador.height());
    $interior.stop().animate({opacity:0}, 300, 'easeOutExpo', function() {
        $buscador.animate({width:'40px', padding:"23px 0px"},500, 'easeOutExpo', function() {
            $buscador.removeClass('desplegado');
            $buscador.addClass('plegado');
        });
    });
}
function mostrarBuscador() {
    var $buscador = $('#buscador');
    var $interior = $('#interior');
    $buscador.stop().animate({width:'221px', padding:"23px 49px"}, 500, 'easeOutExpo', function() {
        $interior.animate({opacity:1},300, 'easeInExpo', function() {
            $buscador.removeClass('plegado');
            $buscador.addClass('desplegado');
        });
    });
}
function setupGallery() {
    $('#thumbnails').jcarousel({
            scroll:1,
            auto:0,
            wrap: 'first'
    });
    $("#thumbnails li a").click(function() {
            var $this = $(this);
            if ($this.parent().hasClass('loaded')) {
                var $contenedor = $("#contenedor_imagen");
                var $imgActual = $contenedor.find("img");
                var href = getBaseUrl()+$this.attr("href");
                $contenedor.prepend('<img width="516" height="386" title="" alt="" src="'+href+'">');
                $imgActual.fadeOut(400, function() {$imgActual.remove();});
                $("#thumbnails li a").removeClass("selected");
                $this.addClass("selected");
            }
            return false;
    });
    var $thumbs = $("#galeria #thumbnails li a");
    var srcs = $thumbs.map(function() {
        return $(this).attr("href");
    });
    $.imgpreload(srcs, {
        each: function() {
            var href = this.src.replace(getBaseUrl(),"");
            $("#galeria li a[href='"+href+"']").parent().addClass("loaded");
        },
        all: function() {
            $("#galeria").addClass("loaded");
        }
    }); 
}


function setupSearch() {
    setupBuscador();
    $(document).ready(function(){
        var $select_tipo = $('select#id_id_tipo');
        var caracteristicas = function() {
            var $option = $select_tipo.find("option:selected");
            var index = $select_tipo.find("option").index($option.get(0))-1;
            if ($option.val() > 0) {
                $("#form_right .caracteristicas").css("display","none");
                $("#form_right .caracteristicas").eq(index).css("display","block");
                $("#form_right").removeClass("disabled");
            }
            else {
                $("#form_right").addClass("disabled");
            }
        };
        caracteristicas();
        $select_tipo.change(function() {
            caracteristicas();
            $(".caracteristicas .checked").click();
        });
        $('#form_buscador').submit(function() {
            if ($('select#id_id_operacion').val() <= 0 || $('select#id_id_operacion').customSelects('getValue') == "-1") {
                $('select#id_id_operacion').next('dl').find('dt a').css("color","#933D63");
                $("#form_msg").html("Debes seleccionar una operacion. Ej. Venta.");
                $("#form_msg").fadeIn();
                setTimeout(function() {$("#form_msg").fadeOut();$('select#id_id_operacion').next('dl').find('dt a').css("color","#ffffff");}, 5000);
                return false;
            }
        });
        $("form .bloqueo").click(function() {
            if ($('select#id_id_tipo').val() <= 0) {
                $('select#id_id_tipo').next('dl').find('dt a').css("color","#933D63");
                $("#form_msg").html("Debes seleccionar un tipo de propiedad. Ej. Casa.");
                $("#form_msg").fadeIn();
                setTimeout(function() {$("#form_msg").fadeOut();$('select#id_id_tipo').next('dl').find('dt a').css("color","#ffffff");}, 5000);
                return false;
            }
        });
        caracteristicas();
        $("#fecha").change(function() {
            var periodos = getPeriodos();
            var periodo = periodos[$(this).val()];
            var inicio = '';
            var fin = '';
            if (periodo.inicio != '' && periodo.fin != '') {
                var d = new Date();
                var yearInicio, yearFin;
                if (d.getMonth()+1 > parseInt(periodo.inicio[0])) {
                    yearInicio = parseInt(d.getFullYear())+1;
                }
                else {
                    yearInicio = parseInt(d.getFullYear());
                }
                if (parseInt(periodo.inicio[0]) > parseInt(periodo.fin[0])) {
                    yearFin = yearInicio+1;
                }
                else {
                    yearFin = yearInicio;
                }
                var mesInicio = periodo.inicio[0];
                var diaInicio = periodo.inicio[1];
                var mesFin = periodo.fin[0];
                var diaFin = periodo.fin[1];
                if (mesInicio == 2 && diaInicio == 14 && esBiciesto(yearInicio)) {
                    diaInicio = 15;
                }
                if (mesFin == 2 && diaFin == 28 && esBiciesto(yearFin)) {
                    diaFin = 29;
                }
                if (mesInicio < 10) {
                    mesInicio = '0'+mesInicio;
                }
                if (diaInicio < 10) {
                    diaInicio = '0'+diaInicio;
                }
                if (mesFin < 10) {
                    mesFin = '0'+mesFin;
                }
                if (diaFin < 10) {
                    diaFin = '0'+diaFin;
                }
                inicio = yearInicio+'-'+mesInicio+'-'+diaInicio;
                fin = yearFin+'-'+mesFin+'-'+diaFin;
            }
            $("#inicio").val(inicio);
            $("#fin").val(fin)
        });
        $('select#id_id_operacion').change(function() {
            var $fecha = $('select#fecha');               
            var $precio_alquiler = $('select#precio_alquiler');
            var $precio_venta = $('select#precio_venta');
            if ($(this).val() == 2) {
                $fecha.attr("disabled","");
                $fecha.next('dl').removeClass('disabled');
                $precio_alquiler.attr("disabled","");
                $precio_alquiler.next('dl').removeClass('disabled').removeClass("oculto");
                $precio_venta.next('dl').addClass('disabled').addClass("oculto");
            }
            else if ($(this).val() == 1) {
                $fecha.attr("disabled","disabled");
                $fecha.next('dl').addClass('disabled');
                $precio_venta.attr("disabled","");
                $precio_venta.next('dl').removeClass('disabled').removeClass("oculto");
                $precio_alquiler.next('dl').addClass('disabled').addClass("oculto");
                $("#inicio").val('');
                $("#fin").val('')
            }
        });
        $('select#precio_alquiler').change(function() {
            var rangos = getRangosPrecioAlquiler();
            var rango = rangos[$(this).val()];
            var minimo = rango.minimo;
            var maximo = rango.maximo;
            $("#minimo").val(minimo);
            $("#maximo").val(maximo);            
        });
        $('select#precio_venta').change(function() {
            var rangos = getRangosPrecioVenta();
            var rango = rangos[$(this).val()];
            var minimo = rango.minimo;
            var maximo = rango.maximo;
            $("#minimo").val(minimo);
            $("#maximo").val(maximo);
        });
        var skipElements = [$select_tipo.get(0)];
        if ($('select#id_id_operacion').customSelects('getValue') == "-1") {
            skipElements.push($("#id_id_operacion").get(0));
        }
        $("select.custom").customSelects('sync',{skipValues : ['0'], triggerChange:true, skipElements:skipElements});
        selectRango();
        selectPeriodo();
    });
}
function selectRango() {
    var idOperacion = $('select#id_id_operacion').val();
    var minimo = $('#minimo').val();
    var maximo = $('#maximo').val();
    var rangos = null;
    var $precio;
    if (idOperacion == 1) {
        rangos = getRangosPrecioVenta();
        $precio = $('select#precio_venta');
    }
    else if (idOperacion == 2) {
        rangos = getRangosPrecioAlquiler();
        $precio = $('select#precio_alquiler');
    }
    if (rangos != null) {
        var i = matchRango(rangos, minimo, maximo);
        if (i >= 0) {
            $precio.customSelects('select', {value: i});
        }
    }
}
function selectPeriodo() {
    var idOperacion = $('select#id_id_operacion').val();
    if (idOperacion == 2) {
        var inicio = $('#inicio').val();
        var fin = $('#fin').val();
        if (inicio != '' && fin != '') {
            var inicio_ar = inicio.split('-');
            var fin_ar = fin.split('-');
            inicio = [inicio_ar[1], inicio_ar[2]];
            fin = [fin_ar[1], fin_ar[2]];
            var periodos = getPeriodos();
            var $fecha = $('select#fecha');
            if (periodos != null) {
                var i = matchPeriodo(periodos, inicio, fin);
                if (i >= 0) {
                    $fecha.customSelects('select', {value: i});
                }
            }
        }
    }
}
function matchRango(rangos, minimo, maximo) {
    var largo = rangos.length;
    for(var i = 0; i < largo; i++) {
        if (parseInt(minimo) == parseInt(rangos[i].minimo) && parseInt(maximo) == parseInt(rangos[i].maximo)) {
            return i;
        }
    }
    return -1;
}
function matchPeriodo(periodos, inicio, fin) {
    var largo = periodos.length;
    for(var i = 0; i < largo; i++) {
        if (parseInt(inicio[0]) == parseInt(periodos[i].inicio[0]) && parseInt(inicio[1]) == parseInt(periodos[i].inicio[1]) && parseInt(fin[0]) == parseInt(periodos[i].fin[0]) && parseInt(fin[1]) == parseInt(periodos[i].fin[1])) {
            return i;
        }
    }
    return -1;
}
function getRangosPrecioAlquiler() {
    var rangos = [                
       {minimo : '', maximo : ''},
       {minimo : '0', maximo : '2500'},
       {minimo : '2500', maximo : '5000'},
       {minimo : '5000', maximo : '10000'},
       {minimo : '10000', maximo : '15000'},
       {minimo : '15000', maximo : '25000'},
       {minimo : '25000', maximo : ''}
    ];
    return rangos;
}
function getRangosPrecioVenta() {
    var rangos = [                
       {minimo : '', maximo : ''},
       {minimo : '0', maximo : '150000'},
       {minimo : '150000', maximo : '350000'},
       {minimo : '350000', maximo : '500000'},
       {minimo : '500000', maximo : '1000000'},
       {minimo : '1000000', maximo : ''}
    ];
    return rangos;
}
function getPeriodos() {
    var periodos = [
       {inicio : '', fin : ''},
       {inicio : [12,01], fin : [12,31]},
       {inicio : [12,24], fin : [01,01]},
       {inicio : [01,01], fin : [01,31]},
       {inicio : [01,01], fin : [01,15]},
       {inicio : [01,15], fin : [01,31]},
       {inicio : [02,01], fin : [02,28]},
       {inicio : [02,01], fin : [02,15]},
       {inicio : [02,14], fin : [02,28]},
       {inicio : [03,01], fin : [03,31]},
       {inicio : [03,25], fin : [04,30]},
       {inicio : [01,01], fin : [12,31]}
    ];
    return periodos;
}
function setupDestacados() {
    $('#thumbnails li a').click(function() {
        var $this = $(this);
        var $li = $this.parent();
        if (!$li.hasClass('selected')) {
            var $img = $this.find('img');
            var title = $img.attr("title");
            var desc = $li.find('p.desc').html();
            var src = $img.attr('src');
            var $caption = $('#principal .caption');
            $('#thumbnails li').removeClass('selected');
            $li.addClass('selected');
            $caption.stop().animate({bottom:'-100px'},750,'easeInExpo',function() {
                $caption.find('h3 a').attr("href",$this.attr("href")).html(title);
                $caption.find('p').html(desc);
                $caption.animate({bottom:'0px'},750,'easeOutExpo');
            });
            var $contenedor = $("#contenedor_imagen");
            var $imgActual = $contenedor.find("img");
            $contenedor.prepend('<img width="516" height="386" title="'+title+'" alt="'+title+'" src="'+src+'">');
            $imgActual.fadeOut(600, function() {$imgActual.remove();});
        }
        return false;
    });
}
function destacadoNext() {
    
}
