/**
 * Fix links script
 * 
 *   Replaces '(at)' and '(dot)' in mailto: links with '@' and '.'
 *   Makes external links opened in new window
 *   Adds 'external' and 'email' class names to the links 
 *
 *
 * The script is freely distributable under the terms of an MIT license.
 *
 *
 * @author Hristo Drumev
 * @license MIT license
 * @copyright Hristo Drumev [www.hdrumev.com]
 * @version 0.11 / 13.04.2010
 *
 *
 * >>> Do not needed any libraries or frameworks
 *
 * Usage : 1. type mail links with '(at)' and '(dot)' instead of '@' and '.'
 *            script automaticaly replace in mailto links '(at)' with '@' and '(dot)' with '.'
 *            ex.: admin(at)domain(dot)com => admin@domain.com
 *         2. all external links (begans with 'http' or 'ftp' or having attribute rel="external")
 *            excluding these in same domain will opened in new window (XHTML 1.0 Strict compatible)
 *         3. your external links will have class 'external' and email links class 'email',
 *            type CSS for this
 *         4. include this file in your (x)HTML page
 *
 */


( function() {

var links = document.getElementsByTagName( 'a' );
var host  = window.location.host.toLowerCase();

/**
 * Fix e-mail and external links
 *
 */
function fixLinks()
{
  for( var i = links.length; i--; )
  {
    var link = links[i];
    var href = ( link.getAttribute( 'href', 2 ) || '' ).toLowerCase();
    var rel  = link.getAttribute( 'rel' ) || '';

    // mailto links
    if( href.indexOf( 'mailto:' ) === 0 )
    {
      var html = link.innerHTML;
      href = href.replace( /\(at\)/i, '@' ).replace( /\(dot\)/i, '.' );
      html = html.replace( /\(at\)/i, '@' ).replace( /\(dot\)/i, '.' );
      link.setAttribute( 'href', href );
      link.innerHTML = html;
      link.className += ( link.className === '' ? '' : ' ' ) + 'email';
    }

    // external links
    else if( ( href.indexOf( '://' ) > -1 && 
               href.indexOf( '://' + host ) === -1 &&
               href.indexOf( '://www.' + host ) === -1 )
             || rel.indexOf( 'external' ) > -1 )
    {
      addEvent( link, 'click',
        function( event )
        {
          window.open( this.href );
          event.preventDefault ? event.preventDefault() : event.returnValue = false;
        } );
      link.className += ( link.className === '' ? '' : ' ' ) + 'external';
    }
  }
}

// ----------------------------------------------------------------------------

/**
 * Add event listener to the HTML element
 *
 * @param {HTMLElement} element - HTML element
 * @param {String} type - Type of the event
 * @param {Function} fn - The event listener
 */
var addEvent = ( function() {
  return document.addEventListener
  ? function( element, type, fn ) { element.addEventListener( type, fn, false ); }
  : function( element, type, fn ) { element.attachEvent( 'on' + type, function(){ fn.call( element, window.event ); } ); }
} )();

/**
 * DOM ready event listener
 *
 * @param {Function} fn - listener
 */
var domReady = function( fn )
{
  document.addEventListener
  ? document.addEventListener( 'DOMContentLoaded', fn, false )
  : ( function() { document.body ? fn() : setTimeout( arguments.callee, 0 ); } )();
}

// ----------------------------------------------------------------------------

domReady( fixLinks );

} )();
