$(document).ready(function () {	
	
if( window.rxs_navbase == undefined)
	rxs_navbase = "http://www.healthcare.gov/glossary/99445.html";

	var ajax = new Ajax();	
	if(window.location.href.indexOf("/glossary/") == -1  && window.location.href.indexOf("www.cuidadodesalud.gov") == -1  && window.location.href.indexOf("glossary.html") == -1 && window.location.href.indexOf("/news/videos/") == -1  && window.location.href.indexOf("/law/timeline/") == -1 && window.location.href.indexOf("/center/councils/nphpphc/") == -1){
		ajax.doGet(rxs_navbase + "/glossary/99445.html",function(str){
			
			var the_words = str.split("\n");
			var glossary;
			var the_words_check = new Array(the_words.length);
			
			
			for(i=0;i<the_words.length-1;i++){
//				if($("p").parents("[id=main]") {
//					alert("asdfadsf");
					if(the_words_check[i]) {continue;}
					glossary = the_words[i].split("|")
					glossary[0].replace(" ","\\s");
					//alert(glossary[0] + i + the_words_check[i]);
					//alert(glossary[0]);
					$("#main").find("p:contains('" + glossary[0] + "')").each(function(){
//					$("[id*=main]:contains('" + glossary[0] + "')").each(function() {
			//		 $("p:contains('Affordable Care Act')").parents("[class!=new_law_header]").each(function() {

						if(! the_words_check[i]){
							var reg = new RegExp("(" + glossary[0].replace(" ","\\s") + ")(\\W)", "");
							$(this).html($(this).html().replace(reg," <div class='wglossary'><div class='def_link'>$1<div class='definition'><div class='def_middle'><div class='def_body'><b>" + glossary[0] + "</b><br/>" + glossary[1] + "</div><!-- end def_body --></div><!--end def_middle --><div class='def_bottom'>&nbsp;</div></div></div></div>$2"));
						the_words_check[i] = true;
						}
					
					});
	//			}
			}
			
			$("a div.wglossary").each(function(){			
				$(this).children("div.def_link").children("div.definition").detach();
				$(this).children("div.def_link").replaceWith( $(this).children('div.def_link').text() );
				$(this).replaceWith($(this).text());
			});
			
			$("div.wglossary div.wglossary").each(function(){			
				$(this).children("div.def_link").children("div.definition").detach();
				$(this).children("div.def_link").replaceWith( $(this).children('div.def_link').text() );
				$(this).replaceWith($(this).text());
			});
			
			
			$("div.wglossary div.def_link").each(function(){
				var position = $(this).position();
				var def = $(this).find('div.definition');
				var posx = $(this).offset().left;
				var wsize = $(window).width() /2 + 80;				
				def.css("top",(position.top - def.height() - 4) + "px");				
				
				if(posx < wsize){										
					def.css("left",position.left + "px");					
				}
				else{										
					def.addClass("def_right");
					def.css("left",(position.left - 310) + "px");
				}
				
				$(this).replaceWith("<a href='#' class='glossary_link' onclick='return false'>" + $(this).html() + "</a>");
			});
		}); 
		
		//end doget
	}
});
