$(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:     'GET',
          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()


// jquery scrollTo plug-in
;(function(h){var m=h.scrollTo=function(b,c,g){h(window).scrollTo(b,c,g)};m.defaults={axis:'y',duration:1};m.window=function(b){return h(window).scrollable()};h.fn.scrollable=function(){return this.map(function(){var b=this.parentWindow||this.defaultView,c=this.nodeName=='#document'?b.frameElement||b:this,g=c.contentDocument||(c.contentWindow||c).document,i=c.setInterval;return c.nodeName=='IFRAME'||i&&h.browser.safari?g.body:i?g.documentElement:this})};h.fn.scrollTo=function(r,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};a=h.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=h(k),d=r,l,e={},p=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(d)){d=n(d);break}d=h(d,this);case'object':if(d.is||d.style)l=(d=h(d)).offset()}h.each(a.axis.split(''),function(b,c){var g=c=='x'?'Left':'Top',i=g.toLowerCase(),f='scroll'+g,s=k[f],t=c=='x'?'Width':'Height',v=t.toLowerCase();if(l){e[f]=l[i]+(p?0:s-o.offset()[i]);if(a.margin){e[f]-=parseInt(d.css('margin'+g))||0;e[f]-=parseInt(d.css('border'+g+'Width'))||0}e[f]+=a.offset[i]||0;if(a.over[i])e[f]+=d[v]()*a.over[i]}else e[f]=d[i];if(/^\d+$/.test(e[f]))e[f]=e[f]<=0?0:Math.min(e[f],u(t));if(!b&&a.queue){if(s!=e[f])q(a.onAfterFirst);delete e[f]}});q(a.onAfter);function q(b){o.animate(e,j,a.easing,b&&function(){b.call(this,r,a)})};function u(b){var c='scroll'+b,g=k.ownerDocument;return p?Math.max(g.documentElement[c],g.body[c]):k[c]}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);