var carousel_prodid;
var carousel_rootfold;
var carousel_accessfold;

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

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

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

    jQuery.get(
        carousel_rootfold + 'Modules/ajax/carousel.php',
        {
            first: carousel.first,
            last: carousel.last,
		prod_id: carousel_prodid
        },
        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()));

    var items = jQuery('item', xml);

    items.each(function(i) {
        carousel.add(first + i, mycarousel_getItemHTML(this));
    });

};


/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item)
{
    return '<a class="lienProd" href="'+carousel_accessfold+'shop/'+jQuery('url', item).text()+'"><img src="'+jQuery('img', item).text()+'" width="100" height="100" /><br /><b>'+mycarousel_truncate(jQuery('ref', item).text(), 10)+'</b></a>';
};

/**
 * Utility function for truncating a string without breaking words.
 */
function mycarousel_truncate(str, length, suffix) {
	return str;
/*
    if (str.length <= length) {
        return str;
    }

    if (suffix == undefined) {
        suffix = '...';
    }

    return str.substr(0, length).replace(/\s+?(\S+)?$/g, '') + suffix;
*/
};

