/*
 * jQuery replaceText - v1.1 - 11/21/2009
 * http://benalman.com/projects/jquery-replacetext-plugin/
 * 
 * Copyright (c) 2009 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($){$.fn.replaceText=function(b,a,c){return this.each(function(){var f=this.firstChild,g,e,d=[];if(f){do{if(f.nodeType===3){g=f.nodeValue;e=g.replace(b,a);if(e!==g){if(!c&&/</.test(e)){$(f).before(e);d.push(f)}else{f.nodeValue=e}}}}while(f=f.nextSibling)}d.length&&$(d).remove()})}})(jQuery);
;(function($){
	var descs = {};
	$(document).ready(function(){
		//$('body').prepend('<p class="tooltip"><span class="tooltip-head"></span><span class="tooltip-content"></span></p>');
		$.ajax({'url':'/words.txt','dataType':'text','success':init_words});
	});
	function init_words(resp){
		resp = resp.split('\n');
		var rl = resp.length, reg = '';
		for(var i=0;i<rl;i++){
			var w = resp[i].split('":"');
			w[0] = w[0].substr(1);
			reg = new RegExp(w[0],'gi');
			descs[w[0].toLowerCase()] = [w[1].substr(0,(w[1].length-1)),0];
			$("body .entry-content *").filter(':not(a)').replaceText(reg, add_words );
		}
		init_tooltips();
	};
	function add_words(str){
		var desc = descs[str.toLowerCase()];
		if(desc[1] > 0){
			return str;
		} else {
			desc[1]++
			return '<span class="wtooltip" title="'+desc[0]+'">'+str+'</span>';
		}
	};
	function init_tooltips(){
		$('.wtooltip').each(function() {
			var $this = $(this);
			$this.data('title',$this.attr('title'));
			$this.removeAttr('title');
		});	
		$('.wtooltip').hover(function () {  
			$('.tooltip-content').html($(this).data('title'));
			$(".tooltip").css({top: $(this).position().top - $(".tooltip").outerHeight() - 10, left: $(this).position().left + 10 });
		});
		$(".wtooltip").hover(
			function() { $(".tooltip").css({ display: "inline-block" }); },
			function() { $(".tooltip").css({ display: "none" }); }
		);
	};
})(jQuery);