var canSendMail = true;

var successText = "Письмо успешно отправлено.";
var errorText = "Письмо не отправлено. Попробуйте еще раз.";
var informationText = "Письмо отправляется.";

var htmlContentSuccess  = '<table cellpadding="0" cellspacing="0" border="0" id="successMessage" style="opacity:0;" id="successTable">';

    htmlContentSuccess += '<tr>';
	
    htmlContentSuccess += '<td id="success_left_top">';               
    htmlContentSuccess += '</td>';                 
    htmlContentSuccess += '<td id="success_top">';                
    htmlContentSuccess += '</td>';                 
	htmlContentSuccess += '<td id="success_right_top">';
	htmlContentSuccess += '</td>';
	
	htmlContentSuccess += '</tr>';
	htmlContentSuccess += '<tr>';
	
	htmlContentSuccess += '<td id="success_left">';
	htmlContentSuccess += '</td>';
	htmlContentSuccess += '<td id="success_center" valign="top">' + successText;
	htmlContentSuccess += '</td>';
	htmlContentSuccess += '<td id="success_right">';
	htmlContentSuccess += '</td>';
                       
    htmlContentSuccess += '</tr>';
	htmlContentSuccess += '<tr>';                
                       
	htmlContentSuccess += '<td id="success_left_bottom">';               
    htmlContentSuccess += '</td>';                 
    htmlContentSuccess += '<td id="success_bottom">';                
    htmlContentSuccess += '</td>';                 
	htmlContentSuccess += '<td id="success_right_bottom">';
	htmlContentSuccess += '</td>';
                       
    htmlContentSuccess += '</tr></table>';                   
                       
var htmlContentError  = '<table cellpadding="0" cellspacing="0" border="0" id="errorMessage" style="opacity:0;" id="errorTable">';

    htmlContentError += '<tr>';
	
    htmlContentError += '<td id="error_left_top">';               
    htmlContentError += '</td>';                 
    htmlContentError += '<td id="error_top">';                
    htmlContentError += '</td>';                 
	htmlContentError += '<td id="error_right_top">';
	htmlContentError += '</td>';
	
	htmlContentError += '</tr>';
	htmlContentError += '<tr>';
	
	htmlContentError += '<td id="error_left">';
	htmlContentError += '</td>';
	htmlContentError += '<td id="error_center" valign="top">' + errorText;
	htmlContentError += '</td>';
	htmlContentError += '<td id="error_right">';
	htmlContentError += '</td>';
                       
    htmlContentError += '</tr>';
	htmlContentError += '<tr>';                
                       
	htmlContentError += '<td id="error_left_bottom">';               
    htmlContentError += '</td>';                 
    htmlContentError += '<td id="error_bottom">';                
    htmlContentError += '</td>';                 
	htmlContentError += '<td id="error_right_bottom">';
	htmlContentError += '</td>';
                       
    htmlContentError += '</tr></table>';  

var htmlContentInformation  = '<table cellpadding="0" cellspacing="0" border="0" id="informationMessage" style="opacity:0;" id="informationTable">';

    htmlContentInformation += '<tr>';
	
    htmlContentInformation += '<td id="information_left_top">';               
    htmlContentInformation += '</td>';                 
    htmlContentInformation += '<td id="information_top">';                
    htmlContentInformation += '</td>';                 
	htmlContentInformation += '<td id="information_right_top">';
	htmlContentInformation += '</td>';
	
	htmlContentInformation += '</tr>';
	htmlContentInformation += '<tr>';
	
	htmlContentInformation += '<td id="information_left">';
	htmlContentInformation += '</td>';
	htmlContentInformation += '<td id="information_center" valign="top">' + informationText;
	htmlContentInformation += '</td>';
	htmlContentInformation += '<td id="information_right">';
	htmlContentInformation += '</td>';
                       
    htmlContentInformation += '</tr>';
	htmlContentInformation += '<tr>';                
                       
	htmlContentInformation += '<td id="information_left_bottom">';               
    htmlContentInformation += '</td>';                 
    htmlContentInformation += '<td id="information_bottom">';                
    htmlContentInformation += '</td>';                 
	htmlContentInformation += '<td id="information_right_bottom">';
	htmlContentInformation += '</td>';
                       
    htmlContentInformation += '</tr></table>';   	
                       
function _hover(id, dx)
     {
	    if (canSendMail) $(id).css('backgroundPosition', '0px ' + dx + 'px');
	 }
	 
function nextSuccesMessage()
	{	
		$("#systemMessage").html(htmlContentSuccess);
		$("#successMessage").animate({opacity : 1}, 1000)
							.delay(5000)
							.animate({opacity : 0}, 1000);
	}
	
function nextErrorMessage(_time)
	{	
		$("#systemMessage").html(htmlContentError);
		$("#errorMessage").animate({opacity : 1}, 1000)
						  .delay(5000)
						  .animate({opacity : 0}, 1000);
	}
 
	 
function send_message(){
	
    if ( canSendMail )
	{
		$.ajax({url  : "feedback_ad.php",
				type : "POST",
				data : ({ 
						message:	$("#message").val(), 
						mail:		$("#mail").val()
						}),
				beforeSend : function(val1)
								{
									canSendMail = false;
									$("#btn_submit").css('backgroundPosition', '0px -129px');
									$("#submit_span").text("Отправка...");
									$("#systemMessage").html(htmlContentInformation);
									$("#informationMessage").animate({opacity : 1}, 500);
								},
				error : function(val1, val2, val3)
							{
								canSendMail = true;
								$("#informationMessage").animate({opacity : 0}, 500);
								window.setTimeout("nextErrorMessage()", 1000);
						
								$("#btn_submit").css('backgroundPosition', '0px 0px');
								$("#submit_span").text("Отправить");
							},
				success : function(data2)
							{
								canSendMail = true;
								
										
								$("#informationMessage").animate({opacity : 0}, 500);
								
								if (data2 == '1')
									{
										window.setTimeout("nextSuccesMessage()", 1000);	
							
										$("#btn_submit").css('backgroundPosition', '0px 0px');
										$("#submit_span").text("Отправить");
										$("#message").val("");
									}
								
								else
									{
										window.setTimeout("nextErrorMessage()", 1000);
						
										$("#btn_submit").css('backgroundPosition', '0px 0px');
										$("#submit_span").text("Отправить");
									}
							}
			});
	}
}


$(document).ready(function() {

	
	$("#type").change(function()
						{
							if ( $(this).val() == 'sms' )
								{ 
									$('#phone_wrap').show(); 
								}
							else
								{ 
									$('#phone_wrap').hide(); 
								}
						});
	
	$("#btn_submit").mouseover(function ()
			{
				_hover(this, '-43');
			});
	
	 $("#btn_submit").mouseout(function ()
			{
				_hover(this, '0');
			});
	
	$("#btn_submit").mousedown(function ()
			{
				_hover(this, '-86');
			});	
			
	$("#btn_submit").mouseup(function ()
			{
				_hover(this, '0');
			});
			
	$("#btn_submit").click(function ()
			{
				send_message();
			});
});

