var curpos, batchSize, items = [];
var comments = [];
function getImageTag(img) {
    return "<img src=\"" + img + "\">";
}

function getImages(o) {
    var carousel = this,
    imgurl = "fileadmin/getimages.php",
    revealEnabled = carousel.get("revealAmount"),
    numVisible = carousel.get("numVisible"),
    curpos = o.num > numVisible + (revealEnabled?1:0) ? o.last - (o.last % numVisible) : o.first;
	  
    YAHOO.util.Connect.asyncRequest("GET", imgurl+ "?r="+Math.random()+"&batchSize="+ batchSize + "&pos=" + curpos, {
        success: function (o) {
        	
            var i = curpos,
            j = 0,
            r = eval("(" + o.responseText + ")"),
            item,
            loadingItems;

            curpos += r.length;

            while (i < curpos) {

                if (r[j]) {
                if (r[j].name)
            		{
                		// items.push(r[i].name);
                		item = getImageTag(r[j].name);
                		// comments.push(r[j].desc);
                		comments[i] = r[j].desc;
                }
                else{
                		
                		item = getImageTag(r[j]);
                		comments[i] = "kein Kommentar"
                		// comments.push("kein Kommentar");
                }
                    loadingItems = carousel.getLoadingItems();

                    if(loadingItems[i]){
                        // We use replaceItem to swap out a placeholder loading item, which allows for backwards navigation. 
                        carousel.replaceItem(item,i);
                    } else {
                    	
                        carousel.addItem(item);
                    }

                } else {
                    break;
                }
                i++;
                j++;
            }
            

            carousel.set("selectedItem", carousel.get("firstVisible"));
        },

        failure: function (o) {
            alert("Ajax request failed (2)!");
        }
    });
}

// Get the image link from within its (parent) container.
function getLargeImage(parent) {
    var el = parent.firstChild;

    while (el) { // walk through till as long as there\'s an element
    if (el.nodeName.toUpperCase() == "IMG") { // found an image
        // flickr uses "_s" suffix for small, and "_m" for big
        // images respectively
        // return el.src.replace(/_s\.jpg$/, "_l.jpg");
        // picasa uses s128 ffür lkeiner bilder, s400 für mittel s800 für grosse
        return el.src.replace(/\/s128\//, "/s800/");
    }
    el = el.nextSibling;
}
return "";
}

YAHOO.util.Event.onDOMReady(function (ev) {
		
    var carousel, spotlight = YAHOO.util.Dom.get("spotlight");
	  
    /* Allow for anchor pagination to better demonstrate backwards loading. 
       Note: MAX_PAGER_BUTTONS defaults to 5 making a Carousel with more than
       5 pages spawn a select box containing pages instead of page anchors. */

    YAHOO.widget.Carousel.prototype.CONFIG.MAX_PAGER_BUTTONS = 100;
 
  /* Setting numItems is required for dynamic loading. This property lets Carousel 
           know how many total items it will ever be populated with.
           Not to be confused with numVisible: the number of items to display
           per page. In this case we don't specify it and Carousel 
           defaults to 3 items visible per page. */
    carousel = new YAHOO.widget.Carousel("container", {
 				numItems: 50,
 				numVisible:6
    });
	
	YAHOO.util.Connect.asyncRequest("GET", "fileadmin/getimages.php" + "?getanz="+Math.random(), {
        success: function (o) {
        // 	alert (carousel.numItens);
          anz = parseInt(o.responseText);
          carousel.set("numItems", anz);
        	// carousel.numItens=parseInt(o.responseText);
        	// alert (carousel.numItens);
        	// console.log(carousel);
        	}
    });
	
    batchSize = carousel.get("numVisible");

    YAHOO.util.Connect.asyncRequest("GET", "fileadmin/getimages.php" + "?r="+Math.random()+"&batchSize=" + batchSize, {
        success: function (o) {
            var i, r = eval("(" + o.responseText + ")");

            curpos = r.length;

            for (i = 0; i < curpos; i++) {
            	if (r[i])
            	{
            		if (r[i].name)
            		{
                		items.push(r[i].name);
                		comments.push(r[i].desc);
                }
                else{
                		items.push(r[i]);
                		comments.push("kein Kommentar");
                }
              }
            }

            // check if the Carousel widget is available
            if (typeof carousel != "undefined") {
                for (i = 0; i < curpos; i++) {
                    // if so, shove the elements into it
                    carousel.addItem(getImageTag(items[i]));
                }
                carousel.set("selectedItem", 0);
                items = [];
            }
        },

        failure: function (o) {
            alert("Ajax request failed (1)!");
        }
    });

    carousel.on("loadItems", function (o) {
        // more items available?
        getImages.call(this, o);
    });

    carousel.on("itemSelected", function (index) {
        // item has the reference to the Carousel\'s item
        var item = carousel.getElementForItem(index);
				
        if (item) {
        	 
            	var content = "";
           
        	 if (comments[index])
            	content += "<div class='kommentar'>" + comments[index] +"</div>";
           content += "<img src=\""+getLargeImage(item)+"\">";
            	
            spotlight.innerHTML = content	;
        }
    });

    carousel.render();
    carousel.show();
});
