/** Defines the googletracker() jQuery function.
 *
 * @author Josh Zerin <josh.zerin@standingdog.com>
 * @since Monday, May 11, 2009
 *
 * @package StandingDog
 * @subpackage jQuery
 */
(function( $ ) {
  $.fn.googleTracker = function( $options ) {
    $options = $.extend({'pageTracker': null}, $options);

    if( $options.pageTracker )
    {
      $(this).each( function(  ) {
        /* Convenience alias. */
        var $this = $(this);

        switch( String($this.attr('nodeName')).toLowerCase() )
        {
          /* Anchors use the click() handler. */
          case 'a':
            $this.click( function(  ) {
              var $target = $this.attr('target');

              /* Kludge:  _link() doesn't handle targets properly. */
              if( $target )
              {
                window.open(
                  $options.pageTracker._getLinkerUrl($this.attr('href')),
                  $target
                );
              }
              else
              {
                $options.pageTracker._link($this.attr('href'));
              }

              return false;
            });
          break;

          /* Forms use the submit() handler and might require serialization. */
          case 'form':
            $this.submit( function(  ) {
              switch( String($this.attr('method')).toLowerCase() )
              {
                /* POST forms are easy. */
                case 'post':
                  $options.pageTracker._linkByPost(this);
                break;

                /* GET forms are tricky b/c the browser will overwrite the URL,
                 *  so we have to manually append the linker parameters to the
                 *  form.
                 *
                 * Because a form might potentially be submitted multiple times,
                 *  it is much simpler to modify the URL than try to create new
                 *  hidden inputs (also, this behavior is consistent with
                 *  _linkByPost()).
                 *
                 * Note that GET is the default b/c unknown or missing method
                 *  attributes get treated as GET by most (all?) browsers.
                 */
                default:
                  var $target = $this.attr('target');
                  var $action = $this.attr('action');

                  var $sep = $action.indexOf('?') > -1 ? '&' : '?';

                  /* Kludge:  _link() doesn't handle targets properly. */
                  if( $target )
                  {
                    window.open
                    (
                      $options.pageTracker._getLinkerUrl(
                        $action + $sep + $this.serialize()
                      ),
                      $target
                    );
                  }
                  else
                  {
                    $options.pageTracker._link(
                      $action + $sep + $this.serialize()
                    );
                  }

                  return false;
                break;
              }
            });
          break;
        }
      });
    }
  };
})(jQuery);