window.addEvent('domready', function(){

	var Slideshow = new Class({
		_timeline : undefined,
		_list : undefined,
		_current : 0,
        _old : 0,
        _auto : true,
        _total : 0,
        _nav : undefined,
        _delay : 8000,
        _effect : Fx.Transitions.linear,
        _duration : 1500,
        
        initialize: function(){
        	
        	if (!$('gallery')){
        		return;
        	}
        	
        	this._list = $('gallery');
        	var children = this._list.getElements("div.photo-zoom");
        	this._total = children.length;
        	
        	if (this._total < 2){
        		return;
        	}
        	
        	var num;
        	for (var i = 0; i < this._total; i++){
				num = i + 1;
				opacity = (i === this._current) ? 1 : 0;
				$("photo-zoom-"+num).setStyles({
					opacity : opacity
				});
			}

        	this._timer = this._autoRun.periodical(this._delay, this);
        },
        _change: function(num){
            if (num != this._current){
                this._old = this._current;
                this._current = num;
                this._hide();
            }
        },
        _show: function(){ 
        	
        	var num = this._current + 1;
            var element = $("photo-zoom-" + num);
            if (element.hasClass('hide')){
            	element.removeClass('hide');            	
            }
            element.setStyles({display : 'block', visibility : 'visible'});
            
            var myFx = new Fx.Tween(element);
            myFx.start('opacity', '0', '1');
        },
        _hide: function(){
        	var num = this._old + 1;
            var element = $("photo-zoom-" + num);
            
            var myFx = new Fx.Tween(element);
            myFx.addEvent("complete", this._onHide.bind(this));
            myFx.start('opacity', '1', '0');
        },
        _onHide : function(){
        	var num = this._old + 1;
            var element = $("photo-zoom-" + num);
        	element.setStyles({display : 'none', visibility : 'hidden'});
            this._show();
        },
		_autoRun : function(){
			if (this._auto){
				var num = this._current + 1;
				if (num >= this._total){
					num = 0;
				}
				this._change(num);
			}
		}
    });
    var slideshow = new Slideshow;
});







/*
var button_cliquable = true;
var running_slidshow = true;
var intervalle_duration  = 10000;

var json_files;
var json_total;
var current_json = 0;

var e_image;
var current_href;

var durationIn  = 1500; 					// durée du fadeIn
var durationOut = 1000; 					// durée du fadeOut
var fxFunction  = Fx.Transitions.linear;	// transition
var fadeOut;
var fadeIn;

var oFadeIn = {
			duration   : durationIn,
			wait       : false,
			transition : fxFunction
			};
var oFadeOut = {
			duration   : durationOut,
			wait       : false,
			transition : fxFunction
			};
			
// chargement du fihciers json
window.addEvent('domready', load_json_file);

function load_json_file()
{
	var sPath     = "http://" + http_host + racine_site;
	var sFile     = sPath + "includes/ajax/get_pictures_list.php";
	var sMeth     = "post";
	var sEncoding = "utf-8";
	var sQuery    = "type="+slideshow_type+"&id_article="+current_art_id;
	var request = new Request.JSON({
					method     : sMeth,
					url        : sFile,
					onComplete: function(json_response){
						json_files = json_response.fichier;
						json_total = json_response.total;
						if (json_total > 1){
							next_json.delay(intervalle_duration);
						}
					}	
	}).send(sQuery);	
}

function next_json(){
	// json suivant
	current_json++;
	
	if (current_json >= json_total){
		current_json = 0;
	}
	
	var counter = 0;
	
	json_files.each(function(fichier){
		// json courant
		if (counter == current_json) {
			// prochain href du lien pour la lightbox
			current_href = fichier.aupt_fichier;
			// préchargement de l'image
			e_image = new Asset.image(fichier.aupt_vign, {
				id : 'visuel',
				title : fichier.aupt_alt,
				onload : function(){
					fade_out();
				},
				onerror : function(){
					//alert("Error "+fichier.aupt_vign);
					return;
				}
			});
		}
		counter++;
	});
}

function change_image(){
	var div_slideshow = $(div_element);
	div_slideshow.empty();
	
	var div_photo = new Element('div', {'id' : 'bloc-photo'});
	var div_legende = new Element('div', {'id' : 'zoom-legende'});
	var img_link = new Element('img', {
		'src' : racine_site + 'medias/commun/pixel.gif',
		'width' : '1',
		'height' : '1',
		'alt' : 'zoom'
	});
	var link_zoom = new Element('a', {
    	'href': current_href,
	    'id' : 'zoom',
		'rel' : 'milkbox[gall1]'
    });
	// Image
	e_image.inject(div_photo);
	div_photo.inject(div_slideshow);
	// Lien
	img_link.inject(link_zoom);
	link_zoom.inject(div_legende);
	div_legende.inject(div_slideshow);
	
	milkbox.reloadGalleries();
	
	fade_in();
}

// losque l'image est chargée
function fade_out(){
	var slide_show = $(div_element);
	var fx         = new Fx.Morph(slide_show, oFadeOut);
	fx.addEvent ("complete", change_image);
	
	// j'assigne un délai avant le prochain changement
	//do_slide_show.delay (inter);
	fx.start({'opacity': 0});
}
// lorsque le contenu est changé
function fade_in(){
	var slide_show = $(div_element);
	var fx         = new Fx.Morph(slide_show, oFadeIn);
	
	fx.start({'opacity': 1});
	
	next_json.delay(intervalle_duration);
}*/
