/*
**  Plugin para abrir enlaces externos
*/

(function($) {
    $.fn.extend({
        enlacesExternos: function (options) {

			var defaults = {
				mostrarIcono: false,
				modoImagen: false,
				rutaIcono: "/img/interface/ico-enlaces-externos.png",
				mostrarTitle: true,				
				title: "Este enlace se abrirá en una ventana nueva",
				abrirVentana: true
			};
			var options = $.extend(defaults, options);
            			
			var prefijoUrl = String(document.location)
                .replace(/^(https?:\/\/[^:\/]+).*$/, "$1")
                .replace(/^((site)?file:\/\/.+\/)[^\/]+$/, "$1")
                .replace(/(\\.)/g, "\\$1");
            var nombreDominio = String(document.location)
                .replace(/^/, "X")
                .replace(/^X(https?|ftp):\/\/([^:\/]+).*$/, "$1")
                .replace(/^X.*$/, "")
                .replace(/(\\.)/g, "\\$1");

			//$("a", this) //esto serviria para seleccionar los "a" dentro de this, la opción usada debe recibir directamente los "a"
			$(this) //a partir de aqui filtramos los enlaces:
				.filter(function (i) { //solo enlaces externos
					var href = $(this).attr("href");
					if (href == null)
						return false;
					return (
						   href.match(RegExp(
							   "^(" + prefijoUrl + ".*" +
							   (nombreDominio != "" ? ("|" + "(https?|ftp)://" + nombreDominio + "([/:].*)?") : "") +
							   "|" + "((https?|ftp):)?/[^/].*" +
							   ")$"
						   )) == null
						&& href.match(RegExp("^(https?|ftp)://.+")) != null
					);
				})
				.not(".noExterno") //si el enlace posee la clase "noExterno", no se aplica el plugin
				.each(function () { //recorremos los enlaces una vez filtrados
					//PARAMETROS
					if( $(this).not(":has(img)") ) { //si no tiene imagen dentro del enlace, habilitamos la opcion de mostrar icono
						if(options.mostrarIcono == true) { //mostramos icono si/no?
							if(options.modoImagen == false) { //mostramos icono como background del enlace
								$(this)
									.css("backgroundImage",    "url("+options.rutaIcono+")")
									.css("backgroundRepeat",   "no-repeat")
									.css("backgroundPosition", "right center")
									.css("padding-right",      "11px");
							} else if(options.modoImagen == true) { //añadimos el icono como etiqueta img detras del enlace, para evitar sobreescribir backgrounds
								$(this).after('&nbsp;<img src="' + options.rutaIcono + '" alt="' + options.title + '" title="' + options.title + '">');
							}
						}
					}
					if(options.mostrarTitle == true) { //mostramos title en el enlace, si ya existe, se añade antes de lo existente
						var titulo = $(this).attr("title");
						if( titulo == "" ){
							$(this).attr("title", options.title);
						} else {
							$(this).attr("title", options.title+": "+titulo);
						}
					}
					if(options.abrirVentana == true) {
						$(this).click(function(){
							window.open(this.href);
							return false;
						});
					}

				});
        }
    });
})(jQuery);