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 = 'opportunités';
  } else {
    txtopportunities = 'opportunité';
  }

  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 = resCount[0].data.total.value;
      var title;
      if(countIPViewerItems=="0"){
        title = "Experts de votre profession";
        $('#mycarousel_title').html(title);
        var output = "<div><img alt=\"\" src=\"/files/img_exp/ipviewer-default.jpg\" /></div>";
        $('#mycarousel').html(output);
      } else {
        title = "Consultez nos offres en intérim, CDD et CDI";
        $('#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:mycarousel_itemLoadCallback,
          initCallback:mycarousel_initCallback

        });
      }
    }
  });




});

