$(document).ready(function(){
	initShoppingbag();
	editShoppingbag();
	showInschrijfFormulier();
});

function showInschrijfFormulier(){
	$('.nieuwsbrief_button').click(function(){
		$(this).fadeOut();
		$('#nieuwsbrief').fadeIn();
	});
}

function addProduct() {
	$('.add-this').click(function() {
		var nAantal = $(this).prev('.input').children('.ammount').val();
		var nProduct = $(this).prev('.input').children('.productID').val();
		
		if(nAantal > 0) {
			$.ajax({
				type: 'POST',
				url: 'requests/addproduct.req.php',
				data: 'nAantal='+nAantal+'&nProduct='+nProduct,
				success: function(result){
					aResult = result.split('|');
					if(aResult[0] == 1) {
						$('#list').hide();
						$('html, body').animate({
							scrollTop: 0
						}, 500, function(){
							var sProducten = ' producten';
							if(aResult[1] == 1) {
								sProducten = ' product';
							}
							$('#total_products').html(aResult[1]+sProducten);
							$('#list').html(aResult[2]);
							$('#list').fadeIn(800);
							initShoppingbag();
							editShoppingbag()
						});
					}
				}
			});
		}
	});
}

function editShoppingbag() {
	$('.invul_aantal_producten').change(function(){
		var nProduct = $(this).attr('id').replace('item-','');
		var nAantal = $(this).val();		
		loadShoppingbag('productID=' + nProduct + '&updateamount=' + nAantal);
	});
	$('.delete').click(function(){
		var nProductID = $(this).attr('id');
		loadShoppingbag('productID=' + nProductID + '&updateamount=0');
	});
}

function loadShoppingbag(p_sData) {
	$.ajax({
		type: 'POST',
		url: 'controllers/offerte.ctr.php',
		data: p_sData,
		success: function(result){
			aResult = result.split('||');
			var sProducten = ' producten';
			if(aResult[0] == 1) {
				sProducten = ' product';
			}
			$('#total_products').html(aResult[0]+sProducten);
			$('#list').html(aResult[1]);
			if(aResult[2] != '') {
				$('#orderlist').html(aResult[2]);
			} else {
				$('#orderlist').html('<div class="right_content"><p>U heeft geen producten in uw offertelijst..</p></div>');
				$('.form').hide();
			}
			editShoppingbag();
		}
	});
}

function initShoppingbag() {
	$('#shopping-cart').hover(function() {
		$(this).children('#list').show();
	}, function() {
		$(this).children('#list').hide();
	});
}

function showSubmenu(){
	$('.hoofd').click(function(){
		$('.middle li').each(function(){
			$(this).removeClass('active');
		});
		$(this).parent('li').toggleClass('active');
	});
}

//cufon replace
Cufon.replace('.droid ', { 
	fontFamily: 'Droid Sans' ,
	hover: true
});
Cufon.replace('h1.droid, strong.droid, #right h2.droid, #products h3.droid ', { 
	textShadow: '1px 1px 1px #fff'
});

function clearInput() {
	$('.fade').focus(function() {
	   $(this).select();
	   $(this).prev().fadeTo(150, 0);
	   $(this).keydown(function() {
		   $(this).css('background-repeat', 'repeat');                  
	   });
	});
	$('.fade').blur(function() {
	   if($(this).val() == '' ) {
		   $(this).css('background-repeat', 'no-repeat');
		   $(this).prev().fadeTo(150, 1);
	   }
	});
	$('.fade').each(function(){
	   if($(this).val() != '') {
		   $(this).css('background-repeat', 'repeat');
	   };
	});
}

function initSlideshow() {
	$('#image')
	.cycle({
		fx:     'scrollHorz',
		speed:	 300,
		timeout: 0,
		next: '#next',
		prev: '#prev',
		after: function(curr, next, opts) {
			var nCurrent = (opts.currSlide + 1);
			var nTotal = opts.slideCount;
			$('#current').html(nCurrent);
			$('#total').html(nTotal);
		}
	});
}

function checkBox() {
	$('.checkbox').click(function() {
		if($(this).hasClass('checked')) {
			$(this).removeClass('checked');
			$(this).children('input').attr('checked','');
		} else {
			$(this).addClass('checked');
			$(this).children('input').attr('checked','checked');
			if($(this).parent().hasClass('zelf')){
				$('#ander-adres').hide();
				$('#sAfleveradres').attr('checked','');
				$('#sAfleveradres').parents('.checkbox').removeClass('checked');
				$('#sAfleveradres').parents('.check').removeClass('checked');
				$('#ander-adres').find('input').attr('value','');
			}
		}
	});
}


function changeAdres() {
	$('.input.check.adres').click(function() {
		if($(this).hasClass('checked')) {
			$(this).removeClass('checked');
			$(this).children('.checkbox').removeClass('checked');
			$(this).children('input').attr('checked','');
			$('#ander-adres').hide();
		} else {
			$(this).addClass('checked');
			$(this).children('.checkbox').addClass('checked');
			$(this).children('input').attr('checked','checked');
			$('#ander-adres').show();
			$('#sAfhalen').attr('checked','');
			$('#sAfhalen').parent('.checkbox').removeClass('checked');
		}
	});
}

function initSelectboxes(){
	$('div.select select').selectbox();
	var zIndex = 9999;
	$('div.form div.entry').each(function(){
		$(this).css('z-index',zIndex);
		zIndex--;
	});
}

function validateForm(p_sForm) {
	var iconOK = {
		'background-position' : '0px -32px'
	}
	var iconError = {
		'background-position' : '0px 0px'
	}
	
	var rules = {};
	rules[aFields[0]] = {
		required: true
	};
	rules[aFields[1]] = {
		required: true
	};
	rules[aFields[3]] = {
		required: true,
		postalcode: true
	};
	rules[aFields[4]] = {
		required: true,
		digits: true
	};
	rules[aFields[6]] = {
		required: true,
		email: true
	};
	rules[aFields[8]] = {
		required: true,
		number: true
	};
	rules[aFields[9]] = {
		required: true,
		nonumber: true
	};
	rules[aFields[10]] = {
		required: true,
		notags: true,
		minlength: 5
	};
	rules[aFields[11]] = {
		required: true,
		email: true
	};
	
	var messages = {};
	messages[aFields[0]] = {
		required: 'Vul uw naam in.'
	};
	messages[aFields[1]] = {
		required: 'Vul uw achternaam in.'
	};
	messages[aFields[3]] = {
		required: 'Vul een postcode in',
		postalcode: 'Vul een geldige postcode in.'
	};
	messages[aFields[4]] = {
		required: 'Vul uw telefoonnummer in',
		digits: 'Vul een geldig telefoonnummer in.'
	};
	messages[aFields[6]] = {
		required: 'Vul uw e-mailadres in.',
		email: 'Vul een geldig e-mailadres in.'
	};
	messages[aFields[8]] = {
		required: 'Vul geen getallen in',
		number: 'Dit veld mag alleen getallen bevatten'
	};
	messages[aFields[9]] = {
		required: 'Vul geen getallen in',
		nonumber: 'Dit veld mag geen getallen bevatten'
	};
	messages[aFields[10]] = {
		required: 'Vul iets in',
		notags: 'Er zijn geen tags toegestaan',
		minlength: 'Lengte minimaal 5 karakters'
	};
	messages[aFields[11]] = {
		required: 'Vul een e-mailadres in',
		email: 'Vul een geldig e-mailadres in'
	};
	$('#'+p_sForm).validate({
		rules: rules,
		messages: messages,
		errorPlacement: function(error, element) {
			var obj = element.parent().next('div.icon');
			error.insertAfter(obj);
			obj.attr('class','icon invalid');
		},
		success: function(label) {
			label.prev('div.icon').attr('class','icon valid');
			label.remove();
		},
		submitHandler: function(form) {
			form.trigger('submit');
		}
	});	
}

$(document).ready(function(){
	clearInput();
	checkBox();
	changeAdres();
	showSubmenu();
});
