function mycarousel_itemLoadCallback(carousel, state)
{
    if (carousel.prevFirst != null) {
        // Remove the last visible items to keep the list small
        for (var i = carousel.prevFirst; i <= carousel.prevLast; i++) {
            // jCarousel takes care not to remove visible items
            carousel.remove(i);
        }
    }

    var per_page = carousel.last - carousel.first + 1;
    var currPage = 0;
    var f,l;
    var cr = carousel;

    for (var i = carousel.first; i <= carousel.last; i++) {
        var page = Math.ceil(i / per_page);

        if (currPage != page) {
            currPage = page;

            f = ((page - 1) * per_page) + 1;
            l = f + per_page - 1;

            f = f < carousel.first ? carousel.first : f;
            l = l > carousel.last ? carousel.last : l;

            if (carousel.has(f, l)) {
                continue;
            }

            mycarousel_makeRequest(carousel, f, l, per_page, page);
        }
    }
};

function mycarousel_makeRequest(carousel, first, last, per_page, page)
{
    // Lock carousel until request has been made
    carousel.lock();

    jQuery.get(
        '/api/dynamic_flickr_api.php',
        {
            'per_page': per_page,
            'page': page
        },
        function(data) {
            mycarousel_itemAddCallback(carousel, first, last, data, page);
        },
        'xml'
    );
};

function mycarousel_itemAddCallback(carousel, first, last, data, page)
{
    // Unlock
    carousel.unlock();

    // Set size
    carousel.size($('photos', data).attr('total'));

    var photos = $('photo', data);
    var per_page = carousel.last - carousel.first + 1;

    for (var i = first; i <= last; i++) {
        var pos = i - 1;
        var idx = Math.round(((pos / per_page) - Math.floor(pos / per_page)) * per_page);

        carousel.add(i, mycarousel_getItemHTML(photos.get(idx)));
    }
	
	//Captify
    $('img.captify').captify({
    // all of these options are... optional
    // speed of the mouseover effect
    speedOver: 'fast',
    // speed of the mouseout effect
    speedOut: 'normal',
    // how long to delay the hiding of the caption after mouseout (ms)
    hideDelay: 500,
    // 'fade', 'slide', 'always-on'
    animation: 'slide',
    // text/html to be placed at the beginning of every caption
    prefix: '',
    // opacity of the caption on mouse over
    opacity: '0.7',
    // the name of the CSS class to apply to the caption box
    className: 'caption-bottom',
    // position of the caption (top or bottom)
    position: 'bottom',
    // caption span % of the image
    spanWidth: '100%'
    });	
	
};

/**
 * Global item html creation helper.
 */
function mycarousel_getItemHTML(photo)
{
	// $(photo).attr('owner') = "33518318%40N00";
    var url = 'http://farm'+$(photo).attr('farm')+'.static.flickr.com/'+$(photo).attr('server')+'/'+$(photo).attr('id')+'_'+$(photo).attr('secret')+'_s.jpg';
    return '<div class="shadow"><a href="http://www.flickr.com/photos/'+"33518318%40N00"+'/'+$(photo).attr('id')+'/" target="_blank" title="'+$(photo).attr('title')+'">' +
	'<img id="flickrthere" class="captify" src="' + url + '" width="75" height="75" alt="'+$(photo).attr('title')+'" />'+
	'</a></div>';
};

jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({
		scroll:5,
		visible:6,
        itemLoadCallback: mycarousel_itemLoadCallback
    });	
});
