// JavaScript Document

try {
    console.assert(1);
} catch(e) {
    if (typeof loadFirebugConsole == 'function') {
         loadFirebugConsole();
    }  else {
        console = {
            log: function() {},
            debug: function() {},
            info: function() {},
            warn: function() {},
            assert: function() {}
        }
    }
}
/* Fade home top image */
function initFader(){
	$('fadeshow').getElements('img').each(function(theEle){
		theEle.theEffect = new Fx.Style( theEle.id , 'opacity',{	duration: 3000	});
	});
	$('fadeshow').getLast().theEffect.set(0);	
	fadeTopImage.delay(5000);
}

function fadeTopImage(){
	var frontOne=$('fadeshow').getLast();
//		console.log('NOW INVISIBLE:'+frontOne.id);
	var doEffect=frontOne.getPrevious('img');
	frontOne.injectBefore($('fadeshow').getFirst());
	frontOne.theEffect.set(1);	
	doEffect.theEffect.start(1,0);
	fadeTopImage.delay(5000);										
}

window.addEvent('domready',initFader);


/// Mootools Form Check implementation   MOOCHECK
window.addEvent('domready', mooCheckInit);
function mooCheckInit(){
	$$('form.MooCheck').each(
		function(theForm){
			theForm.addEvent('submit',mooCheckSubmit);
			theForm.getElements('.MooCheck').each(
				function(theFormElement){
					theFormElement.addEvent('blur',mooFieldCheck);
				}
			);
		}
	);
}
function mooFieldCheck(){
	// Only return on errors..  after passing another error could occur..
	if(this.hasClass('MooNotEmpty') && this.getValue().trim()==''){
		mooRaiseError(this);
		return;
	} else if(this.hasClass('MooNotEmpty')){
		mooRemoveError(this);
	}
	if(this.hasClass('MooValidEmail') && /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,4}$/.test(this.getValue()) ){
		mooRemoveError(this);
	} else if(this.hasClass('MooValidEmail')){
		mooRaiseError(this);
		return;
	}
	if(this.hasClass('MooNotFirstOption') && this.selectedIndex==0){ // Selectbox may not have first option selected.
		mooRaiseError(this);
		return;
	} else if(this.hasClass('MooNotFirstOption')){
		mooRemoveError(this);
	}
}
function mooRaiseError(theElement){
	// Check if there is an _error field for this..
	if(theElement.hasClass('hasError')){
		return;
	}
	var theErrorInfo;
	if(theErrorInfo=$(theElement.getProperty('id')+'_error')){
		var ErrorElement = new Element('span',{'class':'error'});
		ErrorElement.appendText(theErrorInfo.getValue());
		ErrorElement.injectBefore(theErrorInfo);
		theElement.addClass('hasError');
	}
}
function mooRemoveError(theElement){
	var ErrorElement;
	if(theElement.hasClass('hasError')){
		ErrorElement=$(theElement.getProperty('id')+'_error').getPrevious();
		if(ErrorElement.getTag()=='span' && ErrorElement.hasClass('error')){
			ErrorElement.remove();
			theElement.removeClass('hasError');
		}
	}
}
function mooCheckSubmit(event){
	var event = new Event(event);
	this.getElements('.MooCheck').each(
		function(formElement){
			formElement.fireEvent('blur');
		}
	)
	if(this.getElements('.hasError').length>0){
		event.stop();
		alert('Please correct all errors');
		return;
	}
}

function mandje(answer){
	if(answer.status=='added'){
		var theBestelAlert = new Custom.BestelAlert('Bestelmandje', 'Het artikel is toegevoegd aan uw bestellijst.', 
			{alerthead: 'bestelhead', alertbox: 'bestelbox', alertbody: 'bestelbody', autoclosetime:4000 }
			);
		
		theBestelAlert.create();
	}
	else {
		var theBestelAlert = new Custom.BestelAlert('Bestelmandje ERROR', 'Artikel toegevoegen mislukt!', 
			{alerthead: 'bestelhead', alertbox: 'bestelbox', alertbody: 'bestelbody' }
			);
			
		theBestelAlert.create();
	}
}	


window.addEvent('domready',initInputFields);
function initInputFields(){
	// Nice handling for hinting formfields 
	$$('#login input.loginform').each(function(theElement){
			 theElement.addClass('nocontent');
			 theElement.addEvent('blur',function(){
				  this.removeClass('focus');
				  if(this.value==''){
					  this.addClass('nocontent');
					  this.removeClass('hascontent');
				  } else {
					  this.addClass('hascontent');
					  this.removeClass('nocontent');
				  }
			 });
			 theElement.addEvent('focus',function(){
				  this.addClass('focus');
			 });
		}
	);
}
