var ct;

jQuery(document).ready(function() {
    if (jQuery('#news-carousel li').length > 1) {
        jQuery('#news-carousel li').each(function(i) {
            jQuery('.jcarousel-control').append(jQuery('<a class="image" href="#">'+(i+1)+'</a>'));
        });
        jQuery('.jcarousel-control a:first').addClass('selected');
        jQuery('#news-carousel').jcarousel({
            scroll: 1,
            animation: "slow",
            initCallback: nc_initCallback,
            buttonNextHTML: null,
            buttonPrevHTML: null
        });
        ct = setInterval("scrollCarousel()", 5000);
    }
});

function nc_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        jQuery('.jcarousel-control a.selected').removeClass('selected');
        jQuery(this).addClass('selected');
        return false;
    });
    jQuery('.jcarousel-control a').bind('focus', function() { clearTimeout(ct); this.blur() });
}

function scrollCarousel() {
    var cur = jQuery('.jcarousel-control a.selected').removeClass('seleted');
    var next = (cur.next().length > 0) ? cur.next() : cur.parent().children('a:first');
    next.click().addClass('selected');
}
