/*
 * 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;
	
		/*var bildspelBg = new Element("div");
		bildspelBg.setStyle({
			backgroundColor:"white",
		});
		$(document.body).insert(bildspelBg);*/
	
		// position settings for picturefader
		this.canvas = $(bildspelCanvas);
		this.canvas.setStyle({
			overflow:"hidden"
		});
		this.current = new Element("img").writeAttribute("src",this.list[this.iterator]).setStyle({width:"920px",zIndex:"2"});
		this.canvas.update(this.current);
		
		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",
			overflow:"hidden",
			//backgroundImage:"url("+this.list[this.iterator]+")",
			zIndex:1
		});*/
		var img = new Element("img").setStyle({width:this.canvas.getWidth()+"px",zIndex:"1"}).writeAttribute("src",this.list[this.iterator]);
		next.insert(img);
		next.setStyle({display:"none",position:"absolute",top:this.canvas.cumulativeOffset().top+"px",height:"188px",overflow:"hidden",left:this.canvas.cumulativeOffset().left+"px",zIndex:"1"});
		//next.clonePosition(this.canvas);
		$(document.body).insert(next);
		
		new Effect.Appear(next, { 
			afterFinish: function() {
				this.canvas.update(this.current = new Element("img").setStyle({width:this.canvas.getWidth()+"px",zIndex:"1"}).writeAttribute("src",this.list[this.iterator]));
				this.current.setStyle({overflow:"hidden",zIndex:"2"});
				next.remove();
				if (this.iterator == this.list.size()-1) this.iterator = 0;
				else this.iterator++;		
			}.bind(this) 
		});
		/*var opacity = new Effect.Opacity(next, { 
			from: 0, 
			to: 1, 
			afterFinish: function() {
				this.canvas.update(new Element("img").writeAttribute("src",this.list[this.iterator]));
				next.remove();
				if (this.iterator == this.list.size()-1) this.iterator = 0;
				else this.iterator++;		
			}.bind(this) 
		});*/
		
	}
	
});