
var selectedCarousel;
var CarouselWidget  = function (opts,render,vertical) {
  this.inToolTip = false;
  this.pause=false;
  this.render = render;
  this.vertical = vertical;
  this.carousel = null;
  this.carouselIsStarted = false;
  this.opts = opts;
  this.prefix = opts.prefix;
  this.qstring = opts.qstring;
};


CarouselWidget.prototype = {

  startCarousel: function() {
    jQuery('#'+this.render).jcarousel({
      vertical : this.vertical,
      scroll: 1,
      auto: 5,
      wrap: 'last',
      //scroll: this.opts.nbToShow,
      //visible: this.opts.nbToShow,
      //visible: 4,
      initCallback : this.onInitCarousel,
      itemLoadCallback: {onBeforeAnimation: this.itemLoadCallback},
      options: { widget: this }
    });
    this.carouselIsStarted = true;
  },

  onInitCarousel: function(carousel,state) {
    if(state=='init') {
      carousel.options.options.widget.carousel = carousel;
    }
  },
  itemLoadCallback: function(carousel,state) {
    if(!carousel.has(carousel.first, carousel.last)){
    var url = carousel.options.options.widget.prefix+'ajax/carousel.json';
    jQuery.getJSON(
		    url,
      carousel.options.options.widget.qstring,
      function(data) {
//        console.log(data.length);
        if(typeof data.items === 'undefined')
        {
          carousel.options.size=0;
	  carousel.size(0);
        }
        else {
	  carousel.size(data.items.length);
          carousel.options.size=data.items.length;
          for(i=0;i<data.items.length;i++){
            var item = carousel.add(i,getItemHTML(data.items[i], data.prefix))[0];
						$(item.firstChild).data("item", data.items[i]);
          }
        }
      }
    );
    }
  },
  
  updateCarousel : function(id) {
    this.qstring.id = id;
    if(this.carouselIsStarted==false) {
      this.startCarousel();
    }
    else {
      this.carousel.reset();
    }
  }
};

function getItemHTML(item,prefix) {
    var ItemHtml='<div class="carousel-item">';
    
    switch (item.type)
    {
    	default:
	  var title = item.title;
	  if (title.length>18)
		  title = title.substring(0,18) + '...';
          if (item.thumbnail == null)
            ItemHtml = ItemHtml +'<div><p><a href="'+item.openUrl+'">'+title+'</a></p></div>';
          else if (item.thumbnail.substring(0,7)=='http://' || item.thumbnail.substring(0,8)=='https://')
            ItemHtml = ItemHtml +'<div><a href="'+item.openUrl+'"><img src="'+ item.thumbnail+'" alt="" /></a><p><a href="'+item.openUrl+'">'+title+'</a></p></div>';
          else
            ItemHtml = ItemHtml +'<div><a href="'+item.openUrl +'"><img src="'+ ((item.thumbnail_animate != null && item.thumbnail_animate!="") ? item.thumbnail_animate : item.thumbnail) +'" alt="" /></a><p><a href="'+item.openUrl +'">'+title+'</a></p></div>';
            break;
    }
    ItemHtml = ItemHtml + '</div>';
    
    return ItemHtml;
  };


$(".carousel-item").live("mouseover", function(evt) {
	var item = $(this).data("item");
	item["hasPrefix"] = true;
	if (tooltip != null)
		tooltip.show("Informations", [item], evt);	
});

$(".carousel-item").live("mouseout", function(evt) {
	if (tooltip != null)
		tooltip.askForHide();
});
