
var JSONP = {
	parse : function (text) {
		return $try(function () {
					return eval('('+text+')');
				});
	}
};


var Fader = {
	data		: null,	// {"img":[{"url"},{"url"}]}
	now 		: 0,
	nummer		: 1,
	
	next 		: function () {
	
		$('image_'+this.now).fade('out');
		$('image_'+(this.now^1)).fade('in');
		
		if((this.nummer+1) == this.data.img.length) {
			this.nummer = 0;
		} else {
			this.nummer++;
		}
		var t = this.now;
		this.setImage.delay(500,this,[t,this.data.img[this.nummer].url]);	
		this.now = this.now ^ 1;
	},
	
	setImage	: function (num,src) {
		$('image_'+num).setProperty('src',src);
	},
	
	init 		: function (images) {
		this.data = JSONP.parse(images);
		
		$$('#fader .fade').each(function (el) {
			if(el.get('id') != 'image_0') {
				el.setStyle('opacity',0);
			}
		});
		
		this.timerStart();
	},
	
	timerStart 	: function () {
		this.next.periodical(5000,this);
	}
};

var Mover = {
	move : null,
	images : [],
	current : 0,
	init : function () {
		this.move = new Fx.Scroll('moverz',{});
		$$('#moverz img.foto').each(function(el,index) {
			if($defined(el)) {
				Mover.images[index] = el;
			}
		});
	},
	next : function () {
		if((this.current+1) > (this.images.length-1)) {
			this.current = 0;
		} else {
			this.current++;
		}
		
		this.move.toElement(this.images[this.current]);
	},
	prev : function () {
		
		if((this.current-1) < 0) {
			this.current = (this.images.length-1);
		} else {
			this.current--;
		}
		
		this.move.toElement(this.images[this.current]);
	}
};
	

window.addEvent('domready', function () {
	Fader.init(images);
	
	if($chk($('moverz'))) {
		Mover.init();
		$('next').addEvent('click', function () { Mover.next(); });
		$('prev').addEvent('click', function () { Mover.prev(); });
	}
	
	if($chk($('zoeken'))) {
		$('zoeken').addEvent('focus', function (e) {
			if(this.get('value') == 'Zoeken') {
				this.set('value','');
			}
		});
	}
	
	$$('.zoekForm img').each(function (el) {
		el.addEvent('click', function (e) {
			el.getParent().submit();
		});
	});
	
});

