/**
 * Rank: 3
 */

var contactForm = {}

contactForm.send = function()
{
	$('.error').hide();
	var email_regex = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

	if ($('#name').val() == ''
    || $('#name').val() == 'Your Name')
  {
		$('.error').html('Please enter your name').fadeIn();
		setTimeout(function()
    {
      $('#name').focus();
    }, 400);
		return;
	}
  else if ($('#email').val() == ''
    || $('#email').val() == 'Your Email')
  {
		$('.error').html('Please enter your email address').fadeIn();
		setTimeout(function()
    {
      $('#email').focus();
    }, 400);
		return;
	}
  else if (!email_regex.test($('#email').val()))
  {
		$('.error').html('Please enter a valid email address').fadeIn();
		setTimeout(function()
    {
      $('#email').focus();
    }, 400);
		return;
	}
  else if ($('#message').val() == '' || $('#message').val() == 'Enter your message...')
  {
		$('.error').html('Please construct a message').fadeIn();
		setTimeout(function()
    {
      $('#message').focus();
    }, 400);
		return;
	}

	var name = $('#name').val();
	var email = $('#email').val();
	var message = $('#message').val();

	$('#main').slideUp();

  var sendUrl = $('#AjaxSendUrl').val();
console.log(sendUrl);
	$.post(
    sendUrl, {
      'name': name,
      'email': email,
      'message': message
    }, function()
    {
      $('#main')
        .html('<h1>Contact</h1>Thank you for sending me your message, I will be sure to get back to you as soon as possible.')
        .slideDown();
    });
}

contactForm.attachHandlers = function()
{
  $('#contact input[type!=button], #contact textarea').focus(function()
  {
		if (!$(this).hasClass('input_text_over'))
    {
			$(this).addClass('input_text_over');
			$(this).val('');
      
      var $placeholder = $(this).next('div');
      var thisName = '#' + $placeholder.attr('id');
      var offset = $(this).offset();
      var width = $(this).width();

			if (thisName != '#message-place')
      {
				$(thisName).show().animate({
          color: '#dddddd',
          left: (offset.left + width + 20) + 'px'
        },400);
			}
      else
      {
				$(thisName).show().animate({
          left: (offset.left + width - 140) + 'px'
        }, 500)
        .animate({
          color: '#dddddd',
          top: (offset.top + 5) + 'px'
        }, 300);
			}
		}
	});

	$('#contact input[type!=button], #contact textarea').blur(function()
  {
		$('.error').hide();

		if ($(this).val() == '')
    {
			$(this).removeClass('input_text_over');

      var $placeholder = $(this).next('div');
      var thisVal = $placeholder.text();
      var thisName = '#' + $placeholder.attr('id');
      var thisInput = '#' + $(this).attr('id');
      var offset = $(this).offset();

			if (thisName != '#message-place')
      {
				$(thisName).animate({
          color: '#555555',
          left: offset.left + 'px'
        }, 400, function()
        {
          $(this).hide();
          $(thisInput).val(thisVal);
        });
			}
      else
      {
				$(thisName).animate({
          color: '#555555',
          top: (offset.top + 35) + 'px'
        }, 300)
        .animate({
          left: offset.left + 'px'
        }, 400, function()
        {
          $(this).hide();
          $(thisInput).val(thisVal);
        });
			}
		}
	});

	$('#contact input').keypress(function(e)
  {
		if (e.keyCode == 13)
    {
      contactForm.send();
		}
	});

	$('#contact #button').click(function()
  {
		contactForm.send();
	});
}

contactForm.positionPlaceholders = function()
{
  $('#contact .placeholder').each(function()
  {
    var $input = $(this).prev();
    var offset = $input.offset();
    $(this).css('top', offset.top + 34).css('left', offset.left);
  });
}

$(function()
{
	$('.placeholder').hide();
  contactForm.positionPlaceholders();
  contactForm.attachHandlers();
});
