$(document).ready(function() {
     // cmt_name, cmt_email, cmt_url
     $( '#comment_form input.text' ).each( function() {
          $( this ).attr( 'def_val', $( this ).val() );
          $( this ).bind( 'focus', function() { check_focus( this ); } );
          $( this ).bind( 'blur', function() { check_focus( this ); } );
     } );
     
     $( '#comment_form' ).bind( 'submit', function() {
          comment_submit();
          return false;
     } );
     
     $( '#comment_form h4' ).bind( 'click', function() {
          if( $('#comment_form_contents').css( 'display' ) == 'none' ) {
               $.ajax( {
                    type:     'POST',
                    cache:    false,
                    url:      '/ajax/comment_form/',
                    dataType: 'html',
                    success:  function( response ) {
                         $('#comment_form_contents').html( response );
                         $('#comment_form_contents').attr( 'action', '/ajax/comment' );
                         $('#comment_form_contents').slideDown();
                         // create focus, blur actions
                         $( '#cmt_name, #cmt_email, #cmt_url' ).each( function() {
                              var def_val = $( this ).val();
                              $( this ).bind( 'focus', function() {
                                   if( $( this ).val() == def_val ) $( this ).val( '' );
                              } );
                              $( this ).bind( 'blur', function() {
                                   if( $( this ).val() == '' ) $( this ).val( def_val );
                              } );
                         } );
                    }
               } );
          }
          else {
               $('#comment_form_contents').slideUp();
          }
     } );
} );

function check_focus( this_input ) {
     var this_val = $( this_input ).val();
     var def_val = $( this_input ).attr( 'def_val' );
     if( this_val == def_val ) 
          $( this_input ).val( '' );
     else if( this_val == '' ) 
          $( this_input ).val( def_val );
} // end function check_focus()



function comment_submit() {
// todo: add captcha, email validation
/*
     if( $('#cmt_name').val() == $('#cmt_name').attr('def_val') ) {
          alert('Please enter your name.');
          return false;
     }
     if( $('#cmt_email').val() == $('#cmt_email').attr('def_val') ) {
          alert('Please enter your email address.');
          return false;
     }
*/
     if( $('#cmt_comment').val() == '' ) {
          alert('Please enter your comment.');
          return false;
     }
     $.ajax( {
          type:     'POST',
          cache:    false,
          url:      '/ajax/comment/',
          data:     $('#comment_form').serialize(),
          dataType: 'json',
          success:  function( response ) {
               if( response.success == true ) {
                    $( '#comment_form' ).slideUp();
                    $( 'dd.comments_none' ).slideUp();
                    $( '#comments dl' ).append( response.message );
                    $.scrollTo( '#comments dl', 800, {easing:'swing'} );
               }
               else {
                    $('#recaptcha_response_field').val( '' );
                    $('#recaptcha_response_field')[0].focus();
                    alert( response.message );
               }
          }
     } );
} // end function comment_submit()
