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

    jQuery.get(
        '/bloques/dynamic_ajax_php.php',
        {
            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('elemento', xml).each(function(i) {
        carousel.add(first + i, mycarousel_getItemHTML(jQuery(this).find('image').text(), jQuery(this).find('titulo').text(), jQuery(this).find('texto').text(), jQuery(this).find('url').text(), jQuery(this).find('descargable').text(),  jQuery(this).find('archivo').text()));
    });
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(imagen, titulo, texto, direccion, decargable, archivo)
{
    if(decargable == 1){
        var descarga = '<a href="/archivos/campanyas/'+archivo+'" title="Descargar" class="download" target="_blank"><img alt="Descargar" src="/images/bot_descarga.png" title="Descargar"></a>';
		return '<div class="pasti"><a href="/archivos/campanyas/'+archivo+'" title="Descargar" target="_blank"><img src="' + imagen + '" alt="" border="0" width="156" height="85"/></a><h4><a href="'+direccion+'"  target="_blank">'+titulo+'</a></h4> '+texto+' '+descarga+'</div>';
    }else{
        var descarga = '';
		return '<div class="pasti"><a href="'+direccion+'" target="_blank"><img src="' + imagen + '" alt="" border="0" width="156" height="85"/></a><h4><a href="'+direccion+'"  target="_blank">'+titulo+'</a></h4> '+texto+' '+descarga+'</div>';
    }
    
};

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