$(document).ready(function(){
	$('div.carusel').fadeGallery({
		gallery : '.gallery ul',
		autoplay : 5000,
		duration : 1000
	});
});

jQuery.fn.fadeGallery = function(options){
	// default options	
	var options = jQuery.extend({
		gallery : '.gallery',
		prev : 'a.prev, a.btn-prev, a.link-prev',
		next : 'a.next, a.btn-next, a.link-next',
		play : 'a.play',
		pause : 'a.pause, a.stop',
		thumbnails : '.thumbnails',
		generate_thumbnails : false,
		autoheight:false,
		IE:false,
		autoplay : 4000,
		duration : 500
	}, options);
	
	return this.each(function(){
		var holder = $(this),
			gallery = $(options.gallery, holder),
			prev = $(options.prev, holder),
			next = $(options.next, holder),
			play = $(options.play, holder),
			pause = $(options.pause, holder),
			autoplay = options.autoplay,
			thumbnails = $(options.thumbnails, holder),
			slides = gallery.find('> li'),
			thumbs = thumbnails.find('a'),
			autoheight = options.autoheight,
			IE = options.IE,
			timer,
			current;
		// auto numbers
		if (options.generate_thumbnails) {
			thumbnails.html('');
			var i = 0;
			while (i < slides.length) {
				thumbnails.append('<li><a href="#">' + (i+1) + '</a></li>');
				i++;
			}
			var thumbs = thumbnails.find('a');
		}
		
		// set active slide
		if (slides.index(slides.filter('.active')) == -1) {
			current = 0;
			slides.eq(0).addClass('active');
			if (options.thumbnails) thumbs.eq(0).addClass('active');
		} else {
			current = slides.index(slides.filter('.active'));
			if (options.thumbnails) thumbs.eq(current).addClass('active');
		}
		
		// init default css styles
		if (autoheight) gallery.css({height: slides.eq(current).height()});
		slides.css({
			opacity:0,
			display:'none',
			position:'absolute',
			top:0,
			left:0
		});
		slides.eq(current).css({
			opacity:1,
			display:'block',
			position:'relative'
		});
		if (IE && $.browser.msie) {
			slides.css({opacity:'auto'});
		}
		
		// prev/next element functions
		function nextEl() {
			var tmp = current;
			if (tmp < slides.length-1) tmp++;
			else tmp = 0;
			return tmp;
		}
		
		function prevEl() {
			var tmp = current;
			if (tmp > 0) tmp--;
			else tmp = slides.length-1;
			return tmp;
		}
		
		// main animation
		function rotate(next_ind) {
			if (timer) clearTimeout(timer);
			if (IE && $.browser.msie) {
				slides.eq(current).stop().removeClass('active').css({
					position:'absolute',
					zIndex:0,
					display:'none'
				})
				slides.eq(next_ind).stop().addClass('active').css({
					position:'relative',
					zIndex:1,
					display:'block'
				});
			} else {
				slides.eq(current).stop().removeClass('active').animate({
					opacity:0
				},options.duration, function(){
					$(this).css({
						display:'none'
					})
				}).css({
					position:'absolute',
					zIndex:0
				})
				slides.eq(next_ind).stop().addClass('active').css({
					display:'block'
				}).animate({
					opacity:1
				},options.duration).css({
					position:'relative',
					zIndex:1
				});
			}
			if (autoheight) {
				if (gallery.css('height') == 'auto') {
					gallery.stop().css({
						height: slides.eq(current).height()
					}).animate({
						height: slides.eq(next_ind).height()
					}, options.duration , function(){
						$(this).css({
							height:'auto'
						});
					});
				} else {
					gallery.stop().animate({
						height: slides.eq(next_ind).height()
					}, options.duration , function(){
						$(this).css({
							height:'auto'
						});
					});
				}
			}
				
			if (options.thumbnails) {
				thumbs.eq(current).removeClass('active');
				thumbs.eq(next_ind).addClass('active');
			}
			current = next_ind;
			
			if (timer) {
				timer = setTimeout(function(){
					rotate(nextEl());
				}, autoplay);
			}
		}
	
		// events
		next.click(function(){
			rotate(nextEl());
			return false;
		});
		prev.click(function(){
			rotate(prevEl());
			return false;
		});
		play.click(function(){
			if (!timer) {
				timer = setTimeout(function(){
					rotate(nextEl());
				}, autoplay);
			} 
			return false;
		});
		pause.click(function(){
			if (timer) {
				clearTimeout(timer);
				timer = false;
			}
			return false;
		});
		// thumbnails
		thumbs.each(function(i){
			$(this).click(function(){
				clearTimeout(timer);
				if (current !== i) {
					rotate(i)
				} else {
					if (autoplay) {
						timer = setTimeout(function(){
							rotate(nextEl());
						}, autoplay);
					}
				}
				return false;
			})
		});
		
		// autoplay
		if (autoplay) {
			timer = setTimeout(function(){
				rotate(nextEl());
			},autoplay);
		}
	});
}

function initPage()
{
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		filterClass: "default"
	});
}
function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filterClass) o.filterClass = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass)) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass)) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}
if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage);

function echeck(str) {
	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);

	if (str.indexOf(at) == -1){
	   return false
	}

	if ( str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr ){
	   return false
	}

	if ( str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr ){
	    return false
	}

	 if ( str.indexOf(at,(lat+1)) != -1 ){
	    return false
	 }

	 if ( str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot ){
	    return false
	 }

	 if ( str.indexOf(dot,(lat+2)) == -1 ){
	    return false
	 }

	 if ( str.indexOf(" ") != -1 ){
	    return false
	 }

	return true					
}

function fn_check_contact() {
	var get_bericht = $('#bericht').val();
	var get_value = $('#frmValue').val();

	var get_woonplaats = $('#woonplaats').val();
	var get_value_woonplaats = $('#woonplaatsValue').val();

	var get_phone = $('#telefoon').val();
	var get_value_phone = $('#telefoonValue').val();

	var get_naam = $('#naam').val();
	var get_naam_value = $('#naamValue').val();

	var get_email = $('#email').val();

	var get_naam = $('#naam').val();
	var get_naam_value = $('#naamValue').val();

	var get_lang = $('#frm_lang').val();

	var get_firmanaam = $('#firmanaam').val();
	var get_firmanaam_value = $('#firmanaamValue').val();

	var error = '';

	if ( get_bericht == '' || get_bericht == get_value ) {
		if ( get_lang == 'NL' ) {
			$('#error').html('Gelieve een bericht in te vullen');
		} else {
			$('#error').html('S\'il vous pla&icirc;t remplir un message');
		}
		error = 'true';
	}

	if ( get_woonplaats == '' || get_woonplaats == get_value_woonplaats ) {
		if ( get_lang == 'NL' ) {
			$('#error').html('Gelieve een woonplaats in te vullen');
		} else {
			$('#error').html('S\'il vous plaît entrer dans une résidence');
		}
		error = 'true';
	}

	if ( get_phone == '' || get_phone == get_value_phone ) {
		if ( get_lang == 'NL' ) {
			$('#error').html('Gelieve een geldig telefoonnummer in te vullen');
		} else {
			$('#error').html('S\'il vous pla&icirc;t entrer un num&eacute;ro de t&eacute;l&eacute;phone valide');
		}
		error = 'true';
	}

	if ( get_phone == '' || get_phone == get_value_phone ) {
		if ( get_lang == 'NL' ) {
			$('#error').html('Gelieve een geldig telefoonnummer in te vullen');
		} else {
			$('#error').html('S\'il vous pla&icirc;t entrer un num&eacute;ro de t&eacute;l&eacute;phone valide');
		}
		error = 'true';
	}

	if ( get_email == '' || echeck(get_email) === false ) {
		if ( get_lang == 'NL' ) {
			$('#error').html('Gelieve een emailadres in te vullen');
		} else {
			$('#error').html('S\'il vous pla&icirc;t remplir un e-mail');
		}
		error = 'true';
	}

	if ( get_naam == '' || get_naam == get_naam_value ) {
		if ( get_lang == 'NL' ) {
			$('#error').html('Gelieve een naam in te vullen');
		} else {
			$('#error').html('S\'il vous pla&icirc;t remplir un nom');
		}
		error = 'true';
	}

	if ( get_firmanaam == '' || get_firmanaam == get_firmanaam_value ) {
		if ( get_lang == 'NL' ) {
			$('#error').html('Gelieve een firmanaam in te vullen');
		} else {
			$('#error').html('S\'il vous pla&icirc;t entrez un nom de la soci&eacute;t&eacute;');
		}
		error = 'true';
	}

	if ( error == '' ) {
		$("#frm_contact").attr("action", "/core/mailer.php");
		$('#frm_contact').submit();
	} else {
		$('#error').delay(2000).fadeOut('slow', function(){
			$('#error').html('');
			$('#error').show();	
		});
	}
}

function fn_check_contact_big() {
	var fields = new Array('firmanaam', 'naam', 'telefoon', 'email', 'omschrijving', 'projectnummer');
	var error = false;
	
	for(var i=0, len=fields.length; i<len; i++)
	{
		var curfield = fields[i];
		var check = ($('#frm_' + curfield).val() == '');
		
		// E-Mail uitzondering
		if (curfield == 'email') { check = ($('#frm_' + curfield).val() == '' || echeck($('#frm_' + curfield).val()) === false) }
		if (curfield == 'btw') { fn_check_btw(); check = (btwreturn=='false') }
		
		if (check)
		{
			$('#' + curfield + 'DIV').addClass('error');
			$('#' + curfield + 'DIVError').show();
			error = true;
		}
		else
		{
			$('#' + curfield + 'DIV').removeClass('error');
			$('#' + curfield + 'DIVError').hide();
		}
	}

	if ( error == '' ) {
		$("#frm_contact_big").attr("action", "/core/mailer-big.php");
		$('#frm_contact_big').submit();
	}
}
