function news_carousel_itemLoadCallback(carousel, state) {

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

	$.getJSON("files/news.ajax.php", {
		first: carousel.first,
		last: carousel.last,
		num_per_call: 3
	}, 
	function(jsonData) {
		news_carousel_itemAddCallback(carousel, carousel.first, carousel.last, jsonData);
	});
};

function news_carousel_itemAddCallback(carousel, first, last, jsonData) {

    // Set the size of the carousel
    carousel.size(parseInt(jsonData.TotalItems));

	jQuery.each(jsonData.News, function(i, val) {
        carousel.add(first + i, news_carousel_getItemHTML(val));
    });
};

/**
 * Item html creation helper.
 */
function news_carousel_getItemHTML(objNews) {

	itemHTML = '<div class="newsItem">';
		itemHTML += '<div class="newsTitle">' + objNews.Title + '</div>';
		itemHTML += '<div class="newsSynopsis">' + objNews.Synopsis + '</div>';
		itemHTML += '<div class="newsLink"><a href="' + objNews.Link + '">More</a></div>';
	itemHTML += '</div>';

    return itemHTML;
};

jQuery(document).ready(function() {
    jQuery('#news_carousel').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: news_carousel_itemLoadCallback
    });
});
