$dr(document).ready(function(){
	
	$dr('.header-contact .contact-phone a').click(function(elem){
		elem.preventDefault();
		$dr('.region-callback').css('display', 'block');
		$dr('.region-callback .callback-container').slideDown('fast');
	});
	
	$dr('.region-callback .callback-container').each(function(){
		var curContainer = $dr(this);
		
		$dr(document).click(function(elem){
			if ( $dr(elem.target).closest('.callback-container').length == 0 && $dr(elem.target).closest('.header-contact .contact-phone a').length == 0 ) {
				$dr('.region-callback').css('display', 'none');
				$dr('.region-callback .callback-container').css('display', 'none');
			}
		});
		
		curContainer.find('form').submit(function(elem){
			elem.preventDefault();
			curContainer.find('.callback-process').css('display', 'block');
			curContainer.find('.callback-value').children('span').css('display', 'none');
			
			$dr.ajax({
				url: $dr(this).attr('action'),
				global: false,
				type: "post",
				data: $dr(this).serialize(),
				dataType: "json",
				success: function(json){
					curContainer.find('.callback-process').css('display', 'none');
					
					if (json.status == '1') {
						curContainer.find('.callback-submit').css('display', 'none');
						curContainer.find('.callback-result').css('display', 'block');
					} else {
						if (json.error.fio == '1') curContainer.find('input[name=fio]').closest('.callback-value').children('span').css('display', 'block');
						if (json.error.phone == '1') curContainer.find('input[name=phone]').closest('.callback-value').children('span').css('display', 'block');
					}
				}
			});
		});
		
		curContainer.find('.btn-red').click(function(elem){
			elem.preventDefault();
			$dr(this).closest('form').submit();
		});
	});








	$dr('.header-contact .contact-online a').click(function(elem){
		elem.preventDefault();
		$dr('.region-sendmail').css('display', 'block');
		$dr('.region-sendmail .callback-container').slideDown('fast');
	});
	
	$dr('.region-sendmail .callback-container').each(function(){
		var curContainer = $dr(this);
		
		$dr(document).click(function(elem){
			if ( $dr(elem.target).closest('.callback-container').length == 0 && $dr(elem.target).closest('.header-contact .contact-online a').length == 0 ) {
				$dr('.region-sendmail').css('display', 'none');
				$dr('.region-sendmail .callback-container').css('display', 'none');
			}
		});
		
		curContainer.find('form').submit(function(elem){
			elem.preventDefault();
			curContainer.find('.callback-process').css('display', 'block');
			curContainer.find('.callback-value').children('span').css('display', 'none');
			
			$dr.ajax({
				url: $dr(this).attr('action'),
				global: false,
				type: "post",
				data: $dr(this).serialize(),
				dataType: "json",
				success: function(json){
					curContainer.find('.callback-process').css('display', 'none');
					
					if (json.status == '1') {
						curContainer.find('.callback-submit').css('display', 'none');
						curContainer.find('.callback-result').css('display', 'block');
					} else {
						if (json.error.fio == '1') curContainer.find('input[name=fio]').closest('.callback-value').children('span').css('display', 'block');
						if (json.error.phone == '1') curContainer.find('input[name=phone]').closest('.callback-value').children('span').css('display', 'block');
						if (json.error.email == '1') curContainer.find('input[name=email]').closest('.callback-value').children('span').css('display', 'block');
						if (json.error.text == '1') curContainer.find('textarea[name=text]').closest('.callback-value').children('span').css('display', 'block');
					}
				}
			});
		});
		
		curContainer.find('.btn-red').click(function(elem){
			elem.preventDefault();
			$dr(this).closest('form').submit();
		});
	});
	
});