var SideShow = Class.create(
{
	initialize:function(ele,options){
		this.ele = $(ele);
		this.ele_id = this.ele.identify();
		var thisObj = this;
		
		//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();
				if (navigator.userAgent.indexOf("Safari")>0) highest-=18; else highest-=4; 
			}.bind(this)	
		);
		

		this.options = Object.extend({
			duration:6,
			//img_select:"li",
			img_select:"tr",
			afterChange:Prototype.emptyFunction,
			beforeChange:Prototype.emptyFunction,
			effect:"fade"/*fade,ghost,sidehori*/,
			delay:0,
			width:widest,
			button:'',
			button_img_out:'',
			button_img_over:'',
			button_img_current:''
		},options);		

		var s = {
				  width:widest+"px",
				  height:highest+"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);
			}
		} else 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();
			}
		} else if (["build"].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();
			}
		}
		
		if (this.options.button) {
			$$(this.options.button).each(function(e){
												  
				thisObj.buttons=[];
				
				for (var i=0; i<thisObj.data.length; i++) {
					var src = i>0 ? thisObj.options.button_img_out : thisObj.options.button_img_current;
					thisObj.buttons[i] = Builder.node('dd', { style:'margin:0px; display:inline;' }, [
						/*Builder.node('a', { href:'#', onclick:"$('"+thisObj.ele_id+"').getcmp('SideShow').goto("+i+"); return false;" }, [
							Builder.node('img', { src:src, border:0, page:i })
						])*/
						Builder.node('img', { src:src, border:0, page:i, style:'cursor:pointer' })
					]);
				}
				
				thisObj.button = Builder.node('dt', {} , thisObj.buttons);
				e.appendChild(thisObj.button);
				
				for (var i=0; i<thisObj.data.length; i++) {
					Event.observe(e.down().down('dd', i).down(), 'click', function(ele) {
						thisObj.goto(this.readAttribute('page'));
					});
				}
				
			});
		}
			
	},
	change:function(){
		var thisObj = this;
		
		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));
			
		} else 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()}),{duration:0.3,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)});	
			
		} else if (["build"].include(this.options.effect)) {
			
			this.current_effect= new Effect.Fade(this.data.find(function(e){return e.visible()}),{duration: 0.5, afterFinish:function(){
				this.data.invoke("hide");
				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)});	
			
		}
		
		if (this.options.button) {
			$$(this.options.button).each(function(e){
				for (var i=0; i<thisObj.data.length; i++) {		  
					var btn = e.down().down('dd', i);
					if (btn) btn.down('img').src = thisObj.pos==i ? thisObj.options.button_img_current : thisObj.options.button_img_out;
				}
			});
		}
		
		this.is_effecting=true;
	},
	goto:function(page){
		if(!this.is_effecting){
			this.stop();
			this.lastpos=this.pos;
			this.pos=page;
			this.change();
			this.start();
		}
	},
	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
		);
	}
}
);

Event.observe(window,"load",
	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());
			}
		);
		
		$$(".slideshow_build").each(
			function(ele){
				regcmp(new SideShow(ele.identify(),{
							effect:"build",
							delay:Number(ele.readAttribute("option:delay")||0),
							duration:Number(ele.readAttribute("option:duration")||6),
							button:String(ele.readAttribute("option:button")||''),
							button_img_out:String(ele.readAttribute("option:button_img_out")||'../images/dot_gray.jpg'),
							button_img_over:String(ele.readAttribute("option:button_img_over")||'../images/dot_gray.jpg'),
							button_img_current:String(ele.readAttribute("option:button_img_current")||'../images/dot_green.jpg')
							}),ele.identify());
			}
		);
		$$(".slideshow_ghost2").each(
			function(ele){
				regcmp(new SideShow(ele.identify(),{
							effect:"fade",
							delay:Number(ele.readAttribute("option:delay")||0),
							duration:Number(ele.readAttribute("option:duration")||6),
							button:String(ele.readAttribute("option:button")||''),
							button_img_out:String(ele.readAttribute("option:button_img_out")||'../images/dot_gray.jpg'),
							button_img_over:String(ele.readAttribute("option:button_img_over")||'../images/dot_gray.jpg'),
							button_img_current:String(ele.readAttribute("option:button_img_current")||'../images/dot_green.jpg')
							}),ele.identify());
			}
		);
		
	}
);

//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());
//			}
//		);
//		
//		$$(".slideshow_build").each(
//			function(ele){
//				regcmp(new SideShow(ele.identify(),{
//							effect:"build",
//							delay:Number(ele.readAttribute("option:delay")||0),
//							duration:Number(ele.readAttribute("option:duration")||6),
//							button:String(ele.readAttribute("option:button")||''),
//							button_img_out:String(ele.readAttribute("option:button_img_out")||'../images/dot_gray.jpg'),
//							button_img_over:String(ele.readAttribute("option:button_img_over")||'../images/dot_gray.jpg'),
//							button_img_current:String(ele.readAttribute("option:button_img_current")||'../images/dot_green.jpg')
//							}),ele.identify());
//			}
//		);
//		$$(".slideshow_ghost2").each(
//			function(ele){
//				regcmp(new SideShow(ele.identify(),{
//							effect:"fade",
//							delay:Number(ele.readAttribute("option:delay")||0),
//							duration:Number(ele.readAttribute("option:duration")||6),
//							button:String(ele.readAttribute("option:button")||''),
//							button_img_out:String(ele.readAttribute("option:button_img_out")||'../images/dot_gray.jpg'),
//							button_img_over:String(ele.readAttribute("option:button_img_over")||'../images/dot_gray.jpg'),
//							button_img_current:String(ele.readAttribute("option:button_img_current")||'../images/dot_green.jpg')
//							}),ele.identify());
//			}
//		);
//		
//	}
//);