$(document).ready(function() { 
		bindFocusEvents();
		$("#errors").hide();
		$("#q").focus(function() { if ($(this).val() == "") $(this).val(""); });
		$("#q").blur(function() { if ($(this).val() == "") $(this).val("Product Search"); });
		
		$("#signup > #signupLink").click(function() {
			var e = $(this).parent();
			//alert(e.children("ul").css("visibility"));
			var uls = e.children("ul");
			if (uls.css("visibility") == "hidden" || $("#thx").is(":hidden")) {
				uls.hide().fadeIn("slow").css("visibility","visible");
				$("#thx").parent().show();
				$("#thx").show().html("<span style=\"float: left;\">E-mail Address:&nbsp;</span>" +
						"<input type=\"text\" class=\"searchBox\" name=\"newsletter_email\" id=\"newsletter_email\" maxlength=\"255\">" + 
						"<input type=\"button\" class=\"searchBtn\" title=\"Subscribe\" alt=\"Subscribe\" onClick=\"subscribe();\" />");
				bindFocusEvents();
				$("#signup").addClass("shown");
			} else {
				$("#signup").removeClass("shown");
				uls.fadeOut("slow",function() { $(this).css("display","block").css("visibility","hidden"); });
			}
			return false;
		});
	});

	function bindFocusEvents() {
		$("#newsletter_email").each(function() {
			$(this).val("E-mail Address");
			$(this).focus(txtfocusin);
			$(this).blur(txtfocusout);
		});
	}

	function subscribe() {
		// don't forget to validate email
		var email = $("#newsletter_email").val();
		
		// do ajax calls to website to add this person to the e-mail subscriber list.
		$.ajax({
			url: "ajaxMarketing/",
			type: "GET",
			data: "action=subscribe&email="+email,
			async: true,
			success: function(data) {
				if (data == "1" || data == "0") {
					$("#thx").text("Thanks for subscribing!");
					setTimeout(function() { 
						$("#thx").fadeOut("slow",function() { 
							$("#signup").removeClass("shown"); 
							$("#signup").removeClass("thanks"); 
							$("#thx").parent().hide();
						}); 
					},1000);
				} /*else if (data == "0") { 
					alert("That e-mail is already subscribed.");
					$("#newsletter_email").val("");
					$("#newsletter_email").get(0).focus();
				}*/ else if (data == "-1") {
					$("#errors").text("E-mail not valid, please try again.");
					onSubscribeError();
				} else if (data == "500") { 
					$("#errors").text("Service unavailable, please try again.");
					onSubscribeError();
				} else {
					// for dev use only
					//alert("DEBUG: " + data);
				}
			}
		});
	}

	function onSubscribeError() {
		$("#errors").fadeIn(750,function() { 
			setTimeout(function() { 
				$("#errors").fadeOut(750,function() { 
					$("#newsletter_email").each(function() { 
						$(this).val(""); 
						$(this).get(0).focus();
					});
				});
			},1250);
		});
	}

	function txtfocusin() {
		if ($("#newsletter_email").val() == "E-mail Address") {
			$("#newsletter_email").val("");
		}
	}

	function txtfocusout() {
		if ($("#newsletter_email").val() == "") {
			$("#newsletter_email").val("E-mail Address");
		}
	}
