(function(jQuery) {
  jQuery.fn.extend({
    extlink: function() {
      var site = String(document.location)
        .replace(/^(https?:\/\/[^:\/]+).*$/, "$1")
        .replace(/^((site)?(file:\/\/.+\/))[^\/]+$/, "$3")
        .replace(/(\\.)/g, "\\$1");
      
      jQuery("a", this).filter(function(i) {
        var href = jQuery(this).prop("href");
        if (href == null)
          return false;
        return (
          href.match(RegExp("\\.(pdf|docx?|xlsx?|pptx?|txt|eps|tif|psd|dwg|vsd)$")) != null || href.match(RegExp("^(" + site + "|(https?:)?/[^/])")) == null && href.match(RegExp("^(https?|ftp)://.+")) != null
        );
        
      }).each(function() {
        if (jQuery(this).prop("title").length == 0) {
          jQuery(this).prop("title", jQuery(this).html());
        }
    if(!jQuery(this).hasClass("not-external")) {
        jQuery(this)
      .attr("title", jQuery(this).attr("title") + " (Öppnas i ett nytt fönster)")
      .addClass("external");
      jQuery(this).click(function() {
        window.open(jQuery(this).attr("href"));
        return false;
      });
    }

      });
    }
  });
})(jQuery);

jQuery(document).ready(function() {
  
  if (jQuery.isFunction(jQuery.fn.extlink)) {
     jQuery(this).extlink(); 
  }
  
});
