/**
 * Permet de rendre une liste en diaporama (quoiqu'il y ai a l'intérieur : liens, texte...)
 * 
 * @param {Object} container
 * @param {Object} options
 */
var diaporamize = Class.create({
	initialize: function( container, options ){
		this.container = $( container );
		this.options = Object.extend({
			tag			: 'li',			// Nom des tag enfants
			treeTag		: 'ul',			// Nom du tag container
			effectOn	: effet_appear, // Fonction d'effet d'apparition
			effectOff	: effet_fade,	// Fonction d'effet de disparition
			autostart	: true,		// 
			time		: 3				// Durée entre chaque defilement
			
		}, options || {});
		
		//
		this.timer = null;
		this.current_frame = 0;
		this.next_frame = 1;
		
		// Récupère tous les enfants du container
		this.childrens = Element.findChildren( this.container, false, false, this.options.tag);
		
		var childrens = this.childrens;
		this.childrens.each( function( element ){
			if( element !== childrens[0] )element.style.display = "none";
		});
		
		if(this.childrens.length > 1 && this.options.autostart )this.start();
		
	},
	
	start: function(){this.timer = new PeriodicalExecuter(this.nextFrame.bind(this), this.options.time );},
	stop: function(){this.timer.stop();},
	
	/**
	 * 
	 */
	nextFrame: function()
	{
		// masque avec effet
		this.options.effectOff( this.childrens[this.current_frame] ); 
		
		// affichage avec effet
		this.options.effectOn(this.childrens[this.next_frame]);
		
		if(this.next_frame == (this.childrens.length-1)) {
			this.current_frame = this.next_frame;
			this.next_frame = 0;
		} else {
			this.current_frame = this.next_frame;
			this.next_frame++;
		}

	}
});

/**
 * voir Scriptaculous::dragdrop
 * 
 * @param {Object} element
 * @param {Object} only
 * @param {Object} recursive
 * @param {Object} tagName
 */
Element.findChildren = function(element, only, recursive, tagName) {   
  if(!element.hasChildNodes()) return null;
  tagName = tagName.toUpperCase();
  if(only) only = [only].flatten();
  var elements = [];
  $A(element.childNodes).each( function(e) {
    if(e.tagName && e.tagName.toUpperCase()==tagName &&
      (!only || (Element.classNames(e).detect(function(v) { return only.include(v) }))))
        elements.push(e);
    if(recursive) {
      var grandchildren = Element.findChildren(e, only, recursive, tagName);
      if(grandchildren) elements.push(grandchildren);
    }
  });

  return (elements.length>0 ? elements.flatten() : []);
}



/** effets possibles */
function effet_fade( element ){ $( element ).fade(); }
function effet_appear( element ){ $( element ).appear(); }
function effet_slideDown( element ){Effect.SlideDown( element );}
function effet_slideUp( element ){Effect.SlideUp( element );}
function effet_parrallel( element )
{
	new Effect.Parallel([
		    new Effect.Move( element , { sync: true, x: 400, y: 0, mode: 'relative' }), 
		    new Effect.Opacity( element , { sync: true, from: 0, to: 1 })
		  ], { 
		    duration: 1.5
		  });

}
