window.onload = function(){
	var oImg0 = $('header_actie-img0'); // wordt gebruikt als preloader
	var oDiv1 = $('header_actie-img1');
	var oDiv2 = $('header_actie-img2');

	if(null != oImg0
	&& null != oDiv1
	&& null != oDiv2)
		new nrheader_slide(oImg0, oDiv1, oDiv2, [
			 /*'images/header_bg.jpg'*/
			 'images/header_bg_oester.jpg'
			,'images/header_bg_kreeft.jpg'
			,'images/header_bg_mossel.jpg'
		]);
};

var nrheader_slide = function(oImg0, oDiv1, oDiv2, aImages){

	// initializeren van het object
	this.init = function(oImg0, oDiv1, oDiv2, aImages){

		// images intern opslaan
		this.oImg        = oImg0;
		this.aDiv        = [oDiv1,oDiv2];
		this.bDiv1Active = true;
		this.aImages     = aImages;
		/*this.sPrevImg    = '';*/
		this.sPrevImg    = this._rand(); // met random img starten

		// events toevoegen
		this._add_events();

		// start de eerste
		this._load_image();
	}

	// events toevoegen
	this._add_events = function(){
		this.oImg.onload = function(){
			this._slide();
		}.bind(this);
	};

	this._slide = function(){

		if(this['rTimer'])
			clearTimeout(this['rTimer']);

		var oDivActive  = (this.bDiv1Active) ?this.aDiv[1] :this.aDiv[0];
		var oDivLoading = (this.bDiv1Active) ?this.aDiv[0] :this.aDiv[1];
		oDivLoading.set({
			 'morph': {
				'duration':1500
			 }
		});

		oDivActive.setStyle('z-index', -2);
		oDivActive.setStyle('opacity', 1);

		oDivLoading.setStyle('opacity', 0);
		oDivLoading.setStyle('z-index', -1);

		oDivLoading.setStyle('backgroundImage', 'url('+this.oImg.src+')');
		oDivLoading.morph({'opacity':1});

		this.bDiv1Active = !this.bDiv1Active;

		this['rTimer'] = setTimeout(this._load_image.bind(this), 8000);
	};

	this._load_image = function(){
		this.oImg.src = this._next_image();
	}

	this._next_image = function(){
		this.sPrevImg++;

		if(this.sPrevImg >= this.aImages.length)
			this.sPrevImg = 0;

		return this.aImages[this.sPrevImg];
	}

	this._rand_image = function(){
		sRandImg = this.aImages[this._rand()];

		if(this.sPrevImg == sRandImg)
			return this._rand_image();

		this.sPrevImg = sRandImg;
		return sRandImg;
	}

	this._rand = function(){
		return Math.floor(Math.random()*(this.aImages.length));
	}

	// initializeren
	this.init(oImg0, oDiv1, oDiv2, aImages);
};

