/**
 * This overrules the default function 'process' from MooFlow class
 * so links(href) can be used
 */
if (typeof(MooFlow) != 'undefined') {
	var MooFlowTS = new Class({
	    Extends: MooFlow,
	    options:{},
		clickTo: function(index){
			if(this.index == index) {
				cimg=this.master.images[index];
				if($defined(cimg.href)) {
					if(cimg.target == '_blank'){
						window.open(cimg.href);
					} else {
						document.location.href=cimg.href;				
					}
				}
			} else {
				if(this.sli) this.sli.set(index);
				this.glideTo(index);
			}
		}
	});
}

function fadeMe(p_sId){
	var showDuration = 5000;
	var container = $(p_sId);
	var images = container.getElements('img');
	var currentIndex = 0;
	var interval;
	
	images.each(function(img,i){ 
		if(i > 0) {
			img.set('opacity',0);
	    }
	});
	
	var show = function() {
		images[currentIndex].fade('out');
		images[currentIndex = currentIndex < images.length - 1 ? currentIndex+1 : 0].fade('in');
	};
	
	window.addEvent('load',function(){
		interval = show.periodical(showDuration);
	});
}

var mf;
//Mootools carousel banner
window.addEvent('domready', function(){
	
	if($('MooFlow')){ 
	     mf = new MooFlowTS($('MooFlow'), {
	     startIndex: 		2,
	     useSlider: 		true,
	     useAutoPlay: 		true,
	     useCaption: 		false,
	     useResize: 		false,
	     useWindowResize: 	false,
	     useMouseWheel: 	true,
	     useKeyInput: 		true,
	     useViewer:			true
	    });
	}
				
	if($('MooFade')){
		fadeMe('MooFade');
	}
	if($('widgetBannerFader')){
		fadeMe('widgetBannerFader');
	}

	// Input text field toggle
	// Set default text in attr value and add class to input
	// field with
	// classname: toggle	
	$$('input:text.toggle').each(function(elem) {
		var oInput = $(elem.id);
		oInput.addEvent('click', function(){
			if (oInput.get('rel') == null || oInput.get('rel') == oInput.value) {
				oInput.set('rel', oInput.value);
				oInput.value = '';
			}
		});
		oInput.addEvent('blur', function(){
			if (oInput.value == '') {
				oInput.value = oInput.get('rel');
			}
		});
	});
	
    if ($('toolbar')) {

    	if(Cookie.read('mvAccount') == null || Cookie.read('mvAccount') == 'open'){
    		$('toolbar').setStyle('height','65px');
    	};
    	
        $('toolbarToggle').addEvent('click', function(){
            var myAccountKnobTween = new Fx.Tween('toolbar');
            if (this.hasClass('open')) {
                myAccountKnobTween.addEvent('complete', function(){
                    var myCookie = Cookie.write('mvAccount', 'close', {path: '/', duration: 30});
                    $('toolbarToggle').removeClass('open').addClass('close');
                });
                myAccountKnobTween.start('height', 65, 0);
            }
            else {
                myAccountKnobTween.addEvent('complete', function(){
                    var myCookie = Cookie.write('mvAccount', 'open', {path: '/', duration: 30});
                    $('toolbarToggle').removeClass('close').addClass('open');
                });
                myAccountKnobTween.start('height', 0, 65);
            }
        });
    }
});

var submitDone = false;
function submitForm(button) {
 if(document.getElementById('name').value=='' ||
    document.getElementById('company').value=='' ||
	  document.getElementById('email').value=='' ||
	  document.getElementById('email').value.search(/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i) == -1) {
	  alert('Alle velden dienen ingevuld te worden, inclusief een geldig emailadres.');
	  return false;
  } else {
    if (!submitDone) {
       submitDone = true;
       button.value = 'Een moment';
       button.disabled = true;
       document.newsletter.submit();
    } else {
       alert ('Nog een moment, uw formulier wordt verzonden...');
    }
	  return true;
  }
}
