var community;

$(document).ready(function() {
	$(".contact").fancybox({
			'hideOnContentClick': false,
			'callbackOnShow': function() {
				$('#fancy_ajax').css({'overflow':'hidden'});
				$('#fancy_outer').hide().slideDown('slow');
				seeForm('/form_submission/contact_us');
				valContact();
				Cufon.refresh();
				$('#contactbox').selectbox();
			},
			'centerOnScroll': true,
			'frameWidth': 774,
			'frameHeight': 511,
			'padding': 0,
			'zoomSpeedIn': 0
		});

		$(".customhome").fancybox({
				'hideOnContentClick': false,
				'callbackOnShow': function(elem) {
					community = $(elem).attr('rel');
					$('#fancy_ajax').css({'overflow':'hidden'});
					$('#fancy_outer').hide().slideDown('slow');
					seeForm('/form_submission/custom_home');
						if(community != '')
						{
							$('#textarea').val('I am interested in building a home in '+community+'.');
							$('#hidden_community').val(community);
						}
					valCustom();
					Cufon.refresh();
				},
				'centerOnScroll': true,
				'frameWidth': 774,
				'frameHeight': 511,
				'padding': 0,
				'zoomSpeedIn': 0
			});
			
		 $('#availableHomes ul li a').mouseover(function(e)
		 {
		 	$(this).next('ul').css({'bottom': '23px'}).animate({'bottom': '27px'}, {queue: false, duration: 200}).fadeIn(200);
		 })
		 .mouseout(function()
		 {
		 	$(this).next('ul').hide();
		 })
		 .mousemove(function(e)
		 {
		 	$(this).next('ul').css({'left': e.pageX -$(this).offset().left -40 +'px'});
		 });
});

function seeForm(post_to) {
	
	$('#frmContact').submit(function(e) {
		e.preventDefault();		
		if(post_to == '/form_submission/contact_us')
		{
			if(checkContact())
			{
				$.post(post_to, $(this).serialize(), function(data){
					$('#fullform').fadeOut('slow', function(){
							if($('#contactbox').val() == 'New Home Consultant')
							{
								$('#success').fadeIn('slow', function() {
									setTimeout('$.fn.fancybox.close()', 3500);
								});
							}
							else if($('#contactbox').val() == 'Customer Care')
							{
								$('#success2').fadeIn('slow', function() {
									setTimeout('$.fn.fancybox.close()', 3500);
								});	
							}
							else if($('#contactbox').val() == 'New Condominium Consultant')
							{
								$('#success3').fadeIn('slow', function() {
									setTimeout('$.fn.fancybox.close()', 3500);
								});
							}
					});
				});
			}
		}
		else if(post_to == '/form_submission/custom_home')
		{
			if(checkCustom())
			{
				$.post(post_to, $(this).serialize(), function(data){
					$('#fullform').fadeOut('slow', function(){
						$('#success').fadeIn('slow', function() {
							setTimeout('$.fn.fancybox.close()', 3500);
						});
					});
				});
			}
		}
		else
		{
			if(checkCustom()) // Says custom but uses same checks as checkCustom for Showing.
			{
				$.post(post_to, $(this).serialize(), function(data){
					$('#fullform').fadeOut('slow', function(){
						$('#success').fadeIn('slow', function() {
							setTimeout('$.fn.fancybox.close()', 3500);
						});
					});
				});
			}
		}
		});
    }

function valContact() {
	$('#contactbox').change(function () {
		if($('#contactbox').val() != 'all') {
			$('#select_box_error').css("display","none");
		}
	});
	
	$('#first_name').focus(function () {
		$('#first_name').removeClass('contactError');
		if($('#first_name').val()=='First Name')
		{
			$('#first_name').val('');
		}
	});
	
		$('#first_name').blur(function () {
			if($('#first_name').val()=='')
			{
				$('#first_name').val('First Name');
			}
		});
	
	$('#last_name').focus(function () {
		$('#last_name').removeClass('contactError');
		if($('#last_name').val()=='Last Name')
		{
			$('#last_name').val('');
		}
	});

			$('#last_name').blur(function () {
				if($('#last_name').val()=='')
				{
					$('#last_name').val('Last Name');
				}
			});

	$('#phone').focus(function () {
		$('#phone').removeClass('contactError');
		if($('#phone').val()=='Phone Number')
		{
			$('#phone').val('');
		}
	});

		$('#phone').blur(function () {
			if($('#phone').val()=='')
			{
				$('#phone').val('Phone Number');
			}
		});	

	$('#email').focus(function () {
		$('#email').removeClass('contactError');
		if($('#email').val()=='Email Address')
		{
			$('#email').val('');
		}
	});

		$('#email').blur(function () {
			if($('#email').val()=='')
			{
				$('#email').val('Email Address');
			}
		});	

	$('#textarea').focus(function () {
		$('#textarea').removeClass('contactError');
		if($('#textarea').val()=='Message')
		{
			$('#textarea').val('');
		}
	});

		$('#textarea').blur(function () {
			if($('#textarea').val()=='')
			{
				$('#textarea').val('Message');
			}
		});
}

function valCustom() {	
	$('#first_name').focus(function () {
		$(this).removeClass('contactError');
		if($(this).val()=='First Name')
		{
			$(this).val('');
		}
	});
	
		$('#first_name').blur(function () {
			if($(this).val()=='')
			{
				$(this).val('First Name');
			}
		});
	
	$('#last_name').focus(function () {
		$(this).removeClass('contactError');
		if($(this).val()=='Last Name')
		{
			$(this).val('');
		}
	});

			$('#last_name').blur(function () {
				if($(this).val()=='')
				{
					$(this).val('Last Name');
				}
			});

	$('#phone').focus(function () {
		$(this).removeClass('contactError');
		if($(this).val()=='Phone Number')
		{
			$(this).val('');
		}
	});

		$('#phone').blur(function () {
			if($(this).val()=='')
			{
				$(this).val('Phone Number');
			}
		});	

	$('#email').focus(function () {
		$(this).removeClass('contactError');
		if($(this).val()=='Email Address')
		{
			$(this).val('');
		}
	});

		$('#email').blur(function () {
			if($(this).val()=='')
			{
				$(this).val('Email Address');
			}
		});	

	$('#textarea').focus(function () {
		$('#textarea').removeClass('contactError');
		if($('#textarea').val()=='Message')
		{
			$('#textarea').val('');
		}
	});

			$('#textarea').blur(function () {
				if($('#textarea').val()=='')
				{
					$('#textarea').val('Message');
				}
			});
}

function checkContact() {
	
	var success = true;
	
	if($('#contactbox').val() == 'all') {
		$('#select_box_error').css("display","block");
		success = false;
	}
	
	if ($('#first_name').val() == '')
	{
		$('#first_name').addClass('contactError');
		success = false;
	}
	
	if ($('#last_name').val() == '')
	{
		$('#last_name').addClass('contactError');
		success = false;
	}
	
	if ($('#phone').val() == '')
	{
		$('#phone').addClass('contactError');
		success = false;
	}
	
	if (!$('#email').val().match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/))
	{
		$('#email').addClass('contactError');
		success = false;
	}
	
	if ($('#textarea').val() == '')
	{
		$('#textarea').addClass('contactError');
		success = false;
	}
	
	return success;
}

function checkCustom() {
	
	var success = true;
	
	if ($('#first_name').val() == '')
	{
		$('#first_name').addClass('contactError');
		success = false;
	}
	
	if ($('#last_name').val() == '')
	{
		$('#last_name').addClass('contactError');
		success = false;
	}
	
	if ($('#phone').val() == '')
	{
		$('#phone').addClass('contactError');
		success = false;
	}
	
	if (!$('#email').val().match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/))
	{
		$('#email').addClass('contactError');
		success = false;
	}
	
	if ($('#textarea').val() == '')
	{
		$('#textarea').addClass('contactError');
		success = false;
	}
	
	return success;
}