$(document).ready(function() {
	hoverMenu();
	subMenu();
	popupInlog();
	ingelogdBlijven();
});

//function radioButtons() {
//	$('.radio').click(function() {
//		$(this).parent('.radios').find('.radio.active').removeClass('active');
//		$(this).addClass('active');
//		$(this).parent('.radios').find('input').attr('checked','');
//		$(this).find('input').attr('checked','checked');
//	});
//}

function triggerLightbox() {
	$('#link_lb').click(function() {
		$('#bladmuziek_wrapper a.first').trigger('click');							 
	});
}

function hoverMenu() {
	$('.menu_item:not(.active)').hover(function() {
		$(this).addClass('active');
	}, function(){
		$(this).removeClass('active');
	});
}

function subMenu() {
	$('.menu_item, .submenu').hover(function() {
		$(this).children('.submenu').show();
	}, function(){
		$(this).children('.submenu').hide();
	});
}

function sfeerCycle(){
	$('#cycle')
		.after('<div id="nav">')
		.cycle({
			fx:     'fade',
			timeout: 5000,
			speed:	 900,
			pager: '#nav',
			after: function(curr,next,opts) {

			}
	});
}

function hoverProduct() {
	$('.product').hover(function(){
		$(this).css('background-image','url('+sDomain+'images/template/bg_product_hover.gif)');							 
	}, function(){
		$(this).css('background-image','url('+sDomain+'images/template/bg_product.gif)');							 
	});
}

function clearFields() {
	$('#zoeken form label input').focus(function() {
		$(this).prev().fadeTo(150, 0.33);
		$(this).keydown(function() {
			$(this).css('background-repeat', 'repeat');			 
		});
	});
	$('#zoeken form label input').blur(function() {
		if($(this).val() == '' ) {
			$(this).css('background-repeat', 'no-repeat');
			$(this).prev().fadeTo(150, 1);
		}
	});
	$('#zoeken form label input').each(function(){
		if ( $(this).val() != '') {
			$(this).css('background-repeat', 'repeat');
		};
	});
}

function tabs() {
	$('.tab').click(function() {
		var sActiveTab = $('.tab.active').attr('id');
		$('#content_'+sActiveTab).hide();
		$('.tab.active').removeClass('active');
		
		var sTab = $(this).attr('id');
		$('#content_'+sTab).show();
		$(this).addClass('active');

		var sTab = $(this).attr('id');
		var nTab = sTab.replace('tab','');
		if(nTab > 2) {
			$('.tab:not(.active)').addClass('not_active');
			$('.tab').each(function(){
				var nLoopTab = $(this).attr('id').replace('tab','');
				if(nTab < nLoopTab) {
					$(this).removeClass('not_active');
				}
			});
		} else {
			$('.tab').removeClass('not_active');
		}
		
		sIFR.replace(sifr, {
		selector: 'h2.fs20'
		,css: [
		  '.sIFR-root { padding: 0px; font-weight: bold; font-size: 20px; color: #9b8c65; line-height: 25px; height: 25px; width: auto;}'
		],
		wmode: 'transparent'
		});
	});
}

function algemeneVoorwaarden() {
	$('#balk_afrekenen .checkbox').click(function() {
		if($(this).hasClass('checked')) {
			$(this).children('input').attr('value','0')
			$(this).removeClass('checked');		
			$('#balk_afrekenen #afrekenen').hide();
			$('#ga_eerst_akkoord').show();
		} else {
			$(this).children('input').attr('value','1')
			$(this).addClass('checked');
			$('#ga_eerst_akkoord').hide();
			$('#balk_afrekenen #afrekenen').fadeIn(650);
		}
	});
}

var bReady = true;
function stap1() {
	$('.keuze_titel').click(function(){
		if(bReady) {
			bReady = false;
			if($(this).hasClass('open')) {
				$(this).removeClass('open');
				$(this).next('.content_keuze').children('.content_keuze_top').css('background','none');
				$(this).next('.content_keuze').slideUp(350, function(){
					$(this).prev().children('.icon_status').css('background','url(images/template/pijltje_dicht.gif)');
					bReady = true;														 
				});
			} else {
				$('.keuze_titel').removeClass('open');
				$('.icon_status').css('background','url(images/template/pijltje_dicht.gif)');
				$(this).addClass('open');
				$('.content_keuze').hide();
				$('.open').next('.content_keuze').slideDown(350, function(){
					$(this).children('.content_keuze_top').css('background','url(images/template/content_keuze_top.gif)');
					$(this).prev().children('.icon_status').css('background','url(images/template/pijltje_open.gif)');
					bReady = true;
				});
			}
		}
	});
}

function betaalmethodes() {
	$('.betaalmethode_content .left').click(function() {
		$('.bank').removeClass('checked');
		$('.bank input').attr('checked','');
		
		$('.betaalmethode_content .left input').attr('checked','');
		$('.betaalmethode_content .left').removeClass('checked');
		$(this).children('input').attr('checked','checked');
		$(this).addClass('checked');
	});
}

function banken() {
	$('.bank').click(function() {
		$('.betaalmethode_content .left input').attr('checked','');
		$('.betaalmethode_content .left').removeClass('checked');
		$(this).parents('.betaalmethode').children('.left input').attr('checked','checked');
		$(this).parents('.betaalmethode_bottom').prev().children('.left').addClass('checked');
							  
		$('.bank input').attr('checked','');
		$('.bank').removeClass('checked');
		$(this).children('input').attr('checked','checked');
		$(this).addClass('checked');
	});
}

var bReady = true;
function anderAdres() {
	$('#ander_adres a').click(function() {
		if(bReady) {
			if($(this).hasClass('checked')) {
				bReady = false;
				$(this).removeClass('checked');
				$(this).parent().next('#form_ander_adres').slideUp(500, function(){
					bReady = true;
					$('.field input').removeClass('required');
					$('#bAnderadres').val('false');
				});
			} else {
				bReady = false;
				$(this).addClass('checked');
				$(this).parent().next('#form_ander_adres').slideDown(500, function(){
					bReady = true;
					$('.field input').addClass('required');
					$('#bAnderadres').val('true');
				});
			}
		}
	});
}

function validateForm(p_sForm) {
	var iconOK = {
		'background-position' : '0px -32px'
	}
	var iconError = {
		'background-position' : '0px -64px'
	}
	$(p_sForm).validate({
		rules: {
			sEmail: {
				required: true,
				email: true
			},
			sWachtwoord1: {
				required: true
			},
			sWachtwoord2: {
				required: true,
				equalTo: '#sWachtwoord1'
			}
		},
		messages: {
			sNaam: 'Vul uw naam in.',
			sNaam2: 'Vul een naam in.',
			sOnderwerp: 'Vul een onderwerp in.',
			sOpmerking: 'Vul uw vraag of opmerking in.',
			sWachtwoord1: 'Vul een wachtwoord in.',
			sWachtwoord2: {
				required: 'Vul opnieuw uw wachtwoord in.',
				equalTo: 'Wachtwoorden komen niet overeen.'
			},
			sBedrijfsnaam: 'Vul de bedrijfsnaam in.',
			sContactpersoon: 'Vul een contactpersoon in.',
			sAdres: 'Vul uw adres in.',
			sPostcode: 'Vul uw postcode in.',
			sPlaats: 'Vul uw plaats in.',
			sTelefoon: 'Vul uw telefoonnummer in.',
			sEmail: {
				required: 'Vul uw e-mailadres in.',
				email: 'Vul een geldig e-mailadres in.'
			},
			sEmail2: {
				required: 'Vul een e-mailadres in.',
				email: 'Vul een geldig e-mailadres in.'
			}
		},
		errorPlacement: function(error, element) {
			error.insertAfter(element.parents('.field'));
			element.parent().next('div.controle').css(iconError);
		},
		success: function(label) {
			label.prev().children('div.controle').css(iconOK);
			label.remove();
		},
		submitHandler: function(form) {
			form.submit();
		}
	});	
}

function popupInlog() {
	$('#inlog').click(function() {
		$('#popup_overlay').fadeTo(0,0.95);
		$('#popup_overlay, #popup').fadeIn(500);	
		
		sIFR.replace(sifr, {
		selector: 'h1'
		,css: [
		  '.sIFR-root { padding: 0px; font-weight: bold; font-size: 21px; color: #000000; line-height: 25px; height: 25px; width: auto; text-transform: uppercase;}'
		],
		wmode: 'transparent'
		});
	});
	$('#btn_sluiten').click(function() {
		$('#popup_overlay, #popup').fadeOut(500);					   
	});
}

function ingelogdBlijven() {
	$('#ingelogd_blijven').click(function() {
		if($(this).hasClass('checked')) {
			$(this).removeClass('checked');
			$(this).children('input').val('0');
		} else {
			$(this).addClass('checked');
			$(this).children('input').val('1');
		}
	});
}

function nietSturen() {
	$('.niet_sturen').click(function() {
		if($(this).hasClass('active')) {
			$(this).removeClass('active');
			$(this).find('.bevestigen').hide();
		} else {
			$(this).addClass('active');
			$(this).find('.bevestigen').show();
		}
		if($(this).hasClass('checked')) {
			$(this).removeClass('checked');
			$(this).find('#nNietSturen').val('0');
		} else {
			$(this).addClass('checked');
			$(this).find('#nNietSturen').val('1');
		}
	});
}

function filterProducts() {
	$('.categorie a').click(function() {
		var sCategorie = $(this).attr('id');
		$.ajax({
			type: 'POST',
			url: sDomain+'inc/request.producten.php',
			data: 'sCategorie='+sCategorie,
			success: function(result){
				$('#ajax_producten').html(result);
				filterProducts();
			}
		});
	});
}

function toevoegenWinkelmand() {
	$('#input_toevoegen, #btn_neem_in_consignatie, .btn_inkopen a, .btn_opnieuw_in_consignatie a').click(function() {
		var nAantal = $('#nAantal').val();
		var nProduct = $('#nProduct').val();
			
		var bConsignatie = false;
		var bInkopen = false;
		var bOpnieuwConsignatie = false;
		var bInkopenOpnieuwConsignatie = false;
			
		if($(this).hasClass('consignatie')) {
			bConsignatie = true;
		} else if($(this).parent().hasClass('inkopen')) {
			var nAantal = 1;
			if($(this).hasClass('opnieuw')) {
				bInkopenOpnieuwConsignatie = true;
				nAantal = 2;
			}
			bInkopen = true;
			var nProduct = $(this).parent().find('input').val();	
			$(this).parent().addClass('delete_button');
		} else if($(this).hasClass('opnieuw-consignatie')) {
			bOpnieuwConsignatie = true;
			var nAantal = 1;
			var nProduct = $(this).find('input').val();	
			$(this).addClass('delete_button');
		}
		
		if(nAantal > 0) {
			$.ajax({
				type: 'POST',
				url: sDomain+'inc/toevoegen.inc.php',
				data: 'nAantal='+nAantal+'&nProduct='+nProduct+'&bConsignatie='+bConsignatie+'&bInkopen='+bInkopen+'&bInkopenOpnieuwConsignatie='+bInkopenOpnieuwConsignatie+'&bOpnieuwConsignatie='+bOpnieuwConsignatie,
				success: function(result){
					if(bConsignatie) {
						$('#in_consignatie_nemen').hide();
					}
					if(bInkopen || bOpnieuwConsignatie) {
						$('.delete_button').hide();
						window.location.href=sDomain+'winkelwagen';
					}
					if(bInkopen == false) {
						$('html, body').animate({
							scrollTop: 0
						}, 500, function(){
							$('#toegevoegd').fadeIn(650, function(){
								$('#totaalArtikelen').html(result);
								setTimeout(function() {
									$('a.link').show();
									$('#toegevoegd').fadeOut(650);
								}, 3700); 
							});
						});
					}
				}
			});
		}
	});
}

function winkelwagen() {
	$('.input_aantal input').change(function(){
		var nProduct = $(this).attr('id').replace('product_','');
		var nAantal = $(this).val();		
		loadWinkelwagen('productID=' + nProduct + '&updateaantal=' + nAantal + '&bConsignatie=false&bInkopen=false');
	});
	$('.itembalk .delete_item').click(function(){
		var bConsignatie = false;
		var bInkopen = false;
		if($(this).hasClass('consignatie')) {
			var bConsignatie = true;
		} else if($(this).hasClass('inkopen')) {
			var bInkopen = true;
		}
		var nProduct = $(this).parents('.itembalk').find('.input_aantal input').attr('id').replace('product_','');
		loadWinkelwagen('productID=' + nProduct + '&updateaantal=0&bConsignatie=' + bConsignatie + '&bInkopen=' + bInkopen);
	});
}

function loadWinkelwagen(p_sData) {
	$.ajax({
		type: 'POST',
		url: 'inc/winkelwagen.inc.php',
		data: p_sData,
		success: function(result){
			$('#ajax_winkelwagen').html(result);
			winkelwagen();
			algemeneVoorwaarden()			
			sIFR.replace(sifr, {
			selector: 'h2.fs20'
			,css: [
			  '.sIFR-root { padding: 0px; font-weight: bold; font-size: 20px; color: #9b8c65; line-height: 25px; height: 25px; width: auto;}'
			],
			wmode: 'transparent'
			});
		}
	});
}


function mailafriend(){
	$('a#mailafriend-button').click(function(){
		$('#mailafriend').slideToggle(300);
	});
	validateForm('#mailafriend-form');
}



