jQuery(document).ready(function(){

    $("a").each(function(){
        var $a = jQuery(this);
        var href = this.href;

        // strip the host name down, removing subdomains or www
        var host = window.location.host.replace(/^(([^\/]+?\.)*)([^\.]{4,})((\.[a-z]{1,4})*)$/, '$3$4');

        //if (this.href != null) {
        //}

        if (this.href != null && !$a.is(".exempt")) {
            /*
            if (this.href.match(/\.pdf$/)) {
             $a.addClass('pdf');
             // $a.before('<img src="/sites/default/files/pdf.gif" width="19" height="16" alt="PDF" align="top">&nbsp;');
            }
            */
            if (
								(this.href.match(/^http/))
								&& 
								(
										(! this.href.match(host))
										&& 
										(
												((!this.href.match(/^https?:\/\/[^\/]*\.gov$/)) && (!this.href.match(/^https?:\/\/[^\/]*\.gov\//)))
												||
												(this.href.match(/\.louisiana\.gov/))
										)
										&& 
										(! this.href.match(/\.mil/))
										&&
										(! this.href.match(/\.oh\.us/))
								)
								&& 
								(! this.href.match(/^javascript/))
            ) {

				$a.addClass('thickbox').addClass('external');
                $a.attr('href', '#TB_inline?height=240&width=400&inlineId=tb_external');
                $a.attr('title', '');
				tb_init(this);

                // on click, add external link code to the thickbox
                $a.click(function () {
				  var link = $(this);
                  jQuery('#tb_external_thelink')
                   .before('<p id="tb_external_thelink"><a id="tb_link" href="' + href + '">' + href + '</a></p>')
                   .remove();
				  $("div#TB_closeAjaxWindow").before("<div id='TB_ajaxWindowTitle'><a id='ExLink' href='" + href + "'>External Link</a></div>");
				  $("a#ExLink").focus();
				  openNew = function(){
				  	window.open(href);
					tb_remove();
					return false;
				  }
				  $("a#tb_link").click(openNew);
				  $("a#ExLink").click(openNew);
                });
            }
        }
    });
});
