
LinkTracker = Class.create();
LinkTracker.prototype = {
  
  initialize: function() {
    this.attachOutboundTracking();
  },

  // track a click on an outbound link
  trackClick: function(evt, p, orig) {
    pageTracker._trackPageview(p);
  },
  
  attachOutboundTracking: function() {
    // attach handlers to outbound links
    var local_re = new RegExp('^\\w+://(?:\\w+\\.)*' + window.location.host.match(/[\w-]+\.[\w-]+(?::\d+)?$/)[0] + '/');
    var links = $A(document.getElementsByTagName('a'));
    that = this;
    links.each( function(l) {
      if (!local_re.test(l.href)) {
        // this is an external link
        
        target_url = l.href;
        target_url = target_url.replace(/^mailto:(.*)@(.*)$/, "mailto/$1/$2");
        target_url = target_url.replace(/^https?:\/\//, '');
        var virtual_prefix = '/virtual/outbound/';
 
        Event.observe(l, 'click', that.trackClick.bindAsEventListener(that, virtual_prefix+target_url, l.href));
      } 
    } );
  }
  
}


ExternalLinksInNewWindow = Class.create();
ExternalLinksInNewWindow.prototype = {
  
  initialize: function() {
    var local_re = new RegExp('^\\w+://(?:\\w+\\.)*' + window.location.host.match(/[\w-]+\.[\w-]+(?::\d+)?$/)[0] + '/');
    var links = $A(document.getElementsByTagName('a'));
    that = this;
    links.each( function(l) {
      if (!local_re.test(l.href)) {
        // this is an external link
        l.target = '_blank';
      } 
    } );
  }
  
}



function initAfterLoad() {
  new LinkTracker();
  new ExternalLinksInNewWindow();
}

Event.observe(window, 'load', initAfterLoad);

