$(document).ready(function(){
	$('.callMe').click(function(event){

		if ($('#callMe').length) {
			return;
		}
		//-----------------------------------------------
		  //Cancel the link behavior  
		       //  e.preventDefault();  
		         //Get the A tag  
		         var id = $(this).attr('href');  
		       
		         //Get the screen height and width  
		         var maskHeight = $(document).height();  
		         var maskWidth = $(window).width();  
		       
		         //Set height and width to mask to fill up the whole screen  
		         $('#mask').css({'width':maskWidth,'height':maskHeight});  
		           
		         //transition effect       
		         $('#mask').fadeIn(1000);      
		         $('#mask').fadeTo("slow",0.8);    
		       
		//-----------------------------------------------
		html =	'<div id="callMe" style="position: absolute; background-color: #fff; border: 1px #ccc solid; padding: 10px; display: none;z-index:9102 !important;">'+
				'Пожалуйста, оставьте контактные данные,<br/>'+
				'и наш менеджер свяжется с Вами для более<br/>'+
				'детальной консультации.'+
				'<table>'+
				'<tr><td>ФИО:</td><td><input type="text" id="callMeFio"/></td></tr>'+
				'<tr><td>Название организации:</td><td><input type="text" id="callMeFirm"/></td></tr>'+
				'<tr><td>Должность:</td><td><input type="text" id="callMePosition"/></td></tr>'+
				'<tr><td>Телефон:</td><td><input type="text" id="callMePhone"/></td></tr>'+
				'<tr><td>Удобное время:</td><td><input type="text" id="callMeTime"/></td></tr>'+
				'<tr><td colspan="2"><input type="button" value="OK" id="callMeOK"/> <input type="button" value="Отмена" id="callMeCancel"/></td></tr>'+
				'</table>'+
				'</div>';		
		$(this).parent().append(html);
		$('#callMe').css('left', event.pageX - $('#callMe').outerWidth());
		$.getDocHeight = function(){
		    return Math.max(
		        $(document).height(),
		        $(window).height(),
		        /* For opera: */
		        document.documentElement.clientHeight
		    );
		};
		boxHeight=$('#callMe').height();
		diff=$.getDocHeight() - event.pageY;
		if(diff<boxHeight){
			$('#callMe').css('top', event.pageY-boxHeight-20);
		} else {
			$('#callMe').css('top', event.pageY);
		}
		$('#callMe').css('width', $('#callMe').outerWidth());
		$('#callMeCancel').click(function(){
			$('#callMe').slideUp(function(){
				$('#callMe').hide();
				$('#mask').fadeOut("slow");
				$('#callMe').remove();
			});
		});
		$('#callMeOK').click(function(){
			fio			= $('#callMeFio');
			firm		= $('#callMeFirm');
			position	= $('#callMePosition');
			phone		= $('#callMePhone');
			time		= $('#callMeTime');
			if ($.trim(fio.val()) == '') {
				alert('Пожалуйста, укажите ФИО');
				fio.focus();
				return;
			}
			if ($.trim(firm.val()) == '') {
				alert('Пожалуйста, укажите название организации');
				fio.focus();
				return;
			}
			if ($.trim(phone.val()) == '') {
				alert('Пожалуйста, укажите телефон для связи');
				phone.focus();
				return;
			}
			$('#callMeCancel').attr('disabled', 'true');
			$('#callMeOK').attr('disabled', 'true');
			$.ajax({
				type: 'POST',
				url: '/_service/form_call_me.php',
				data: {
					fio: fio.val(),
					firm: firm.val(),
					position: position.val(),
					phone: phone.val(),
					time: time.val(),
					from: location.href
				},
				complete: function(result){
					var message=result.responseText;
					var pattern=/OK/g;
					if (message.match(pattern)) {
					//if (result.responseText == 'OK') {
						html =	'Спасибо за проявленный интерес,<br/>'+
								'мы с Вами свяжемся в указанное Вами время.<br/>';
					} else {
						html =	'Заявка не была отправлена, попробуйте позже.<br/>';
					}
					// {{{ debug
					// html += '<pre>'+result.responseText+'</pre>';
					// }}}
					html += '<input type="button" id="callMeCancel" value="OK"/>';
					$('#callMe').html(html);
					$('#callMeCancel').click(function(){
						$('#callMe').slideUp(function(){
							$('#callMe').hide();
							$('#mask').fadeOut("slow");
							$('#callMe').remove();
						});
					});
				}
			});
		});
		$('#callMe').slideDown();
	});
});
