function mycarousel_itemLoadCallback(carousel, state)
{
    // Check if the requested items already exist
    if (carousel.has(carousel.first, parseInt(carousel.last+1))) {
        return;
    }
        
    $.ajax({
    type: "GET",
    dataType: "json",
    url: "/jquery/getIPViewerItems/"+carousel.first+"/"+parseInt(carousel.last+1),
    success: function(res, status) {
            mycarousel_itemAddCallback(carousel, carousel.first, parseInt(carousel.last+1), res);
        }
    });
};


function mycarousel_itemAddCallback(carousel, first, last, res)
{
	var result_data = res[0].data;
	carousel.size(parseInt(result_data.total.value));
	
    jQuery(result_data.partners).each(function(i) {
    	var image = this.image;
    	var nboffers = this.nboffers;
    	var name = this.name;
    	var targeturl = this.targeturl;
        carousel.add(first + i, mycarousel_getItemHTML(image, nboffers, name, targeturl));
    });
	
};

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(image, nboffers, name, targeturl)
{
	var txtopportunities;
	var output;
	if(nboffers>1){
		txtopportunities = 'offres';
	} else {
		txtopportunities = 'offre';
	}
	
	output	=	"<div class=\"jcarousel-item-detail\">";
	output +=			"<div class=\"jcarousel-item-detail-image\"><a href=\""+targeturl+"\"><img src=\""+image+"\" /></a></div>";
	output +=			"<div class=\"jcarousel-item-detail-text\">";
	output +=				"<div class=\"jcarousel-item-detail-link-company\"><a href=\""+targeturl+"\">"+name+"</a></div>";
	output +=				"<div class=\"jcarousel-item-detail-link-offers\"><a href=\""+targeturl+"\">"+nboffers+" "+txtopportunities+"</a></div>";
	output +=			"</div>";
	output +=		"</div>";
    return output;
};

//see http://sorgalla.com/projects/jcarousel/#Configuration to set the configuration parameters
//see http://sorgalla.com/projects/jcarousel/ for examples
jQuery(document).ready(function() {
	$.ajax({
    type: "GET",
    dataType: "json",
    url: "/jquery/countIPViewerItems",
    success: function(resCount) {
    	var countIPViewerItems = parseInt( resCount[0].data.total.value );
			var title;
			// affiche les logos s'il y a au moins 4 logos
    	if( countIPViewerItems < 4 ){
				title = "Publication d'offres d'emploi gratuites";
				$('#mycarousel_title').html(title);
				var output = "<div><a href=\"../inscription-publication-offres-emploi-gratuites/inscription\"><img alt=\"Publication d'offres d'emploi gratuites\" src=\"/files/img_rf/shared/bandeau_Steph_FJB_161.jpg\" /></a></div>";
				$('#mycarousel').html(output);
			} else {
				if ($(".offerNumber a").length > 0){
					titleText = $(".offerNumber a").html()
					titleLink = $(".offerNumber a").attr("href")
				}else{
					titleText = "Consulter nos offres d'emploi en CDI, CDD et intérim"
					titleLink = "/recherche_offres/all"
				}
				
				title = "<a href=\""+titleLink+"\">"+titleText+"</a>";
				$('#mycarousel_title').html(title);
			    jQuery('#mycarousel').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); }},
			        scroll:1,
			        auto:4,
			        wrap:"last",
			        itemLoadCallback: { onBeforeAnimation:mycarousel_itemLoadCallback },
			        initCallback:mycarousel_initCallback
			    });
			}
        }
    });
});

