$(function() {
	/* 'autoclear' velden */
	$('input.autoclear').each(function() {
		this.defaultValue = this.value;
	}).focus(function() {
		if (this.value==this.defaultValue) {
			this.value = '';
		}
	}).blur(function() {
		if (this.value=='') {
			this.value = this.defaultValue;
		}
	});
	
	/* submenu */
	$('div.hoofdmenu li a').mouseover(function() {
		$('div.submenu').hide();
		var maxWidth = 960;
		var li = $(this).parent('li');
		var submenu = $('#sub' + li.attr('id'));
		var position = li.position().left;
		var submenuWidth = submenu.outerWidth();
		if (position + submenuWidth > maxWidth) {
			position = li.position().left + li.outerWidth() - submenuWidth;
		}
		submenu.css('left', position);
		submenu.show('slow');
	}).mouseout(function() {
		var li = $(this).parent('li');
		var submenu = $('#sub' + li.attr('id'));
		submenu.hide();
	});
	$('div.submenu').mouseover(function() {
		$(this).show();
	}).mouseout(function() {
		$(this).hide();
	});
	
	/* mijnmenu */
	$('a#mijntamika').mouseover(function() {
		$('div.mijnmenu').show('slow');
	}).mouseout(function() {
		$('div.mijnmenu').hide();
	});
	$('div.mijnmenu').mouseover(function() {
		$(this).show();
	}).mouseout(function() {
		$(this).hide();
	});
	
	/* winkelmandje click-area */
	$('div#header div.winkelmandje').click(function() {
		location.href = $('a', this).attr('href');
	});
	
	/* winkelmandje ophalen met ajax */
	$.ajax({
		url: baseUrl+'home/winkelmandje/',
		dataType: 'json',
		success: function(data, textStatus, jqXHR) {
			$('#winkelmandje_inhoud').html((data.aantal==0 ? 'Geen' : data.aantal) + ' product' + (data.aantal==1 ? '' : 'en') + ' - ' + data.bedrag);
		}
	});
	
	/* winkelmand wijzigen aantal */
	$('div.winkelwagen form input').change(function() {
		$.ajax({
			url: baseUrl+'/bestellen/',
			dataType: 'json',
			type: 'POST',
			data: $(this.form).serializeArray(),
			success: function(data, textStatus, jqXHR) {
				for (artikelnr in data.producten) {
					$('#totaalprijs_' + artikelnr).html(data.producten[artikelnr]['prijs']);
					var aantal = $('input#aantal_' + artikelnr);
					if ($(aantal).val()!=data.producten[artikelnr]['aantal']) {
						$(aantal).val(data.producten[artikelnr]['aantal']);
					}
				}
				$('#productentotaal').html(data.productenTotaal);
				$('#bezorgkosten').html(data.bezorgkosten);
				$('#administratiekosten').html(data.administratiekosten);
				$('#ordertotaal').html(data.orderTotaal);
			}
		});
	});
	
	/* klikken op product in hoofdcategorie */
	$('div.hoofdcategorie_product').click(function() {
		location.href = $('a', this).attr('href');
	});
	
	/* productoverzicht */
	$('table.productoverzicht td.hascontent').click(function() {
		location.href = $('a', this).attr('href');
	});
	
	/* product thumbnails & videos */
	$('div.productdetails .thumbs div,div.productdetails .videos div').click(function() {
		$('div.productdetails .thumbs div,div.productdetails .videos div').removeClass('selected');
		$(this).addClass('selected');
		if ($(this).parent('div').hasClass('thumbs')) {
			$('div.productdetails .afbeelding').show().css('background-image', $(this).css('background-image').replace('82/82', '375/380'));
			$('div.productdetails .video').hide();
		} else if ($(this).parent('div').hasClass('videos')) {
			$('div.productdetails .afbeelding').hide();
			$('div.productdetails #large'+$(this).attr('id')+' object, div.productdetails #large'+$(this).attr('id')+' embed').width(375).height(380);
			$('div.productdetails #large'+$(this).attr('id')).show();
		}
	});
	$('div.productdetails .thumbs div:first').click();
	
	/* prijs bijwerken a.d.h.v. bestel-aantal */
	$('div.productdetails .prijsinfo input').keyup(function() {
		var aantal = $(this).val();
		if (!/^[0-9]*$/.test(aantal)) {
			aantal = aantal.replace(/[^0-9]/g, '');
			$(this).val(aantal);
		}
		var artikelnr = $('#artikelnr').val();
		$.ajax({
			url: baseUrl+'shop/product/',
			dataType: 'json',
			success: function(data, textStatus, jqXHR) {
				$('div.productdetails .prijsinfo .prijs.totaal').html(data.totaal);
			},
			data: {
				aantal: aantal,
				artnr: artikelnr
			}
		});
	});
	
	/* beoordelingen */
	if ($('.product_beoordeling .list ul li').length>0) {
		$(function() {
		    $('.product_beoordeling .list').jCarouselLite({	    	
		        speed: 1000,
		        btnNext: '.product_beoordeling .nav .next',
		        btnPrev: '.product_beoordeling .nav .prev',
		        visible: 1,
		        scroll: 1,
		        afterEnd: function(elm) {
		        	$('.product_beoordeling .nav #position').html($('.position', elm).html());
		        }
		    });		    
		});
	}
	
	/* veel verkocht */
	$('.product_veelverkocht td').click(function() {
		location.href = $('a', this).attr('href');
	});
	
	/* extra hoogte i.v.m. linker menu */
//	$('div.balk_links_inner').each(function() {
//		var height = $(this).outerHeight() + $(this).position().top;
//		if ($('#page_content').height()<height) {
//			$('#page_content').height(height);
//		}
//	});
//	$('#page_content').each(function() {
//		var height = $(this).outerHeight() + $(this).position().top - 275;
//		if ($('div.balk_links_inner').height()<height) {
//			$('div.balk_links_inner').height(height);
//		}
//	});
//	
	/* afwijkend afleveradres */
	$('input#afwijkendadres').change(function() {
		if (this.checked) {
			$('#afleveradres').show();
		} else {
			$('#afleveradres').hide();
		}
	}).each(function() {
		if (this.checked) {
			$('#afleveradres').show();
		} else {
			$('#afleveradres').hide();
		}
	});
	
	/* nummerieke velden */
	$('input.numeric').keyup(function() {
		var aantal = $(this).val();
		if (!/^[0-9]*$/.test(aantal)) {
			aantal = aantal.replace(/[^0-9]/g, '');
			$(this).val(aantal);
		}
	});
	
	/* formulier validatie */
	$('form.validate').submit(function() {
		var flds = $('input.required:visible,textarea.required:visible,select.required:visible', $(this));
		for (i=0; i<flds.length; i++) {
			var titel = $(flds[i]).attr('title');
			if (titel==null) {
				var titel = $(flds[i]).attr('name');
			}
			if ($(flds[i]).attr('type')=='radio') {
				var value = $('input[name='+$(flds[i]).attr('name')+']:checked', $(flds[i].form)).val();
			} else if($(flds[i]).attr('type')=='checkbox') {
				if (flds[i].checked) {
					var value = $(flds[i]).val();
				} else {
					var value = null;
				}
			} else {
				var value = $(flds[i]).val();
			}
			if (value=='' || value==null || value==undefined) {
				alert('Het veld \''+titel+'\' is verplicht!');
				flds[i].focus();
				return false;
			}
		}
		return true;
	});
	
	/* betaal-logo's klikbaar */
	$('table.betaalmethoden img').click(function() {
		var el = this;
		while (el.tagName!='TR') {
			el = el.parentNode;
		}
		$('input', el).attr('checked','checked')
	});
	
	/* rekeningnummer voor eenmalige machtiging en acceptgiro */
	$('input[name=betaalmethode]').change(function() {
		if ($('input[name=betaalmethode]:checked').val()=='eenmalige machtiging' || $('input[name=betaalmethode]:checked').val()=='acceptgiro') {
			$('#rekeningnummer').show();
		} else {
			$('#rekeningnummer').hide();
		}
	}).each(function() {
		if ($('input[name=betaalmethode]:checked').val()=='eenmalige machtiging' || $('input[name=betaalmethode]:checked').val()=='acceptgiro') {
			$('#rekeningnummer').show();
		} else {
			$('#rekeningnummer').hide();
		}
	});
	
	/* sortering */
	$('form.sortering select').change(function() {
		$(this.form).submit();
	});
	
	/* zoeksuggesties */
	$('#zoeken input').focus(function() {
		suggestieUpdate();
		var suggestieInterval = setInterval('suggestieUpdate()', 1000);
		$('#zoeken_suggesties').slideDown('slow');
	}).blur(function() {
		$('#zoeken_suggesties').slideUp('slow');
		if (typeof suggestieInterval != 'undefined') {
			clearInterval(suggestieInterval);
		}
	});
});

/* suggestie-click */
function suggestieClick(artnr, titel) {
//	$('form#zoeken input').val($('td.titel', tr).html());
//	$('form#zoeken').submit();
	document.location.href = baseUrl + 'product/' + artnr + '/' + titel.replace(/ /g, '+').replace(/\//g, '-');
}

/* suggestie-interval */
function suggestieUpdate() {
	var zoekvak = $('form#zoeken input');
	if (zoekvak.val().length>0 && zoekvak.val()!=zoekvak.attr('zoektekst')) {
		$.ajax({
			url: baseUrl + 'home/suggestie/',
			dataType: 'json',
			data: {
				zoektekst: zoekvak.val()
			},
			success: function(data) {
				$('#zoeken_suggesties table').html('');
				for (i=0; i<data.length; i++) {
					var html = '<tr onclick="suggestieClick(\'' + data[i].artnr + '\',\'' + data[i].simple_titel + '\');"><td class="afbeelding"><div style="background-image: url(' + data[i].afbeelding + ')"></div></td><td class="titel">' + data[i].titel + '</td><td class="prijs">' + data[i].prijs + '</td></tr>';
					$('#zoeken_suggesties table').append(html);
				}
			}
		});
		zoekvak.attr('zoektekst', zoekvak.val());
	} else if(zoekvak.val().length==0) {
		$('#zoeken_suggesties table').html('');
	}
}

