jQuery(document).ready(function(){   
  obscure_init("a[rel^='obscure']");//pass where to apply thickbox
});

function obscure_init(domChunk){
  if(jQuery(domChunk) != null){
   jQuery(domChunk).each(function(i){
    var g = this.rel || false;
    if(g != false){
      var obscureValues = g.replace(/obscure\[(.*)\]/, '$1').split(',');
      var name = obscureValues[0].replace(/'/g, ''); 
      var domain = obscureValues[1].replace(/'/g, '');
      jQuery(this).text(name + html_entity_decode("&#x24D0;") + domain); 
      if(obscureValues.length > 2){
        text = obscureValues[2].replace(/'/g, '');
        $(this).text(text); 
      }
      jQuery(this).click(obscure_mailto(name, domain, this));
    }
   });
  }
}

function Rint(n)                   // Random integer r: 1 <= r <= n 
{ return Math.floor( n*Math.random() + 1 );  
}      
function s2h(str)                  // Replaces chars randomly by three 
{ var s = "", i, z1 = 0, z2, cod;  // different HTML character notations,
  for( i = 0; i < str.length; i++) // namely direct notation,    
  { cod = str.charCodeAt(i);       // decimal notation, and
    z2 = Rint(3);                  // hexadecimal notation.
    while( z2 == z1 ) { z2 = Rint(3); } // Avoids repetitions.
    z1 = z2;
    switch( z1 )
    { case 1: s += "&#"  + cod.toString(10) + ";"; break;
      case 2: s += "&#x" + cod.toString(16) + ";"; break;
      case 3: s += String.fromCharCode(cod); break;
    } 
  }
  return s;
}



function html_entity_decode(str)
{
      jQuery("<div id='htmlconverter' style='display:none'><textarea id='innerConverter'>" + str + "</textarea></div>").appendTo("body");
      jQuery('#innerConverter').get().innerHTML = str;
      var value = document.getElementById('innerConverter').value;
      jQuery('#htmlconverter').remove();
      return value;
}


function obscure_mailto(Name, Domain, Element)
//       #######
//
// The three parameters Name, Domain, and Text are of type string,
// the third parameter 'Text' is optional. Its content replaces
// the otherwise displayed e-mail-address. 
//
{ 
   Name = s2h(Name);
   Domain = s2h(Domain);
   Element.href = html_entity_decode(s2h(decodeURIComponent("%6D%61%69%6C%74%6F%3A")) + Name + '&#x40;' + Domain);
   return true;
}


function obscure( Name, Domain, Text, OptionalAnchorParams)
//       #######
//
// The three parameters Name, Domain, and Text are of type string,
// the third parameter 'Text' is optional. Its content replaces
// the otherwise displayed e-mail-address. 
//
{ 
   Name = s2h(Name);
   Domain = s2h(Domain);
   Params = "";
   if( arguments.length < 3 )
   {
 	Text = Name + "&#x24D0;" + Domain;
   }
   if( arguments.length == 4 )
   {
	Params = OptionalAnchorParams;
   }
   document.write("<a href=\""
               + s2h(decodeURIComponent("%6D%61%69%6C%74%6F%3A"))
               + Name + '&#x40;' + Domain + "\""
	       + " " + Params + ">"
               + Text + "</a>");
}

