var now_pid=0;
var show_ajax_runer=0;
$(document).ready(function(){

	//Возвращает высоту прокрутки
	function getBodyScrollTop(){  
    	return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);  
	}
	
	//Ajax Runer
	$("#ajax_loader").ajaxStart(function(){
		ajax_runer=1;
		if(show_ajax_runer==1){
			$(this).css("left",click_l+$(this).width()/2);
			$(this).css("top",click_t-$(this).height()-5);
   			$(this).show();
   		}
	}).ajaxStop(function(){
   		$(this).hide();
   		ajax_runer=0;
   		show_ajax_runer=0;
	}).mousemove(function(e){
		$(this).css("left",e.pageX-19);
		$(this).css("top",e.pageY-$(this).height()-22);
	});
	
	
	function close_win(){
		$("#new_window").css("display","none");
		$("#call_me").css("display","none");
	}
	
	$("a.close_window, a.close_gal").live("click",function(){
		close_win();
		return false;
	});
	///////////////////////////////
	
	
	$("a.map").click(function(e){
		var lnk=$(this).attr("href");
		
		$.post(lnk, function(html){
		
		$("#new_window .m").css("width", 609);
		$("#new_window .m").css("height", 468);
		$("#new_window .m").html(html);
		
		//$("#new_window").css("width","646px");
		$("#new_window").css("left",$(window).width()/2-$("#new_window").width()/2);
		
		
		
		$("#new_window").css("left",$(window).width()/2-$("#new_window").width()/2);
		$("#new_window").css("top",$(window).height()/2-$("#new_window").height()/2+getBodyScrollTop());
		$("#new_window").css("display","block");
			
		});
	
		return false;
	});
	
	
	$("a.get_tarifi").click(function(e){
		var lnk=$(this).attr("href");
		
		$.post(lnk, function(html){
		
		$("#new_window .m").css("width", 609);
		$("#new_window .m").css("height", 520);
		$("#new_window .m").html(html);
		
		//$("#new_window").css("width","646px");
		$("#new_window").css("left",$(window).width()/2-$("#new_window").width()/2);
		
		
		
		$("#new_window").css("left",$(window).width()/2-$("#new_window").width()/2);
		$("#new_window").css("top",$(window).height()/2-$("#new_window").height()/2+getBodyScrollTop());
		$("#new_window").css("display","block");
			
		});
	
		return false;
	});
	
	
	
	
	//Обратный звонок
	$(".call").click(function(){
		$("#call_me").css("left",$(window).width()/2-$("#call_me").width()/2);
		$("#call_me").css("top",$(window).height()/2-$("#call_me").height()/2+getBodyScrollTop());
		$("#call_me").css("display","block");
		
		return false;
	});
	
	
	
	$('#call_me').ajaxForm({
	beforeSubmit: check_call_me,
	success: function(data) {
		//alert(data);
		if (/ok/.test(data)) {
			$("#call_me .alert").replaceWith("");
			
			$('#call_me').slideUp("fast");	
			$('#call_me_output').html("Ваша заявка отправлена");	
			$('#call_me_output').addClass("green");
			$('#call_me_output').slideDown("slow");
		}
	}
	});
	
	function check_call_me(a,f,o){
		var ret=true;
		o.dataType = "html";
		
		$("#call_me .alert").replaceWith("");
				
		var errors= new Array();
	
		if($("#call_me input[name='telephone']").length>0 && ($("#call_me input[name='telephone']").val()!='' || $("#call_me input[name='telephone']").hasClass("ob"))){
			var temp=$("#call_me input[name='telephone']").val();
			var temp_mask = /^[\s\-\(\)0-9]{5,15}$/i
			var ar_temp= new Array();
			ar_temp["pole"]="telephone";
			ar_temp["alert"]="Вы не верно ввели номер телефона!";
			if(!temp_mask.test(temp)){
				errors.push(ar_temp);
			}
		}

		if(errors.length>0){	
			for(var i=0;i<errors.length;i++){
				var div="<a class=\"alert\" title=\""+errors[i]["alert"]+"\"><img src=\"/tpl/images/attention.png\"/></a>";
				var div_alert="<div class=\"comments warning\" >"+errors[i]["alert"]+"</div>";
				if($("#call_me input[name='"+errors[i]["pole"]+"']").length>0){
					$("#call_me input[name='"+errors[i]["pole"]+"']").before(div);
				}else{
					$("#call_me textarea[name='"+errors[i]["pole"]+"']").before(div);
				}
			}
			ret=false;
		}
		return ret;
	}
	
	
	
});
