var GEO = Class.create();
GEO.prototype = {
  initialize: function() {
    this.rot13 = true;
    this.map = [];
  },
  execute: function() {
    this.rot13init(); 
  },
  decode: function (anchor) { // function to recompose the orginal address
    var href = anchor.getAttribute('href');
    var address = href.replace(/.*keskus\/([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3');
    var linktext = anchor.innerHTML; // IE Fix
    if (href != address) {
      anchor.setAttribute('href', 'mailto:' + (this.rot13 ? this.str_rot13(address) : address)); // Add mailto link	
      anchor.innerHTML = linktext; // IE Fix
    }
  },
  rot13init: function() {
    var s = "abcdefghijklmnopqrstuvwxyz";
    for (var i = 0 ; i < s.length ; i++) {
      this.map[s.charAt(i)] = s.charAt((i+13)%26);
    }
    for (i = 0 ; i < s.length ; i++) {
      this.map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
    }
  },
  str_rot13: function(a) {
    var s = "";
    for (var i = 0 ; i < a.length ; i++) {
      var b = a.charAt(i);
      s += (b >= 'A' && b <= 'Z' || b >= 'a' && b <= 'z' ? this.map[b] : b);
    }
    return s;
  }
}

Event.observe(window, 'load', function() {
  var geo = new GEO();
  geo.execute();
  $$('a').each(function(link) {
    link.observe('click', function() {
      geo.decode(link);
    });
    link.observe('mouseover', function() {
      geo.decode(link);
    });
  });
});