/*
jquery.outsourceIt.js
Copyright 2010 Hugo Soucy <hugo@soucy.cc>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/

(function($) {

    $.fn.outsourceIt = function(params) {

        params = $.extend( {extLinkIcon: false, allowSubDomain:false}, params);

		this.find("a").each(function() {

				//var regExpDomain = /\W[a-z0-9]{2,65}\.[a-z]{2,6}$|\W[a-z0-9]{2,65}\.[a-z]{2,6}\.[a-z]{2}$/;
				var regExpDomain = /[\.\/][a-z0-9\-_]{2,65}\.[a-z]{2,6}$|[\.\/][a-z0-9\-_]{2,65}\.[a-z]{2,6}\.[a-z]{2}$/;
				var docURI = document.location.hostname; //Nom de domaine du document courant
				var hrefValue = this.href; //l'URL complet de l'hyperlien actif

				var extracDomain = docURI.match(regExpDomain);

				if(!(hrefValue.match(docURI))){
					$(this).attr("target","_blank");

					if(params.extLinkIcon==true){
						$(this).after("<span class='extLinkIcon'>&thinsp;<img src='icon-external-link.png'/></span>");
					}

					if(params.allowSubDomain==true){
						if(hrefValue.match(extracDomain)){
							$(this).removeAttr("target");
							$(this).next(".extLinkIcon").remove();
						}
					}

				}

		});

		return $(this);

    };
})(jQuery);

