function mycarousel_initCallback(carousel) {
    jQuery('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });

    jQuery('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};


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(
        '/wp-content/themes/elixie/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)));
    }
};

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



jQuery(document).ready(function() {
	
    $('#mycarousel').jcarousel({
        scroll: 1,
		itemLoadCallback: mycarousel_itemLoadCallback,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
	
	$('img#flkrprev').hover(function() {
		$(this).attr("src","/wp-content/themes/elixie/images/context_arrow_more_left_over.gif.v1.gif");
			}, function() {
		$(this).attr("src","/wp-content/themes/elixie/images/context_arrow_more_left_out.gif.v1.gif");
	});
	$('img#flkrnext').hover(function() {
		$(this).attr("src","/wp-content/themes/elixie/images/context_arrow_more_right_over.gif.v1.gif");
			}, function() {
		$(this).attr("src","/wp-content/themes/elixie/images/context_arrow_more_right_out.gif.v1.gif");
	});
	$('img#flkrbrowse').hover(function() {
		$(this).attr("src","/wp-content/themes/elixie/images/context_view_page_over.gif.v1.gif");
			}, function() {
		$(this).attr("src","/wp-content/themes/elixie/images/context_view_page_out.gif.v1.gif");
	});
});
