CntxLinks = {
	ACTIVE:true,
	ttID: 'trrToolTip',
	ttLT: 'SearchKey_Text', //Dejar vacio para Contextualizar TODO el contenido HTML
	ContentSite: 'http://buscador.terra.com.mx/LinkRedirection.aspx?ca=t&query=',
	DictSite: 'http://wsmoc.terra.com.mx/wsbuscador/Acel.asmx/BuscaTermino?term=',
	exact: true,
	onload: true,
	followMouse: true,
	showDelay: 200,
	hideDelay: 1000,
	offSetW: 2,
	offSetH: 12,
	IE:document.all,
	NS:document.getElementById && !document.all,
	overL: false,
	overT: false,
	tooltip:null,
	onMouseMove:null,
	activeLink:"",
	dwnlData:false,
	Cache: new Array,
	debug:false
};

CntxLinks.Body = function() {
	return (document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body;
}

CntxLinks.isArray = function(obj) {
	if(CntxLinks.isObject(obj) && obj.constructor == Array)
		return true;
	else
		return false;
}

CntxLinks.isFunction = function(obj) {
	if(typeof obj !="undefined" && typeof obj =="function")
		return true;
	else
		return false;
}

CntxLinks.isObject = function(obj) {
	if(typeof obj !="undefined" && typeof obj =="object")
		return true;
	else
		return false;
}

CntxLinks.isNumber = function(obj) {
	if(typeof obj !="undefined" && typeof obj =="number")
		return true;
	else
		return false;
}

CntxLinks.isText = function(obj) {
	if(typeof obj !="undefined" && typeof obj =="string")
		return true;
	else
		return false;
}

CntxLinks.MouseMove = function(active) {
	if (CntxLinks.onMouseMove!=null && active)
	{
		document.onmousemove = function(ev) {
			CntxLinks.onMouseMove(ev);
			CntxLinks.Move(ev);
		};
	}
	else if(active)
		document.onmousemove=CntxLinks.Move;
	else
		document.onmousemove=CntxLinks.onMouseMove;
}

CntxLinks.Show = function(key, str, color, wh) {
	if (CntxLinks.tooltip!=null && (CntxLinks.NS||CntxLinks.IE)) {
		if (CntxLinks.isNumber(wh))
			CntxLinks.tooltip.style.width=wh+"px";
		if (CntxLinks.isText(color) && color.length>0)
			CntxLinks.tooltip.style.backgroundColor=color;
		if(CntxLinks.activeLink!=key.toLowerCase())
		{
			CntxLinks.activeLink=key.toLowerCase();
			if(typeof CntxLinks.Cache[CntxLinks.activeLink]!='undefined')
			{
				CntxLinks.tooltip.innerHTML = CntxLinks.Cache[CntxLinks.activeLink];
				CntxLinks.dwnlData = true;
			}
			else
			{
				CntxLinks.getDef(key);
				//CntxLinks.tooltip.innerHTML=str; //Go get definition
				CntxLinks.dwnlData=false;
			}
		}
		CntxLinks.overL=true;
		if(CntxLinks.tooltip.style.visibility!='visible' || CntxLinks.followMouse || !CntxLinks.dwnlData)
			CntxLinks.MouseMove(true);
		return false;
	}
}

CntxLinks.ShowTooltip =	function() {
	if(CntxLinks.overL)
		CntxLinks.tooltip.style.visibility='visible';
	if(!CntxLinks.followMouse || CntxLinks.dwnlData)
		CntxLinks.MouseMove(false);
}

CntxLinks.Move = function (e) {
	var mouseX = CntxLinks.NS ? e.pageX : window.event.x + CntxLinks.Body().scrollLeft;
	var mouseY = CntxLinks.NS ? e.pageY : window.event.y + CntxLinks.Body().scrollTop;
	var mouseMaxX = CntxLinks.NS ? window.innerWidth+window.pageXOffset : CntxLinks.Body().clientWidth+CntxLinks.Body().scrollLeft;
	var mouseMaxY = CntxLinks.NS ? window.innerHeight+window.pageYOffset : CntxLinks.Body().clientHeight+CntxLinks.Body().scrollTop;
	var freeX = CntxLinks.IE && !window.opera ? CntxLinks.Body().clientWidth-event.clientX-CntxLinks.offSetW : window.innerWidth-e.clientX-CntxLinks.offSetW-20;
	var freeY = CntxLinks.IE && !window.opera ? CntxLinks.Body().clientHeight-event.clientY-CntxLinks.offSetH : window.innerHeight-e.clientY-CntxLinks.offSetH-20;
	var twentieth3 = (CntxLinks.offSetW<0) ? CntxLinks.offSetW*(-1) : -960;

	if (CntxLinks.overL && CntxLinks.isObject(CntxLinks.tooltip)) {
		if (freeX<CntxLinks.tooltip.offsetWidth)
			CntxLinks.tooltip.style.left = CntxLinks.IE ? CntxLinks.Body().scrollLeft+event.clientX-CntxLinks.tooltip.offsetWidth+"px" : window.pageXOffset+e.clientX-CntxLinks.tooltip.offsetWidth+"px";
		else if (mouseX<(twentieth3 + 5))
			CntxLinks.tooltip.style.left = "5px";
		else
			CntxLinks.tooltip.style.left = mouseX+CntxLinks.offSetW+"px";

		if (freeY<CntxLinks.tooltip.offsetHeight)
			CntxLinks.tooltip.style.top = CntxLinks.IE ? CntxLinks.Body().scrollTop+event.clientY-CntxLinks.tooltip.offsetHeight-CntxLinks.offSetH+"px" : window.pageYOffset+e.clientY-CntxLinks.tooltip.offsetHeight-CntxLinks.offSetH+"px";
		else
			CntxLinks.tooltip.style.top = mouseY+CntxLinks.offSetH+"px";

		if(CntxLinks.tooltip.style.visibility!='visible' || CntxLinks.dwnlData)
			window.setTimeout("CntxLinks.ShowTooltip();", CntxLinks.showDelay);
	}
	if(CntxLinks.debug)
		window.status = "xMousePos=" + mouseX + ", yMousePos=" + mouseY + ", xMousePosMax=" + mouseMaxX + ", yMousePosMax=" + mouseMaxY + ", TTx=" + CntxLinks.tooltip.style.left + ", TTy=" + CntxLinks.tooltip.style.top;
}

CntxLinks.Hide = function () {
	if (CntxLinks.tooltip!=null && (CntxLinks.NS||CntxLinks.IE)) {
		CntxLinks.overL=false;
		CntxLinks.overT=false;
		if(CntxLinks.followMouse)
			CntxLinks.MouseMove(false);
		window.setTimeout("CntxLinks.HideTooltip();", CntxLinks.hideDelay);
	}
}

CntxLinks.HideTooltip =	function() {
	if(!CntxLinks.overL && !CntxLinks.overT)
		CntxLinks.tooltip.style.visibility="hidden";
}
	
CntxLinks.MakeCall = function(key) {
	if(typeof CntxLinks.Cache[key]=='undefined')
	{
		CntxLinks.tooltip.innerHTML =  "Cargando...";
		CntxLinks.MouseMove(false);
		CntxLinks.dwnlData = true;
		var content=document.createElement('script');
		content.setAttribute('type','text/javascript');
		var compKey;
		if(key.indexOf(' ')>-1 && key.indexOf('"')==-1)
			compKey = '"'+key+'"';
		else
			compKey = key;
		content.setAttribute('src',CntxLinks.ContentSite+escape(compKey));		
		document.body.appendChild(content);
	}
	else
		CntxLinks.tooltip.innerHTML = CntxLinks.Cache[key];
}

CntxLinks.getDef = function(key) {
	if(typeof CntxLinks.Cache[key]=='undefined')
	{
		CntxLinks.MouseMove(false);
		CntxLinks.dwnlData = true;
		var content=document.createElement('script');
		content.setAttribute('type','text/javascript');
		content.setAttribute('src',CntxLinks.DictSite+escape(key));
		document.body.appendChild(content);
	}
	else
	{
		CntxLinks.tooltip.innerHTML = CntxLinks.Cache[key];
	}
}

CntxLinks.ContentLoaded = function() {
	CntxLinks.Cache[CntxLinks.activeLink] = CntxLinks.tooltip.innerHTML;
}

CntxLinks.Click = function(a) {
	// Ini Click Count on Terra Mx
	var p=(a.protocol)?a.protocol.toLowerCase():"",target="";
	if (p&&p!="mailto:"&&p!="javascript:") {
		var fullA = a.href;
		var c=fullA.indexOf("?",0);
		if(c>0)
			target = escape(fullA.substring(0,c));
		else
			target = escape(fullA);
		if(target!="") {
			var img=new Image;
			img.src='http://rt.terra.com.mx/TD.asp?idRND='+Math.random()+'&bstat=ts01&type=CLK&typever=1&id=clksl&host='+location.hostname+'&url='+location.pathname+'&target='+target+'&estat=ts01';
		}
	}
	return true;
	// Fin Click Count on Terra Mx
}

CntxLinks.hiliteHTML = function(html, query) {
	var re = new Array();
	for (var i = 0; i < query.length; i ++) {
		query[i] = query[i].toLowerCase();
		if (CntxLinks.exact)
			re.push('\\b'+query[i]+'\\b');
		else
			re.push(query[i]);
	}
	re = new RegExp('('+re.join("|")+')', "gi");
	var infoText = typeof msgTT != 'undefined' && CntxLinks.isText(msgTT) ? msgTT : "Noticias de";
	var subs = '<a href="javascript:CntxLinks.MakeCall(\'$1\')" class="SearchKey_Href" onMouseover="CntxLinks.Show(\'$1\', \''+infoText+' $1 \');" onMouseout="CntxLinks.Hide();">$1</a>';	
	
	var last = 0;
	var tag = '<';
	var skip = false;
	var skipre = new RegExp('^(script|style|textarea|a|object)', 'gi');
	var part = null;
	var result = '';
	
	while (last >= 0) {
		var pos = html.indexOf(tag, last);
		if (pos < 0) {
			part = html.substring(last);
			last = -1;
		} 
		else {
			part = html.substring(last, pos);
			last = pos+1;
		}
		
		if (tag == '<') {
			if (!skip)
			{
				if(part.replace(/^\s*|\s*$/g,"").length>0)
					part = part.replace(re, subs);
			}
			else
				skip = false;
		} 
		else if (part.match(skipre))
			skip = true;
		else if(part.toLowerCase().indexOf('span')==0 && part.indexOf('googlesyndication.com')>-1)
		{	//Google Syndication Contextual Ad's
			last = html.toLowerCase().indexOf('</span', last) + 6;
			part += html.substring(pos, last);
			last++;
		}

		result += part + (pos < 0 ? '' : tag);
		tag = tag == '<' ? '>' : '<';
	}
	return result;
};

CntxLinks.hiliteElement = function(elm, query) {
	if(CntxLinks.debug)
		window.status = "Inicia Indexación de Ligas";
	if (!query)
		return;
	var oldhtml = elm.innerHTML;
	var newhtml = CntxLinks.hiliteHTML(oldhtml, query);
	if (oldhtml != newhtml)
		elm.innerHTML = newhtml;
	if(CntxLinks.debug)
		window.status = "Termina Indexación de Ligas";
};

CntxLinks.hilite = function() {
	var e = null;
	if (CntxLinks.IE || CntxLinks.NS)
		CntxLinks.tooltip = document.all ? document.all[CntxLinks.ttID] : document.getElementById ? document.getElementById(CntxLinks.ttID) : null;
	if(CntxLinks.tooltip!=null)
	{
		if ((e = document.getElementById('content')) || (e = document.getElementById('container')) || (e = document.body))
		{
			if(CntxLinks.ttLT=='')
			{
				// Cuando se quiere contextualizar TODO el html de la página
				CntxLinks.hiliteElement(e, linkKeywords);
			}
			else
			{
				// Cuando existe como elemento único
				var objTextoLinkeable = document.getElementById(CntxLinks.ttLT);
				if(objTextoLinkeable != null)
					CntxLinks.hiliteElement(objTextoLinkeable, linkKeywords);

				// Cuando existe como elemento dividido en partes
				var skt = 1;
				objTextoLinkeable = document.getElementById(CntxLinks.ttLT+skt);
				while(objTextoLinkeable != null)
				{
					CntxLinks.hiliteElement(objTextoLinkeable, linkKeywords);
					skt++;
					objTextoLinkeable = document.getElementById(CntxLinks.ttLT+skt);
				}
			}

			if(typeof offSetX != 'undefined' && CntxLinks.isNumber(offSetX))
				CntxLinks.offSetW = CntxLinks.offSetW + offSetX;
			if(typeof offSetY != 'undefined' && CntxLinks.isNumber(offSetY))
				CntxLinks.offSetH = CntxLinks.offSetH + offSetY;

			if(CntxLinks.debug)
				document.onmousemove = CntxLinks.Move;

			//ToolTip
			if (document.layers) // Netscape
	    		document.captureEvents(Event.MOUSEMOVE);
			CntxLinks.onMouseMove = document.onmousemove;
			if (CntxLinks.IE || CntxLinks.NS)
				CntxLinks.tooltip = document.all ? document.all[CntxLinks.ttID] : document.getElementById ? document.getElementById(CntxLinks.ttID) : "";
		}

	}
};

if (typeof dictKeywords!="undefined" && typeof dictKeywords=="object" && dictKeywords.constructor==Array)
{
	linkKeywords = linkKeywords.concat(dictKeywords);
}

if(CntxLinks.ACTIVE && typeof linkKeywords != 'undefined' && CntxLinks.isArray(linkKeywords) && linkKeywords.length>0)
{
	if (CntxLinks.onload)
	{
		if (window.onload)
		{
			CntxLinks._old_onload = window.onload;
			window.onload = function(ev) {
				CntxLinks._old_onload(ev);
				CntxLinks.hilite();
			};
		} 
		else 
			window.onload = CntxLinks.hilite;
	}
	else
		CntxLinks.hilite();
}
else if(CntxLinks.debug)
	window.status = "No entro a cargar";

