/* (c) Jean Luc Biellmann - Groupe Ressources - 2010  */

// preload
var imgs = [];
for (n=0;n<5;n++) {
	var id = 'slide0object'+n;
	imgs.push(new Element('img',{src:'img/p'+n+'.jpg'}));
}

function showBand () {
	var o = $('band').style.opacity;
	if (o<1.) {
		if (o+s>1.) {
			$('band').style.opacity = '1.';
		} else {
			$('band').style.opacity = o+0.05;
			$('band').style.visibility = 'visible';
			setTimeout(showBand(),100);
		}
	}	
}


var Timer = Class.create({
	initialize: function (id,t1,t2,action) {
		this.id = $(id);
		this.t1 = t1; // time start
		this.t2 = t2; // time end
		this.method = this.t2>this.t1 ? 'inc' : 'dec';
		this.action = action;
		$(this.id).update(t1);
		this.to = null; // current timeout
	},
	start: function () {
		this.stop();
		var m =  'this.'+this.method+'.bind(this)';
		this.to = setTimeout(eval('this.'+this.method+'.bind(this)'),1000);
	},
	stop: function () {
		if (this.to != null)
			clearTimeout(this.to);
	},
	reset: function () {
		this.stop()
		$(this.id).update(this.from);
		this.start()
	},
	inc: function () {
		var t = parseInt($(this.id).innerHTML)+1;
		if (t >= this.t2) {
			$(this.id).update(this.t2);
			return eval(this.action);
		}
		$(this.id).update(t);
		this.to = setTimeout(this.inc.bind(this),1000);
	},
	dec: function () {
		var t = parseInt($(this.id).innerHTML)-1;
		if (t <= this.t2) {
			$(this.id).update(this.t2);
			return eval(this.action);
		}
		$(this.id).update(t);
		this.to = setTimeout(this.dec.bind(this),1000);
	}		
});

var Effect = Class.create({
	fps : 25,
	initialize : function (id) {
		this.id = $(id);
		this.x1 = this.x2 = $(id).style.offsetLeft;
		this.y1 = this.y2 = $(id).style.offsetTop;
		this.w1 = this.w2 = $(id).style.offsetWidth;
		this.h1 = this.h2 = $(id).style.offsetHeight;
		this.o1 = this.o2 = $(id).style.opacity;
		this.z1 = this.z2 = $(id).style.zIndex;
	},
	setOpacity : function (o) {
		$(this.id).style.opacity = o>1. ? 1. : o;
	}
});

var Fade = Class.create(Effect,{
	o : 0,
	to : 1, // timeout step (ms)
	os : 0.01, // opacity step
	fadeInStep : function () {
		this.o += this.os;
		this.setOpacity(this.o);
		if (this.o<1.)
			setTimeout(this.fadeInStep.bind(this),this.to);
	},
	fadeIn : function (duration) {
		this.o1 = 0.;
		this.o2 = 1.;
		this.o = this.o1;
		this.setOpacity(this.o);
		$(this.id).style.visibility = 'visible';
		var n = duration*25+1;
		this.os = 1./n;
		this.to = parseInt(duration*1000/n); // not exact...
		setTimeout(this.fadeInStep.bind(this),this.to);
	}
});



Event.observe(window,'load',function () {
	var _Timer1 = new Timer('timer1',12,0,'document.location=\'/vitrine/\';');
	//var _Timer1 = new Timer('timer1',12,0,'');
	_Timer1.start();
	var _FadeIn1 = new Fade('band');
	_FadeIn1.fadeIn(1);
});

