
	$(document).ready(function() {
	
		Cufon.replace('.typeface-js');
	
		/* 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 == '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')
	});

	function replaceSubmitButton(obj) {
		if ($(obj).length > 0) {
			$('input[type=submit]', obj).replaceWith(		
				$('<a href="#" class="submit typeface-js"></a>').html(
					$('input[type=submit]', obj).val()
				).click(function() {
					$(obj).submit();
				})
			);
		}
	}
	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();
		}
	}
