/* behavior-external-linkify.js
   (requires behavior.js to be loaded already)
   Adds an onclick event to all external links so they are passed
   through the WebMD interstitial page.
 */

/* Behavior rules */
var externalLinkRules = {

  /* Behavior rule for the <A> element */
  'a' : function(element) {

    /* List of strings that start an internal link.  Note that relative
       links (that do not start with http) do not have to be listed here.
    */
    var internalLinkRegexpArray =
    [
     new RegExp('^https?://[^/]*(dirittiglobali\.it|dirittiglobali-dev\.etabeta\.it|etabeta\.whiteready\.it/dirittiglobali)', 'i')
     ];

    /* Get the HREF for this link and convert to lower case */
    var url = element.href.toLowerCase();

    /* Skip if a relative URL (doesn't start with http) */
    if (url.substr(0, 4) != 'http') { return; }

    /* Loop through all the internal link regular expressions */
    for (var i=0; i<internalLinkRegexpArray.length; i++) {
      
      /* Check for a match with this regular expression */
      if (url.match(internalLinkRegexpArray[i])) {

	/* Got a match, so it's an internal link and we're done */
	return;

      }
    }

    /* If we get here it's an external link */

    /* Add an onclick event to the link */
    element.onclick = function(){

      var url = "http://www.dirittiglobali.it/click.php?url=" + encodeURIComponent(this.href);
      location.href = url;
      return false;

    }

  }

};

Behaviour.register(externalLinkRules);
