var SideShow = Class.create(
{
	initialize:function(ele,options){
		this.ele = $(ele);
		
		this.options = Object.extend({
			duration:5,
			img_select:"li",
			afterChange:Prototype.emptyFunction,
			beforeChange:Prototype.emptyFunction,
			effect:"ghost"/*fade,ghost*/,
			delay:0
		},options);
		this.data=[];
		
		//alert(ele.readAttribute("w:delay"));
		
		this.data=this.ele.select(this.options.img_select);
		this.ele.makeClipping();
		this.ele.cleanWhitespace();
		//find the highest
		var widest=0;
		var highest=0;
		var p = this.ele.positionedOffset();
		this.data.each(
			function(e){
				if(e.getWidth()>widest) widest=e.getWidth();
				if(e.getHeight()>highest) highest=e.getHeight();
			}.bind(this)
		);
		

		this.ele.setStyle({
						  width:widest+"px",
						  height:(highest-4)+"px",
						  listStyle:"none",
						  margin:"0px",
						  padding:"0px"
						  });
		
		this.is_effecting=false;
		this.pos=0;

		
		this.data.invoke("hide");

		this.data[this.pos].show();
		
		if(this.data.length>1){
			window.setTimeout((function() {
 			this.start() }).bind(this), Number(this.options.delay)*1000);
		}
	},
	change:function(){
		
		for(var i=0; i<this.data.length; i++){
			this.data[i]=$(this.data[i].identify());
			
		}
		
		this.options.beforeChange(this.pos,this.data[this.pos]);
		
		if(this.options.effect=="ghost"){
			this.ele.setStyle({backgroundImage:"url("+this.data[this.pos].down("img").src+")",backgroundRepeat:"no-repeat"});
		}
		
		this.current_effect= new Effect.Fade(this.data.find(function(e){return e.visible()}),{afterFinish:function(){
			//this.ele.setStyle({backgroundImage:""});
			this.data.invoke("hide");
			if(this.options.effect=="ghost"){
				this.data[this.pos].show();
			}else{
				this.current_effect= new Effect.Appear(this.data[this.pos]);
			}
			this.is_effecting=false;
			this.options.afterChange(this.pos,this.data[this.pos]);
		}.bind(this)});	
		this.is_effecting=true;
	},
	next:function(){
		if(!this.is_effecting){
			this.stop();
			if((++this.pos)>=this.data.length) this.pos=0;
			this.change();
			this.start();
		}
	},
	previous:function(){
		if(!this.is_effecting){
			this.stop();
			if((--this.pos)<0) this.pos=(this.data.length-1);
			this.change();
			this.start();
		}
	},
	stop:function(){
		if(this.current_effect)
			this.current_effect.cancel();
		this.pe.stop();
	},
	start:function(){
		this.pe=new PeriodicalExecuter(
			function(){
				//alert(this.pos);
				if((++this.pos)>=this.data.length) this.pos=0;
				
				this.change();
			}.bind(this),
			this.options.duration
		);
	}
}
);

document.observe(
	"dom:loaded",
	function(){
		//$$(".slideshow").each(function(e){e.identify()});
		$$(".slideshow").each(
			function(ele){
				regcmp(new SideShow(ele.identify(),{delay:Number(this.ele.readAttribute("option:delay")|0)}),ele.identify());
			}
		);
		
		$$(".slideshow_fade").each(
			function(ele){
				regcmp(new SideShow(ele.identify(),{effect:"fade",delay:Number(ele.readAttribute("option:delay")|0)}),ele.identify());
			}
		);
		
	}
);