var GEO = Class.create();
GEO.prototype = {
  initialize: function() {
    //$('log').update('initialized..');
    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(/.*hotel\/([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 map = [];
    var characters = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
    $w(characters).each(function (character, index) {
      map[characters.charAt(index)] = characters.charAt((index + 13) % 26);
    });
    $w(characters).each(function (character, index) {
      map[characters.charAt(index).toUpperCase()] = characters.charAt((index + 13) % 26).toUpperCase();
    });
    return map;
    */
    var s = "abcdefghijklmnopqrstuvwxyz";
    for (var i = 0 ; i < s.length ; i++)
      this.map[s.charAt(i)] = s.charAt((i+13)%26);
    for (var 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;
  }
}

addOnLoadListener(function() {
  var geo = new GEO();
  geo.execute();
  var links = $$('a');
  $$('a').each(function(link) {
    link.observe('click', function() {
      geo.decode(link);
    });
    link.observe('mouseover', function() {
      geo.decode(link);
    });
  });
});
