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

    jQuery.get(
        '/templates/admin/carousels/carousel.php',
        {
            first: carousel.first,
            last: carousel.last,
	    url: carousel.options.url
        },
        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('product', xml).each(function(i) {
        var str = carousel.options.htmlCallback($(this).find("image").text(), $(this).find("url").text(),
                $(this).find("price").text(), $(this).find("merchant").text(), $(this).find("brand").text(),
                 $(this).find("name").text());
	carousel.add(first + i, str);
    });
};

/**
 * Item html creation helper.
 */
function name_HTML(image, url, price, merchant, brand, name)
{
    var text = '<a href="' + url + '" target="_blank"><img src="' + image + '" class="jcarousel-img" /></a>';
    text += '<div class="jcarousel-item-extrainfo">' + name + '<br />' + price + '</div>';
    return text;
};

function tearoommall_HTML(image, url, price, merchant, brand, name)
{
    var text = '<a href="' + url + '"><img src="' + image + '" class="jcarousel-img" /></a>';
    text += '<div class="jcarousel-item-extrainfo">' + merchant + '<br />' + price + '</div>';
    return text;
};

function generate_carousel(cssid, xmlurl, callback)
{
  jQuery(document).ready(function() {
    jQuery(cssid).jcarousel({
        itemLoadCallback: mycarousel_itemLoadCallback,
        url: xmlurl,
        htmlCallback: callback
    });
  });
}

generate_carousel("#recycle789", "http://shops.popshops.com/shops/8w1db2rgxd4kmjm2eaquthpzv.xml", name_HTML);
generate_carousel("#organic789", "http://shops.popshops.com/shops/2q8kq5r0m8z1wwdldkeg9ixly.xml", name_HTML);

