$(document).ready(function() {
	
	var inputName = 'Vorname und Nachname';
	var inputNummer = 'Telefonnummer';
	var inputEmail = 'E-Mail Adresse';
	var inputNachricht = '';

	$('a[href*=#]').click(function() {
	 if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
	 && location.hostname == this.hostname) {
	   var $target = $(this.hash);
	   $target = $target.length && $target
	   || $('[id=' + this.hash.slice(1) +']');
	   if ($target.length) {
	  var targetOffset = $target.offset().top;
	  $('html,body')
	  .animate({scrollTop: targetOffset}, 500);
	    return false;
	   }
	  }
	  });
	
	
	if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod')
	{	
		function isuperbg(){
			// set image either 100% width or 100% height depending on content
		    var height = $(window).height();
		    var width = $(window).width(); 
			var wrapHeight = $('#wrap').height();

		    if( wrapHeight > height && width > height) {
				$('body').css('background-size','auto 100%');
		    }
			else if( height > width ){
				$('body').css('background-size','auto 100%');
			}
			else if( wrapHeight >= height && width > height ) {
				$('body').css('background-size','100% auto');
		    }
		}
		
		var superbgimage = $('#superbgimage').children('img').attr('src');
		$('body').css('background','url(' + superbgimage + ') no-repeat fixed top center');
		$('body').css('background-attachment','fixed');
		
		isuperbg();
		$( window ).bind( 'resize', isuperbg );
	
		// get rid of the url bar
		window.onload = function() {setTimeout(function(){window.scrollTo(0, 1);}, 100);};
	}

	else
	{
		$.fn.superbgimage.options = {
		showimage: 1,
		transition: 1,
		vertical_center: 0, // 0-align top, 1-center vertical
		preload: 1,
		speed: '10' // animation speed
		};		

		// initialize SuperBGImage
		$('#thumbs').superbgimage().hide();
		$('#overlay').show();
	};
	
	$('#name').val(inputName);
	$('#name').click(function() { if (this.value == inputName) { this.value = '' } } );
	$('#name').blur(function() { if (this.value == '') { this.value = inputName; } });

	$('#nummer').val(inputNummer);
	$('#nummer').click(function() { if (this.value == inputNummer) { this.value = '' } } );
	$('#nummer').blur(function() { if (this.value == '') { this.value = inputNummer; } });

	$('#email').val(inputEmail);
	$('#email').click(function() { if (this.value == inputEmail) { this.value = '' } } );
	$('#email').blur(function() { if (this.value == '') { this.value = inputEmail; } });	

	var formOptions = { 
		target: '#success',
		success: function() { 
			$("#form").fadeOut(500);
			setTimeout("$('#success').fadeIn(500);", 500);
			setTimeout("$('#name').val(inputName);", 2000);
			setTimeout("$('#nummer').val(inputNummer);", 2000);
			setTimeout("$('#email').val(inputEmail);", 2000);
			setTimeout("$('#nachricht').val(inputNachricht);", 2000);
		}
	};

	$.validator.addMethod('checkInput', function(value, element, param) {
	        return this.optional(element) || value!=param;
	    });

	$("#sendme").validate({
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit(formOptions);
		},
		rules: { 
			name: { required: true, checkInput: inputName },
			nummer:{ required: true, checkInput: inputNummer },
			email:{ required: true, checkInput: inputEmail }
	        }, 
		messages: { 
			name: "Fehler! Bitte geben Sie Ihren Namen ein.", 
			nummer: "Fehler! Bitte geben Sie Ihre Telefonnummer an.",
			email: "Fehler! Bitte geben Sie eine gültige E-Mail Adresse an."
	        }
	});

});
