(function($){

  linkifyThis = function () {}
  
  linkifyThis.prototype = {
    init : function (jq)
    {
      this.nodesToSkip = ({
        SCRIPT:1,
        NOSCRIPT:1,
        STYLE:1,
        OBJECT:1,
        IFRAME:1,
        INPUT:1,
        A:1,
        OPTION:1,
        TEXTAREA:1,
        BUTTON:1,
        TITLE:1
      });
      
      this.urls =
      [
        { pattern: /(^|\s)?((?:MCMP)|(?:Music\s+Career\s+Mentoring\s+Program)|(?:Music\s+Careers\s+Mentoring\s+Program)|(?:music\s+career))(\s|$)?/gi, url: 'http://tomhess.net/MusicCareer.aspx' }
        ,{ pattern: /(^|\s)?((?:correspondence\s+guitar\s+lessons)|(?:guitar\s+lessons)|(?:correspondence\s+lessons))(\s|$)?/gi, url: 'http://tomhess.net/CorrespondenceGuitarLessons.aspx' }
        ,{ pattern: /(^|\s)?((?:EGTIC)|(?:Elite\s+Guitar\s+Teachers\s+Inner\s+Circle)|(?:Teachers\s+Inner\s+Circle)|(?:Teachers\s+Circle))(\s|$)?/gi, url:'http://tomhess.net/EliteGuitarTeachersInnerCircle.aspx' }
        ,{ pattern: /(^|\s)?((?:Hess\s*Fest))(\s|$)?/gi, url:'http://tomhess.net/HessFest.aspx' }
		,{ pattern: /(^|\s)?((?:Rock\s*Band\s*Success\s*Coaching)|(?:Rock\s*Band\s*Success)|(?:Rock\s*Band)|(?:Band\s*Coaching)|(?:Success\s*Coaching))(\s|$)?/gi, url:'http://tomhess.net/RockBandSuccessCoaching.aspx' }
      ];
      
      this.nodes = jq.find("*");
      this.node = -1;
      this.nodeCount = this.nodes.length;
      this.process();
    },
    
    isTextNode: function (i)
    {
      if (!i)
        return false;
        
      var nodeName = i.nodeName.toUpperCase();
      
      if (this.nodesToSkip[nodeName])
        return false;

      return true;
    },
    
    process : function()
    {
      var el = null;
      var nodesProcessed = 0;
      var that = this;
      
      for(var i = 0; i < 10; i++)
      {
        this.node++;
        
        if (this.node >= this.nodeCount)
          return;

        var el = this.nodes[this.node];
        
        if (!el || !this.isTextNode(el))
          continue;

        for(var j = 0; j < el.childNodes.length; j++)
        {
          var txt = el.childNodes[j];
          if (txt && txt.nodeType == 3)
            this.processNode(txt);
        }
      }
      
      setTimeout(function() {that.process();}, 2);
    },
    
    processNode : function (n)
    {
        var html = n.nodeValue;
        if (html)
        {
          var replace = false;
          for(var j = 0; j < this.urls.length; j++)
          {
            var ptn = this.urls[j].pattern;
            if (ptn.test(html))
            {
              replace = true;
              html = html.replace(ptn, '$1<span>&nbsp;</span><a href="'+ this.urls[j].url +'" target="_blank">$2</a><span>&nbsp;</span>$3');
            }
          }
          if (replace)
            $(n).after(html).remove();
        }
    }
  };

  $.fn.linkify = function ()
  {
    return new linkifyThis().init(this);
  }
}
)(jQuery);

