var bNext = true;
var bPrev = true;

var nVisible = 3; // Aantal zichtbare items
var nDefaultSlides = 1; // Aantal items per keer sliden
var obj; // Naam van de items 
var nSlides;
var nWidth;
var nTotal;
var nActive;

function productenSlider() {
	
	obj = $('.product_slide'); // Naam van de items 
	nSlides = nDefaultSlides;
	nWidth = parseInt(obj.width()) + parseInt(obj.css('padding-left')) + 1;
	nTotal = parseInt(obj.size());
	nActive = nVisible;
	
	$('.product_slide').hover(function() {
		$(this).css('background','url(images/template/bg_slider_roll.gif)');								   
	}, function(){
		$(this).css('background','');								   
	});
	
	if(nTotal < nVisible) {
		$('#btn_slider_vorige').hide();
		$('#btn_slider_volgende').hide();
	}
	
	$('#btn_slider_volgende').click(function(){
		speed = 250;
		clearTimeout(timeout);
		moveForward();										 
	});
	
	$('#btn_slider_vorige').click(function(){
		speed = 250;
		clearTimeout(timeout);
		moveBack();										 
	});

}

function moveForward() {
	if(bNext == true) {
		var nRemaining = nTotal - nActive;
		if(nRemaining < nSlides) {
			nSlides = nRemaining;
		} else {
			nSlides = nDefaultSlides;	
		}
		if(nActive == 1) {
			nActive = nDefaultSlides;	
		}
		var nLeft = parseInt($('#producten_container').css('left')) - (nWidth * nSlides);
		nActive = nActive + nSlides;
		if(nTotal == nActive) {
			$('#btn_slider_volgende').hide();
		} else {
			$('#btn_slider_volgende').show();
		}
		bNext = false;		
		$('#producten_container').animate({
			'left':nLeft
		},
		speed,
		"swing", 
		function(){ 
			bNext = true;
		});
	}
	$('#btn_slider_vorige').css('display','block');
	if(nRemaining == 1) {
		sDirection = 'moveBack';
	}
	autoSlide();
}

function moveBack() {
	if(bPrev == true) {
		var nRemaining = nActive - nVisible;
		if(nRemaining < nSlides) {
			nSlides = nRemaining;
		} else {
			nSlides = nDefaultSlides;	
		}
		var nLeft = parseInt($('#producten_container').css('left')) + (nWidth * nSlides);
		if(nLeft == 0) {
			$('#btn_slider_vorige').css('display','none');
		}
		bPrev = false;			
		$('#producten_container').animate({
			'left':nLeft
		},
		speed,
		"swing", 
		function(){ 
			bPrev = true;
		});
		nActive = nActive - nSlides;
		if(nTotal > nActive) {
			$('#btn_slider_volgende').show();
		} else {
			$('#btn_slider_volgende').hide();
		}
		if(nRemaining == 1) {
			sDirection = 'moveForward';
		}
		autoSlide();
	}
}

var sDirection = 'moveForward';
var timeout = '';
function autoSlide() {
	speed = 600;
	if(sDirection == 'moveForward') {
		timeout = setTimeout('moveForward()',3000);
	} else if(sDirection == 'moveBack') {
		timeout = setTimeout('moveBack()',3000);
	}
}

var bNext = true;
var nTotal;
var nActive = 1;
function fotoShow() {
	nTotal = parseInt($('#fotoshow_container').find('img').size());
	if(nTotal > 1) {
		$('#fotoshow_btn_next').show();
		$('#fotoshow_nav').children('#nav_streepje').show();
	}
	if(nActive != 1) {
		$('#fotoshow_btn_prev').show();
	}
	$('#fotoshow_btn_prev').click(function(){
		if(bNext) {
			bNext = false;
			nActive = nActive - 1;
			$('#fotoshow_btn_next').show();
			if(nActive == 1) {
				$('#fotoshow_btn_prev').hide();
			}
			var sSrc = $(this).parent('#fotoshow_nav').prev('#fotoshow_foto').find('#foto'+nActive).attr('src');
			$(this).parent('#fotoshow_nav').prev('#fotoshow_foto').children('#showfoto').animate({
				'opacity': 0.2
			}, 150,
			function(){
				$(this).attr('src',sSrc).animate({
				'opacity': 1
				}, 300, function(){
					bNext = true;	
				});
			});
		}
	});
	$('#fotoshow_btn_next').click(function() {
		if(bNext) {
			bNext = false;
			nActive = nActive + 1;
			$('#fotoshow_btn_prev').show();
			if(nActive == nTotal) {
				$('#fotoshow_btn_next').hide();
			}
			var sSrc = $(this).parent('#fotoshow_nav').prev('#fotoshow_foto').find('#foto'+nActive).attr('src');
			$(this).parent('#fotoshow_nav').prev('#fotoshow_foto').children('#showfoto').animate({
				'opacity': 0.2
			}, 150,
			function(){
				$(this).attr('src',sSrc).animate({
				'opacity': 1
				}, 300, function(){
					bNext = true;	
				});
			});
		}
	});
}

function submenu() {
	$('#sub_producten, #submenu').hover(function(){
		$('#sub_producten').css('background','url(images/template/menu_item_hover.jpg)');
		$('#submenu').show();								   
	}, function(){
		$('#sub_producten').css('background','url(images/template/menu_item.jpg)');
		$('#submenu').hide();								   
	});
}

function productRoll() {
	$('.product').hover(function(){
		$(this).find('.vergrootglas').css('background','url('+sDomain+'images/template/vergrootglas_roll.png)');
	}, function(){
		$(this).find('.vergrootglas').css('background','url('+sDomain+'images/template/vergrootglas.png)');
	});
}

function validateDetailForms(p_sForm) {
	var iconOK = {
		'background-position' : '0px 0px'
	}
	var iconError = {
		'background-position' : '0px -32px'
	}

	var aMessage = new Array();
	aMessage[0] = 'Vul uw naam in.';
	aMessage[1] = 'Vul uw telefoonnummer in.';
	aMessage[2] = 'Vul uw e-mailadres in.';
	aMessage[3] = 'Vul een geldig e-mailadres in.';
	aMessage[4] = 'Vul het onderwerp in.';
	
	$.validator.addMethod("telefoon", function(phone_number, element) {
    	phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(\(?\+?[0-9]*\)?)?[0-9_\- \(\)]*$/);
	}, "Vul a.u.b. een geldig telefoonnummer in.");
	
	$.validator.addMethod("geennummers", function(geennummers, element) {
		   var reg = /[0-9]/;
		   return this.optional(element) || !geennummers.match(reg);
	}, "Getallen zijn niet toegestaan.");

	$('#'+p_sForm).validate({
		rules: {
			sNaam: {
				required: true,
				geennummers: true
			},
			sTelefoon: {
				telefoon: true
			},
			sEmail: {
				required: true,
				email: true
			},
			sOnderwerp: {
				required: true
			}
		},
		messages: {
			sNaam: {
				required: aMessage[0]
			},
			sEmail: {
				required: aMessage[2],
				email: aMessage[3]
			},
			sOnderwerp: {
				required: aMessage[4]
			}
		},
		errorPlacement: function(error, element) {
			error.insertAfter(element.parent().next());
			element.parent().next('div.controle').css(iconError);
		},
		success: function(label) {
			label.prev('div.controle').css(iconOK);
			label.remove();
		},
		submitHandler: function(form) {
			switch(p_sForm){
				case 'contactformulier':
					var sUrl = sDomain+'includes/contactform.php';
				break;
			}	
			var sDiv = '';
			
			var options = {
				url: sUrl,
				type: 'POST',
				success: function(sData) {
					var aResponse = sData.split('|');
					if(aResponse[0] == 'ok'){
						
						window.location= sDomain+'contact?bedankt=1';
						
//						$('#'+p_sForm).fadeOut(500, function(){
//							$(this).remove();									 
//						});
//						
//						sDiv += '<div class="succes">';
//						sDiv += aResponse[1];
//						sDiv += '</div>';
						
						
						
					} else if(aResponse[0] == 'error'){
						
						sDiv += '<div class="succes">';
						sDiv += 'Er is iets mis gegaan tijdens het versturen<br/><br/>';
						sDiv += 'Gebruik het volgende email adres om contact op te nemen:<br/>';
						sDiv += '<a href="mailto:info@molendijkservice.nl" title="info@molendijkservice.nl">info@molendijkservice.nl</a><br/><br/>';
						sDiv += 'Vermeld alstublieft dat u deze foutmelding kreeg.';
						sDiv += '</div>';
					}
				
					$('div.melding').html(sDiv);
		
					setTimeout("$('.melding').fadeIn(700)", 800);
					
				}
			}
			$(form).ajaxSubmit(options);
			return false;
		}
	});	
}

