$(document).ready(function(){
	

	
	elem = $(window);
	toggleButton = $('#pop-contact td.toggle > a');
	toggleTable = $('#pop-contact > table');
	toggleY = elem.scrollLeft;
	bodyHeight = $(window).height();
	
	moveScroll = Math.round(bodyHeight/2) + document.documentElement.scrollTop;
	$("#pop-contact").css('top',moveScroll+'px');
	$(toggleTable).stop().animate({marginLeft:'-347px'},{queue:false,duration:600,easing:'easeOutExpo'});
	
	
	if($.support.noCloneEvent){ document.addEventListener('scroll',toggleScroller,false); }
	else{window.attachEvent("onscroll", toggleScroller);}
	
	$('#pop-contact td.toggle > a').click(function(){
		popSlide();
		return(false);
	});

	$('#box-pop a.close').click(function(){
		$('#box-pop').animate({opacity:'hide'},600);
		return(false);
	});

	$("#phone-send").submit(function() {
		
		var name = $("#phone-send input[name='name']").attr("value");
		var phone = $("#phone-send input[name='phone']").attr("value");
		var mess = $("#phone-send textarea[name='mess']").attr("value");
		
		if (name == '' || phone == '') {
			alert('Заполните пожалуйста имя и телефон.');
		} else {
		
			$.ajax({
				type: "POST",
				url: "/pop-contact/phone-send.php",
				data: "name=" + name + "&phone=" + phone + "&mess=" +mess,
				success: function(msg){
					$("#phone-send").html("Ваша заявка отправлена. Спасибо!");
				}
			});
		}
		return false;
		
	});
	
});

function toggleScroller() {
	moveScroll = Math.round(bodyHeight/2) + document.documentElement.scrollTop;
	$("#pop-contact").stop().animate({top:moveScroll+'px'},{queue:false,duration:1500,easing:'easeOutCirc'});
	if($(toggleTable).css('margin-left') != '-347px'){ 
		$(toggleTable).stop().animate({marginLeft:'-347px'},{queue:false,duration:1000,easing:'easeOutExpo'});
		setTimeout("$(toggleButton).find('img:eq(0)').show();$('#pop-contact table').css('height','107px').css('margin-top','0');$(toggleButton).find('img:eq(1)').hide();",1);
	}
}

function popSlide(){
//	if($(toggleTable).css('margin-left') != '-367px'){ 
	if(document.getElementById('pop-table').style.marginLeft != '-347px'){ 
	
		$(toggleTable).stop().animate({marginLeft:'-382px'},{queue:true,duration:500,easing:'swing'}).animate({marginLeft:'-347px'},{queue:true,duration:500,easing:'easeInCubic'});
		setTimeout("$(toggleButton).find('img:eq(0)').show();$('#pop-contact table').css('height','107px').css('margin-top','0');$(toggleButton).find('img:eq(1)').hide();",500);
	}else{
		$(toggleTable).stop().animate({marginLeft:'-382px'},{queue:true,duration:500,easing:'easeOutExpo'}).animate({marginLeft:0},{queue:true,duration:1000,easing:'swing'});
		setTimeout("$(toggleButton).find('img:eq(1)').show();$('#pop-contact table').css('height','446px').css('margin-top','0px');$(toggleButton).find('img:eq(0)').hide();",500);
	}
}



