// JavaScript Document

	String.prototype.trim = function () {
		var str = this.replace(/^\s+/, '');
		for (var i = str.length - 1; i >= 0; i--) {
			if (/\S/.test(str.charAt(i))) { str = str.substring(0, i + 1); break; }
		} return str;
	}

	var prodBox;
	var suppInfo;
$(document).ready(function() {
	prodBox = $("#box");
	suppInfo = $("#supplementInfo");
	
	// jQuery stuff
	prodBox.lightBox().hover(function () { showSupp() }, function () { hideSupp() });
	suppInfo.lightBox().hover(function () { showSupp() }, function () { hideSupp() });
	
	$("div.youtube").toggle(function () {
		$(this).children(".video").show("fast");
		$(this).addClass("active");
	}, function () {
		$(this).children(".video").hide("fast");
		$(this).removeClass("active");
	});
	
	// Contact Form
	
		$("input.email").keyup( function () {
			if ($(this).val().length > 3) {
				if ($(this).val().length > 0) {
					if (checkemail($(this))) { isvalid($(this)); } else { invalid($(this)); }
				}  else { $(this).next("small").remove(); $(this).removeClass("invalid").removeClass("valid"); }
			} else {
				$(this).next("small").remove();
				$(this).removeClass("invalid");
			}
		}).blur(function () { $(this).keyup(); }).keyup();
		
		$("textarea").keyup( function () {
			if ($(this).val().length > 10) {
				if ($(this).val().length > 0) {
					isvalid($(this));
				}  else { $(this).next("small").remove(); $(this).removeClass("invalid").removeClass("valid"); }
			} else {
				$(this).next("small").remove();
				$(this).removeClass("invalid");
			}
		}).blur(function () { $(this).keyup(); }).keyup();

		
			$("#submitform").slideDown("fast").click ( function () {
			var valid = true;
			var reason = $("#reason");
			var email = $("#email");
			var comment = $("#comment");
			var button = $(this);
			var success = '<span class="success">Your message was successfully sent. <br /> Click this box to reset the form.</span>';
			var fail = '<span class="fail">An error occured, your message was not sent. Please try again or use a different method.</span>';
			try {
				if (comment.val().length == 0) { invalid(comment); valid = false;}
				if (email.val().length == 0 || !checkemail(email)) { invalid(email); valid = false; email.focus(); }
				
				if(valid == true) {
					$("#contactform div.cform").prev(".fail").remove();
					$(this).hide().after("<small class='ajaxwait'>sending message...</small>");
					$.post("mailme.php", {email: email.val(), reason: reason.val(), comment: comment.val()}, function(data){
						if (data == "success") {
							$("#contactform div.cform").slideUp("normal", function() {
								$("#contactform div.cform").prev(".fail").remove().end().before(success);
								$(".success").click( function () { 
									$("#contactform div.cform").slideDown("normal");
										email.val("").keyup().focus();
										comment.val("").keyup();
										button.next("small").remove().end().fadeIn("slow");
										$(this).remove();
									});
								});
						} else if (data == "fail") {
							$("#contactform div.cform").before(fail);
							button.next("small").remove().end().fadeIn("slow");
						} else {
							$("#contactform div.cform").before("<span class='fail'>" + data + "</span>");
							button.next("small").remove().end().fadeIn("slow");	
						}
					});
				}
			} catch (err) {
					$("#contactform div.cform").before(fail);
					button.next("small").remove().end().fadeIn("slow");
			}
			return false;	
		});

});


function showSupp() {
	prodBox.stop().animate({backgroundPosition: '0px 0px'}); 
}

function hideSupp() {
	prodBox.stop().animate({backgroundPosition: '-190px 0px'}); 
}


	function isvalid (element) {
		if (element.hasClass("invalid")) { element.next("small").remove(); }
		if (!element.hasClass("valid")) { element.after("<small class='ok'>OK</small>"); }
		element.addClass("valid").removeClass("invalid");
	}
	
	function invalid (element) { 
		if (element.hasClass("valid")) { element.next("small").remove(); }
		if (!element.hasClass("invalid")) { element.after("<small class='nogood'>ERROR</small>"); }		
		element.addClass("invalid").removeClass("valid"); 
	}

	//Advanced Email Check credit - JavaScript Kit (http://www.javascriptkit.com)
	function checkemail(element){
		var str= element.val();
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(str)) {
			return true;
		} else{ return false; }
	}

