
// FOOTER SETTINGS
window.FOOTER_ALLOW_AUTO       = true;
window.FOOTER_ALLOW_AUTO_OLD   = window.FOOTER_ALLOW_AUTO;
window.FOOTER_MORPH_DURATION   = 650;
window.FOOTER_MORPH_TRANSITION = 'linear';
window.FOOTER_AUTO_HIDE_AFTER  = 5000;
window.FOOTER_AUTO_SHOW_AFTER  = 2000;

// TAALSW SETTINGS
window.TAALSW_TIMER             = false; // niet aanpassen!
window.TAALSW_MORPH_DURATION    = 350;
window.TAALSW_MORPH_TRANSITION  = 'linear';
window.TAALSW_AUTO_HIDE_AFTER   = 500;
window.TAALSW_START_OPEN        = true; // wordt overruled door PHP!
window.TAALSW_START_HIDE_AFTER  = 2000;

// HEADER SLIDE
window.HS_SLIDE_AFTER      = 8500;
window.HS_MORPH_DURATION   = 1750;
window.HS_MORPH_TRANSITION = 'linear';

// AUTO SETTINGS, NIET AANPASSEN
window.bHome = 1;
window.iN1   = 1;

// INIT
if(isFront())
	window.addEvent('domready',function(){
		initOCReplace();
		initFrmSys();
		initFrmSysLogin();
		initCufon();
		initFooter();
		initABlank();
		initTaalSW();
		initDriehoek();
		initFocus();
		initHeaderSlide();
	});

// FUNCTIONS
function isFront(){
	return (document.location.href.toString().indexOf('/wba') === -1);
}

function initHeaderSlide(){
	
	// settings
	window.HS_IMG1        = $('hafb-1');
	window.HS_IMG2        = new Element('img',{id:'hafb-2',alt:'',src:'',styles:{opacity:'0','z-index':'2'}}).inject($('header-afbeelding'));
	window.HS_TIMER       = false;
	window.HS_IMAGES      = [$('hafb-1').src.toString()];
	window.HS_ACTIVE_IMG  = 1;
	
	$$(window.HS_IMG1,window.HS_IMG2).each(function(o){
		o.set({
			events: {
				load: function(){
					if(window.HS_IMAGES.length>1){
						var i1 = (window.HS_ACTIVE_IMG === 1)?window.HS_IMG1:window.HS_IMG2;
						i1.morph({opacity:'1'});
						headerSlide();
					}
				}
			},
			morph: {
				duration: window.HS_MORPH_DURATION,
				transition: window.HS_MORPH_TRANSITION
			}
		});
	});
	
	// init
	ajcHeaderSlide();
}

function ajcHeaderSlide(){
	new Request.JSON({
		 url: 'headers.ajax.php'
		,onComplete: function(res){
			if(undefined !== res && res){
				window.HS_IMAGES = res;
				
				// init
				if(window.HS_IMAGES.length>1)
					headerSlide();
			}
		}
	}).send({
		data: {
			 'bHome':window.bHome
			,'iN1':  window.iN1
		}
	});
}

function toggleFile(){
	$$('.file').each(function(o){
		if(o.hasClass('hidden'))
			o.removeClass('hidden');
		else
			o.addClass('hidden');
	});
}

function headerSlide(){
	if(window.HS_TIMER)
		clearTimeout(window.HS_TIMER);
		
	window.HS_TIMER = setTimeout(function(){
		var i1 = (window.HS_ACTIVE_IMG === 1)?window.HS_IMG1:window.HS_IMG2;
		var i2 = (window.HS_ACTIVE_IMG === 1)?window.HS_IMG2:window.HS_IMG1;
		window.HS_ACTIVE_IMG = (window.HS_ACTIVE_IMG === 1)?2:1;
		
		i2.src = headerSlideRandImg(i1.src.toString());
		i2.set({styles:{opacity:'0','z-index':'3'}});
		i1.set({styles:{opacity:'1','z-index':'2'}});
	},window.HS_SLIDE_AFTER);
}

function headerSlideRandImg(sOld){
	var i = Math.floor(Math.random() * window.HS_IMAGES.length) + 0;
	
	var bDonker = (window.HS_ACTIVE_IMG === 1);
	
	if(window.HS_IMAGES[i] == sOld)
		return headerSlideRandImg(sOld);

	else if((bDonker === false && window.HS_IMAGES[i].toString().indexOf('donker') === -1) || (bDonker === true && window.HS_IMAGES[i].toString().indexOf('licht') === -1))
		return headerSlideRandImg(sOld);
		
	else return window.HS_IMAGES[i];
}

function initFrmSys(){
	oFrm = $('frmSys');
	
	if(null !== oFrm)
		oFrm.set({
			events: {
				submit: function(){
					deFocus();
					
					var aO = $$('#frmSys input,#frmSys textarea');
					var sMSG = '';
					var mVal = '';
					var mTmp = '';
					
					for(var iO=0; iO<aO.length; iO++){
						o = aO[iO];
						
						switch(o.name){
							case 'p[naam]':
							case 'p[bericht]':
								mVal = o.value.toString();
								if(mVal.length>0 && mVal !== o.oldValue){
									mTmp = mVal.match(/([&\(\)*\na-z0-9\\\/' \.-]{1,})/gi);
									if(null !== mTmp){
										mVal = mTmp.join('');
										o.removeClass('error');
									} else { o.addClass('error'); sMSG += "\n - "+o.title.toString();}
								} else { o.addClass('error'); sMSG += "\n - "+o.title.toString();}
								o.value = mVal;
							break;
							case 'p[email]':
								mVal = o.value.toString();
								if(mVal.length>0 && mVal !== o.oldValue){
									if(mVal.test(/^([A-Z0-9_\-\.])+\@([A-Z0-9_\-\.])+\.([A-Z]{2,4})$/i))
										o.removeClass('error');
									else { o.addClass('error'); sMSG += "\n - "+o.title.toString();}
								} else { o.addClass('error'); sMSG += "\n - "+o.title.toString();}
								o.value = mVal;
							break;
							case 'p[tel]':
								mVal = o.value.toString();
								if(mVal.length>0 && mVal !== o.oldValue)
									o.removeClass('error');
								else { o.addClass('error'); sMSG += "\n - "+o.title.toString();}
								o.value = mVal;
							break;
						}
					}
					
					if(sMSG === ''){
						oFrm.submit();return true;
					}
						
					alert(window.L_PUNTEN_NOK+sMSG);
					return false;
				}
			}
		});
}

function initFrmSysLogin(){
	oFrm2 = $('frmSysLogin');
	if(null !== oFrm2)
		oFrm2.set({
			events: {
				submit: function(){
					deFocus();
					
					var aO = $$('#frmSysLogin input');
					var sMSG = '';
					var mVal = '';
					var mTmp = '';
					
					for(var iO=0; iO<aO.length; iO++){
						o = aO[iO];
						
						switch(o.name){
							case 'invoice_id':
							case 'code':
								mVal = o.value.toString();
								if(mVal.length>0 && mVal !== o.oldValue){
									mTmp = mVal.match(/([&\(\)*\na-z0-9\\\/' \.-]{1,})/gi);
									if(null !== mTmp){
										mVal = mTmp.join('');
										o.removeClass('error');
									} else { o.addClass('error'); sMSG += "\n - "+o.title.toString();}
								} else { o.addClass('error'); sMSG += "\n - "+o.title.toString();}
							break;
						}
					}
					
					if(sMSG === ''){
						oFrm2.submit();return true;
					}
						
					alert(window.L_PUNTEN_NOK+sMSG);
					return false;
				}
			}
		});
}

function deFocus(){
	var o = new Element('input',{id:'dummy'}).inject($('container'));
	o.focus();
	o.destroy();
}

function initOCReplace(){
	$$('.ocreplace').each(function(o){
		o.oldValue = o.value.toString();
		o.set({
			events: {
				 focus: function(){
					if(this.value === this.oldValue)
						this.value = '';
				 }
				,blur: function(){
					if(this.value === '')
						this.value = this.oldValue;
				}
			}
		});
	});
}

function initFocus(){
	$$('.focus').each(function(o){
		o.focus();
	});
}

function initDriehoek(){
	$('header-inhoud-arrow').addEvent('click',function(){
		doRedirect($('logo').href.toString());
	});
}

function initTaalSW(){
	var oOT = $('overige-talen');
	
	oOT.oldHeight = oOT.getHeight();
	oOT.set({
		morph: {
			duration: window.TAALSW_MORPH_DURATION,
			transition: window.TAALSW_MORPH_TRANSITION
		},
		events: {
			mouseover: function(){
				taalSWOpen();},
			mouseout: function(){
				taalSWClose();}
		}
	});
	
	$('huidige-taal').set({
		events: {
			mouseover: function(){
				taalSWOpen();},
			mouseout: function(){
				taalSWClose();}
		}
	});
	
	// start open, sluit met morph
	if(window.TAALSW_START_OPEN)
		taalSWClose();
	else $('overige-talen').set({styles:{height:0}});
}
function taalSWClose(){
	if(window.TAALSW_TIMER)
		clearTimeout(window.TAALSW_TIMER);
		
	window.TAALSW_TIMER = setTimeout(
		function(){
			$('overige-talen').morph({height:0});
		}
	,((window.TAALSW_START_OPEN === true)?window.TAALSW_START_HIDE_AFTER:window.TAALSW_AUTO_HIDE_AFTER));
	
	if(window.TAALSW_START_OPEN)
		window.TAALSW_START_OPEN = false;
}
function taalSWOpen(){
	if(window.TAALSW_TIMER)
		clearTimeout(window.TAALSW_TIMER);
		
	$('overige-talen').morph({height:$('overige-talen').oldHeight});
}

function initABlank(){
	$$('a.blank').each(function(a){
		a.addEvent('click',function(){
			this.target = '_blank';
			return true;
		});
	});
}

function initCufon(){

	// H[2-6]{1} = HelveticeNeueLTStd Lt
	Cufon.replace('h2, h3, h4, h5, h6, a.submit', {
		fontFamily:'HelveticeNeueLTStd Lt',
		hover:false
	});
	
	// Menu = HelveticaNeueLTStd Roman
	Cufon.replace('ul#menu a', {
		fontFamily:'HelveticaNeueLTStd Roman',
		hover:true
	});
}

// FOOTER
function initFooter(){
	
	window.iOldClrImg = -1;
	
	$$('div#footer li').each(function(li){
		
		li.set({
			events: {
				mouseover: function(){
					var img = this.getElement('img');
					img.morph({opacity:1});
					
					window.FOOTER_ALLOW_AUTO_OLD = window.FOOTER_ALLOW_AUTO;
					window.FOOTER_ALLOW_AUTO     = false;
				},
				mouseout: function(){
					var img = this.getElement('img');
					img.morph({opacity:0});
					
					window.FOOTER_ALLOW_AUTO = window.FOOTER_ALLOW_AUTO_OLD;
					if(window.FOOTER_ALLOW_AUTO)
						clrRandImg();
				}
			}
		});
		
		// img = color, footer bg = b&w
		var img = li.getElement('img');
		img.set({
			styles: {
				visibility: 'hidden',
				opacity: 0},
			morph: {
				duration: window.FOOTER_MORPH_DURATION,
				transition: window.FOOTER_MORPH_TRANSITION}
		});
		
		if(undefined === window.aClrImg)
			window.aClrImg = [img];
		else window.aClrImg[window.aClrImg.length] = img;
	});
	
	if(window.FOOTER_ALLOW_AUTO)
		clrRandImg();
}
function clrRandImg(){
	
	var i = getRandClrImg();
	window.iOldClrImg = i;
	
	if(window.rFOOTERTIMER)
		clearTimeout(window.rFOOTERTIMER);
		
	window.rFOOTERTIMER = setTimeout(function(){
		if(window.FOOTER_ALLOW_AUTO){
			window.aClrImg[i].morph({opacity:1});
			
			setTimeout(function(){
				window.aClrImg[i].morph({opacity:0});
				clrRandImg();
			},window.FOOTER_AUTO_HIDE_AFTER);
		}
	},window.FOOTER_AUTO_SHOW_AFTER);
}
function getRandClrImg(){
	var i = Math.floor(Math.random() * (5 - 0 + 1)) + 0;
	
	if(i === window.iOldClrImg)
		return getRandClrImg();
	return i;
}

function doRedirect(sHref){
	document.location = sHref;
}
