var carousel;

// Get the image link from within its (parent) container.
function getImage(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
            //	In the carousel, images have their matching links in the "alt" tag
			//	We grab the image path and the link and return them as an array
			var values = new Array();
			values[0] = el.alt;
			values[1] = el.src;	
            return values;
        }
        el = el.nextSibling;
    }

    return "";
}

function countCarouselItems() {
	var carouselOl = document.getElementById("carousel");					
	return carouselOl.getElementsByTagName("img").length;
}

YAHOO.util.Event.onDOMReady(function (ev) {
	var itemCount = countCarouselItems();
    var el, item,
        spotlight   = YAHOO.util.Dom.get("spotlight"),
        carousel    = new YAHOO.widget.Carousel("container", {
			isCircular: true,
			numVisible: 1, 
			autoPlay: 6000,
			animation: { speed: 0.5 }
		});
    carousel.render(); // get ready for rendering the widget	
    carousel.show();   // display the widget
	carousel.startAutoPlay();

    // display the first selected item in the spotlight
    item = carousel.getElementForItem(carousel.get("selectedItem"));
    if (item) {
		var results = getImage(item);
        spotlight.innerHTML = "<a href=\""+results[0]+"\"><img src=\""+results[1]+"\"></a>";
    }

    carousel.on("itemSelected", function (index) {
        // item has the reference to the Carousel's item
        item = carousel.getElementForItem(index);

        if (item) {
			var results = getImage(item);
            spotlight.innerHTML = "<a href=\""+results[0]+"\"><img src=\""+results[1]+"\"></a>";
        }
    });
});