$(document).ready(function(){
	$('#dialog').dialog({
		buttons: {
			"Ok": function() { 
				$(this).dialog("close");
			} 
		},
		resizable: false,
		title: 'Sign up for offers',
		autoOpen: false,
		modal: true
	});
	
	var offersEmptyText = 'Sign up for offers now!';
	$('.newsletter-field').blur(function(e){
		if ($(this).val() == '')
			$(this).val(offersEmptyText);
	});
	$('.newsletter-field').focus(function(e){
		if ($(this).val() == offersEmptyText)
			$(this).val('');
	});
	
	$('.newsletter-btn').click(function(){
		var A = $('.newsletter-field').val();
		if (A == '' || A == offersEmptyText)
			return false;
	    $.ajax({
	        url: baseUrl+"/newsletter",
	        cache: false,
	        dataType: "json",
			type: 'POST',
			data: { email: A},
	        error: function(D, B, C){
	            $('#dialog p').html("Unexpected error");
				$('#dialog').dialog('open');
	        },
	        success: function(C, B){
	            if (C.success == true) {
					$('.newsletter-field').val(offersEmptyText);
	                $('#dialog p').html("You have successfuly signed up for offers.");
					$('#dialog').dialog('open');
	            }
	            else {
					$("#add-to-cart").html('add to cart');
	                $('#dialog p').html(C.msg);
					$('#dialog').dialog('open');
	            }
	        }
	    })
	    return false
	});
});