/*
 * Javascript Class for handling the category tree
 * @author: Daniel Auener <daniel@internetavdelningen.se>
 */
var Bildspel = Class.create({ 


	// Initialize one Category Tree
	initialize: function(bildspelCanvas,start,change_time,bildList) { 		
		
		this.iterator = 0;
		
		// Array with picture names
		this.list = bildList.split(",");
		this.iterator = 0;
	
	
		// position settings for picturefader
		this.canvas = $(bildspelCanvas);
		this.canvas.setStyle({
			backgroundImage:"url("+this.list[this.iterator]+")"
		}); 
		new PeriodicalExecuter(function(pe) {
										new PeriodicalExecuter(this.change.bind(this), change_time);
										this.change();
										pe.stop();
								}.bind(this), start);
		this.iterator = 1; 
	},
	
	change: function() {
		var next = new Element("div");
		next.setStyle({
			opacity:0,
			position:"absolute",
			backgroundImage:"url("+this.list[this.iterator]+")",
			zIndex:1
		});
		next.clonePosition(this.canvas);
		$(document.body).insert(next);
  		var opacity = new Effect.Opacity(next, { 
			from: 0, 
			to: 1, 
			afterFinish: function() {
				this.canvas.setStyle({backgroundImage:"url("+this.list[this.iterator]+")"});
				next.remove();
				if (this.iterator == this.list.size()-1) this.iterator = 0;
				else this.iterator++;		
			}.bind(this) 
		});
	}
	
});