var fl; // array of all feature links/images
window.addEvent('domready', sShow);
function sShow() {
	if ($('slideshow')) {
		var sShowContainer = new Element('div', {
		    'class': 'slideshowContainer',
		    'id': 'sShowContainer'
			});
		var backBtn = new Element('a', {
			'styles': {'right': '38px', 'cursor':'pointer'},
			'events': {
				'click': function(){ sShowMove(-1); return false; }
				},
			'class': 'sShowBtn'
			});
		var fwdBtn = new Element('a', {
			'styles': {'right': '0px', 'cursor':'pointer'},
			'events': {
				'click': function(){ sShowMove(1); return false; }
				},
			'class': 'sShowBtn'
			});
		var pauseBtn = new Element('a', {
			'styles': {'right': '19px', 'cursor':'pointer'},
			'events': {
				'click': function(){ sShowMove(0); return false; }
				},
			'class': 'sShowBtn'
			});
		backBtn.injectInside(sShowContainer);
		fwdBtn.injectInside(sShowContainer);
		pauseBtn.injectInside(sShowContainer);
		backBtn.setHTML('<img src="/images/slideshow/prev_up.gif" width="20" height="19" alt="'+sShow_init.prev+'" title="'+sShow_init.prev+'" />');
		fwdBtn.setHTML('<img src="/images/slideshow/next_up.gif" width="20" height="19" alt="'+sShow_init.next+'" title="'+sShow_init.next+'" />');
		pauseBtn.setHTML('<img src="/images/slideshow/pause_up.gif" width="20" height="19" alt="'+sShow_init.pause+'" title="'+sShow_init.pause+'" />');
		sShowContainer.injectBefore('slideshow');
		fl = $ES('a','slideshow');
		if (fl) {
			for (var i=0; fl[i]; i++) {
				fl[i].injectInside(sShowContainer);
				fl[i].setStyle('position', 'absolute');
				fl[i].setStyle('left', '0px');
				fl[i].setStyle('top', '0px');
				fl[i].setStyle('z-index', '0');
				fl[i].fx = new Fx.Style(fl[i], 'opacity', {duration: 500, wait: false}).set(0);
			}
			fl[0].setStyle('z-index', '1');
			fl[0].fx.start(1);
			$('slideshow').remove();
		}
	}
}
var isStopped = 0;
var sShow_speed = (sShow.speed ? sShow.speed : 5);
var sShowTimer = setInterval("sShowMove(1)", sShow_speed*1000);
function sShowMove(motion) {
	if (!$('slideshow') || !fl) return;
	if (!motion && isStopped) {
		sShowTimer = setInterval("sShowMove(1)", sShow_speed*1000);
		isStopped = 0;
	} else if (!motion && !isStopped) {
		clearInterval(sShowTimer);
		isStopped = 1;
	}
	if (!isStopped) {
		clearInterval(sShowTimer);
		sShowTimer = setInterval("sShowMove(1)", sShow_speed*1000);
	}
	for (var i=0; fl[i]; i++) {
		if (fl[i].getStyle('z-index').toInt() == 1) {
			fl[i].fx.start(0);
			fl[i].setStyle('z-index', '0');
			if (fl[i+motion]) {
				fl[i+motion].setStyle('z-index', '1');
				fl[i+motion].fx.start(1);
				i=999999;
			} else if (motion > 0) {
				fl[0].setStyle('z-index', '1');
				fl[0].fx.start(1);
				i=999999;
			} else if (motion < 0) {
				fl[fl.length-1].setStyle('z-index', '1');
				fl[fl.length-1].fx.start(1);
				i=999999;
			}
		}
	}
}

