function loadItemHandler(carousel, start, last, available)
{
    if (available) {
        // Trigger loaded
        carousel.loaded();
        return;
    }

    var cr = carousel;

    jQuery.get("fetch/pics.txt", function(data) {
        appendItemCallback(cr, start, last, data);
    });
};

function appendItemCallback(carousel, start, last, data)
{
    var items = data.split("|");

    for (i = start; i <= last; i++) {
        if (items[i-1] == undefined) {
            break;
        }

        var item = carousel.add(i, getItemHTML(items[i-1]));

        item.each(function() {
            // Urgh...ThickBox should provide a function for that
            jQuery("a.thickbox", this).bind("click", function() {
                var t = this.title || this.name || null;
                var g = this.rel || false;
                TB_show(t,this.href,g);
                this.blur();
                return false;
            });
        });
    }

    // Trigger loaded
    carousel.loaded();
};

function getItemHTML(data)
{
    var split = data.split(";end;");
    var url   = jQuery.trim(split[0]);
    var title = jQuery.trim(split[1]);
	var isnew = jQuery.trim(split[2]);
    //var url_m = url.replace(/_s.jpg/g, '.jpg');
	var url_t = url.replace(/.jpg/g, '_m.jpg');
    return '<a href="' + url + '" title="' + title + '" alt=" " class="thickbox ' + isnew + '" rel="hoygroup" style="background-image:url(' + url_t + ');" ><span></span></a>';
};

// Next-Button handling...
var nextOver = function() {jQuery(this).attr("src", "img/next-over.gif");};
var nextOut = function() {jQuery(this).attr("src", "img/next.gif");};
var nextDown = function() {jQuery(this).attr("src", "img/next-down.gif");};

function nextButtonStateHandler(carousel, button, enabling)
{
    if (enabling) {
        jQuery(button).attr("src", "img/next.gif")
                      .bind("mouseover", nextOver)
                      .bind("mouseout", nextOut)
                      .bind("mousedown", nextDown);
    } else {
        jQuery(button).attr("src", "img/next-disabled.gif")
                      .unbind("unmouseover", nextOver)
                      .unbind("unmouseout", nextOut)
                      .unbind("unmousedown", nextDown);
    }
}

// Prev-Button handling
var prevOver = function() {jQuery(this).attr("src", "img/prev-over.gif");};
var prevOut = function() {jQuery(this).attr("src", "img/prev.gif");};
var prevDown = function() {jQuery(this).attr("src", "img/prev-down.gif");};

function prevButtonStateHandler(carousel, button, enabling)
{
    if (enabling) {
        jQuery(button).attr("src", "img/prev.gif")
                      .bind("mouseover", prevOver)
                      .bind("mouseout", prevOut)
                      .bind("mousedown", prevDown);
    } else {
        jQuery(button).attr("src", "img/prev-disabled.gif")
                      .unbind("unmouseover", prevOver)
                      .unbind("unmouseout", prevOut)
                      .unbind("unmousedown", prevDown);
    }
}

// Ride the carousel...
jQuery(document).ready(function() 
{
    jQuery().ajaxStart(function() {
        jQuery(".loading").show();
    });

    jQuery().ajaxStop(function() {
        jQuery(".loading").hide();
    });

    jQuery("#mycarousel").jcarousel({
        itemVisible: 5,
        itemScroll: 5,
        wrap: true,
        wrapPrev: true,
		scrollAnimation: "slow",
        loadItemHandler: loadItemHandler,
        nextButtonStateHandler: nextButtonStateHandler,
        prevButtonStateHandler: prevButtonStateHandler
    });
});
