$(function() {

	var Revealer = function(relativeParent,absoluteChild,speed) {
			var revealHeight = absoluteChild.height();
			relativeParent.height(revealHeight);
			relativeParent.addClass('hidden');
			this.toggleReveal = function() {
				if (!relativeParent.parent().find(':animated').length) {
					if (relativeParent.css('display')!='none' ) {
						relativeParent.animate({height:0}, speed, function() {
							relativeParent.hide();
						});
					} else {
						relativeParent.height(0).show().animate({height: revealHeight}, speed);
					}
				}
			};
		};
		
		$('#contact').after($('#contact-form'));
		
		var contactForm = new Revealer(
			$('#contact-form .flow'),				// the relatively positioned container
			$('#contact-form .flow .contents'),		// the absolutely positioned child
			300										// the speed of the animation
		);
		
		$('.contact-link').live('click',function(){
			$('html, body').animate({scrollTop:0}, 'fast');
			contactForm.toggleReveal();
			return false;
		});

		$('#contactSend').click(function() {
			var name = $('#name').val();
			var email = $('#email').val();
			var email2 = $('#email2').val();
			var message = $('#message').val();
			var company = $('#company').val();
			var phone = $('#phone').val();
			if (name == "" || message == "") {
				alert('Please fill in all fields!');
			} else if (email.indexOf('@') < 1) {
				alert('Please enter a valid email!');
			} else if (email != email2) {
				alert('The two emails must be the same!');
			} else {
				$('#contactForm').hide();
				$('#contactLoader').show();
				$.post('ajaxcontact.php', { name: name, email: email, message: message, company: company, phone: phone }, function(data) {
					if (data == 'OK') {
						$('#contactLoader').hide();
						$('#contactSent').show();
					} else {
						alert('Error!');
						$('#contactLoader').hide();
						$('#contactForm').show();
					}
				});
			}
		});

});