/*
* visSkrivTil (1.0) // 2008.07.08 //
* 
* REQUIRES jQuery 1.2.3+ <http://jquery.com/>, visSkrivTil
* 
* Copyright (c) 2008 Jens Ebbe Thomsen 
* Licensed under GPL and MIT licenses
*
* visSkrivTil make an email as href on mouseover and focus.
* 
* The html has to look like:
* <a href="http://www.abc.dk/formmail/" rel="jens+ebbe+thomsen/gmail+com"
*    title="Remember att. Jens Ebbe Thomsen" class="skrivtil">
*   Jens Ebbe Thomsen (GMAIL)
* </a>
* <!-- The href attribute points to a formmail in case javascript is disabled -->
* <!-- Information about the email is keept in the rel attribute -->
*
* Sample Configuration:
* $("a.skrivtil").visSkrivTil({Dom: "abc.dk", replace: "+"});
* 
* Result will be:
* <a href="mailto:jens.ebbe.thomsen@gmail.com" rel="jens ebbe thomsen/gmail com"
*   title="jens.ebbe.thomsen@gmail.com" class="skrivtil">
*     Jens Ebbe Thomsen GMAIL
*   </a>
* 
* Config Options:
* replace, replaceWith: replace all 'replace' with 'replaceWith' in rel
*                       // default: ' ' and '.'
* pseudoCharBeforeDom: if found in rel this char is replaced with charBeforeDom
*                      if not found then charBeforeDom and Dom is appended 
*                      // default: '/'
* Dom: email Domain // default: gmail.com
* charBeforeDom: has to be @, so don't change it
* 
* We can override the defaults with:
* $.fn.visSkrivTil.defaults.replaceWith = '';
* 
* @param properties  An object with the visSkrivTil parameters
* @param settings  An object with configuration options
* @author    Jens Ebbe Thomsen <jens[dot]ebbe[dot]thomsen[at]gmail[dot]com>
*/

(function($){
  $.fn.visSkrivTil = function(options) {
  
  var opts = $.extend({}, $.fn.visSkrivTil.defaults, options);
  
  doVisSkrivTil = function(obj) {
    if (obj.attr("rel")) {
      var e = obj.attr("rel");
      var i = "ilt";
      var regexp = eval ("/\\" + opts.replace + "/g");
      e = e.replace(regexp, opts.replaceWith);
      if (e.indexOf(opts.pseudoCharBeforeDom) != -1) {
        var regexp = eval ("/\\" + opts.pseudoCharBeforeDom + "/");
        e = e.replace(regexp, opts.charBeforeDom);
      } else {
        e = e + opts.charBeforeDom + opts.Dom;
      }
      obj.attr("href", 'ma' + i + 'o:' + e);
      obj.attr("title", e);
    }
  }
  
  return $(this).each(function() {
    $(this).bind("focus", (function(){
      doVisSkrivTil($(this));
    })).bind("mouseover", (function(){
      doVisSkrivTil($(this));
    }));
  });
};
// plugin defaults - added as a property on our plugin function
$.fn.visSkrivTil.defaults = {
    replace: " ",
    replaceWith: ".",
    pseudoCharBeforeDom: "/",
    Dom: "silkeborg.bib.dk",
    charBeforeDom: "@"
};
})(jQuery);
