//jQuery plugin for external links
(function($) {

	$.extend({
    externalLinks: function (target) {
      //Apply CSS class to all A links that have dbdocs
      $(target).find("a[href*='/dbdocs/']").filter(function() {
        return this.hostname && this.hostname === location.hostname;
      }).addClass("file_link").click(function() {
        window.open(this.href);
        return false;
      });

      $(target).find("a.file_link[href$='.csv']").addClass("file_csv");
      $(target).find("a.file_link[href$='.doc'],a.file_link[href$='.docx']").addClass("file_doc");
      $(target).find("a.file_link[href$='.pdf']").addClass("file_pdf");
      $(target).find("a.file_link[href$='.ppt']").addClass("file_ppt");
      $(target).find("a.file_link[href$='.xls'],a.file_link[href$='.xlsm']a.file_link[href$='.xlsb'],a.file_link[href$='.xlsx']").addClass("file_xls");
      $(target).find("a.file_link[href$='.jpg'],a.file_link[href$='.gif'],a.file_link[href$='.png'],a.file_link[href$='.bmp']").addClass("file_image");
      $(target).find("a.file_link[href$='.ai']").addClass("file_ilustrator");
      $(target).find("a.file_link[href$='.mov'],a.file_link[href$='.avi']").addClass("file_film");
      $(target).find("a.file_link[href$='.swf'],a.file_link[href$='.flv']").addClass("file_flash");
      $(target).find("a.file_link[href$='.htm'],a.file_link[href$='.html']").addClass("file_html");
      $(target).find("a.file_link[href$='.mdb']").addClass("file_mdb");
      $(target).find("a.file_link[href$='.mp3'],a.file_link[href$='.ogg'],a.file_link[href$='.wav']").addClass("file_music");
      $(target).find("a.file_link[href$='.psd']").addClass("file_psd");
      $(target).find("a.file_link[href$='.txt']").addClass("file_txt");

      //Apply CSS class to all A links that start with mailto:
      $(target).find("a[href^='mailto:']").addClass("file_link mail_link");

      //Apply CSS class to all a links that start with http:// not on the local site
      $(target).find("a[href^='http://']").filter(function() {
				if ($(this).hasClass('noexternal')) return false;			// XXX: class can be used to skip certain links
        return this.hostname && this.hostname !== location.hostname;
      }).not('.noexternal').addClass("external_link").click(function() {
        window.open(this.href);
        return false;
      });

      //Apply CSS class to all a links that start with http:// not on the local site
      $(target).find("a[rel=external]").filter(function() {
				if($(this).hasClass("external_link")) return false;
      }).click(function() {
        window.open(this.href);
        return false;
      });
    }
  });
})(jQuery);

