var Rotator = new Class(
{
	imgCounter : 0,

	options:
	{
		'baseElement' : null,
		'titleList' : null,
		'contentshortList' : null,
		'readmoreList' : null,
		'imageList' : null,
		'duration' : null
	},

	initialize:	function(options)
	{
		this.setOptions(options);
		this.build();
	},

	build: function()
	{
		this.options.baseElement.empty();
		this.title = new Element('p', {'class' : 'title'});
		this.contents = new Element('div', {'class' : 'contents'});
		this.readmore = new Element('a', {'class' : 'readmore'});
		this.image = new Element('img', {'class' : 'image'});

		this.options.baseElement.appendChild(this.title);
		this.options.baseElement.appendChild(this.image);
		this.options.baseElement.appendChild(this.contents);
		this.options.baseElement.appendChild(this.readmore);
		this.options.baseElement.setStyle('visibility', 'visible');
		this.loadNextItem();
	},

	loadNextItem: function()
	{
		if (this.options.titleList.length > 1)
		{
			this.fx = this.options.baseElement.effect('opacity',
			{
				duration: 500,
				onComplete : function()
				{
					if(this.imgCounter == (this.options.titleList.length))
					{
						this.imgCounter = 0;
					}

					this.title.setHTML(this.options.titleList[this.imgCounter]);
					this.contents.setHTML(this.options.contentshortList[this.imgCounter]);
					
					if (this.options.readmoreList[this.imgCounter] != "")
					{
						this.options.baseElement.setProperty('onClick', 'window.location="'+this.options.readmoreList[this.imgCounter]+'"');
						this.readmore.setHTML('Lees meer');
						this.readmore.setProperty('href', this.options.readmoreList[this.imgCounter]);
						this.readmore.setStyle('display', 'block');
					}else
					{
						this.readmore.setStyle('display', 'none');
					}
					
					if (this.options.imageList[this.imgCounter] != "")
					{
						this.image.setProperty('src', this.options.imageList[this.imgCounter]);
						this.image.setStyle('display', 'block');
					}else
					{
						this.image.setStyle('display', 'none');
					}

					this.imgCounter++;

					this.fx2 = this.options.baseElement.effect('opacity',
					{
						duration: 500
					}).start(0,1)
				}.bind(this)
			}).start(1, 0);

			this.loadNextItem.delay(this.options.duration,this);
		}
		else
		{
			// onzichtbaar maken om effect zichtbaar te maken:

			this.options.baseElement.setStyle('opacity', '0');
			this.title.setText(this.options.titleList[0]);
			this.image.setProperty('src', this.options.imageList[0]);
			this.contents.setText(this.options.contentshortList[0]);
			this.readmore.setText('Lees meer');
			this.readmore.setProperty('href', this.options.readmoreList[0]);

			this.fx2 = this.options.baseElement.effect('opacity',
			{
				duration: 350
			}).start(0,1)
		}
	}

});
Rotator.implement(new Options, new Events);
