//Microlslide 1.0

function loadMicroslide(){
	$$('.microslide').each(function(elmt){
		if (!elmt.id){elmt.id = new Date() *1.0 + parseInt(Math.random() * 65535);};
		if (microslideElmtChild(elmt) != null){
			eval("var tmpVar = new microslide(); window['"+ elmt.id+"'] = tmpVar; tmpVar.id = '"+elmt.id+"'; tmpVar.tagName = '"+microslideElmtChild(elmt).nodeName.toLowerCase()+"';tmpVar.load();");
		}
	});
}

function microslideElmtChild(elmt){
	var elmtType = elmt.firstChild;
	while (elmtType != null&&elmtType.nodeType != 1){elmtType = elmtType.nextSibling;};
	return elmtType;
}

function microslide(){
	this.id = '';
	this.delay = 5;
	this.transition = .9;
	this.tagName = '';
	this.frame = 0;
	this.nbElmt =  0;
	this.trans = 0;
}

microslide.prototype.load = function(){
	var elmtId = 0;
	var that = this;
	if (!$(this.id)){alert('Objet introuvable.')};
	this.tagName = (this.tagName == '')?microslideElmtChild($(this.id)).nodeName.toLowerCase():this.tagName;
	$$('#'+this.id+' '+this.tagName).each(function(elmt) {
		elmt.style.display = (elmtId==0)?'':'none';
		elmt.style.position = 'absolute';
		elmt.id = that.id+'Elmt' + elmtId++;
	});
	this.nbElmt = elmtId-1;
	this.pe = new PeriodicalExecuter(function(){that.swap();},this.delay); 
}

microslide.prototype.swap = function(frame){
	var currentFrame = this.frame;
	var that = this;
	frame = (isNaN(frame))?-1:frame;
	if (frame!=this.frame&&frame<=this.nbElmt&&this.trans==0){
		this.frame = (frame>=0)?frame:(this.frame == this.nbElmt)?0:this.frame+1;
		this.trans = 1;
		Effect.Fade(this.id + 'Elmt' + currentFrame,{ duration: this.transition,afterFinish:function(){that.trans = 0} });
		Effect.Appear(this.id + 'Elmt' + this.frame,{ duration: this.transition });
	}
}

microslide.prototype.next = function(){
	this.stop();
	var currentFrame = this.frame;
	var that = this;
	this.frame = (this.frame == this.nbElmt)?0:this.frame+1;
	this.trans = 1;
	Effect.Fade(this.id + 'Elmt' + currentFrame,{ duration: this.transition,afterFinish:function(){that.trans = 0} });
	Effect.Appear(this.id + 'Elmt' + this.frame,{ duration: this.transition });	
}


microslide.prototype.previous = function(){
	this.stop()
	var currentFrame = this.frame;
	var that = this;
	this.frame = (this.frame == 0)?this.nbElmt:this.frame-1;
	this.trans = 1;
	Effect.Fade(this.id + 'Elmt' + currentFrame,{ duration: this.transition,afterFinish:function(){that.trans = 0} });
	Effect.Appear(this.id + 'Elmt' + this.frame,{ duration: this.transition });	
}

microslide.prototype.stop = function(){
	if (this.pe!=null){
		this.pe.stop();
		this.pe=null;
	}
}

microslide.prototype.start = function(){
	if (!this.pe){
		var that = this;
		this.pe = new PeriodicalExecuter(function(){that.swap();},this.delay); 
	}
}

microslide.prototype.show = function(frame){
	this.stop();
	this.swap(frame);
}

if (window.addEventListener) { 
	window.addEventListener("load", loadMicroslide, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", loadMicroslide);
}
