
jQuery.fn.center = function() {
	var w = $(window);
	this.css("position","absolute");
	this.css("top",(w.height()-this.height())/2+w.scrollTop() + "px");
	this.css("left",(w.width()-this.width())/2+w.scrollLeft() + "px");
	return this;
}


function send_mail() {
	
	$('input#but').attr('disabled', 'disabled').attr('value', 'Отправка...');
		
	var url = '/ajax?action=send_mail&' + $('#form_feedback').serialize() + '&r=' + Math.round(Math.random() * 6);

	$.getJSON(url, 
		function(data) {
			
			$('input#but').attr('disabled', '').attr('value', 'Отправить');
		
        	if (data.error) {
        		var pos = $('input#' + data.field).offset();
        		
        		if ( $('#err').length == 0 ) $(document.body).append('<div id="err"></div>');
        		else $('#err').show().stopTime('hi');	
        		
        		$('#err').css({top: (pos.top + $('input#' + data.field).height() + 6 + 'px'), left: (pos.left + 'px')})
        		$('#err').html(data.error).oneTime('2s', 'hi', function () { $('#err').hide(400);});
       		
        		return;
        	}
        	
        	if ( data.html ) {
				
				createBackgroundDiv();
				        	
				if ( $('#lbl_message').length == 0 ) $(document.body).append('<div id="lbl_message"></div>');
				$('#lbl_message').show();
				$("#lbl_message").html(data.html).show();
				$('#lbl_message').center();
				
				document.getElementById('form_feedback').reset();
				
        	}	
        	
    	});
	return false;
}

function CloseMessage() {
	BackgroundDivClose();
	$('#lbl_message').remove();
}


function BackgroundDivClose() {
	$('#lbl_background_div').remove();
}

function createBackgroundDiv() {
	if ( $('#lbl_background_div').length == 0 ) $(document.body).append('<div id="lbl_background_div"></div>');
	$('#lbl_background_div').show();
	$('#lbl_background_div').css({width: $(document.body).width() + 'px', height: $(document.body).height() + 'px', top: '0px', left: '0px'});
}


