var Rotator = new Class({
	initialize: function(element, template, items) {
		this.element = $(element);
		this.template = template;
		this.items = items;
		this.freq = 0;
		this.nextItem = 0;
		if (arguments.length > 3) this.freq = arguments[3];
		if (arguments.length > 4) {
			var preload = '';
			var prefix = '';
			if (arguments[4]['preload']) {
				preload = arguments[4]['preload'];
				if (arguments[4]['preloadFrom']) prefix = arguments[4]['preloadFrom'];
				for (var i = 0; i < this.items.length; i++) {
					new Asset.image(prefix+(items[i][preload]));
				}
			}
		}
		this.randomise();
		this.inject();
		if (this.freq > 0) {
			this.inject.bind(this).periodical(this.freq);
		}
	},
	inject: function() {
		var html = this.template;
		var item = this.items[this.nextItem];
		for (key in item) {
			var regExp = new RegExp(key, "g");
			html = this.doReplace(html, regExp, item[key]);
		}
		this.element.innerHTML = html;
		this.calculateNextItem();
	},
	doReplace: function(target, placeholder, value) {
		return target.replace(placeholder, value);
	},
	randomise: function() {
		this.items.sort(function(){
			return Math.round(Math.random()) - 0.5;
		});
	},
	calculateNextItem: function() {
		this.nextItem++;
		if (this.nextItem >= this.items.length) this.nextItem = 0;
	}
});

window.addEvent('domready', function() {
	var imageDiv = $('image');
	if (imageDiv) {
	
		var expImages = new Array(
			{
				'xxxxIMAGExxxx': 'top-1.gif',
				'xxxxTITLExxxx': i18n.t('Welcome to Freight Link Solutions'),
				'xxxxWIDTHxxxx': 695,
				'xxxxHEIGHTxxxx': 193
			},
			{
				'xxxxIMAGExxxx': 'top-2.gif',
				'xxxxTITLExxxx': i18n.t('Welcome to Freight Link Solutions'),
				'xxxxWIDTHxxxx': 695,
				'xxxxHEIGHTxxxx': 193
			},
			{
				'xxxxIMAGExxxx': 'top-3.gif',
				'xxxxTITLExxxx': i18n.t('Welcome to Freight Link Solutions'),
				'xxxxWIDTHxxxx': 695,
				'xxxxHEIGHTxxxx': 193
			},
			{
				'xxxxIMAGExxxx': 'top-4.gif',
				'xxxxTITLExxxx': i18n.t('Welcome to Freight Link Solutions'),
				'xxxxWIDTHxxxx': 695,
				'xxxxHEIGHTxxxx': 193
			},
			{
				'xxxxIMAGExxxx': 'top-5.gif',
				'xxxxTITLExxxx': i18n.t('Welcome to Freight Link Solutions'),
				'xxxxWIDTHxxxx': 695,
				'xxxxHEIGHTxxxx': 193
			},
			{
				'xxxxIMAGExxxx': 'top-5.gif',
				'xxxxTITLExxxx': i18n.t('Welcome to Freight Link Solutions'),
				'xxxxWIDTHxxxx': 695,
				'xxxxHEIGHTxxxx': 193
			}
		);
		
		var html = '<img src="/assets/images/xxxxIMAGExxxx" width="xxxxWIDTHxxxx" height="xxxxHEIGHTxxxx" alt="xxxxTITLExxxx" title="xxxxTITLExxxx" border="0">';
		new Rotator(imageDiv, html, expImages);
		
	}
});