﻿//=====================================================
// H G   L I N K
//=====================================================

//
// create closure
//
(function($) {

 	//=====================================================
 	// plugin definition
	//
	$.fn.hgLink = function(options) {

		//debug(this.size());

		// build main options before element iteration
		//var opts = $.extend({}, $.fn.hgMenu.defaults, options);

		//=====================================================
		// iterate and reformat each matched element
		return this.each(function(i) {

			// build element specific options
			//var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

			var $this = $(this);
			var hr = $this.attr('href');
			var m;

			if (hr == undefined)
				return;

			if (m = hr.match(/^mailto:([\d,]+)$/i))
			{
				//debug( m );
				var i, ms = '', ma;
				ma = m[1].split(',');
				for(i in ma)
					ms += String.fromCharCode(ma[i]-i);
				$this.attr('href', 'mailto:'+ms);
				if ($this.text() == m[1])
					$this.text(ms);
			}

		});

	};

	//
 	// plugin defaults
	//
	$.fn.hgLink.defaults = {
		background: 'yellow'
	};


	//=====================================================
 	// plugin private methods definition
	//

 	//=====================================================
 	// private function for debugging
	//
	function debug(o, msg) {
		var s = '['+(typeof o) + "]:\n", v;
		if (typeof o == 'object') {
			for( k in o ) {
				switch(typeof (o[k])) {
				case 'object':
				case 'function':
					v = '['+(typeof (o[k]))+']';
					break;
				case 'string':
					v = o[k].length > 50 ? o[k].substr(0, 46) + ' ...' : o[k];
					break;
				default:
					v = o[k];
				}
				s += k + ': ' + v + "\n";
			}
		}
		else
			s += o;

		alert( (msg ? msg + "\n" : '' ) + s );
	};


//
// end of closure
//
})(jQuery);


//
// run on document ready
//
jQuery(function($){
	   $('a').hgLink();
});


