// This is done after page load.
$(document).ready(function() {

	// Show layer by clicing the trigger.
	$(".show_product_stock_email").click(function() {
		$(this).siblings(".request_product_stock_email_wrapper").fadeIn().find(".email_address").focus();
		return false;
	});
	
	// Close layer by clicking close link.
	$(".request_product_stock_email_wrapper a.layer_close").click(function() {
		$(this).parents(".request_product_stock_email_wrapper").fadeOut();
		return false;
	});

	// Send request by clicking the button.
	$(".request_product_stock_email_wrapper button.send_request").click(function() {
		var thisButton = $(this);
		var wrapper = $(this).parents(".request_product_stock_email_wrapper");
		var email = wrapper.find("input.email_address").val().trim();
		if (email != "")
		{
			thisButton.attr("disabled",true);
			var ajax_loader = $("<span class='ajax_loader'/>");
			wrapper.find(".request_result_messsage").empty().before(ajax_loader);
			
			var product_id = wrapper.find("input.product_id").val();
			var params = {
				product_id: product_id,
				email: email
			};
			$.post(AJAX_URL + "/product_stock_email_ajax.php", params, function(data) {
				thisButton.attr("disabled",false);
				//wrapper.find(".ajax_loader").remove();
				ajax_loader.remove();
				wrapper.find(".request_result_messsage").text(data.message);
			}, "json");
		}
		return false;
	});
	
	// Send request also on ENTER keypress.
	$(".request_product_stock_email_wrapper input.email_address").keypress(function(e) {
		if (e.keyCode == 13)
		{
			var button = $(this).parents(".request_product_stock_email_wrapper").find("button.send_request");
			if (!button.attr("disabled"))
			{
				button.click();
			}
			return false;
		}
	});


});

