
var slideshow = function(images) {

	var options = arguments[1] || {};
	var current = 0, self = this, timer = null, timeout = null;
	var timeout = 5000;
	
	this.start = function() {
		
		//next link
		var next = new Element("a", {
			'href': 'javascript:;',
			'html': 'Next',
			'class': 'next_btn',
			'events': {
				'click': function() {
					self.next();
					self.autorotate('stop');
				}
			}
		});
		next.inject(options['next']);
		
		//previous link
		var previous = new Element("a", {
			'href': 'javascript:;',
			'html': 'Previous',
			'class': 'previous_btn',
			'events': {
				'click': function() {
					self.previous();
					self.autorotate('stop');
				}
			}
		});
		previous.inject(options['previous']);
		
		this.autorotate('start');
		this.next();
	};
	
	this.autorotate = function(mode) {
		switch(mode) {
			case "start" :
				timer = setInterval( function() { self.next(); }, timeout);
			break;
			case "stop" :
				clearInterval(timer);
			break;
		}
	};
	
	this.next = function() {
		if ( (images.length - 1) == current) {
			current = 0;
		}
		else {
			current++;
		}
		self.display();
	};
	
	this.previous = function() {
		if (0 == current) {
			current = (images.length - 1);
		}
		else {
			current--;
		}
		self.display();
	};
	
	this.display = function() {

		var img = options['container'].getElement('img');
		if (img) {
			img.destroy();
		}

		var img = new Element("img", {
			'src': images[current].thumbnail
		});
		img.setStyle('opacity', '0.0');
		img.inject(options['container']);
		img.fade('in');
		
		var text = options['container'].getElement('div');
		if (text) {
			text.destroy();
		}
		
		var text = new Element("div", {
			'html': images[current].title,
			'class': 'title',
			'styles': {
				'opacity': '0.7'
			}
		});
		text.inject(options['container']);

	};
	
};