/***************************************
   Link Hints Plugin for  Special File Types and Google Analytics Tracking
   @author Karl Swedberg
   @version 0.2 (07/23/2008)
   @requires jQuery v1.2.6+
   
   @options 
   these are the defaults. you can change them to whatever you want.
   
   extensions:    'doc,xls,exe,zip,pdf,swf',
   hints:         'inline,summary', // one of null or inline or summary or inline,summary
   hintsInline:   {
                    init: true,
                    external: 'external', // set this to the img name, not including path (imgPre) or extension (imgPost); 
                    imgPre:   '/images/icons/',
                    imgPost:  '.png',
                    text:   '{type}',
                    hintClass: 'icon  {type}'
                  },
   hintSummary:   {
                    init: true,
                    text: 'The links below will attempt to either download a file or open it in another application',
                    pos: 'prepend',
                    hintClass: 'link-summary'

   }
   @examples
   **Change a default option globally:
        $.fn.linkhints.defaults.imgPre = 'images/';
   
   **Change the text and placement of a hint summary:
     $('#pdf').linkhints({
       hintSummary: {
        text: 'If you are unable to view the above files, you may have to install <a href="http://adobe.com/reader/">Adobe Reader</a>.' ,
        pos:  'append'
       }
     });

*/


(function($) {
$.fn.linkhints = function(options) {
  var opts = $.extend(true, {}, $.fn.linkhints.defaults, options);

  var replaceType = function(string, replacement) {
    return string.replace(/\{type\}/g,replacement);
  };

  return this.each(function(event) {
    var ext = opts.extensions.replace(/\s+/g,'').replace(/,/g,'|'),
      extPattern = new RegExp('\.(' + ext + ')$');
    var $container = $(this),
      inline = opts.hintsInline,
      summary = opts.hintSummary;
    
    // link hints...
    var aNum = 0;
    $('a', $container).each(function() {
      var $a = $(this),
        aPath = this.pathname,
        aExt = aPath.slice(aPath.lastIndexOf('.')),
        aType = aExt.slice(1),
        alt = replaceType(inline.text, aType.toUpperCase());          
      if (extPattern.test(aExt) 
        || (inline.external && !$('img', this).length && this.hostname !== location.hostname)
      ) {
        if (!extPattern.test(aExt)) {
          aType = inline.external;
        } else {
          aNum +=1;
        }
        if (inline && inline.init) {
          $a[inline.imgPlacement](inline.imgPost ? '<img class="' + replaceType(inline.hintClass, aType) + '" src="' + inline.imgPre + aType + inline.imgPost + '" alt="' + alt + '" />' : '<span class="' + inline.hintClass + '">' + alt + '</span>');
        }
      } 
    });
    if (aNum && summary && summary.init) {
      $container[summary.pos]('<div class="' + summary.hintClass + '">' + summary.text + '</div>');
    }
  });
};

$.fn.linkhints.defaults = {
  extensions:   'doc,xls,exe,zip,pdf,swf',
  hintsInline:  {
                  init: true,
                  mailto: 'mail', // set to null or img file name,
                                  // not including path (imgPre) or extension (pimgPost); 
                  external: 'external', // (same as mailto option above) 
                  imgPlacement: 'append',
                  imgPre:   '/images/',
                  imgPost:  '.png',
                  text:   '{type}',
                  hintClass: 'icon  {type}'
                },
  hintSummary:  {
                  init: true,
                  text: 'The links below will attempt to either download a file or open it in another application',
                  pos: 'prepend',
                  hintClass: 'link-summary'
  }
};

})(jQuery);
