var slideShow = Class.create();
Object.extend(slideShow, {
	events: {
		lissajous: function (show) {
			var i,o,t,cycles,
				slides=show.slides,slidect=show.slides.length,
				transpos,holdrem,
				time=((new Date).getTime()-show.startPeriod)/1000,
				cur,last,next;

			cycles=Math.floor(time/(show.hold+show.trans));
			holdrem=Math.max(0,show.hold-Math.min(time-cycles*(show.hold+show.trans),show.hold));
			transpos=Math.max((time-(cycles*(show.hold+show.trans))-show.hold)/show.trans,0);
			
			last=show.slide((cycles-1+slidect) % slidect);
			cur=show.slide((cycles) % slidect);
			next=show.slide((cycles+1) % slidect);

			Element.setOpacity(cur,1);
			Element.setOpacity(next,transpos);
			
			if (next.parentNode!==show.el)
				show.el.appendChild(next);
			if (last.parentNode!==null) 
				last.parentNode.removeChild(last);
				
			show.lissajousHandle.frequency=Math.max(holdrem/2,1/slideShow.framerate);
		},
		controlShow: function (show,go) {
			if (!show instanceof slideShow) return false;
			if (go==false && show.lissajousHandle instanceof PeriodicalExecuter)
				show.lissajousHandle.stop();
			else
				show.lissajousHandle=new PeriodicalExecuter(show.lissajous,1/slideShow.framerate);
		}		
	},
	pi: Math.PI,
	framerate: 100
});
Object.extend(slideShow.prototype,{
	initialize: function (el,slides) {
		var i,o,pi=slideShow.pi;
		if (typeof el == 'string') {
			el=document.getElementById(el);
		}
		this.el=el;
		Event.observe(window,'blur',slideShow.events.controlShow.curry(this,false));
		Event.observe(window,'focus',slideShow.events.controlShow.curry(this,true));
		this.slides=slides;
		this.cacheSlides();
		this.holdmin=6;
		this.holdmax=8;
		this.lissajous=slideShow.events.lissajous.curry(this);
		this.control=slideShow.events.controlShow.curry(this);
		if (typeof el.getAttribute('delay')) {
			i=el.getAttribute('delay');
			if (/,/.test(i)) {
				i=i.split(/,/);
				this.holdmin=parseInt(i[0]);
				this.holdmax=parseInt(i[1]);
			}else
				this.holdmin=this.holdmax=parseInt(i);
		}
		this.hold=Math.random()*(this.holdmax-this.holdmin)+this.holdmin; //Time to hold each image
		this.trans=this.hold/6; //Time for transition
		this.counter=0;
		var links=document.getElementsByTagName('a');
		for (i=0; i<links.length; i++) {
			Event.observe(links[i],'click',function () {
				this.pause();
				return true;
			});
		}
	},
	start: function () {
		while (this.el.firstChild!==null) this.el.removeChild(this.el.firstChild);
		this.el.appendChild(this.slides[0]);
		this.startPeriod=(new Date).getTime();
		this.control(true);
	},
	pause: function () {
		this.control(false);
	},
	slide: function (ind) {
		if (typeof this.slides[ind].nodeName == 'undefined') {
			var sl=document.createElement('img');
			Object.extend(sl,this.slides[ind]);
			Object.extend(sl.style,{
				position: 'absolute',
				top:'0',
				left:'0'
			});
			this.slides[ind]=sl;
		}
		return this.slides[ind];
	},
	cacheSlides: function () {
		for (i=0; i<this.slides.length; i++) {
			if (i>=2) 
				setTimeout(this.slide.bind(this,i),2500*Math.max(0,i-2)+(i<2?0:Math.random()*2500));
			else this.slide(i);
		}
	}
});

