/**
 * @author Dominik Scholz
 */

var slideshow = {
	// vars
	pos: 0,
	effect: null,
	slides: null,
	width: 0,
	lightbox: null,
	
	// init slideshow
	init: function(e) {
		Event.observe($('previous'), 'click', this.previous.bind(this));
		Event.observe($('next'), 'click', this.next.bind(this));
		Event.observe($('corner'), 'click', this.zoom.bind(this));
		this.slides = $('slides');
		var children = this.slides.childElements();
		this.width = children[0].offsetWidth;
		this.slides.style.width = (children.length * this.width) + 'px';
		this.refreshButtons();
	},
	
	// goto previous image
	previous: function() {
		if (this.effect) this.effect.cancel();
		this.pos--;
		this.effect = new Effect.Move(this.slides, {
			x: -this.pos * this.width,
			y: 0,
			mode: 'absolute',
			transition: Effect.Transitions.sinoidal,
			duration: 1
		});
		this.refreshButtons();
	},
	
	// goto next image
	next: function() {
		if (this.effect) this.effect.cancel();
		this.pos++;
		this.effect = new Effect.Move(this.slides, {
			x: -this.pos * this.width,
			y: 0,
			mode: 'absolute',
			transition: Effect.Transitions.sinoidal,
			duration: 1
		});
		this.refreshButtons();
	},
	
	// zoom current image
	zoom: function() {
		var i, url;
		var children = this.slides.childElements();
		var data = [];
		for (i=0; i<children.length; i++) {
			url = children[i].src + "&t=large";
			data.push([url, '']);	
		}
		cxLightbox.inject(data, this.pos);
	},
	
	// hide or show buttons, depended on current position
	refreshButtons: function() {
		var max = this.slides.childElements().length-1;
		$('previous').style.display = (this.pos <= 0)? 'none':'block';
		$('next').style.display = (this.pos >= max)? 'none':'block';
	}
};

// register buttons
document.observe("dom:loaded", function() {
	slideshow.init();
});



