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

var _sunlight = {
	psrcs : {},
	refs : [],
	nb : 5,
	inc : 0.05, // opacity inc
	duration : 50, // setTimout inc
	pos : $A(), // indexes
	to : $A(), // timeout
	opacity : $A(), // timeout
	timeout : function () {
		var to = parseInt(Math.random()*10000);
		if (to<5000) // mini
			to=5000;
		return to;
	},
	alea : function (pnum) {
		var j,wonderland;
		wonderland=$A();
		for (var i=0;i<this.refs.length;i++)
			if (!this.pos.inArray(i))
				wonderland.push(i);
		// take care if wonderland is 1 !!!
		j = parseInt(Math.random()/parseFloat(1./wonderland.length));
		if (j>=wonderland.length)
			j = wonderland.length;
		//console.debug(wonderland+' '+j);
		this.pos[pnum] = wonderland[j];
	},
	fill : function (pnum) {
		var img,a,j,ref;
		j = this.pos[pnum];
		//console.debug(this.pos);
		$('sunlight'+pnum).style.opacity = this.opacity[pnum] = 0;
		ref = this.refs[j];
		img = E('img',{'src':'/mod/showroom/img/thumbs/'+this.psrcs[ref][0]+'.jpg','alt':ref});
		img.addClassName('signal').addClassName('thumb').addClassName('shadow');
		a = E('a',{'href':'index.php?p=Meubles&ref='+ref+'&psrc='+this.psrcs[ref][0]});
		A(a,img);
		A($('sunlight'+pnum),a);
	},
	hide : function (pnum) {
		clearTimeout(this.to[pnum]);
		if (this.opacity[pnum]>0) {
			this.opacity[pnum]-=this.inc;
			$('sunlight'+pnum).style.opacity = this.opacity[pnum];
			setTimeout(_sunlight.hide.bind(_sunlight,pnum),this.duration);
		} else {
			$('sunlight'+pnum).update('');
			this.alea(pnum);
			this.fill(pnum);
			this.show(pnum);
		}
	},
	show : function (pnum) {
		if (this.opacity[pnum]<1) {
			this.opacity[pnum]+=this.inc;
			$('sunlight'+pnum).style.opacity = this.opacity[pnum];
			setTimeout(_sunlight.show.bind(_sunlight,pnum),this.duration);
		} else {
			this.to[pnum] = setTimeout(_sunlight.hide.bind(_sunlight,pnum),this.timeout());
		}
	},
	init : function (psrcs) {
		var div,pnum;
		this.psrcs = psrcs;
		this.refs = $H(psrcs).keys();
		for (pnum=0;pnum<this.nb;pnum++) {
			div = E('div').addClassName('sunlight');
			div.id = 'sunlight'+pnum;
			A($('rule'),div);
		}
		for (pnum=0;pnum<this.nb;pnum++) {
			this.alea(pnum);
			this.fill(pnum);
			this.show(pnum);
		}
	}
};

