var SideShow = Class.create(
{
	initialize:function(ele,options){
		this.ele = $(ele);
		
		this.options = Object.extend({
			duration:6,
			img_select:"li",
			afterChange:Prototype.emptyFunction,
			beforeChange:Prototype.emptyFunction
		},options);
		this.data=[];
		
		this.data=this.ele.select(this.options.img_select);
		
		//find the highest
		var highest=0;
		this.data.each(
			function(e){
				if(e.getHeight()>highest) highest=e.getHeight();
			}
		);
		
		
		
		/*var html = "<ul style='list-style:none;padding:0px;margin:0px;height:"+highest+"px'>"+this.ele.innerHTML+"</ul>";
		
		
		var id=this.ele.identify();
		this.ele.replace("<div id=\""+id+"\" style=\"height:"+highest+"px\"></div>");
		this.ele=$(id);
		this.ele.update(html);*/
		
		//this.ele.down('div').setStyle({height:highest+"px"});
		/*this.ele.insert({after:"<div></div>"});
		var d = this.ele.next('div');
		d.setStyle({height:highest+"px"});
		d.insert({bottom:this.ele});*/
		
		
		
		//this.data=this.ele.select(this.options.img_select);
		
		
		
		this.ele.setStyle({height:highest+"px"});
		//this.ele.makeClipping();
		
		
		
		
		
		
		this.is_effecting=false;
		this.pos=0;
		//this.ele.update(this.data[this.pos]);
		
		this.data.invoke("hide");
		//this.ele.insert({bottom:"<#{tag}></#{tag}>".interpolate({tag:this.options.img_select})});
		//this.display_ele = this.ele.childElements().last();
		//this.display_ele.setStyle({height:500+"px"});
		
		//this.display_ele.update(this.data[this.pos].innerHTML);
		this.data[this.pos].show();
		
		if(this.data.length>1){
			this.start();
		}
	},
	change:function(){
		this.options.beforeChange(this.pos,this.data[this.pos]);
		
		this.current_effect= new Effect.Fade(this.data.find(function(e){return e.visible()}),{afterFinish:function(){
			this.data.invoke("hide");
			
			//this.display_ele.setStyle({height:500+"px"});
			//this.display_ele.update(this.data[this.pos].innerHTML);
			//this.data[this.pos].show();
			
			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),ele.identify());
			}
		);
	}
);
