/**
 * Javascript brugt på forsiden
 */

jQuery(document).ready(
    function() {
        $('#shovel').empty();
    }
);

jQuery(document).ready(function() {
    // Last position of start is ...
    var start = parseInt($.cookie('jCarouselStart'));
    if (isNaN(start) || start < 1) {
        start = 1;
    }

    jQuery('#shovel').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: function (carousel, state)
        {
            // Check if the requested items already exist
            /*
            if (carousel.has(carousel.first, carousel.last)) {
                return;
            }
            */

            jQuery.get(
                '/fotos/xml2/orderby/id DESC/',
                {
                    first: carousel.first - 1,
                    last:  carousel.last
                },
                function(xml)
                {
                    var first = carousel.first;

                    // Set the size of the carousel
                    carousel.size(parseInt(jQuery('total', xml).text()));

                    // Save the current position of start. Then we will return to this next time we visit this page.
                    $.cookie('jCarouselStart', first);

                    jQuery('image', xml).each(function(i) {
                        carousel.add(first + i, jQuery(this).text());
                    });
                },
                'xml'
            );
        },

        initCallback: function (carousel, state)
        {
            var size = carousel.last - carousel.first - 1;
            carousel.options.scroll = size;
        },

        visible: null,
        start: start
    });
});

