/**
 * @author jurriaan
 */

//klote probleem, this niet te gebruiken in combinatie met setInterval en $(function(){})
//oplossing-> we houden bij welke objecten er zijn en geven ze een uniek id
var RoulatieStore = Array();
function RoulatieRegister(object)
{
	id = RoulatieStore.length;
	RoulatieStore[id] = object;
	return id;
}

$(function () {
	for(i = 0; i < RoulatieStore.length; i++)
	{
		RoulatieStore[i].Initialize();
	}
});

//we gaan object georienteerd javascript doen
function Roulatie(name, speed, delay)
{
	//alert('constructor');
	this.name = name;
	this.changeInterval = speed*1000; //speed of animation 1 = 1s
	this.delay = delay*1000;
		
	this.currentActive = 0; //currently active item
	this.lastActive = 0; //last active item
	this.lastInterval = 0; //interval for cycle of images
	this.images = new Array();  // store for registered images
	this.urls = new Array();  // store for registered urls
	this.titles = new Array();  // store for registered titels
	
	//images to fade
	this.displayimages;
	this.activeImage = 0;
	
	this.id = RoulatieRegister(this);
}

Roulatie.prototype.Initialize = function()
{
	//console.log('init ' + this.name)
	var myContainer = $('#' + this.name);
	//init the images
	displayimages = myContainer.find("img");
	swapimage = new Image();
	displayimages.after(swapimage);
	$(swapimage).hide();
	this.displayimages = myContainer.find("img");
	if(this.images.length > 1)
	{
		//console.log('RoulatieStore[' + this.id + '].Switch() every ' + this.changeInterval + 'ms');
		this.lastInterval = setTimeout('RoulatieStore[' + this.id + '].Switch()', this.changeInterval + this.delay);
	}
}

Roulatie.prototype.RegisterItem = function(src, url, title)
{
	id = this.images.length;
	this.images[id] = new Image();
	this.images[id].src = src;
	this.urls[id] = url;
	if(title != undefined) this.titles[id] = title;
}
	
Roulatie.prototype.XFade = function()
{
	//alert("xfade");
	activeImage = this.activeImage;
	this.displayimages.eq(activeImage).fadeOut(this.changeInterval/3);
	this.displayimages.eq(1-activeImage).fadeIn(this.changeInterval/3);
	this.activeImage = 1-activeImage;
}
	
Roulatie.prototype.Switch = function()
{
	//firefox bug!!! have to repeat timeouts instead of using setInterval
	this.lastInterval = setTimeout('RoulatieStore[' + this.id + '].Switch()', this.changeInterval);
	
	//console.log('switch ' + this.name)
	this.currentActive = this.currentActive+1;
	if(this.currentActive >= this.images.length) this.currentActive = 0;
	this.displayimages.eq(1-this.activeImage).attr("src",this.images[this.currentActive].src);
	//change the link content
	$('#' + this.name).find('a').attr("href",this.urls[this.currentActive]);
	
	if(this.titles[this.currentActive] != undefined)
	{
		setTimeout('RoulatieStore[' + this.id + '].SwitchText('+this.currentActive+')', this.changeInterval/6);
	}
	this.XFade();
	this.lastActive = this.currentActive;
}

Roulatie.prototype.SwitchText = function(active)
{
	$('#' + this.name+'Text').find('a').attr("href",this.urls[active]);
	$('#' + this.name+'Text').find('a').html(this.titles[active]);
}