var SideShow = Class.create(
{
	initialize:function(ele,options){
		this.ele = $(ele);
		
		//find the highest
		var widest=0;
		var highest=0;
		var p = this.ele.positionedOffset();

		this.data=[];
		this.data=this.ele.select("li");
		this.data.each(
			function(e){
				if(e.getWidth()>widest) widest=e.getWidth();
				if(e.getHeight()>highest) highest=e.getHeight();
			}.bind(this)	
		);
		

		this.options = Object.extend({
			duration:6,
			img_select:"li",
			afterChange:Prototype.emptyFunction,
			beforeChange:Prototype.emptyFunction,
			effect:"fade"/*fade,ghost,sidehori*/,
			delay:0,
			width:widest
		},options);
		

		var s = {
				  width:widest+"px",
				  height:(highest-4)+"px",
				  listStyle:"none",
				  margin:"0px",
				  padding:"0px"
				  };
		
		this.is_effecting=false;
		this._isPrevius=false;
		
		this.ele.cleanWhitespace();
		this.ele.select(this.options.img_select).each(function(e){e.cleanWhitespace()});
		this.pos=0;
		this.lastpos=0;
		if(this.options.effect=="sidehori"){ 
			s = Object.extend(s,{
				width:widest+"px",
				whiteSpace:"nowrap"
			});
			this.ele.setStyle(s);
			this.ele.insert({bottom:this.data.map(function(e){return "<li>"+e.innerHTML+"</li>"}).join('')});
			
			this.ele.select(this.options.img_select).each(
				function(e){
					e.setStyle({display:"inline"});
				}
			);
			this.ele.makeClipping();
			if(this.data.length>1){
				window.setTimeout((function() {
 				this.start() }).bind(this), Number(this.options.delay)*1000);
			}
		}
		
		if(["fade","ghost"].include(this.options.effect)){
			this.ele.setStyle(s);
			this.ele.makeClipping();
			
			this.data.invoke("hide");
			if(this.data.length>1){
				this.start();
				this.data[this.pos].show();
			}
		}
			
	},
	change:function(){
		for(var i=0; i<this.data.length; i++){
			this.data[i]=$(this.data[i].identify());	
		}

		if(this.options.effect=="sidehori"){ 
			
			if(this._isPrevius) {
				
				this._isPrevius=false;
				var cpos = this.pos*2;
				if(cpos==this.data.length){
					pos1=this.data.length;
					pos2=this.data.length-1;
				}else{
					pos1=cpos+1;
					pos2=cpos;
				}
			}else{
			
				var cpos = this.pos;
				if(cpos==0){
					pos1=this.data.length-1;
					pos2=this.data.length;
				}else{
					pos1=cpos-1;
					pos2=cpos;
				}
			}

			var d=this.options.width*pos2;
			this.current_effect=new Effect.Tween(this.ele,this.options.width*pos1,this.options.width*pos2,function(p){
				this.ele.scrollLeft=p;
				if(p==d){
					this.is_effecting=false;
					
				}
			}.bind(this));
		}
		
		if(["fade","ghost"].include(this.options.effect)){
			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();
			this.lastpos=this.pos;
			if((++this.pos)>=this.data.length) this.pos=0;
			this.change();
			this.start();
		}
	},
	previous:function(){
		if(!this.is_effecting){
			this._isPrevius=true;
			this.stop();
			this.lastpos=this.pos;
			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);
				this.lastpos=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(),{
					effect:ele.readAttribute("option:effect")||"fade",
					delay:Number(ele.readAttribute("option:delay")||0),
					duration:Number(ele.readAttribute("option:duration")||6)
					}),ele.identify());
			}
		);
		
		$$(".slideshow_ghost").each(
			function(ele){
				regcmp(new SideShow(ele.identify(),{
						effect:"ghost",
						delay:Number(ele.readAttribute("option:delay")||0),
						duration:Number(ele.readAttribute("option:duration")||6)
						}),ele.identify());
			}
		);
		
		$$(".slideshow_sidehori").each(
			function(ele){
				regcmp(new SideShow(ele.identify(),{
							effect:"sidehori",
							delay:Number(ele.readAttribute("option:delay")||0),
							duration:Number(ele.readAttribute("option:duration")||6)
							}),ele.identify());
			}
		);
		
	}
);