function mycarousel_itemLoadCallback(carousel, state)
{
	// Check if the requested items already exist
	if (carousel.has(carousel.first, carousel.last)) {
		return;
	}

	jQuery.get(
			'/home/ajax_banner_image',
			{
				first: carousel.first,
	last: carousel.last
			},
			function(xml) {
				mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, xml);
			},
			'xml'
		  );
};

function mycarousel_itemAddCallback(carousel, first, last, xml)
{
	// Set the size of the carousel
	carousel.size(parseInt(jQuery('total', xml).text()));

	jQuery('banner', xml).each(function(i) {

		var image = jQuery('image',this).text();
		var link = jQuery('link',this).text();
		var intro = jQuery('intro',this).text();

		carousel.add(first + i, mycarousel_getItemHTML( link, image, intro ));
	});
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(href, url, alt)
{
	return '<a href="'+href+'"><img  src="' + url + '" alt="'+alt+'" title="'+alt+'" /></a>';
};

function mycarousel_initCallback(carousel)
{
	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
};

jQuery(document).ready(function() {

	jQuery('#bannercarousel').jcarousel({
		// Uncomment the following option if you want items
		// which are outside the visible range to be removed
		// from the DOM.
		// Useful for carousels with MANY items.

		// itemVisibleOutCallback: {onAfterAnimation: function(carousel, item, i, state, evt) { carousel.remove(i); }},
		itemLoadCallback: mycarousel_itemLoadCallback,
		scroll : 1,
		visible : 1,
		initCallback: mycarousel_initCallback,
		auto: 5,
		wrap: 'last',
		itemFallbackDimension : 530
	});

});

