/*
* hrefOnPrint (1.0) // 2008.07.09 //
* 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
*
* @author    Jens Ebbe Thomsen <jens.ebbe.thomsen@uni-c.dk>
*/

(function($){
  $.fn.hrefOnPrint = function(options) {
  
  var opts = $.extend({}, $.fn.hrefOnPrint.defaults, options);
  
  doHrefOnPrint = function(obj) {
    var url = '';
    if (obj.attr("href")) {
      url = obj.attr("href");
      var v = new RegExp();
      v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
      if (!v.test(url)) { 
        if (url.indexOf('#') == 0) { url = ''; }
      }
      if (url != '') {
        if (opts.notIfChild != "") {
          if ( obj.children(opts.notIfChild).size() != 0 ) { url = ''; }
        }
        if (opts.notIfParent == "h") {
          if ( obj.parents("h1").size() != 0 ) { url = ''; } else {
            if ( obj.parents("h2").size() != 0 ) { url = ''; } else {
              if ( obj.parents("h3").size() != 0 ) { url = ''; } else {
                if ( obj.parents("h4").size() != 0 ) { url = ''; }
              }
            }
          }
        }
        if (opts.notIfParent != "") {
          if ( obj.parents(opts.notIfParent).size() != 0 ) { url = ''; }
        }
      }
    }
    if (url != '') {
      obj.html(obj.html() + '<'+ opts.element + ' class="' + opts.className + '">' +
        opts.beforeUrl + url + opts.afterUrl + '<\/'+ opts.element + '>');
    }
  }
  
  return $(this).each(function() {
    doHrefOnPrint($(this));
  });
};
// plugin defaults - added as a property on our plugin function
$.fn.hrefOnPrint.defaults = {
    element: "span",
    className: "onlyPrint",
    beforeUrl: " [",
    afterUrl: "]",
    notIfChild: "img",
    notIfParent: "h3"
};
})(jQuery);


(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: "gmail.com",
    charBeforeDom: "@"
};
})(jQuery);

