
var drEnchainement = new Class({
	initialize: function(photos, rep){
		this.element1 = $('drEnchainement1');
		this.element2 = $('drEnchainement2');
		this.style2 =  new Fx.Style(this.element2, 'opacity', {duration:2000});
		this.photos = photos;
		
		if(Cookie.get("startAnimation") != false)
			this.pointerImage = Cookie.get("startAnimation").toInt();
		else
			this.pointerImage = 0;

		this.lastPhoto = this.photos[this.pointerImage];

		this.rep = rep;
		this.preload();
		this.setup.bind(this)();
		this.work.bind(this)();
		
		if(window.opera)
			window.addEvent('unload', this.setCook.bind(this));
		else
			window.addEvent('beforeunload', this.setCook.bind(this));
	},
	setCook: function(){
		nombre = this.pointerImage - 1;
		if(nombre < 1)
			nombre = 0;
		Cookie.set("startAnimation", nombre);
	},
	preload : function(){
		this.photos.each(function(el,i){
			img = new Element('img', {
				'src' : this.rep + el
			});
		});
	},
	setup: function(){
		this.element2.setStyle('background-image', 'url("' + this.rep  + this.photos[this.pointerImage] + '")');
		this.upPointer();
	},
	upPointer: function(){
		this.lastPhoto = this.photos[this.pointerImage];
		this.pointerImage++;
		if(this.pointerImage >= this.photos.length)
			this.pointerImage = 0;
	},
	wait: function(){
		this.work.delay(2000, this);
	},
	work: function(){
		this.element1.setStyle('background-image', 'url("' + this.rep  + this.lastPhoto + '")');
		this.style2.start(1, 0).chain(function(){
			this.element2.setStyle('background-image', 'url("' + this.rep  + this.photos[this.pointerImage] + '")');
			this.upPointer();
			this.style2.start(0, 1).chain(this.wait.bind(this));
			
		}.bind(this));
	}
	
});

