/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var curpos, items = [];
var contador = 0;

var cargar_reciente = function(pos1, pos2){
    if (pos1 >= contador) {
        var callback = {
            success: function(o){
                ultimos = YAHOO.lang.JSON.parse(o.responseText);
                //alert(ultimos.length)
                for (i = 0; i < ultimos.length; i++) {
                    //llenar datos
                    datos = '<div class="izquierdo"><a href="anuncio.php?ide='+ultimos[i].ide+'"><img src="WebServices/imagenes_class.php?caso=1&ideBien='+ultimos[i].ide+'&numFoto=1"/></a></div><div class="derecho texto_derecho"><div class="carrusel_marca"><a href="anuncio.php?ide='+ultimos[i].ide+'">'+ultimos[i].marca+'</a></div><div class="carrusel_modelo"><a href="anuncio.php?ide='+ultimos[i].ide+'">'+ultimos[i].modelo+' '+ultimos[i].ano+'</a></div><div class="carrusel_precio">Precio:<span>'+ultimos[i].precio+'</span></div><div class="carrusel_fecha">Fecha:<span>'+ultimos[i].fecha+'</span></div></div>'
                    //datos = "<div style='float:left;width:100px;'><a href='anuncio.html?ide="+ultimos[i].ide+"'><img border=\"0\"  src=\"WebServices/imagenes_class.php?caso=1&ideBien="+ultimos[i].ide+"&numFoto=1\"/></a></div><div style='float:right;width:130px;' align='left'><div class='datos_operacion'><a href='anuncio.html?ide="+ultimos[i].ide+"'>"+ultimos[i].operacion+"</a></div><div class='datos_precio'>Precio: <span>"+ultimos[i].precio+"</span></div><div>"+ultimos[i].ciudad+"</div><div class='datos_fecha'>Fecha: <span>"+ultimos[i].fecha+"</span></div><div>"+ultimos[i].colonia+"</div></div>"
                    document.getElementById("con" + ultimos[i].lugar).innerHTML = datos;
                }
            },
            failure: function(){

            }
        };
        var sUrl = "WebServices/lista_recientes.php";
        var params = "pos1=" + pos1 + "&pos2=" + pos2;
        var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, params);
		contador = pos1+pos2;
    }
}

var ver_carrusel = function(){
    var carousel
    carousel = new YAHOO.widget.Carousel("container", {
        numItems: 52,
        animation: {
            speed: 0.5
        },
        //revealAmount: 30,
        numVisible: 4
    });
    for (i = 1; i <= 52; i++) {
        carousel.addItem("<div id='con" + i + "'>cargando...</div>");
    }
    //*********************************MINI Paginador
	var numVisible = carousel.get("numVisible");
	var paginator = new YAHOO.widget.Paginator({
	      containers: "pagination",
	      rowsPerPage: 1,
	      template: "{PreviousPageLink} {NextPageLink}",
	      totalRecords: 13,
		  previousPageLinkLabel : "&lt; anterior",
		  nextPageLinkLabel     : "siguiente &gt;"
	});
	paginator.subscribe("changeRequest", function (state) {
	  // set the selectedItem so that the Carousel scrolls to the page automatically
	  carousel.set("selectedItem", (state.page - 1) * numVisible);
	  paginator.setState(state);
	});
	carousel.on("pageChange", function (page) {
	  // Paginator's page begins from 1
	  // Also, we need to suppress this triggering a changeRequest event.
	  paginator.setPage(page + 1, true);
	});
	carousel.on("itemSelected", function (index) {
	  // item has the reference to the Carousel's item
	  var item = carousel.getElementForItem(index);
	});
	//*********************************
	cargar_reciente(0, 4);

	carousel.set("selectedItem", 0);

	carousel.on("beforeScroll", function(o){
        //alert(o.first + " " + o.last + " " + o.dir)
        if (o.last==contador && o.dir=="forward"){
			//alert("cargar mas" + o.last)
			valor = o.last;
			cargar_reciente(valor,4);
		}else{
			//alert("no cargar")
		}
		//cargar_reciente(2,4):
        return true; // so that the event is not aborted
    });
    carousel.render();
    carousel.show();
    paginator.render();
}
var validar_busqueda2 = function(){
    estilo = document.getElementById("lista_estilos").value;
    marca = document.getElementById("lista_marcas2").value;
    modelo = document.getElementById("lista_modelos2").value;
    ano1 = document.getElementById("txt_ano1").value;
    ano2 = document.getElementById("txt_ano2").value;
    precio1 = document.getElementById("txt_precio1").value;
    precio2 = document.getElementById("txt_precio2").value;
    window.location.href = 'busquedas.php?estilo='+estilo+'&marca='+marca+'&modelo='+modelo+'&a1='+ano1+'&a2='+ano2+'&p1='+precio1+'&p2='+precio2+'&t=&p=1&max=10';
}