// JavaScript Document
var Fader = new Class({
	
	Implements: [Options, Events],	  
	options: {
		container: 'letthemfade',
		interval : 7000,
		duration : 2000
		
	},

	initialize: function(e,options){
		
		this.setOptions(options);
		
		this.container = ($(e) != null) ? $(e) : $(this.options.container) 

		
		var obj = this
		
		if(this.container != null){
			this.container.setStyles({
				position: 'relative'									 
			})
			this.images = $(this.container).getElements('div');
		
			this.images.each(function(e, index){
				e.set('tween', {duration: obj.options.duration});	
				
				e.setStyles({
					position: 'absolute',
					top: 0,
					left: 0					
				})
				if(index > 0) e.setStyles({'opacity' : 0})
			})
			
			this.current = 0;
			
			obj.FadeToNext.periodical(obj.options.interval, obj)
		
		}
	},
	
	FadeToNext: function(){
		var from = this.current;
		var to = (this.current + 1 >= this.images.length) ? 0 : this.current + 1

		this.images[to].tween('opacity', [0,1])
		this.images[from].tween('opacity', [1,0])
		
		this.current = to;
	}	  				  
});
var Formcheck = new Class({
	Implements: [Options, Events],
	options: {
		container: 'container',
		addClass: true,
		className: 'fc-error',
		showMessage: true,
		errorMessage: 'Dit veld is verplicht',
		offsetX: -70,
		offsetY: 0,
		onblur: true,
		onsubmit: true,
		regexp : {
			required : /[^.*]/,
			alpha : /^[a-z ._-]+$/i,
			alphanum : /^[a-z0-9 ._-]+$/i,
			digit : /^[-+]?[0-9]+$/,
			nodigit : /^[^0-9]+$/,
			number : /^[-+]?\d*\.?\d+$/,
			email : /^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i,
			phone : /^[\d\s ().-]+$/,
			url : /^(http|https|ftp)\:\/\/[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z0-9\-\._\?\,\/\\\+&amp;%\$#\=~])*$/i
		}
	},
	
	initialize: function(form,options){
		this.setOptions(options);
		this.form = form;
		this.fields = this.getFields();
		this.isValid = true;
		this.container = $(this.options.container);
		
		if(this.options.showMessage){
			this.errorMessage = new Element('div').addClass(this.options.className).setStyles({'visibility':'hidden','opacity':0}).addEvent('click', function(){
				this.removeMessage();
			}.bind(this));
			
			this.errorMessage.set('tween', {duration: 'short'} )
		}
		
		this.setEvents();
	},
	
	setEvents: function(){

		this.form.addEvent('submit', function(e){
			e.stop();
			this.doCheck();
			if(this.isValid){
				this.form.submit();	
			}
		}.bindWithEvent(this));
		
		this.fields.each(function(e){
			e.addEvents({
				'blur': function(){
					this.handleError(e);				  
				}.bindWithEvent(this)			
			});
		},this);
	},
	
	getFields: function(){
		var fields = [];
		this.form.getElements('input').each(function(e){
			if(e.hasClass('required')){
				fields.push(e);
			}
		},this);
		
		return fields;
	},
	
	doCheck: function(e){
		this.isValid = true;
		this.isChecked = false;
		
		this.fields.each(function(e){
			if(this.isValid == true){
				this.handleError(e);
			}
		},this);
	},
	
	handleError: function(e){
		
		if((e.get('value').replace(' ','') == '' || e.get('value') === e.name)){
			
			this.isValid = false;
			
			if(this.options.showMessage){
				var pos = e.getPosition();
				this.errorMessage.setStyles({
					'top': pos.y + e.getHeight() + this.options.offsetY,
					'left': pos.x + e.getWidth() + this.options.offsetX			
				}).set('text',e.get('title')).injectInside(document.body);
				
				this.showMessage();
			}
		}
		else{
			this.removeMessage();
		}
	},
	showMessage: function(){
		this.errorMessage.fade('in');
	},
	removeMessage: function(){
		this.errorMessage.fade('out');
	}				  
						  
});

var KelvinTip = new Class({
	Implements: [Options, Events],
	
	initialize: function(elements,options){
		this.elements = $$(elements);
		
		this.tooltip = new Element('div').addClass('tt_container').setStyles({'position': 'absolute'}).injectInside($('kaart'));
		this.closebutton = new Element('div').addClass('tt_close').setStyles({'visibility':'hidden','opacity':0}).injectInside(this.tooltip);
		
		this.closebutton.addEvent('click', function(){	
			this.removeTip();
		}.bind(this));
		
		this.elements.each(function(element){
			var eAnchor = element.getElement('a');
			eAnchor.addEvent('click', function(e){
				e.stop();
				this.showTip(eAnchor);				 
			}.bind(this));
		},this);
	},
	
	showTip: function(e){
		new Request.HTML({url: e.get('href'), update: this.tooltip}).get();
	},
	removeTip: function(){
		this.tooltip.innerHTML = '';	
	}
						  
});
