

function mycarousel_itemLoadCallback1(carousel, state) {
    //carousel.remove(4);
}

function mycarousel_itemLoadCallback(carousel, state) {
    // Check if the requested items already exist
    if (carousel.has(carousel.first, carousel.last)) {
        return;
    }



    var o = new Object();
    o.GalleryId = carousel.list.attr('title').substring(1, 37);
    o.Culture = '';
    o.First = carousel.first - 1;
    o.Last = carousel.last;

    var postdata = jQuery.toJSON(o);

    jQuery.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: postdata,
        async: false,
        url: CommonAPI.BaseUrl + 'Gallery/GetGalleryItems/',
        dataType: "json",
        error: function (result) {
            alert(result.responseText);
        },
        success: function (result) {
            if (result != null) {
                if(state == "init")
                    carousel.size(result.Count);
                jcarousel_itemAddCallback(carousel, o.First, o.Last, result.Items);
            }
        }
    });

}


function jcarousel_itemLoadCallback(carousel, state)
{
    // Fetch the visible range first (should be already preloaded)
    jcarousel_itemFetchCallback(carousel, carousel.first, carousel.last);

    

    //if (state == 'init' && carousel.carousel.options.visible) {
      //  carousel.last = carousel.options.visible;
    //}
    var visible = carousel.last - carousel.first + 1;

    // ---

    var first = carousel.last + 1;
    var last  = first + visible - 1;

    var first2 = last + 1;
    var last2  = first2 + visible - 1;

    jcarousel_itemFetchCallback(carousel, first, last, first2, last2);

    // ---

    var last  = carousel.first - 1;
    var first = last - visible + 1;

    var last2  = first - 1;
    var first2 = last2 - visible + 1;

    jcarousel_itemFetchCallback(carousel, first, last, first2, last2);
};

function jcarousel_itemFetchCallback(carousel, first, last, first2, last2)
{
	// Remove items to avoid big lists
    jcarousel_itemRemoveCallback(carousel, first2, last2);

	if (first < 1)
    	first = 1;

    // Check if "first" is out of range if the size was already set
    var size = carousel.size();
	if (carousel.options.wrap != 'circular' && size && first > size)
		return;

    jcarousel_itemQueryCallback(carousel, first, last);

};

function jcarousel_itemQueryCallback(carousel, first, last, realFirst)
{
    // Check if the requested items already exist
    if (carousel.has(first, last))
        return;

        var o = new Object();
        o.GalleryId = carousel.list.attr('title').substring(1, 37);
        o.Culture = '';
        o.First = first - 1;
        o.Last = last;

        var postdata = jQuery.toJSON(o);

        jQuery.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: postdata,
            url: CommonAPI.BaseUrl + 'Gallery/GetGalleryItems/',
            dataType: "json",
            error: function (result) {
                alert(result.responseText);
            },
            success: function (result) {
                if (result != null) {
                    carousel.size(result.Count);
                    jcarousel_itemAddCallback(carousel, first, last, result.Items);
                }
            }
        });
       
};

function jcarousel_itemAddCallback(carousel, first, last, items)
{

    // Set the size of the carousel
    //if (items.length < (last - first + 1))
    	//carousel.size(first + items.length - 1);

    jQuery(items).each(function (i) {
        if(!carousel.has(first + i))
            carousel.add(first + i, jcarousel_getItemHTML(this));
    });
};

function jcarousel_itemRemoveCallback(carousel, first, last)
{
    if (!first || !last)
	    return;

    for (var i = first; i <= last; i++)
    	carousel.remove(i);
};

/* circular */

function jcarousel_itemVisibleInCallback(carousel, item, i, state, evt) {
    // The index() method calculates the index from a
    // given index who is out of the actual item range.

    //var idx = carousel.index(i, mycarousel_itemList.length);
    //carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
};

function jcarousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
    carousel.remove(i);
};


/**
 * Item html creation helper.
 */
function jcarousel_getItemHTML(item)
{
    return '<img src="' + item.SmallUrl + '" alt="' +  item.Description + '" />';
};

