function mycarousel_initCallback(carousel, state)
{
    if (state == 'init')
    {
        var id = carousel.container.attr('id');

        carousel.size(carousel_images[id].length);
    }
}

function mycarousel_itemLoadCallback(carousel, state)
{
    var id = carousel.container.attr('id');
    var images = carousel_images[id];

    for (var i = carousel.first; i <= carousel.last; i++)
    {
        if (carousel.has(i))
            continue;

        if(i > images.length)
            break;

        item_data = images[i - 1];
        url_s = item_data.url_s;
        item_data.url_s = '';

        var item = jQuery(mycarousel_getItemHTML(item_data));

        mycarousel_add_lightbox(item);

        $("img.jcsimg", item).bind("load", {i:i,item:item}, function(e){
                i = e.data.i;
                item = e.data.item;
                carousel.add(i, item);
            });

        $("img.jcsimg", item).attr('src', url_s);

    }

};

function mycarousel_add_lightbox(item)
{
    item.prettyPhoto({
            animationSpeed: 'fast', /* fast/slow/normal */
            padding: 40, /* padding for each side of the picture */
            opacity: 0.8, /* Value betwee 0 and 1 */
            showTitle: true, /* true/false */
            allowresize: false /* true/false */
        });
}

/**
 * Item html creation helper.
*/

function mycarousel_getItemHTML(item)
{
    var description;
    var html;
    var desc;

    if (item.description != "")  {
        description = '" title=" <b>Desc: </b>' + item.description + '<br> ';
        desc = 'title="Pais: '+ item.country + ';  Desc: ' + item.description + '"';
        desc_en = 'title="Country: '+ item.country + ';  Desc: ' + item.description + '"';
        }
    else {
        description = '"title="';
        desc = 'title="Pais: '+ item.country + '"';
        desc_en = 'title="Country: '+ item.country + '"';
        }

    if (page_lang == "es"){
        dis = '<br><p style=font-size:9px;font-family:arial;> El copyright de las imágenes pertenece a sus respectivos autores y/o productoras/distribuidoras. </p>';
        html = '<a class = "lightbox" href="'+ item.url_l + description + '<b>Tipo: </b>' + item.type_id + '<br> <b>Pais: </b>' + item.country + dis +'"><img class="jcsimg" src="' + item.url_s + '"'+ desc +'" width="120" height="120"/></a>';
    }
    else{
        dis = '<br><p style=font-size:9px;font-family:arial;>All images are copyrighted by their respective copyright holders, manufacturers/distributors. </p>';
        html ='<a class = "lightbox" href="'+ item.url_l + description + '<b>Type: </b>' + item.type_id + '<br> <b>Country: </b>' + item.country + dis +'"><img class="jcsimg" src="' + item.url_s + '"'+ desc_en +'" width="120" height="120"/></a>';
        }

    return html;
};


jQuery(document).ready(function() {
	 jQuery('.carousel').jcarousel({
	 start:1,
	 scroll:5,
	 animation: 'slow',
	 initCallback: mycarousel_initCallback,
	 itemLoadCallback: {onBeforeAnimation: mycarousel_itemLoadCallback}
 });
});

