var inTextSiteName="";
var inTextSiteURL="";
var baseClass="taxInTextAdHover";

function hasClass(target, theClass) {
	var pattern = new RegExp("(^| )" + theClass + "( |$)");
	if (pattern.test(target.className)) {
		return true;
	}
	return false;
};

function elementY(obj) {
	var top=obj.offsetTop;
	var op=obj.offsetParent;
	while (obj.parentNode && document.body!=obj.parentNode) {
		obj=obj.parentNode;
		if(!isNaN(obj.scrollTop)) {
			top-=obj.scrollTop;
		}
		if(op==obj) {
			top+=obj.offsetTop;
			op=obj.offsetParent;
		}
	}
	return top;
}

function pageScrollY() {
	return document.body.scrollTop || document.documentElement.scrollTop;
}

function trackClick(linkName) {
	if ( typeof s_gi != 'undefined' ) { 
		var s=s_gi('tribglobal'); 
		s.linkTrackVars='eVar23,server'; 
		s.eVar23=inTextSiteURL + ':' + linkName + ' Taxonomy Ad'; 
		s.server=inTextSiteURL; 
		s.tl(true,'o', linkName + ' Taxonomy Ad' );
	}	
}

document.onclick=function(){taxInTextOut(null);};

var hideInText=0;
var lastAd=null;

function upOrDown(elemY,hHeight) {
	return (elemY - hHeight - 10) <= pageScrollY();
}

function taxInTextOver(obj) {
	if (lastAd==null || lastAd == obj) {
		clearTimeout(hideInText);
		hideInText=0;
	} else {
		hideAd(lastAd);	
	}
	lastAd=obj;
	var hov=document.getElementById('inText_'+obj.id);
	var up=true;
	if (hov) {
		if (upOrDown(hov.getAttribute("name"), hov.getAttribute("hh"))) {
			hov.className = baseClass + " up";
			hov.style.top=hov.getAttribute("up")+"px";
		} else {
			up=false;
			hov.className = baseClass + " dn";
			hov.style.top=hov.getAttribute("dn")+"px";
		}
		hov.style.display="block";
		if (document.all && !window.opera) {
			hov.firstChild.firstChild.firstChild.firstChild.style.visibility=up?"visible":"hidden";
			hov.firstChild.firstChild.firstChild.lastChild.style.visibility=up?"hidden":"visible";
			document.getElementById('content').style.zIndex="60";
		}
	} else {
		var spn=obj.nextSibling;
		
		if (!spn.hasChildNodes()
			|| (spn.childNodes.length==1 && spn.firstChild.nodeType == 8)) { // if span is emtpy or only contains a comment
			return false;
		}
		
		var elemY=elementY(obj);
		
		var hov=document.createElement("div");
		hov.setAttribute("id","inText_"+obj.id);
		hov.setAttribute("name",elemY); // store the y-coord of this link in the name attribute of div for fast reference later
		
		var spanInner=spn.innerHTML.replace(/(href=\"[^\"]*\")/ig,"$1 onclick=\"trackClick('"+obj.title+"');\"");

		var hoverInner = '<div class="taxInTextShadow2"><div class="taxInTextShadow3">';
		hoverInner += '<div class="taxInTextAdBlock"><div class="inTextArrowUp">&nbsp;</div>';
		hoverInner += '<div class="inTextTop"><a href="'+obj.href+'">'+obj.innerHTML+'</a><br/>';
		hoverInner += inTextSiteName + ' topic gallery</div>';
		hoverInner += '<div class="inTextBottom"><div class="inTextAdDisclaimer">Sponsored Link</div>';
		hoverInner += '<div class="inTextAdContent">' + spanInner + '</div></div>';
		hoverInner += '<div class="inTextArrowDn">&nbsp;</div></div>';
		hoverInner += '</div></div>';
		hov.innerHTML=hoverInner;
		hov.className=baseClass;
		
		var con = document.getElementById('article');
		con.appendChild(hov);
		
		var aWidth=obj.offsetWidth;
		var aHeight=obj.offsetHeight;
		var aLeft=obj.offsetLeft;
		var aTop=obj.offsetTop;
		var hWidth=hov.offsetWidth;
		var hHeight=hov.offsetHeight;
		
		hov.style.left=(aLeft + aWidth/2 - hWidth/2) + "px";
		var dnVal=aTop - hHeight - 10;
		var upVal=aTop + aHeight + 14;
		
		hov.setAttribute("up",upVal);
		hov.setAttribute("dn",dnVal);
		hov.setAttribute("hh",hHeight);
		
		if (upOrDown(elemY,hHeight)) {
			hov.className = baseClass + " up";
			hov.style.top=upVal+"px";
		} else {
			hov.className = baseClass + " dn";
			hov.style.top=dnVal+"px";
		} 
		
		hov.onmouseover=function(){taxInTextOver(obj);};
		hov.onmouseout=function(){taxInTextOut(obj);};
		hov.onclick=function(e){
				if (!e) 
					window.event.cancelBubble=true;
				else 
					e.stopPropagation();
			};
		obj.onclick=function(e){
				if (!e) 
					window.event.cancelBubble=true;
				else 
					e.stopPropagation();	
				taxInTextOver(obj);
				return false;
			};
		if (document.all && !window.opera ) {
			document.getElementById('content').style.zIndex="60";
		}
		hov.style.visibility="visible";
	}
}


function taxInTextOut(obj) {
	if (obj==null && lastAd==null)
		return;
	var timeOut=400;
	if (obj==null) {
		obj=lastAd;	
		timeOut=30;
	}
	hideInText=setTimeout(function(){hideAd(obj);},timeOut);
}

function hideAd(obj) {
	var hov=document.getElementById('inText_'+obj.id);
	if (hov) {
		hov.style.display="none";
		lastAd=null;
		if (document.all && !window.opera ) {
			document.getElementById('content').style.zIndex="3";
		}
	}
}

function taxInTextClick(obj) {
	// show ad, but if the ad content is empty, taxInTextOver will return false and instead we'll follow the link.
	if (taxInTextOver(obj)==false)
		window.location=obj.href;
	return false;
}