	$(document).ready(function() { 

		/*#################################################################
		# INIT: Miscellaneous
		###################################################################*/		

		$('a.show-contact-form').fancybox({
			'titleShow'		: false
		});

		$('a.show-booking-form').fancybox({
			'titleShow'		: false
		});

		if ($('form#contact-form').length) {
			$('#defaultCatalog').bind('click', function(e) {
				var t;
				if ($(e.target).attr('checked') == false) {
					t = $('[for=defaultAddress1]').attr('data-value-bak');
					$('#address1').attr('required', false);	
					$('[for=address1]').attr('data-value-bak', '');
					$('[for=address1]').text(t);
					
					t = $('[for=zip]').attr('data-value-bak');
					$('#zip').attr('required', false);	
					$('[for=zip]').attr("data-value-bak", '');
					$('[for=zip]').text(t);
					
					t = $('[for=city]').attr('data-value-bak');
					$('#city').attr('required', false);	
					$('[for=city]').attr("data-value-bak", '');
					$('[for=city]').text(t);
				} else {
					t = $('[for=address1]').text();
					$('#address1').attr('required', 'required');					
					$('[for=address1]').attr('data-value-bak', t);
					$('[for=address1]').text(t+'*');
					
					t = $('[for=zip]').text();
					$('#zip').attr('required', 'required');					
					$('[for=zip]').attr('data-value-bak', t);
					$('[for=zip]').text(t+'*');
					
					t = $('[for=city]').text();
					$('#city').attr('required', 'required');					
					$('[for=city]').attr('data-value-bak', t);
					$('[for=city]').text(t+'*');
				}
			});
		
		
			$('form#contact-form').bind('submit', function(e) {
				e.preventDefault();
				var $form = $(e.target);
				
				$('p.error', $form).text('').addClass('none');
				$(':input', $form).removeClass('error');
				var required = $('*[required]', $form);
				var unfilled = [];
				$.each(required, function(i, elm) { 
					if ($.trim($(elm).val()) === '' || $(elm).val() === null) {
						unfilled.push(elm);
					}
				});

				var has_error = unfilled.length > 0;                  
				if (has_error) {
					$.each(unfilled, function(i, elm) {
						$(elm).addClass('error');	
					});
					$('p.error', $form).removeClass('none')
						.text('Bitte füllen Sie die benötigten Formularfelder');
					return false;
				}

				if (typeof _gaq != "undefined") { 
					_gaq.push(['_trackEvent', 'Formsubmitions', $form.attr('id')+' on '+$('body').first().attr('id')]);
				}
				contactAjaxition($form);
			});
		}

		if ($('form#booking-form').length) {
			$('#row-03').addClass('none');
			$('#row-04').addClass('none');
			$('#row-05').addClass('none');
			$('#row-06').addClass('none');
			$('#row-07').addClass('none');
			$('#row-08').addClass('none');
			$('#row-09').addClass('none');
			$('#row-10').addClass('none');
			
			$('#position2'  ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-02').removeClass('none'); } });
			$('#position4'  ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-03').removeClass('none'); } });
			$('#position6'  ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-04').removeClass('none'); } });
			$('#position8'  ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-05').removeClass('none'); } });
			$('#position10' ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-06').removeClass('none'); } });
			$('#position12' ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-07').removeClass('none'); } });
			$('#position14' ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-08').removeClass('none'); } });
			$('#position16' ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-09').removeClass('none'); } });
			$('#position18' ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-10').removeClass('none'); } });
			$('#firstname2' ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-02').removeClass('none'); } });
			$('#firstname4' ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-03').removeClass('none'); } });
			$('#firstname6' ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-04').removeClass('none'); } });
			$('#firstname8' ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-05').removeClass('none'); } });
			$('#firstname10').bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-06').removeClass('none'); } });
			$('#firstname12').bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-07').removeClass('none'); } });
			$('#firstname14').bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-08').removeClass('none'); } });
			$('#firstname16').bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-09').removeClass('none'); } });
			$('#firstname18').bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-10').removeClass('none'); } });
			$('#surname2'   ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-02').removeClass('none'); } });
			$('#surname4'   ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-03').removeClass('none'); } });
			$('#surname6'   ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-04').removeClass('none'); } });
			$('#surname8'   ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-05').removeClass('none'); } });
			$('#surname10'  ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-06').removeClass('none'); } });
			$('#surname12'  ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-07').removeClass('none'); } });
			$('#surname14'  ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-08').removeClass('none'); } });
			$('#surname16'  ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-09').removeClass('none'); } });
			$('#surname18'  ).bind('blur', function(e) { if ($(e.target).val() != "") { $('#row-10').removeClass('none'); } });
			
			$('#termsconditions2').bind('click', function(e) { 
				e.preventDefault();
				var $link = $(this);
				$('#confirm').attr('checked', 'checked');
				window.open($link.attr('href')); 
			});
									
			$('form#booking-form').bind('submit', function(e) {
				e.preventDefault();
				var $form = $(e.target);
				
				$('p.error', $form).text('').addClass('none');
				$('input', $form).removeClass('error');
				$('label', $form).removeClass('error');

				var required = $('*[required]', $form);
				var unfilled = [];
				$.each(required, function(i, elm) { 
					if ($.trim($(elm).val()) === '' || $(elm).val() === null) {
						unfilled.push(elm);
					}
				});

				var has_error = unfilled.length > 0;
				var has_confirmed = $('#confirm').attr('checked'); //jquery bug with checked
				
				if (has_error || !has_confirmed) {
					var text = '';
					if (has_error) {
						$.each(unfilled, function(i, elm) {
							$(elm).addClass('error');	
						});
						text += 'Bitte füllen Sie die benötigten Formularfelder. ';
					}
					if (!has_confirmed) {
						$('label[for="confirm"]').addClass('error');
						text += 'Bitte markieren Sie das Feld zur Bestätigung der Geschäftsbedingungen. ';
					}					
					
					$('p.error', $form).removeClass('none')
						.text(text);
					return false;
				}

				if (typeof _gaq != "undefined") { 
					_gaq.push(['_trackEvent', 'Formsubmitions', $form.attr('id')+' on '+$('body').first().attr('id')]);
				}
					
				bookingAjaxition($form);
			});
		}

		function contactAjaxition($form) {
			$.ajax({
				type: $form.attr('method'),
				url: $form.attr('action'),
				data: $form.serialize()+'&randomizer='+Math.random(),
				dataType: 'script',
				beforeSend: function(xhr) {
					$(':submit', $form)
						.attr('data-value-bak', $(':submit', $form).attr('value'))
						.attr('value', 'Senden…')
						.attr('disabled', 'disabled');
					//$('#processing').addClass('fadein');
				},
				complete: function() { 
					$(':submit', $form)
						.removeAttr('disabled')
						.attr('value', $(':submit', $form).attr('data-value-bak'))
						.removeAttr('data-value-bak');
					//$('#processing').removeClass('fadein');
					$('p.info', $form).text('Danke für Ihre Nachricht!');
					$('fieldset', $form).addClass('none');
				}
			});
		}

		function bookingAjaxition($form) {
			$.ajax({
				type: $form.attr('method'),
				url: $form.attr('action'),
				data: $form.serialize()+'&randomizer='+Math.random(),
				dataType: 'script',
				beforeSend: function(xhr) {
					$(':submit', $form)
						.attr('data-value-bak', $(':submit', $form).attr('value'))
						.attr('value', 'Senden…')
						.attr('disabled', 'disabled');
					//$('#processing').addClass('fadein');
				},
				complete: function() { 
					$(':submit', $form)
						.removeAttr('disabled')
						.attr('value', $(':submit', $form).attr('data-value-bak'))
						.removeAttr('data-value-bak');
					//$('#processing').removeClass('fadein');
					$('p.info', $form).text('Danke für Ihre Buchung!');
					$('fieldset', $form).addClass('none');
				}
			});
		}

	}); // end $(document).ready
