
var Slides = function(options) {

	var _i = document.getElementById('show_target');
	var _c = document.getElementById('show_caption');
	var _delay = options.interval * 1000;
	var _min = options.first || 1;
	var _max = options.last;
	var _captions = options.captions;
	var _index = 0;
	var _to = null;
	var _stopped = true;
	
	var me = this;
	
	var advance = function(step) {
		if(step == null) step = 1;
		_index+=step;
	
		if(_index > _max) _index = _min;
		if(_index < _min) _index = _max;

		_i.src = 'images/photos/' + _index + '.jpg';
		_c.innerHTML = '' + _index + '/' + _max + '' + ': ' + _captions[_index-1];

		if(!_stopped) _to = window.setTimeout(advance, _delay, 1);
	}

	this.pause = function() {
		if(!_stopped) {	
			window.clearTimeout(_to);
			_to = null;
			_stopped = true;
		}
	}
	
	this.play = function() {
		_stopped = false;
		advance(1);
	}

	////////////
	// let's get ourselves some controls:
	////////////
	var link_arr = [
		{ 	text: '&laquo;&laquo;', 
			onclick: function() { me.pause(); advance(-1); return false; }, 
			after: '&nbsp'},
		{ 	text: 'play', 
			onclick: function(){ me.play(); return false; },
			after: '&nbsp;| ' },
		{ 	text: 'pause', 
			onclick: function(){ me.pause(); return false; },
			after: '&nbsp;' },
		{ 	text: '&raquo;&raquo;', 
			onclick: function() { me.pause(); advance(1); return false; },
			after: '' }
	]
	
	var sc = document.getElementById('show_controls');

	for(i=0;i<link_arr.length;i++) {
	
		var a = document.createElement('a');
		a.innerHTML = link_arr[i].text;
		a.href = '#';
		a.onclick = link_arr[i].onclick;
		sc.appendChild(a);
		var span = document.createElement('span');
		span.innerHTML = link_arr[i].after;
		sc.appendChild(span);
	}

	advance(0);
}
