$(function() {
	
	document.onkeydown=function(e) {
		var e=window.event || e
		if (e.keyCode == 27) {
			$(".smokescreen").fadeOut();
			$(".formmodal").animate({"left":$(window).width()+20},500,function() {
				$(".formmodal").css("display","none");
			});
			activeform = null;
		}
	}	

	var activeform = null;

	$("#temp").load("/form.html",function() {
		$("#temp").children().detach().appendTo("body");
		$(this).remove();
		$(".formmodal").css({"top":(document.body.scrollTop+($(window).height()-602)/2)+10+"px","left":($(window).width()+20)+"px"});
		$("#cal").datepicker({
		    beforeShow: function(input, inst)
		    {
		       	scroll(0,0);
		    }
		});
	});
	
	$(".callform").click(function(e) {
		e.preventDefault();
		$(".smokescreen").css({"top":0,"left":0, "width":$(window).width()+"px","height":$(document).height()+"px"}).fadeIn();
		$("#theform1").closest(".formmodal").css("display","block").animate({"left":($(window).width()-714)/2},500);
		activeform = 1;
	});
	
	$(".smokescreen").live('click',function() {
		$(this).fadeOut();
		$(".formmodal").animate({"left":$(window).width()+20},500,function() {
			$(this).css("display","none");
		});
	});
	
	$(window).resize(function() {
		$(".smokescreen").css({"top":0,"left":0, "width":$(window).width()+"px","height":$(document).height()+"px"});
		$(".formmodal").css({"top":(document.body.scrollTop+($(window).height()-602)/2)+10+"px","left":($(window).width()+20)+"px"});
		if (activeform != null) {
			$(".formmodal").eq(activeform-1).css({"top":(document.body.scrollTop+($(window).height()-602)/2)+10+"px","left":($(window).width()-714)/2+"px"});
		}
	});
	
	$(window).scroll(function() {
		$(".formmodal").css({"top":(document.body.scrollTop+($(window).height()-602)/2)+10+"px","left":($(window).width()+20)+"px"});
		$("#ui-datepicker-div").css({"top":(document.body.scrollTop+($(window).height()-602)/2)+63+"px"});
		if (activeform != null) {
			$(".formmodal").eq(activeform-1).css({"top":(document.body.scrollTop+($(window).height()-602)/2)+10+"px","left":($(window).width()-714)/2+"px"});
		}
	});
	
	$(".closeform").live('click',function(e) {
		e.preventDefault();
		$(".smokescreen").fadeOut();
		$(".formmodal").animate({"left":$(window).width()+20},500,function() {
			$(".formmodal").css("display","none");
		});
		activeform = null;
	});
	
	$(".editform").live('click',function(e) {
		$("#theform1").closest(".formmodal").css({"left":"-750px","display":"block"}).animate({"left":($(window).width()-714)/2},500);
		$("#theform4").closest(".formmodal").animate({"left":$(window).width()+20},500,function() {
			$(this).css("display","none");
		});
		activeform = 1;
	});
	
	$("#form1submit").live('click',function() {
		var invalid = false;
		$("#ajaximg").css("display","block");
		$.post("/formcheck.php", { landline: $("#landline").val(), email: $("#email").val() },
		  function(data){
		  
		  	$("#ajaximg").css("display","none");
		    
		    $("#theform1 input:text").each(function() {
		    	if ($(this).val() == "" && $(this).attr("name") != "mobile") {
		    		$(this).css("border-color","#f54");
		    		invalid = true;
		    	} else {
		    		$(this).css("border-color","#ddd");
		    	}
		    });
		    
		    if (data.email == "false") {
		    	$("#email").css("border-color","#f54");
		    	invalid = true;
		    }
		    
		    if (data.landline == "false") {
		    	$("#landline").css("border-color","#f54");
		    	invalid = true;
		    }
		    
		    if (invalid) $("#theform1 .invalid").fadeIn(); else {
		    	$("#theform1 .invalid").fadeOut();
		    	$("#theform1").closest(".formmodal").animate({"left":"-750px"},500,function() {
		    		$(this).css({"display":"none","left":$(window).width()+20});
		    	});
		    	$("#theform2").closest(".formmodal").css("display","block").animate({"left":($(window).width()-714)/2},500);
		    	activeform = 2;
		    }
		    
		},"json");
	});
	
	$("#form2submit").live('click',function(e) {
		e.preventDefault();
		var invalid = false;
		$("#theform2 input:text").each(function() {
			if ($(this).val() == "" && $(this).attr("name") != "location" && $(this).attr("name") != "date") {
				$(this).css("border-color","#f54");
				invalid = true;
			} else {
				$(this).css("border-color","#ddd");
			}
		});
		if (invalid) $("#theform2 .invalid").fadeIn(); else {
			$("#theform2 .invalid").fadeOut();
			$("#theform2").closest(".formmodal").animate({"left":"-750px"},500,function() {
				$(this).css({"display":"none","left":$(window).width()+20});
			});
			$("#theform3").closest(".formmodal").css("display","block").animate({"left":($(window).width()-714)/2},500);
			activeform = 3;
		}
	});
	
	$("#form3submit").live('click',function(e) {
		e.preventDefault();
		gather_populate();
		$("#theform3").closest(".formmodal").animate({"left":"-750px"},500,function() {
			$(this).css({"display":"none","left":$(window).width()+20});
		});
		$("#theform4").closest(".formmodal").css("display","block").animate({"left":($(window).width()-714)/2},500);
		activeform = 4;
	});
	
	function gather_populate() {
	
		var company = $("#company").val(),
			lastname = $("#lastname").val(),
			firstname = $("#firstname").val(),
			middlename = $("#middlename").val(),
			landline = $("#landline").val(),
			mobile = $("#mobile").val(),
			email = $("#email").val(),
			location = $("#location").val(),
			date = $("#cal").val(),
			guests = $("#guests").val(),
			budget = $("#budget").val(),
			info = $("#info").val(),
			phone = (mobile != "") ? landline+", "+mobile : landline,
			service = "";
			
			$("#f-company").empty().append(company);
			$("#f-name").empty().append(firstname+" "+lastname);
			$("#f-phone").empty().append(phone);
			$("#f-email").empty().append(email);
			$("#f-location").empty().append(location);
			$("#f-date").empty().append(date);
			$("#f-guests").empty().append(guests);
			$("#f-budget").empty().append(budget);
			$("#f-info").empty().append(info);

			if ( $("[name='banquet']").attr("checked") ) service = "Банкет";
			if ( $("[name='furche']").attr("checked") ) service += (service != "") ? ", фуршет" : "Фуршет";
			if ( $("[name='coffee']").attr("checked") ) service += (service != "") ? ", кофе-брейк" : "Кофе-брейк";
			if ( $("[name='bbq']").attr("checked") ) service += (service != "") ? ", барбекю" : "Барбекю";
			if ( $("[name='coctail']").attr("checked") ) service += (service != "") ? ", коктейль" : "Коктейль";
			if ( $("[name='other']").attr("checked") ) service += (service != "") ? ", другое." : "Другое.";
			
			$("#f-type").empty().append(service);
	
	}
	
	$("#form4submit").live('click',function(e) {
		e.preventDefault();
		send();
		$("#theform4").closest(".formmodal").animate({"left":"-750px"},500,function() {
			$(this).css({"display":"none","left":$(window).width()+20});
		});
		$("#theform5").closest(".formmodal").css("display","block").animate({"left":($(window).width()-714)/2},500);
		activeform = 5;
	});
	
	function send() {
		var landline = $("#landline").val(),
			mobile = $("#mobile").val(),
			phone = (mobile != "") ? landline+", "+mobile : landline,
			service = "";
			
		if ( $("[name='banquet']").attr("checked") ) service = "Банкет";
		if ( $("[name='furche']").attr("checked") ) service += (service != "") ? ", фуршет" : "Фуршет";
		if ( $("[name='coffee']").attr("checked") ) service += (service != "") ? ", кофе-брейк" : "Кофе-брейк";
		if ( $("[name='bbq']").attr("checked") ) service += (service != "") ? ", барбекю" : "Барбекю";
		if ( $("[name='coctail']").attr("checked") ) service += (service != "") ? ", коктейль" : "Коктейль";
		if ( $("[name='other']").attr("checked") ) service += (service != "") ? ", другое." : "Другое.";
		
		$.post("/formsend.php", { 
			"company" : $("#company").val(),
			"lastname" : $("#lastname").val(),
			"firstname" : $("#firstname").val(),
			"middlename" : $("#middlename").val(),
			"email" : $("#email").val(),
			"location" : $("#location").val(),
			"date" : $("#cal").val(),
			"guests" : $("#guests").val(),
			"budget" : $("#budget").val(),
			"info" : $("#info").val(),
			"phone" : phone,
			"service" : service,
			"send" : "true"
		});
		
	}
	
	$("#form5submit").live('click',function(e) {
		e.preventDefault();
		$(".smokescreen").fadeOut();
		$("#theform5").closest(".formmodal").fadeOut().css({"display":"none","left":$(window).width()+20});
		activeform = null;
	});
	
});

