window.addEvent('domready', function(){

  var sending = new Fx.Tween( $( 'sending' ), {duration: 'long'} );
  var success = new Fx.Tween( $( 'success' ), {duration: 'long'} );
  var fail = new Fx.Tween( $( 'fail' ), {duration: 'long'} );

  function clear_all(){
      sending.set('opacity', 0.0 );
      success.set('opacity', 0.0 );
      fail.set('opacity', 0.0 );
  }

  $('contact_form').addEvent('submit', function(e){
    e.stop(); //jump in and take over
    var req = new Request.HTML({

      url: 'simple_form_submit.php',
      data: $('contact_form'),
      onRequest: function(){
        sending.start( 'opacity', 1.0, 0.0 );
        $('contact_form').reset();
        clear_all.delay( 4000 );
      },
      onSuccess: function(tree, response){
        sending.set('opacity', 0.0 );
        success.start( 'opacity', 0.0, 1.0 );
        $('contact_form').reset();
        clear_all.delay( 4000 );
      },
      onFailure: function( x ){
        sending.set( 'opacity', 0.0 );
        fail.start( 'opacity', 0.0, 1.0 );
        $('contact_form').reset();
        clear_all.delay( 4000 );
      }
    }).send();
  });
});
