function addTermTooltips()
{
	var strTerms = getTermsFromPage();
	
	if(strTerms.length > 0)
	{
		populateTooltips(strTerms);
	}
}

function getTermsFromPage()
{
	var strTerms = "";
				
	$("span[class='tooltip']").each(function(){
	
		var strTerm = $(this).text();
	
		strTerm = strTerm.replace(/ /gi, "_");
		strTerm = strTerm.replace(/[!@#$%^&*()']/gi, "");
		strTerm = strTerm.toLowerCase();
		
		var href = $("#glossary-link").attr("href") + "#"+strTerm;
		$(this).parent().addClass(strTerm);
		//var href = buildUrl("/glossary/index.htm#",strTerm);
		
		$(this).parent().attr('href',href);		
				
		strTerms = strTerms + "," + strTerm;
	});
	
	strTerms = strTerms.substring(1);
	
	return strTerms;
}

function populateTooltips(strTerms)
{	
	//ajax call to get the html from the page
	var htmlTerms = getHtml(strTerms);
	var arTerms = createTermsArray(strTerms);
	var arLen = arTerms.length;
	for(var i=0; i<arLen; i++)
	{
		htmlTooltip = getTermHtml(arTerms[i],htmlTerms);
		if(htmlTooltip != undefined)
		{
			populateTooltip(arTerms[i],htmlTooltip);
		}
	}
}


function populateTooltip(strTerm,htmlTermHtml)
{
	//alert("populateTooltip :term="+strTerm);
	//alert("htmlTerm = [" + htmlTermHtml + "]");
	//var strTermId = "#" + strTerm;
	
	var strTermId = "." + strTerm;
	
	//alert("populateTooltip :strTermId=" + strTermId);
	
	$(strTermId).qtip({
		content: htmlTermHtml,
		show: 'mouseover',
		hide: 'mouseout',
		position: {
		  corner: {
			 target: 'topRight',
			 tooltip: 'bottomLeft'
		  }
		},
		hide: { 
			fixed: true, 
			delay: 750,
			when: 'mouseout'
		}
	});
}

function createTermsArray(strTerms)
{
	if(strTerms.length > 0)
	{			
		var arTerms = new Array();
		arTerms = strTerms.split(",");
	}
	else
	{
		arTerms = undefined;
	}
	
	return arTerms;
}

function getTermHtml(strTerm,htmlTerms)
{	
	var patt = /[^a-zA-Z_0-9|^\x2C]/g;
	
	var strTermToFind = "#" + strTerm.replace(patt, "");
	//alert("TermToFind:" + strTermToFind);
	
	var htmlTermHtml = $(htmlTerms).find(strTermToFind).html();
	
	//alert("termhtml:"+htmlTermHtml);
	
	return htmlTermHtml;
}
		
function getHtml(strTerms)
{        
	var returnHtml;
	
	// in the future use var term passed in to
	// build the url so the static list can skip
	// all other terms.
	//var url = "http://wcm.medtronic.com/www.heart-failure.co.uk/glossary/glossary-lookup/index.htm?Terms=" + strTerms;
	//var url = "../glossary/glossary-lookup/index.htm?Terms=" + strTerms;
	
	//var url = buildUrl("/glossary/glossary-lookup/index.htm?Terms= +"?Terms=",strTerms);
	
	var href = $("#glossary-index-link").attr("href");
	//var href = href.replace(/glossary/ig, "glossary/glossary-lookup");
	var url = buildUrl(href + "?Terms=",encodeURI(strTerms));
	
	$.ajax({
		url: url,
		async: false,
		success: function(data) {
			//alert('Load was performed.');
			returnHtml = data;
			
		},
		error: function() { 
			returnHTML = "error";
			}
	});       	
	//alert(returnHtml);
	return returnHtml;
}

function buildUrl(urlBase, strTerms)
{
	var protocol = window.location.protocol;
	var hostname = window.location.hostname;
	//if(hostname.indexOf("wcm") > -1)
	//{
	//	//var pathname = window.location.pathname;
	//	//pathname = pathname.substring(0,pathname.indexOf("/",1));
	//	urlBase = hostname + urlBase;
	//}
	//var urlBase = protocol + "//" + hostname + pathname + urlBase + strTerms;
	var urlBase = protocol + "//" + hostname + urlBase + strTerms;
	
	return urlBase;
}

function formatGlossaryIndex()
{
	var arActiveLetters = getActiveLetters();
	buildGlossaryLinks(arActiveLetters);
}

function getActiveLetters()
{
	var lettersList = $("#linkedLetters").text();
	lettersList = lettersList.substring(1);
	
	var arActiveLetters = new Array();

	arActiveLetters = lettersList.split(",");
	
	return arActiveLetters;
}

function buildGlossaryLinks(arActiveLetters)
{
	$("#glossary-index").children().each(function(){
		var letter = $(this).text();
		letter = letter.toLowerCase();
		
		if($.inArray(letter,arActiveLetters) > -1)
		{
			var link = createLink(letter);
			$(this).text("");
			$(this).append(link);
		}
		else
		{
			$(this).attr('class','not-linked');
		}
			
	});
}

function createLink(letter)
{
	var newLink = document.createElement('a');
	var href = document.createAttribute('href');
	newLink.href = '#' + letter;
	newLink.innerHTML = letter.toUpperCase();
	
	return newLink;
}
