$(document).ready(function(){
	var trigger = 0;
	function CanselButton(){
		$('#contactMe').slideUp(function(){
			$('#contactMe').hide();
			$('#mask').fadeOut("slow");
			$('#contactMe').remove();
		});
	}
	function OKbutton(){
	fio			= $('#contactMeFio');
	firm		= $('#contactMeFirm');
	position	= $('#contactMePosition');
	phone		= $('#contactMePhone');
	time		= $('#contactMeTime');
	if ($.trim(fio.val()) == '') {
		alert('Пожалуйста, укажите ФИО');
		fio.focus();
		return;
	}
	if ($.trim(firm.val()) == '') {
		alert('Пожалуйста, укажите название организации');
		fio.focus();
		return;
	}
	if ($.trim(phone.val()) == '') {
		alert('Пожалуйста, укажите телефон для связи');
		phone.focus();
		return;
	}
	$('#contactMeCancel').attr('disabled', 'true');
	$('#contactMeOK').attr('disabled', 'true');
	$.ajax({
		type: 'POST',
		url: '/_service/form_contact_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="contactMeCancel" value="Закрыть"/>';
			$('#contactMe').html(html);
	//			CanselButton();
			$('#askMeCancel').select();
			$('#contactMeCancel').click(function(){
				CanselButton();
			});
			var button = document.getElementById('contactMeCancel');
			button.focus();
		}
	});
	
}
	$('.contactMe').click(function(event){
		if ($('#contactMe').length) {
			return;
		}		
		//-----------------------------------------------
		  //Cancel the link behavior  
//		         event.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="contactMe" style="position: absolute; background-color: white; border: 1px #ccc solid; padding: 10px; display: none;z-index:9001;">'+
				'<div class="ok-contactMe">Пожалуйста, оставьте контактные данные,<br/>'+
				'и наш менеджер свяжется с Вами для более<br/>'+
				'детальной консультации.'+
				'<table>'+
				'<tr><td>ФИО:</td><td><input type="text" id="contactMeFio"/></td></tr>'+
				'<tr><td>Название организации:</td><td><input type="text" id="contactMeFirm"/></td></tr>'+
				'<tr><td>Должность:</td><td><input type="text" id="contactMePosition"/></td></tr>'+
				'<tr><td>Телефон <b>(с кодом города)</b>:</td><td><input type="text" id="contactMePhone" value="(  )"/></td></tr>'+
				'<tr><td>Удобное время:</td><td><input type="text" id="contactMeTime"/></td></tr>'+
				'<tr><td colspan="2"><input type="button" value="OK" id="contactMeOK"/> <input type="button" value="Отмена" id="contactMeCancel"/></td></tr>'+
				'</table>'+
				'</div></div>';
		$(this).parent().append(html);
		$('#contactMe').css('left', event.pageX - $('#contactMe').outerWidth());
		$.getDocHeight = function(){
		    return Math.max(
		        $(document).height(),
		        $(window).height(),
		        /* For opera: */
		        document.documentElement.clientHeight
		    );
		};
		boxHeight=$('#contactMe').height();
		diff=$.getDocHeight() - event.pageY;
		if(diff<boxHeight){
			$('#contactMe').css('top', event.pageY-boxHeight-20);
		} else {
			$('#contactMe').css('top', event.pageY);
		}
		$('#contactMe').css('width', $('#contactMe').outerWidth());
		$('#contactMeCancel').click(function(){
			$('#contactMe').slideUp(function(){
				$('#contactMe').hide();
				$('#mask').fadeOut("slow");
				$('#contactMe').remove();
			});
		});
		$('#contactMeOK').click(function(){
			OKbutton();
		});
		$('.ok-contactMe').keypress(function(e)
				{
					if(e.which==13){
//						event.preventDefault();	
						OKbutton();
					}
				});
		
		$('#mask').click(function(){
			CanselButton();
		});
	$('#contactMe').slideDown();
	});
});

