window.addEvent('domready', function() {
	ticker.init();
});

var ticker = {};
ticker.init = function() {
	this.images = [
		'plastic',
		'single_use',
		'pacifiers',
		'baby_bottles',
		'disposables',
		'straws',
		'bottle_caps',
		'toothbrushes',
		'plastic_razors',
		'plastic_cutlery',
		'plastic_lighters',
		'produce_bags',
		'plastic_bags',
		'bottles',
		'plastic_packaging'
	];
	
	this.index = 0;
	this.img = $('ticker');
	this.fx = new Fx.Tween(this.img, {duration: 1000});

	var base = this.img.src.replace(/(^.*\/).*?$/img, '$1');
	var newimg = new Image();
	newimg.src = base + this.images[this.index + 1] + '.gif';
	
	setTimeout('ticker.run()', 3000);
}

ticker.run = function() {
	
	var base = this.img.src.replace(/(^.*\/).*?$/img, '$1');
	
	var next = this.index + 1;
	if(next >= this.images.length) {
		next = 0;
	}
	
	var newimg = new Image();
	newimg.src = base + this.images[next] + '.gif';
	
	this.fx.cancel();
	this.fx.start('opacity', 1, 0).chain(function() {
		this.img.src = base + this.images[next] + '.gif';
		this.fx.start('opacity', 0, 1).chain(function() {
			this.index = next;
			this.fx.cancel();
			setTimeout('ticker.run()', 3000);
		}.bind(this));
	}.bind(this));
}