/*
 * jquery-link 0.3 (21-6-2007)
 *
 * Copyright (c) 2007 Queli Coto (http://www.noth.es)
 * Lincencia bajo CC
 * Built upon jQuery 1.2 (http://jquery.com)
 */


/*
 * Convierte todos los enlaces externos de forma sencilla, para su 
 * uso accesible
 *
 * @name     linksAcesibles
 * @author   Queli (http://www.noth.es)
 * @example  // en la primera linea dentro del document ready pondremos $().linksAccesibles();
 *
 */
jQuery.fn.ALLinksAccesibles = function(favicon)
{ 
	{
	  $('a').each(function(i){
	  	var href =$(this).attr('href');
	  	
      if (!href) href='dummy';

      var thisclass =$(this).attr('class');

  		var RegExPatternRSS ='(/rss/)';
      
      //para enlaces externos 
      var RegExPattern ='^(ht|f)tp(s?)';
			
			
      if(thisclass=='noicon' || thisclass=='rss_a')
			{
        // ej: aggregator icons !
      }
			else if( href.match(RegExPatternRSS))
			{
				$(this).addClass('rss');
			}
      else
      if(href.match(RegExPattern))
			{
				enlace = $(this);
				$(this).attr('target','_blank');
				//para explorer 6
				
				if(favicon)
				{
					var domain=href.split("/");
					//favicom
					//alert(domain[0]+'/'+domain[1]+'/'+domain[2]+'/favicon.ico') ;
					var img = new Image; 
					img.src = domain[0]+'/'+domain[1]+'/'+domain[2]+'/favicon.ico'; 
					$(enlace).css({  "background": "url("+ img.src+") left center no-repeat" , "padding-left": "20px"});
					img.onerror = function () {
						//alert(this.src);
						$(enlace).addClass('external');
	        }
				}
				else
				{
					//$(enlace).addClass('external');
				}
				return;
			}

		//hack para el ie6, colocar las clases para los iconos
		var RegExPattern ='(.pdf)$';
			if(href.match(RegExPattern))
			{
				//para explorer 6
				$(this).addClass('pdf');
			}
		
    var RegExPattern ='^(mailto:)';
			if(href.match(RegExPattern))
			{
				//para explorer 6
				$(this).addClass('mailto');
			}
		
    var RegExPattern ='(.zip)$';
			if(href.match(RegExPattern))
			{
				//para explorer 6
				$(this).addClass('zip');
			}				
	  });
	}
	
	
	  
};

jQuery.fn.linkAccesible = function()
{
	
	if(arguments.length >= 0) 
	{
		this.attr('target','_blank');
		return this;
	}
	else
	{
		return this;
	}
};

$(document).ready(function() {
	$().ALLinksAccesibles(true);
});