$(document).ready(function(){ 

// select current section within viewport and add navigation highlight
$(window).scroll(function(){
	$("a.active").removeClass("active");
	inviewport = $('section:in-viewport').attr('class');
    $('nav ul li a[href$="'+inviewport+'"]').addClass("active");
});

	// slight tweak for iPad for position fixed in landscape
	if(navigator.platform == 'iPad') {
	     $("body").addClass("ipad");
	};
	
	//remove fixed header for short viewports
	var vHeight = $(window).height();
	if(vHeight <= 650) {
	     $("body").addClass("short");
	};
	
	
	$(function() {
		$('header a, section.welcome a, a.backtotop').bind('click',function(event){
			var bwidth = $(window).width(); // detect current browser width
			$('nav ul li a').removeClass(); // remove all classes from nav
			//$(this).addClass("active"); // add active class for a clicked
			var $anchor = $(this);
			if ((bwidth < 962) || ($("body").hasClass('ipad'))) {
				pxOffset = 0; // change offset to account for NO position fixed
			} 
			else {
				pxOffset = 160;
			}
			$('html, body').stop().animate({
				scrollTop: $($anchor.attr('href')).offset().top-pxOffset
			}, 1500,'easeInOutExpo'); 
			event.preventDefault();
		});
	});
	
	
	
	// polyfill placeholder text
	$('[placeholder]').focus(function() {
	  var input = $(this);
	  if (input.val() == input.attr('placeholder')) {
	    input.val('');
	    input.removeClass('placeholder');
	  }
	}).blur(function() {
	  var input = $(this);
	  if (input.val() == '' || input.val() == input.attr('placeholder')) {
	    input.addClass('placeholder');
	    input.val(input.attr('placeholder'));
	  }
	}).blur().parents('form').submit(function() {
	  $(this).find('[placeholder]').each(function() {
	    var input = $(this);
	    if (input.val() == input.attr('placeholder')) {
	      input.val('');
	    }
	  })
	});


// expand textarea
(function($)
{
	// This script was written by Steve Fenton
	// http://www.stevefenton.co.uk/Content/Jquery-Textarea-Expander/
	// Feel free to use this jQuery Plugin
	// Version: 3.0.2
    // Contributions by: 

	$.fn.inputexpander = function (settings) {
	
		var config = {
			classmodifier: "tae"
		};
		
		if (settings) {
			$.extend(config, settings);
		}
		
		function CheckContent(element) {
			if (element.clientHeight < element.scrollHeight){
				var $elem = $(element);
				var height = $elem.height() + 5;
				$elem.height(height);
				CheckContent(element);
			}
		}

		return this.each(function () {
			$(this).addClass(config.classmodifier).css({ overflow: "hidden" });
			$(this).bind("keyup", function () {
				CheckContent(this);
			});
		});
	};
})(jQuery);
	//init
	$("textarea").inputexpander();
	
	
	
	// validate form and ajax post it...
	/*
	Created 09/27/09										
	Questions/Comments: jorenrapini@gmail.com						
	COPYRIGHT NOTICE		
	Copyright 2009 Joren Rapini
	*/
	
		
	
		// Place ID's of all required fields here.
		required = ["name", "email", "phone", "message"];
		// If using an ID other than #email or #error then replace it here
		email = $("#email");
		errornotice = $("#error");
		// The text to show up within a field when it is incorrect
		emptyerror = "Please fill out this field.";
		emailerror = "Please enter a valid e-mail.";
	
		$(".contact-form").submit(function(){	
			//Validate required fields
			for (i=0;i<required.length;i++) {
				var input = $('#'+required[i]);
				if ((input.val() == "") || (input.val() == emptyerror)) {
					input.addClass("needsfilled");
					input.val(emptyerror);
					errornotice.fadeIn(750);
				} else {
					input.removeClass("needsfilled");
				}
			}
			// Validate the e-mail.
			if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
				email.addClass("needsfilled");
				email.val(emailerror);
			}
	
			//if any inputs on the page have the class 'needsfilled' the form will not submit
			if ($(":input").hasClass("needsfilled")) {
				return false;
			} else {
				ajaxMe();
				errornotice.hide();
				return false;
			}
		});
		
		// Clears any fields in the form when the user clicks on them
		$(":input").focus(function(){		
		   if ($(this).hasClass("needsfilled") ) {
				$(this).val("");
				$(this).removeClass("needsfilled");
		   }
		});
	

	

	
	  function ajaxMe() {

			
		  var name = $("input#name").val();
		  var email2 = $("input#email").val();
		  var phone = $("input#phone").val();
		  var where = $("input#where").val();
		  var message = $("textarea#message").val();
		  
		  
			
			var dataString = 'name='+ name + '&email=' + email2 + '&phone=' + phone + '&where=' + where + '&message=' + message;
			//alert (dataString);return false;
			
			$.ajax({
	      type: "POST",
	      url: "contact.php",
	      data: dataString,
	      success: function() {
	      
	        
	      }
	     });
	    //return false;
	    $('#contact-hold').hide();
		$('#contact-hold').fadeIn(1000);
	$('#contact-hold').html("<div id='response'></div>");
	
	$('#response').html("<h2>Thank you, contact form submitted.</h2>")
	.append("<p>We will be in touch soon.</p>")
	
	//.hide()
	.fadeIn(1500, function() {
	  //$('#response').append("<img id='checkmark' src='images/check.png' />");
	});
	
	}
	
	
	
});



