$(document).ready(function(){

	//$(".topmenu > dl[@class!=on]").hover(function(){

	$(".topmenu dl").mouseenter(function(){
		if($(this).attr("class")!='on'){
			if($(this).find("ul").html()){
				$(".topmenu").find("ul").slideUp("fast");
				$(".topmenu").find("dl").removeClass("on");
				$(this).find("ul").slideDown("fast");
				$(this).addClass("on");
			}
		}
	});
	$(".topmenu dl").mouseleave(function(){
		$(".topmenu").find("ul").slideUp("fast");
		$(".topmenu").find("dl").removeClass("on");
	});
	
	
	$("#send").live("click",function(){
		var err = 0;
		
		if($(".form span.f80").length){
			$(".form span.f80").remove();
		}
		if($(".form div.f80").length){
			$(".form div.f80").remove();
		}
		$(".form input.inp").each(function (i) {
			var ci = $(this);			
			if(ci.val()==""){
				if(empty_this(ci)){
					err = 1;
				};
			}else{
				if(ci.attr("name")=="email"){
					var condition = soup(ci);
					if(condition) {
						err = 1;
					};
				};
				if(ci.attr("name")=="phone"){
					var condition = only_number(ci);
					if(condition){
						err = 1;
					};
				};
			};
		});
		if($("#primech").val()==""){
			if (empty_this($("#primech"))){
				err = 1;
			};
		}
		if($("#ch_hum_inp").val()==""){
			$("#ch_hum_inp").after("<div class='red f80 err'>Пожалуйста, заполните поле</div>");
			err = 1;
		}
		
		
		
		if(err == 1){
			$('.result').css('width','730px');
			return false;
		}else{
			return true;
		};
	});
	
	$('#pub a[href^="/accel/content/pic"], #pub a[href^="http://www.vzr.ru/accel/content/pic"]').prettyPhoto();
	
	var curl = window.location.href;
	var re = new RegExp("/order/$", "gi");
	if(curl.search(re) == -1){
		$('a[href="/order/"]').click(function(){
			show_order(0);
			return false;
		});
		$('a[href="/order/?visitor=1"]').click(function(){
			show_order(1);
			return false;
		});
	}

	$('.mask').click(function(){
		closeForm();
	});
	
	$('#tooltip-button').click(function(){
		closeTooltip();
	});
});

function empty_this(empty){
	empty.after("<span class='red f80 err'>Пожалуйста, заполните поле</span>");
	return 1;
}
function soup(soup){
	var mail=soup.val();
	var regexp = /^([a-zA-Z0-9_-]+)@([a-zA-Z0-9_.]+)(\.[a-zA-Z]{2,4})$/g;
	var prov = mail.match(regexp);
	if (prov==null){ 
		soup.after("<span class='red f80 err'>Некорректный e-mail: обязательные символы &quot;.&quot; и &quot;@&quot;. Специальные символы недопустимы." +"</span>");
		return 1;
	}
}
function only_number(number){
	var inn=number.val();
	var inn_pattern=/^\d+$/g;
	var prov=inn_pattern.test(inn);
	if(prov==false){ 
		number.after("<span class='red f80 err'>Введите только цифры</span>");
		return 1;
	}
}

function closeForm() {
	$('.mask').fadeOut(100);
	$('.forma').hide();
}

function closeTooltip() {
	$('#tooltip').fadeOut(100);
}

function initSubmit(vis){
	$('.biggernigger').submit(function(){
		$.ajax({
			url: '/order/index.p3',
			data: $(this).serialize() + '&ajax=1&vis='+vis,
			dataType: 'json',
			type: 'POST',
			success: function(data){
				if(data.error){
					$('.result').html(data.html);
					initSubmit();
					$('.result').css('width','730px');
				}
				else{
					$('.result').html('&nbsp;');
					closeForm();
					showMessage(data.header, data.message, 5000, 3000);
				}
			}
		});
		return false;
	});
}


function show_order(vis) {
	$.ajax({
		url: '/order/index.p3',
		data: {ajax: 1},
		dataType: 'json',
		success: function(data){
			$('.result').html(data.html);
			
			if(typeof(window.innerHeight) == 'number'){
				var heightWindow = parseFloat(window.innerHeight);
			} else if(document.documentElement && document.documentElement.clientHeight) {
				var heightWindow = parseFloat(document.documentElement.clientHeight);
			}
			
			if(heightWindow < 547){
				$('.result #forms').css('height',heightWindow-100);
			}

			$('.result').css('width','510px');
			$('.forma').fadeIn(800);
			$('.mask').css({opacity: 0.5});
			$('.mask').fadeIn(800);
			initSubmit(vis);
		}
	});
}

function showMessage(header, body, delay, fadeDuration){
	$('#tooltip').hide();
	$('#tooltip-header').text(header);
	$('#tooltip-body').html('<p>' + body + '</p>');
	$('#tooltip').show().delay(delay).fadeOut(fadeDuration);
}



function menuovered(){
	$(".topmenu > dl[@class!=on]").hover(function(){
		if($(this).attr("class")!='on'){
			if($(this).find("ul").html()){
				$(".topmenu").find("ul").slideUp("fast");
				$(".topmenu").find("dl").removeClass("on");
				$(this).find("ul").slideDown("fast");
				$(this).addClass("on");
			}
		}
	},function(){
		$(".topmenu").find("ul").slideUp("fast");
		$(".topmenu").find("dl").removeClass("on");
	});
	
}




