// Implements
window.addEvent('domready', function() {
	
	Element.implement({
		
		show: function() {
			this.setStyle('display','block');
		},
		
		hide: function() {
			this.setStyle('display','none');
		}
		
	});
});

/*** Resize Window ***/	
window.addEvent('resize', function() {
	
});

// Startup
window.addEvent("domready",function() {
	
	var options = {
		elements: []
	}
	
	options.elements.push({'selector': 'h1','options': {fontFamily: "Frutiger"}});
	options.elements.push({'selector': '.menukaart .titel h2','options': {fontFamily: "Frutiger"}});
	options.elements.push({'selector': '#mainmenu li a','options': {fontFamily: "Frutiger"}});
	options.elements.push({'selector': '#quote h2','options': {fontFamily: "MinionPro"}});
	options.elements.push({'selector': 'h3','options': {fontFamily: "Eraserdust"}});
	options.elements.push({'selector': 'H4','options': {fontFamily: "Eraserdust"}});
	
	var cufonactions = new cufonActions(options);
	
	var image_slider = new slider({
		fxOptions: {
			duration: 1000,
			transition: "expo:in:out",
			wait: false
		},
		box: $('imagelist'),
		autoPlay: true,
		interval: 5000,
		items: $$('#imagelist .imageitem'),
		size: $('imagecontainer').getSize().x,
		addButtons: {},
		onEndFx: function(currentItem,currentHandle, index, start, end){
			
			if(start) {
				
			}else {
				
			}
			
			if(end) {
				
			}else {
				
			}
		},
		onWalk: function(currentItem,currentHandle, index, start, end){
			
			if(start) {
				
			}else {
				
			}
			
			if(end) {
				
			}else {
				
			}
			
		}
	});
	
	var options = {
		board: $('daghap_container'),
		relativeTo: $('visualsection'),
		openButtons:$$('#daghap_container .open'),
		closeButtons:$$('#daghap_container .close'),
		slider: image_slider
	};
	
	if($('daghap_container')) {
		var chalkboard = new chalkBoard(options);
	}
	
	
	
});

var chalkBoard = new Class({
	Implements: [Events, Options],
	
	options: {
		board: $('daghap_container'),
		relativeTo: $('visualsection'),
		openButtons:$$('#daghap_container .open'),
		closeButtons:$$('#daghap_container .close'),
		slider: false
	},
	
	initialize: function(options) {
		//console.log('chalkBoard');
		this.setOptions(options);
		
		this.options.openButtons.addEvent('mouseover', function(e) {
			this.setStyle('cursor','pointer');
		});
		
		this.options.openButtons.addEvent('mouseout', function(e) {
			this.setStyle('cursor','normal');
		});
		
		this.options.openButtons.addEvent('click', function(e) {
			
			e.preventDefault();
			this.showBoard();
			
		}.bind(this));
		
		this.options.closeButtons.addEvent('mouseover', function(e) {
			this.setStyle('cursor','pointer');
		});
		
		this.options.closeButtons.addEvent('mouseout', function(e) {
			this.setStyle('cursor','normal');
		});
		
		this.options.closeButtons.addEvent('click', function(e) {
			
			e.preventDefault();
			this.hideBoard();
			
		}.bind(this));
		
		this.mover = new Fx.Move(this.options.board, {
            relativeTo: this.options.relativeTo,
            position: 'upperLeft',
            edge: 'upperLeft',
            duration: 850,
			transition: "expo:in:out",
			allowNegative: true
        });				
	},
	
	showBoard: function() {
		//console.log('chalkBoard:: showBoard::');
		
		this.mover.start({offset: {x: 0, y: 0}});
		
		if(this.options.slider) {
			//console.log('chalkBoard:: showBoard:: stopping slider');
			this.options.slider.stop();
		}
	},
	
	hideBoard: function() {
		//console.log('chalkBoard:: hideBoard::');
		
		this.mover.start({offset: {x: 0, y: -250}});
		
		if(this.options.slider) {
			//console.log('chalkBoard:: hideBoard:: starting slider');
			
			this.options.slider.startAutoplay(true, 3000);
		}
	}
});

var cufonActions = new Class({
	Implements: [Events, Options],
	
	options: {
		elements: []
	},
	
	initialize: function(options) {
		//console.log('cufonActions');
		this.setOptions(options);
		
		this.options.elements.each(function(el, index){
			////console.log('cufonActions:: initialize:: '+el.selector);
			Cufon.replace(el.selector, el.options);
			
		});
	},
	
	update: function() {
		this.options.elements.each(function(el, index){
			////console.log('cufonActions:: initialize:: '+el.selector);
			Cufon.refresh(el.selector);
		});
	}
});

var dynamicForm = new Class({
	Implements: [Events, Options],
	
	options: {
		holder: this,
		targetform: $('dynamicform'),
		submitButtons: $$(".submitbutton"),
		errormessage: $("errormessage"),
		check: false
	},
	
	initialize: function(options) {
		//console.log('Initializing dynamicForm');
		this.setOptions(options);
		
		if( !this.options.targetform ){
			//console.log('dynamicForm:: no form:: '+this.options.targetform);
			return;
		}
		
		this.options.errormessage.hide();

		this.options.submitButtons.setStyle("cursor","pointer");
		
		this.options.submitButtons.addEvent('click', function() {
			this.onReleaseSubmit();
		}.bind(this));
		
	},
	
	onReleaseSubmit: function(){
		//console.log("submit released" );
		if( this.checkForm() ){
			
			//console.log("Form okay send it" );
			this.options.errormessage.hide();
			this.options.targetform.getElements("input [type=hidden]").destroy();
			this.options.targetform.submit();
			
		} else {
			
			//console.log("Form error");
			
			this.options.errormessage.show();
		}
	},
	oddEven: function(){
		
		$(this.id + " div.row").each( function(i,e){
							if(i%2){
								$(this).addClass("even");
							} else {
								$(this).addClass("odd");
							}
						} );
			$(this.id + " div.row:last").removeClass('odd');
	},
	checkForm: function(){
		this.options.check = true;
		
		this.options.targetform.getElements("div.row").each( function(e){
			
			if( !this.checkRow( e ) ) {
				this.options.check = false;
			}
			
		}.bind(this));
		
		return this.options.check;
	},
	checkRow: function( e ){
		//console.log('Form.checkRow' );
		var check = true;
		var label = e.getElement("label").get("html");
		
		if( label == null ) return true;
		
		// basic check
		if( label.indexOf("*") > -1 ){
			////console.log('Label:: '+label );
			//console.log("checking row : "+e.getElement("label").value );
			// verplicht
			var children = e.getElement("div.value").getChildren();
			var type = children[0].getProperty("class");
			
			//console.log("type : "+type );
			switch( type ){
				case "frm_text hasDatepicker":
				case "frm_text":
				case "formtext":
					
					//console.log("frm_text " + label + " = " +  e.getElement("input").value );
					
					if( !e.getElement("input").value ){
						this.setError( e, true );
						check = false;
					} else {
						this.setError( e, false );
					}
					
					break;
				case "frm_radio":
				case "formradio":
				case "radiorow":
					//console.log("radiorow " + e.getElements("input[type='radio']:checked").value);
					if( !e.getElements("input[type='radio']:checked").value ){
						this.setError( e, true );
						check = false;						
					} else {
						this.setError( e, false );
					}
					break;
				case "frm_checkbox":
				case "formcheckbox":
				case "checkboxrow":
					//console.log("checkboxrow " + e.getElements("input[type='checkbox']:checked").length);
					if( !e.getElements("input[type='checkbox']:checked").length ){
						this.setError( e, true );
						check = false;						
					} else {
						this.setError( e, false );
					}
					break;
				case "frm_select":
				case "formselect":
				case "frmradio":
					//console.log("frm_select " + e.getElements("select option[selected='selected']").length);
					
					if( !e.getElements("select option[selected='selected']").length ){
						this.setError( e, true );
						check = false;						
					} else {
						this.setError( e, false );
					}
				default:
					break;
			}
		}
		if( !this.customRowCheck( e ) ) check = false;
		
		return check;
	},
	sentenceCase: function( val ){
		return val.substring(0,1).toUpperCase() + val.substr(1,val.length-1);
	},
	customRowCheck: function(e){
		var check = true;
		var label = e.getElement('label').get("html");
		
		// custom checks
		if( label.toLowerCase().indexOf("mail") > -1 ){
			if( !this.validateEmail( e.getElement('input').value ) ){
				this.setError( e, true );
				check = false;
			} else {
				this.setError( e, false );
			}
		}
	
		if( label.toLowerCase().indexOf("postcode") > -1 ){
			if( !this.validateZip(  e.getElement('input').value ) ){
				this.setError( e, true );
				check = false;
			} else {
				this.setError( e, false );
			}
		}
		
		if( label.toLowerCase().indexOf("telefoon") > -1 && label.indexOf("*") > -1 ){
			if( !this.validatePhone(  e.getElement('input').value ) ){
				this.setError( e, true );
				check = false;
			} else {
				this.setError( e, false );
			}
		}
		
		return check;
	},
	validateEmail: function( email ){  
		//console.log("validateEmail " + email );
		var RE_EMAIL = /^[a-zA-Z.0-9]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+.[a-z]{2,4}$/;
		return RE_EMAIL.test( email );
	},
    validateZip: function(zip) {
		//console.log("validateZip " + zip );
        var RE_ZIP = /^[0-9]{4}\s*[a-zA-Z]{2}$/;
        return RE_ZIP.test( zip );
    },	
	validatePhone: function(phone){
		//console.log("validatePhone " + phone );
		return( phone.replace(/\D/g,"").match(/[0-9]{10}/));
	},
	setError: function( e, error ){
		//console.log("error check:: "+ error);
		if( error ){
			e.getElement("label").addClass("error");
		} else {
			e.getElement("label").removeClass("error");
		}
	}
});
