//determine IE version					   
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
	var ieversion=new Number(RegExp.$1)
}

//function for contact form dropdown
function closeContact() {
	$("#contact").slideUp(800);
	$("#backgroundPopup").fadeOut(800);  
	$('.missing').hide();
	setTimeout('$("#social-links").css("z-index","15")', 900);
};
function contact() {
	if ($("#contact").is(":hidden")){
		$('#social-links').css('z-index','1');
		$("#contact").slideDown("slow");
		 $("#backgroundPopup").css({  
			 "opacity": "0.7"  
		});
		 //avoid darkening if IE 6 because of its stupid fucking z-index issues
		 if (ieversion!=6) {
			 $("#backgroundPopup").fadeIn("slow");  
		 }
	}
	else{closeContact();}
}

function validate(theForm) {
	var required = [];
	$('.missing').hide();
	
	$(theForm+' .required').each(function() {
		required[required.length] = $(this).attr('id');
	});
	//Validate required fields
	for (i=0;i<required.length;i++) {
		var input = $('#'+required[i]);
		if (input.val() == "") {
			input.addClass("needsfilled");
			input.prevAll('.missing').show();
		// Validate the e-mail.
		} else if (input.hasClass('email')) {
			if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(input.val())) {
				input.addClass("needsfilled");
				input.prevAll('.missing').show();
			}	
		} else {
			input.removeClass("needsfilled");
		}
	}
	
	fspam = $('#spamcheck').val().toLowerCase();
	if (fspam != "hot") {
		$('#spamcheck').addClass("needsfilled");
		$('#spamcheck').prevAll('.missing').show();
	}
	
	//if any inputs on the page have the class 'needsfilled' the form will not submit
	if ($(":input").hasClass("needsfilled")) {
		return false;
	}  else {
		return true;
	}
}


$(document).ready(function(){	
	$('#navigation li:has(ul)').hover(
		function() { $('ul', this).stop(true, true).slideDown(); },
		function() { $('ul', this).stop(true, true).hide(); }
	);
	 
	//run contact form when either contact me link is clicked
	$(".contact").click(function(){contact()});
	$('#backgroundPopup').click(function() {closeContact()});
	$('#contact input, #message').not('.submit').focus(function() {$(this).parent('td').css('background','#829a51');});
	$('#contact input, #message').blur(function() {$(this).parent('td').css('background','none');});
	$('#contactForm').submit(function() {
		if (validate('#contactForm')) {
			$(this).hide();
			$('#loader').fadeIn(400);
			$.post('/sendform.php',{
					recipient:$('#recipient').val(),
					name:$('#name').val(),
					company:$('#company').val(),
					email:$('#eemail').val(),
					phone:$('#phone').val(),
					message:$('#message').val(),
					spamcheck:fspam
				}, function(data){
					$('#loader').hide();
					if (data) {
						$('#output').html(data).show();
					} else {
						$('#output').html('We have just received your message.<br />Please give us a little time to respond. Thanks!').fadeIn(700);
						setTimeout('closeContact()', 4000);
					}
			});
			return false;
		} else {return false;}
	});

	//animation for same page links #
	$('a[href*=#]').each(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname
		&& this.hash.replace(/#/,'') ) {
		  var $targetId = $(this.hash), $targetAnchor = $('[name=' + this.hash.slice(1) +']');
		  var $target = $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
			if ($(this.hash).length) {
				$(this).click(function(event) {
					var targetOffset = $(this.hash).offset().top;
					var target = this.hash;
					event.preventDefault();			   
					$('html, body').animate({scrollTop: targetOffset}, 500);
					return false;
				});
			}
		}
	});
	
	// Clears any fields in the form when the user clicks on them
	$(":input").focus(function(){		
	   if ($(this).hasClass("needsfilled") ) {
			$(this).removeClass("needsfilled");
	   }
	});
	
	commentCount = 0;
	$('.comment-number').each(function() {
		commentCount++;
		$(this).html(commentCount);
	});
});