/* Global initialisation */
function initUitwegGlobal()
{
	// Start rotating catoons
	var cartoons = new Rotator([$('cartoonPrimary'), $('cartoonSecondary')], {delay: 5000});
	

	// Attach slimbox to cartoon popup links
	$$("#navigation a.cartoon").slimbox({}, null, false);
}

window.addEvent("domready", initUitwegGlobal);

// Simple element rotator script
var Rotator = new Class({
	Implements: Options,
	
	options:
	{
		delay: 3000,
		start: true
	},
	
	initialize: function(elements, options)
	{
		if (!elements.length) return false;
		this.setOptions(options);
		
		// Contains the original display values
		this.display = [];
		
		// Contains elements we should be rotating
		this.elements = elements;
		
		// On first run, this will be incremented to 0
		this.current = -1;
		
		// Start the rotation immediatly by default
		this.running = this.options.start;
		
		// Store current display information, defaults to 'inline'
		for(var i=0; i<this.elements.length; i++)
		{
			this.display[i] = this.elements[i].getStyle("display");
			this.display[i] = (this.display[i] == "none") ? "inline" : this.display[i];
			
			// Hide each element
			this.elements[i].setStyle("display", "none");
		}
		
		// Start the rotation
		this.moveNext();
	},
	
	start: function()
	{
		// See stop()
		this.running = true;
	},
	
	moveNext: function()
	{		
		if (this.running)
		{
			// Hide current
			if (this.elements[this.current])
			{
				this.elements[this.current].fade(0);
				this.elements[this.current].setStyle("display", "none");
			} 
		}
			this.displayNext();	
	},
	
	displayNext: function()
	{
		if (this.running)
		{
			// Calculate next position
			this.current++;
			if (this.current >= this.elements.length)
			{
				this.current = 0;
			}
			
			// Dipslay next
			this.elements[this.current].setStyle("display", this.display[this.current]);
			this.elements[this.current].fade('hide');
			this.elements[this.current].fade(1);
		}
		
		// And round we go.
		setTimeout(this.moveNext.bind(this), this.options.delay);
	},
	
	stop: function()
	{
		// For smooth operation, the clockticks aren't actually stopped
		this.running = false;	
	}
});