/*--------------------------------------------------------------------------
 *  YOJA-WEB.COM : module carousel
 *  Copyright (c) Jamal Chaqouri, Yohann Fontaine, of yoja team (http://www.yoja-web.com)
 *
 *  module carousel is a yoja-web work.
 *  For details, see the yoja web site: http://www.yoja-web.com
/*--------------------------------------------------------------------------*/

CAROUSEL = "<div></div>";
CAROUSEL_CLASS = ".yojaCarousel";
CAROUSEL_CLASS_DEFAULT = "jMyCarousel";
CAROUSEL_WIDTH_DEFAULT = '560px';
CAROUSEL_DEFAULT_PARAM = "defaultCarousel";


var carouselParameters = new Array();
carouselParameters[CAROUSEL_DEFAULT_PARAM]=true;

ImagesCarousel = Class.create();

ImagesCarousel.prototype = {  
  initialize: function(context) {
    this.vignettes = YOJAUtil.cleanStyle($j("div.category-thumbnail img:first", context)).clone(true);
  }
};


ImagesCarouselFormatter = { 
   formater: function(imgsCarousel) {
      var template = $j(CAROUSEL);     
      var vignettes =  imgsCarousel.vignettes;
      vignettes.appendTo(template); 
      YOJAUtil.wrapUlList(vignettes);    
      return template;
   }
};
 
GestionImagesCarousel =  {
  traiter : function() {    
    var context = $j("div.module-products table, div.subcategory table, div.product-block table"); 
   	if(context.length<1){
		return null;
	}
    var carousel = new ImagesCarousel(context);
    var result = ImagesCarouselFormatter.formater(carousel); 
    return result;
  }
};

function initCarousel() {

	var yojaCarousel = GestionImagesCarousel.traiter();

	if(carouselParameters[CAROUSEL_DEFAULT_PARAM]){
		initDefaultCarousel(yojaCarousel);
	}
	$j(CAROUSEL_CLASS).each(function(){
			var currentCarousel = $j(this);
			var params = getCarouselParameters(currentCarousel);					
			if(currentCarousel.hasClass('myImages')){
				currentCarousel.jMyCarousel(params);
			} else {
				var cloneYojaCarousel = GestionImagesCarousel.traiter();
				currentCarousel.append(cloneYojaCarousel);
				cloneYojaCarousel.jMyCarousel(params);
			}
			
			if(currentCarousel.hasClass('reflection')){
				currentCarousel.reflecter(); 
			}
		
		});
		
		
	
}

function initDefaultCarousel(carousel){
	  if(carousel != null){
		  carousel.attr("id","yojaCarousel");
	      carousel.addClass(CAROUSEL_CLASS_DEFAULT);
		  $j(".content:first").prepend(carousel);
		  carousel.jMyCarousel(getCarouselParameters(carousel));
		  carousel.reflecter(); 
	   }
}

function getCarouselParameters(carousel){
   var parameters = carouselParameters[carousel.attr("id")] || [];
   parameters.visible = parameters.visible || getCarouselVisibility(carousel);
   parameters.auto = parameters.auto || false;
   parameters.speed = parameters.speed || 500;
   return parameters;
}

function getCarouselVisibility(carousel){

	  var visibility = CAROUSEL_WIDTH_DEFAULT;
	  var widthCarousel = carousel.css("width");
	  if(widthCarousel && widthCarousel!='auto'){
		  visibility=widthCarousel;
	  }
	  return visibility;
}

function loadCarousel(){
	$j("body").append("<script type=\"text/javascript\">addEvent(window,'load',initCarousel);</script>"); 
}


if(isIE()){
	loadCarousel();
} else {
	DocumentReadyExecuter.addTraitement(loadCarousel);
}






