$(document).ready(function() {
	// Function for scrolling to a specific element
	function scrollTo(elem) {
		var selectedPosX = 0;
		var selectedPosY = 0;

		while(elem != null) {
			selectedPosX += elem.offsetLeft;
			selectedPosY += elem.offsetTop;
			elem = elem.offsetParent;
		}
		window.scrollTo(selectedPosX, selectedPosY);
	}

	// Fallback for placeholder attrbute
	(function checkPlaceholderSuppoert() {
		jQuery.support.placeholder = false;
		var test = document.createElement('input');
		if('placeholder' in test) jQuery.support.placeholder = true;
		
		var phColor = '#909090';

		if(!$.support.placeholder) {
			$('[placeholder]').each(function() {
				var placeholder = $(this).attr('placeholder');
				$(this).val(placeholder).css('color', phColor);
			});

			$('[placeholder]').click(function() {
				var placeholder = $(this).attr('placeholder');

				if( $(this).val() === placeholder ) {
					$(this).val('').css('color', 'black');
				}
			});

			$('[placeholder]').blur(function() {
				var placeholder = $(this).attr('placeholder');

				if( $(this).val() === '' ) {
					$(this).val(placeholder).css('color', phColor);
				}
			});
		}
	})();
	
	
	// Contact form validation
	$('#contact form').submit(function() {
		var $name = $('#contactName'),
			$email = $('#contactEmail'),
			$phone = $('#contactPhone'),
			$msg = $('#contactMessage'),
			nameVal = $name.val(),
			emailVal = $email.val(),
			phoneVal = $phone.val(),
			msgVal = $msg.val(),
			hasErrors,
			errorName = '',
			errorEmail = '',
			errorMsg = '',
			emailTest = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		
		// $('<p></p>').addClass('formError').text('Ditt mail kunde inte skickas').prependTo('#contact').hide();
		$('.formError, .invalidEm').remove();
		$('#contact > h2:first').before('<p style="display: none;" class="formError">Ditt mail kunde inte skickas</p>');
		
		$name.before('<em style="display: none;" class="invalidEm invalidName"></em>');
		$email.before('<em style="display: none;" class="invalidEm invalidEmail"></em>');
		$msg.before('<em style="display: none;" class="invalidEm invalidMsg"></em>');
			
		if(nameVal === '' || nameVal === $name.attr('placeholder')) { hasErrors = true; errorName  = 'Skriv ett giltigt namn'; }
		if(!emailTest.test(emailVal) || emailVal === $email.attr('placeholder')) { hasErrors = true; errorEmail = 'Skriv en giltig e-post'; }
		if(msgVal === '' || msgVal === $msg.attr('placeholder')) { hasErrors = true; errorMsg   = 'Skriv ett giltigt meddelande'; }
		
		if(hasErrors) {
			$('.formError').show();
			
			if(errorName != '') {
				$name.addClass('invalidInput');
				$('.invalidEm.invalidName').text(errorName).show();
			}
			else { $name.removeClass('invalidInput'); }
			
			if(errorEmail != '') {
				$email.addClass('invalidInput');
				$('.invalidEm.invalidEmail').text(errorEmail).show();
			}
			else { $email.removeClass('invalidInput'); }
			
			if(errorMsg != '') {
				$msg.addClass('invalidInput').css('height', '40px');
				$('.invalidEm.invalidMsg').text(errorMsg).show();
			}
			else { $msg.removeClass('invalidInput'); }
			
			return false;
		}
		else {
			$msg.css('height', '100px');
			$('.invalidInput').removeClass('invalidInput');
		}
	});
	
	// Footer contact form validation
	$('#footer form').submit(function() {
		var $fName = $('#footerContactName'),
			$fEmail = $('#footerContactEmail'),
			$fMsg = $('#footerContactMessage'),
			fNameVal = $fName.val(),
			fEmailVal = $fEmail.val(),
			fMsgVal = $fMsg.val(),
			fHasErrors,
			fErrorName = '',
			fErrorEmail = '',
			fErrorMsg = '',
			fEmailTest = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			
		if(fNameVal === '' || fNameVal === $fName.attr('placeholder')) {
			fHasErrors = true;
			$fName.addClass('invalidFooterInput');
		}
		else { $fName.removeClass('invalidFooterInput'); }
		
		if(!fEmailTest.test(fEmailVal) || fEmailVal === $fEmail.attr('placeholder')) {
			fHasErrors = true;
			$fEmail.addClass('invalidFooterInput');
		}
		else { $fEmail.removeClass('invalidFooterInput'); }
		
		
		if(fMsgVal === '' || fMsgVal === $fMsg.attr('placeholder')) {
			fHasErrors = true;
			$fMsg.addClass('invalidFooterInput');
		}
		else { $fMsg.removeClass('invalidFooterInput'); }
		
		if(fHasErrors) { return false; }
	});
	
});
