/**
 * DIAPORAMA PERSONALISE LEB ARTS MEDIAS
 * Dévelopé par Pascal PORTIER, Développeur et Responsable réseau et infrastuctures
 * http://www.lebarts.com
 *
 * OPTIONS
 * bigs: 'bigs', l'ID de l'ensemble des photos
 * running: false, true lance la lecture automatique, false non
 * rollover: false, true précise qu'il y a des images rollover à changer, false non
 * time: 5000, le temps entre 2 photos en ms
 * speed: 0, le temps d'une appartion et d'une disparition en ms, on peut aussi mettre "slow", "fast"...
 *
 */
(function($) {
  //
  // plugin definition
  //
  $.fn.atkDefil = function(options) {



    debug('Application lancée !');

    // build main options before element iteration
    var opts = $.extend({}, $.fn.atkDefil.defaults, options);

	if(opts.speed==0)opts.speed=Math.round(opts.time/10);

   /**
   /* Comportement au lancement
   /**/

	debug('#'+opts.bigs+' img:lt(0)');
   $("#"+opts.bigs+" img:gt(0)").hide();
   if(opts.rollover)$(this).find("a:eq(0)").addClass("actif").find("img").unbind("mouseover").unbind("mouseout").removeClass("rollover").attr("src",$(this).find("a:eq(0) img").attr("src").replace(".","_roll."));

   obj=$(this);
	c=0;

	$(this).find("a").each(function(c){

		debug('Un élément trouvé à '+c);


//		if(c>0){
//		}

		$(this).bind("click",function(e){
			e.preventDefault();
			opts.running = false;


			$("#"+opts.bigs+" img:visible").fadeOut(opts.speed,function(){
				$("#"+opts.bigs+" img:eq("+c+")").fadeIn(opts.speed);
			});

//			if(opts.rollover){

				$(obj).find("a").removeClass("actif").find("img.rollover").removeClass("rollover");
				$(this).addClass("actif").find("img").removeClass("rollover");

				$(obj).find("a img").each(function(z){
					$(this).attr("src",$(this).attr("src").replace("_roll.","."));
					$(this).unbind("mouseover").unbind("mouseout");


					if($(this).parent().hasClass("actif")){
						debug('Un actif trouvé à '+z);
						$(this).attr("src",$(this).attr("src").replace(".","_roll."));
//						$(this).removeClass("rollover");
					}else{
						debug('Un rollover trouvé à '+z);
						$(this).attr("src",$(this).attr("src").replace("_roll.","."));
						attachRollOverEvent(this);
//						$(this).addClass("rollover");
					}
				});

//				$(obj).find("a img.rollover").each(function(){
//					attachRollOverEvent(this);
//				});
//			}
		});
		opts.maxi=c+1;
	});

	if(opts.maxi==1){
		opts.running=false;
		$(obj).hide();
	}

	if(opts.running==true){
		debug("J'ai demandé à animer");
		setTimeout (function(){animate(1);opts.running=true;}, opts.time );
	}

	  function animate(x){
	  		debug("J'anime.... à "+x+" sur "+opts.maxi);
	   	if (opts.running==true) {
	      	if(x>opts.maxi-1)x=0;
	      	$(obj).find("a:eq("+x+")").click();
	      	setTimeout (function(){animate(x+1);opts.running=true;}, opts.time );
	   	}
	   }
  };

  //
  // private function for debugging
  //
  function debug(txt) {
    if (window.console && window.console.log){
//      window.console.log(txt);
    }
  };

  //
  // define and expose our format function
  //
  $.fn.atkDefil.format = function(txt) {
    return '<strong>' + txt + '</strong>';
  };

  //
  // plugin defaults
  //
  $.fn.atkDefil.defaults = {
    bigs: 'bigs',
    maxi: 0,
    running: false,
    rollover: false,
    time: 6000,
    speed: 0
  };
//
// end of closure
//
})(jQuery);
