/**
 * @author MansonPlus
 */
var curpos, items = [];
var contador = 0;

var cargar_reciente = function(pos1, pos2){
    if (pos1 >= contador) {
        //alert("añadir");
        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 style='float:left;width:100px;'><a href='anuncio.php?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.php?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>"
					//datos = "<div style=''><table align='center'><tr><td valign='top'><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></td><td class='datos' valign='top'><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></td></tr></table></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 aaa = 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(); 
}

