
	$(document).ready(function() {
	
		Cufon.replace('.typeface-js');
		
		$( "#agenda_field" ).datepicker({ 
				showOn: "button",
				buttonImage: "/img/agenda_ico.gif",
				buttonImageOnly: true,
				minDate: -20,
				onSelect: function(dateText) {
								dArray = dateText.split("-");
								$('select[name="leverdatum_dag"] option').removeAttr("selected");

				 				$('select[name="leverdatum_dag"] option[value="'+parseInt(dArray[0])+'"]').attr('selected', 'selected');
								 
								$('select[name="leverdatum_maand"] option').removeAttr("selected");
				 				$('select[name="leverdatum_maand"] option[value="'+parseInt(dArray[1],10)+'"]').attr('selected', 'selected');	
								 
								$('select[name="leverdatum_jaar"] option').removeAttr("selected");
				 				$('select[name="leverdatum_jaar"] option[value="'+parseInt(dArray[2])+'"]').attr('selected', 'selected');		
						   }
						   
				});

		/* stretch content block if to small */

		//if ($('#frame').height() < 350) { $('#frame').css({ height: '350px' }); }

		/* Replace a standard submit button with a custom class */

		replaceSubmitButton('#frm-products');
		replaceSubmitButton('#frm-details');
		replaceSubmitButton('#frm-payment');
		replaceSubmitButton('#frm-overview');
		replaceSubmitButton('#frm-contact');
		
		$('#pmcheck').click(function() { showPersonalMessage(); });
		showPersonalMessage();
		
		/* show/hide different delivery address/date section */
		
		$('#diffcheck').click(function() { showDeliveryAddress(); });
		showDeliveryAddress();
		
		$('#delivery-date-check').click(function() { showDeliveryDate(); });
		showDeliveryDate();
		
		/* show/hide ideal bank */
		
		$('#frm-payment input[name=betaalwijze]').click(function() { showIdealBank(); });
		showIdealBank();

		/* movie poster carousel */
		
		$("#movie-poster .carousel").jcarousel({
			wrap: 'both',
			scroll: 1,
			auto: 4,
			visible: 1
		});
		
		/* blik info mouseover */
		
		$('#frm-products .blik .info').mouseenter(function(e) {
			$('<div></div>').attr('id', 'blik-info').html($(this).attr('alt')).css({
				top: e.pageY-30,
				left: e.pageX+20
			}).appendTo('body');
		}).mouseleave(function(e) {
			$('#blik-info').remove();
		});
		
		/* disabled address and city fields for zipcode check */
		
		$('.address, .city', '#frm-details').attr('disabled', 'disabled');
		
		/* submit form but go back */
		
		$('#nav-submit .back').click(function() {
			$('<input type="hidden" name="back" value="1" />').appendTo($(this).closest('form'));
			$(this).closest('form').submit();
			return false;
		});
		
		/* count remain characters for personal message */
		
		$('#personal-message').keyup(function() {
			var max = $(this).attr('maxlength');
			if ($(this).val().length > max) {
				$(this).val($(this).val().substr(0, $(this).attr('maxlength')));
			}
			$(this).parent().find('span i').text(max-$(this).val().length);
		});
		
		/* use rel="external" to open a link in a new window
		   target="_blank" is not XHTML 1.0 valid
		*/
		
		$('a[rel=external]').click(function(){
			this.target = '_blank';
		});
		
		/* Zipcode check */
		
		$('.zipcode-caption').show();

		$('.zipcode-check .house').blur(function() {
		
			var parent = $(this).closest('.zipcode-check');
			var zipcode = $('.zipcode', parent).val();
			var house = $('.house', parent).val();
			
			$('.address, .city', parent).attr('disabled', 'disabled');
		
			$.ajax({
				type: 'GET',
				url: '/ajax/postcode_check.php',
				data: 'zipcode='+zipcode+'&house='+house,
				success: function(msg) {
				    if( msg == 'postcodeError' ){
                        $('.zipcode-caption', parent).addClass('error').html('Geen adres bij deze postcode gevonden. Controleer uw postcode.');
					} else
					if (msg == 'error') {
						$('.zipcode-caption', parent).addClass('error').html('Geen adres bij deze postcode gevonden. Controleer uw postcode of vul hieronder handmatig uw adres in.');
						$('.address, .city',  parent).attr('disabled', '');
					} else {
						var result = msg.split(',');
						$('.address', parent).val(result[0]);
						$('.city', parent).val(result[1]);
						$('.zipcode-caption', parent).html('Uw adres is automatisch ingevuld.');
					}
				}
			});
		});
		
		$('#frm-details').submit(function(){
			$('.address, .city', this).attr('disabled', '');
		});
		
		$('#frm-payment .ideal input')
		
		
		$('#bedrijf-info').qtip({
	         content: 'een bedrijfsbestelling boven de<br />25 stuks is ook mogelijk via het<br /><a href="/zakelijk/15">zakelijke bestelformulier</a>',
	         position: 'topLeft',
	         hide: {
	            fixed: true
	         },
	         style: {
	            padding: '5px 15px',
	            name: 'light'
	         }
	      });
		
	});

	function replaceSubmitButton(obj) {
		if ($(obj).length > 0) {
			$('input[type=submit]', obj).replaceWith(
				$('<a href="#" class="submit typeface-js"><span><span>'+	$('input[type=submit]', obj).val()+'</span></span></a>')			
				.click(function() {
					$(obj).submit();
					return false;
				})
			);
		}
	}
	function showPersonalMessage() {
		if ($('#pmcheck').is(':checked')) {
			$('#personal-message-box').show();
		} else {
			$('#personal-message-box').hide();
		}
	}
	function showDeliveryAddress() {
		if ($('#diffcheck').is(':checked')) {
			$('#different-delivery-address').show();
		} else {
			$('#different-delivery-address').hide();
		}
	}
	
	function showDeliveryDate() {
		if ($('#delivery-date-check').is(':checked')) {
			$('#delivery-date').show();
		} else {
			$('#delivery-date').hide();
		}
	}
	
	function showIdealBank() {
		if ($('#frm-payment input[name=betaalwijze]:checked').val() == 'ideal') {
			$('#frm-payment .idealbank').show();
		} else {
			$('#frm-payment .idealbank').hide();
		}
	}


/* Dutch (UTF-8) initialisation for the jQuery UI date picker plugin. */
/* Written by Mathias Bynens <http://mathiasbynens.be/> */
jQuery(function($){
	$.datepicker.regional['nl'] = {
		closeText: 'Sluiten',
		prevText: 'Vorige maand',
		nextText: 'Volgende maand',
		currentText: 'Vandaag',
		monthNames: ['Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober', 'November', 'December'],
		monthNamesShort: ['jan', 'feb', 'maa', 'apr', 'mei', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec'],
		dayNames: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
		dayNamesShort: ['zon', 'maa', 'din', 'woe', 'don', 'vri', 'zat'],
		dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
		weekHeader: 'Wk',
		dateFormat: 'dd-mm-yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['nl']);
});
