// Nicolas Pereyra
//nico094@gmail.com
//20 August 2009

$(document).ready(function(){

function background(){
		div = document.createElement('div');
		$(div).attr("class", "bg-form");
		$('body').append(div);
		if (typeof document.body.style.maxHeight === "undefined") {
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			$(".bg-form").css({'width' : '100%', 'height' : '100%', 'position' : 'absolute', 'top' : "0", 'right' : "0", 'bottom' : "0", 'left' : "0", 'background-color' : '#000', 'opacity' : "0"}).fadeTo(600, 0.50, formContact);
		}else{
			$(".bg-form").css({'position' : 'fixed', 'top' : "0", 'right' : "0", 'bottom' : "0", 'left' : "0", 'background-color' : '#000', 'opacity' : "0"}).fadeTo(600, 0.50, formContact);
		}
		$(".bg-form").click(exit);
}

function formContact(){
		div = document.createElement('div');
		$(div).attr("class", "wrap-form");
		$('body').append(div);
		$(".wrap-form").css({ "width" : "400px", "height" : "480px", "border" : "1px solid #000", "background" : "#fff" }).html("<h3><img src='images/content/titl-form.jpg' width='351' height='35' alt='ROBERT M. FOX - Contact Us' /><img class='btn-close' style='cursor: pointer;' src='images/content/btn-close.jpg' width='49' height='35' alt='Close' /></h3><div class='content-form'><form method='POST' action='send.php'><label>Name:</label><input name='name-inf' type='text' /><label>Email:</label><input name='contact-inf' type='text' /><label>Phone:</label><input name='phone-inf' type='text' /><label>Message:</label><textarea name='brief-message'></textarea><p><input class='submit' name='submit' value='SEND' /></p></form><div class='message-succ'></div></div>");
		$(".wrap-form .content-form").css({ "padding" : "13px 18px 10px", "border-bottom" : "4px solid #161E34", "height" : "418px" });
		$(".wrap-form form").css("display", "inline");
		$(".wrap-form form label").css({ "display" : "block", "font-size" : "14px", "font-weight" : "bold", "margin" : "0 0 9px 0", "color" : "#484747", "padding" : "0 5px" });
		$(".wrap-form form input").css({ "width" : "355px", "padding" : "7px 3px", "color" : "#484747", "border" : "1px solid #878787", "margin" : "0 0 10px 0" });
		$(".wrap-form form textarea").css({ "width" : "355px", "height" : "127px", "padding" : "5px 3px", "color" : "#484747", "border" : "1px solid #878787", "margin" : "0 0 10px 0" });
		$(".wrap-form form p").css({ "margin" : "0", "text-align" : "center", "background" : "url(images/content/bg-send-form.gif) no-repeat center center" });
		$(".wrap-form form p input.submit").css({ "width" : "75px", "padding" : "7px 0", "text-align" : "center", "border" : "1px solid #878787", "background" : "#13192E", "color" : "#fff", "margin" : "0", "font-size" : "12px", "font-weight" : "bold", "cursor" : "pointer" });
		
		if (typeof document.body.style.maxHeight === "undefined") {
			$(".wrap-form").css({ "position" : "absolute", "left" : "50%", "top" : "50%", "margin-left" : "-201px", "margin-top" : "-211px" });
		}else{
			$(".wrap-form").css({ "position" : "fixed", "left" : "50%", "top" : "50%", "margin-left" : "-201px", "margin-top" : "-211px" });
		}
		$(".wrap-form").fadeTo(400, 1);
		$(".submit").click(function(){
			var formname = $("input[name='name-inf']").attr("value");
			var forminf = $("input[name='contact-inf']").attr("value");
			var formphone = $("input[name='phone-inf']").attr("value");
			var formbrief = $("textarea[name='brief-message']").attr("value");
			
			if(formname == '' || forminf == '' || formphone == '' || formbrief == ''){
				$(".message-succ").text("All fields are requiered.").css("display", "block");	
			}else {
			
				$(".message-succ").text("Sending...").css("display", "block");
				
				$.ajax({
					   type: "POST",
					   url: "send.php",
					   data: "name-inf="+formname+"&contact-inf="+forminf+"&phone-inf="+formphone+"&brief-message="+formbrief,
					   
					   success: function(datos){
						   $(".message-succ").text(datos);
						   $("input[name='name-inf']").attr("value", "");
						   $("input[name='contact-inf']").attr("value", "");
						   $("input[name='phone-inf']").attr("value", "");
						   $("textarea[name='brief-message']").attr("value", "");
					   }
				});
			
			}
			
		});
		$(".btn-close").click(exit);
}

function exit(){
		$(".wrap-form").fadeOut("slow").remove();
			
		$(".bg-form").fadeOut("fast", function(){
		
		if (typeof document.body.style.maxHeight === "undefined") {
			$("body","html").css({height: "auto", width: "auto"});
			$("html").css("overflow","auto");
		}
		
		$(this).remove();
		});
			
}

$(".form-contact").click(background);

});
