$(document).ready(function() {
    var originalFormValue = $('#email_address').attr('value');
    var checkFormValue = 'Are you sure that\'s right?';
    var hasFocusedForm = false;

    
    // Positionoing
    // $.backstretch("images/lilycrop.jpg", {
        // centeredX: true,
        // centeredY: false
    // });
	
    // Contact form
    $('#email_address').focus(function() {
        var curValue = $(this).attr('value');
        hasFocusedForm = true;
        $('#email_address').css('color', 'white');
        
        if (curValue === originalFormValue || curValue === checkFormValue) {
            $(this).attr('value', '');                        
        };
    });
    
    $('#email_address').blur(function() {
        if ($(this).attr('value') === '') {
            $(this).attr('value', originalFormValue);
        };
    });
    
    $('#email_form').submit(function(event) {
        event.preventDefault();
        
        var value   = $('#email_address').attr('value');
        var atpos   = value.indexOf("@");
        var dotpos  = value.lastIndexOf(".");
        
        if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= value.length) {
            $('#email_address').attr('value', checkFormValue);
            $('#email_address').css('color', '#ECB984');
        } else {
            $.ajax({
              url: $(this).attr('action'),
                  type    : $(this).attr('method'),
                  dataType: 'json',
                  data    : $(this).serialize(),
                  success : function( data ) {
                      $('#result').html(data['result']);
                  },
                  error: function() {
					$('#result').html('I\'m afraid something went wrong, could you try again?');
                  }
              });
        }               
    });
    
    // Animation
    var fadeTime = 2500;
    var animation = function() {
        //Form fade out
            // Without money IN
            $('#text2a').delay(500).fadeIn(fadeTime, function() {
                // Why Yes IN
                $('#text2b').fadeIn(fadeTime, function() {
                    // Without money why yes OUT
                    $('#text2').delay(1000).fadeOut(fadeTime/2, function() {
                    });
                    // Flaubert IN
                    $('#text3').delay(1000).fadeIn(fadeTime, function() {
                        // Flaubert OUT
                        $('#text3').delay(2000).fadeOut(fadeTime/2, function() {
                        });                    
                        // Miguel IN
                        $('#text4').delay(2000).fadeIn(fadeTime, function() {
                            // Miguel OUT
                            $('#text4').delay(2000).fadeOut(fadeTime/2, function() {
                            });
                            // Disney In                    
                            $('#text5').delay(2000).fadeIn(fadeTime, function() {
                                // Disney OUT
                                $('#text5').delay(2000).fadeOut(fadeTime/2, function() {
                                });
                            });
                        });
                    });
                });
            });            
        
    };
    
    // Cakes IN
    animation();
});

