/**
* class Slideshow
*
*/

var Slideshow = Class.create({

	initialize: function(parent, slides) {

		// create initialy hidden slideshow
		this.id = '' + new Date().getTime() + Math.round(Math.random()*1000);
		this.parent = parent;
		
		this.wrapper = this.parent.appendChild($(Builder.node('div',{id:this.id}))); // to position absolute
		this.wrapper.absolutize();
		this.wrapper.setStyle({
			'left':'0px',
			'top':'0px',
			'width':'auto'
		});

		// the slides
		this.slides = $A();
		slides.each(function(s) {
			this.addItem(s);
		}.bind(this));
		
		this.current = 0;
		this.duration = 5;
		this.init = true;
		this.playing = false;
		
		//Event.observe(window,"resize",this.resize.bind(this));
	},
	
	show: function(slidenr) {
		if (slidenr >= 0 && slidenr < this.slides.length) {
			this.slides[this.current].fade();
			this.current = slidenr;
			this.slides[this.current].appear();
		}
	},
	
	hide: function() {
		this.stop();
		//Effect.Fade(this.wrapper,{duration:2}); // hide background
		new Effect.Fade(this.background,{from:0.75,to:0,duration:2}); // hide background
	},
	
	start: function() {
		// start slideshow
		this.pe = new PeriodicalExecuter(this.next.bind(this), this.duration);
		this.playing = true;
		if (this.init) {
			new PeriodicalExecuter(function(pe2) {
				this.slides[this.current].appear()
				pe2.stop();
			}.bind(this), 1);
			this.init = false;
		}
	},
	
	stop: function() {
		// stop slideshow
		this.pe.stop();
		this.playing = false;
	},
	
	toggle: function() {
		this.playing?this.stop():this.start();
	},
	
	next: function() {
		// hide current slide and
		// show next slide
		//alert("next");
		this.slides[this.current].fade();
		if (this.current+1 < this.slides.length) {
			this.current++;
		} else {
			this.current = 0;
		}
		this.slides[this.current].appear();
	},
	
	previous: function() {
		// hide current slide and
		// show previous slide
		this.stop();
		this.slides[this.current].fade();
		if (this.current > 0) {
			this.current--;
		} else {
			this.current = this.slides.length - 1;
		}
		this.slides[this.current].appear();
	},
	
	rewind: function() {
		this.slides[this.current].fade();
		this.current = 0;
		this.slides[this.current].appear();
	},

	last: function() {
		this.slides[this.current].fade();
		this.current = this.slides.length - 1;
		this.slides[this.current].appear();
	},
	
	addItem: function(src) {
		this.slides.push(new Slide(this.wrapper,src));
	},
	
	setSize: function(w,h) {
		this.slides.each(function(s, index) {
			s.setSize(w,h);
		});
	},
	
	setPosition: function(x,y) {
		this.wrapper.setStyle({
			'left':x + 'px',
			'top':y + 'px'
		});
	},
	
	resize: function() {
		this.background.setStyle({
			'width': document.viewport.getWidth() + 'px',
			'height': document.viewport.getHeight() + 'px'
		});
		/*this.wrapper.setStyle({
			'left':(document.viewport.getWidth()/2) + 'px',
			'top': (document.viewport.getHeight()/2) + 'px'
		});*/
	}
	
	
});
