var aDefinedTerms = [
	{
		term: /gore(-| )*tex\S* xcr\S*/i,
		definition: "The latest version of the GORE-TEX system keeps your feet in the ideal foot climate comfort zone.",
		url: "golf.aspx#goreTexXCR"
	},
	{
		term: /direct(-| )*inject\S*/i,
		definition: "Gain a lightweight but strong outsole.",
		url: "outsole.aspx#directinject"
	},
	{
		term: /polyurethane \(PU\)/i,
		definition: "Enjoy high shock absorption with minimal thickness.",
		url: "outsole.aspx#polyurethane"
	},
	{
		term: /\S*TPU(\/PU)?\S*/i,
		definition: "Stay comfortable with all-day dual-density support that's flexible but durable.",
		url: "outsole.aspx#tpupu"
	},
	{
		term: /vibram\S*/i,
		definition: "Expect high performance no matter the temp or terrain.",
		url: "outsole.aspx#vibram"
	},
	{
		term: /latex/i,
		definition: "Appreciate natural shock absorption from a thin but strong material.",
		url: "footbed.aspx#latex"
	},
	{
		term: /rubber outsoles?/i,
		definition: "Jump into action with both traction and support.",
		url: "outsole.aspx#rubber"
	},
	{
		term: /comfort fib[a-z][a-z]/i,
		definition: "Keep feet dry and cool with moisture-absorbent foam.",
		url: "footbed.aspx#comfortfibre"
	},
	{
		term: /cork \S*/i,
		definition: "Stay cool, comfortable, and supported with the lightweight combination and good toe grip.",
		url: "footbed.aspx#pucork"
	},
	{
		term: /pu midsole/i,
		definition: "High shock absorption and arch support come wrapped in moisture-absorbent foam.",
		url: "footbed.aspx#polyurethane"
	},
	{
		term: /felt insole/i,
		definition: "Enjoy soft cushioning and great shock absorption.",
		url: "footbed.aspx#felt"
	},
	{
		term: /footbed combination on Latex foam/i,
		definition: "Walk on a flexible but durable surface.",
		url: "footbed.aspx#latex"
	},
	{
		term: /wedge insole/i,
		definition: "Stay cool and comfortable with a removable, comfortable footbed.",
		url: "outsole.aspx"
	},
	{
		term: /heel support/i,
		definition: "Stay secure with enhanced stability.",
		url: "footbed.aspx#integratedheel"
	},
	{
		term: /dual axis forefoot flex channels/i,
		definition: "Let the shoe help you save energy by mirroring the natural flex pattern.",
		url: "performance.aspx#dualaxis"
	},
	{
		term: /Forefoot Propulsion Plate/i,
		definition: "Save energy with every step as the plate provides additional torque.",
		url: "performance.aspx#forefoot"
	},
	{
		term: /Exo-skeletal wrap(-| )*around/i,
		definition: "Stand your ground with an exterior shank that enhances stability.",
		url: "performance.aspx#exoskeletal"
	},
	{
		term: /Earth Sensing Unit/i,
		definition: "Hold your footing with a sensing unit that enhances stability.",
		url: "performance.aspx#earthsensing"
	},
	{
		term: /heel stabilizer/i,
		definition: "Stay secure with enhanced stability.",
		url: "golf.aspx#swingstabilizer"
	},
	{
		term: /Eclipse.*from Trisport\S*/i,
		definition: "Hit the greens in long-lasting polyurethane cleats.",
		url: "golf.aspx#eclipse"
	},
	{
		term: /fast Twist\S*/i,
		definition: "Twist in your cleats to get into the action quickly.",
		url: "golf.aspx#fasttwist"
	},
	{
		term: /Metal Thread Receptacles/i,
		definition: "Trust the traditional insertion method to hold cleats securely during play.",
		url: "golf.aspx#metalthread"
	},
	{
		term: /Spikeless/i,
		definition: "Head to the links with a lightweight shoe that features exceptional traction and grip.",
		url: "golf.aspx#spikeless"
	},
	{
		term: /\S*Waxed.*laces/i,
		definition: "Tie your shoes and they'll stay tied with waxed laces.",
		url: "golf.aspx#waxedlaces"
	},
	{
		term: /Hydromax\S*/i,
		definition: "ECCO has created an exceptional water resistance leather.",
		url: "golf.aspx#hydromax"
	},
	{
		term: /Carbon fib[a-z][a-z] textile/i,
		definition: "Extremely hardwearing carbon fibre textile for enhancing stability in all aspects of golf.",
		url: "golf.aspx#carbonFibre"
	},
	{
		term: /gore(-| )*tex\S*/i,
		definition: "Keep feet dry with GORE-TEX&reg;, a waterproof, breathable membrane that wicks away moisture and prevents it from coming in in the first place.",
		url: "performance.aspx#goretexBlock"
	}
];

var technologyPath = sBaseUrl + "/about-ecco/technology/";

function searchBlock()
{	
	// the block which contains the text to check
	var oTheBlock = getThis("productDetail");
	
	// the child nodes within that block
	var oTheBlockChildren = oTheBlock.getElementsByTagName("p");
	
	// loop through each child node in the block
	for ( i = 0; i < oTheBlockChildren.length; i++ )
	{

		// We loop over each pattern within the Defined Terms array
		for ( j = 0; j < aDefinedTerms.length; j++ )
		{
			// if we actually find something, we're going to make a link
			if (oTheBlockChildren[i].innerHTML.search(aDefinedTerms[j].term) != -1) 
			{
				// get starting and ending characters
				var iStartingCharacter = oTheBlockChildren[i].innerHTML.search(aDefinedTerms[j].term);
				var theMatch = oTheBlockChildren[i].innerHTML.match(aDefinedTerms[j].term);
				var iEndingCharacter = iStartingCharacter + theMatch[0].length;
				
				// set one variable to the string we found, and a second one to that string plus the link we need
				var sFoundString = oTheBlockChildren[i].innerHTML.substring(iStartingCharacter,iEndingCharacter);
        
   	   			if (getThis("wholesale")) {
         			sNewStringWithLink = "<a href=\"#\" id=\"tooltip" + j + "\">" + sFoundString + "</a>";
				} else {
					sNewStringWithLink = "<a href=\"" + technologyPath + aDefinedTerms[j].url + "\" id=\"tooltip" + j + "\">" + sFoundString + "</a>";
				}
				// create a variable which is the new list item, then replace the old list item with it
				var sNewListItem = oTheBlockChildren[i].innerHTML.replace(sFoundString,sNewStringWithLink);
         		oTheBlockChildren[i].innerHTML = sNewListItem;
				break;
			}
		}
			
	}
	attachEvents();	
}

// had to separate this out into a function so that two links in the same sentence would work correctly
function attachEvents()
{
	var oTheBlock = getThis("productDetail");
	var oTheLinks = oTheBlock.getElementsByTagName("a");
	for ( i = 0; i < oTheLinks.length; i++ )
	{
		if (oTheLinks[i].className != "pr-snippet-link") {
			YAHOO.util.Event.on(oTheLinks[i], "mouseover", showTooltip);
			YAHOO.util.Event.on(oTheLinks[i], "mouseout", hideTooltip);
			YAHOO.util.Event.on(oTheLinks[i], "mousemove", Locate);
		}
	}
}

function attachInfo()
{	
	//for the wholesale section that has multiple info buttons (code is in wholesale.js)
	if (getThis("wholesale")) {
		 var isMultiple = document.getElementsByTagName('a');
		  for (var i=0;i<isMultiple.length;i++) {
			if (isMultiple[i].className == 'infoButton') { 
				YAHOO.util.Event.on(isMultiple[i], "mouseover", showInfoWholeTooltip);
				YAHOO.util.Event.on(isMultiple[i], "mouseout", hideWholeTooltip);
				YAHOO.util.Event.on(isMultiple[i], "mousemove", LocateWhole);
				YAHOO.util.Event.on(isMultiple[i], "click", preventClick);
	 		 } 
	    }
	} else {
		var oLink = document.getElementById("infoButton");
		if(oLink)
		{
			YAHOO.util.Event.on(oLink, "mouseover", showInfoTooltip);
			YAHOO.util.Event.on(oLink, "mouseout", hideTooltip);
			YAHOO.util.Event.on(oLink, "mousemove", Locate);
			YAHOO.util.Event.on(oLink, "click", preventClick);
		}
	}
}

function showInfoTooltip(e)
{
	
	// the text shown
	var sTheDefinition = document.getElementById("infoButton").getAttribute("tooltip");

	// create the DIV to show the definition
	var oTooltip = document.createElement("div");
	oTooltip.setAttribute("id", "popupTooltip");
	oTooltip.style.position = "absolute";
	
	var oDefinition = document.createElement("div");
	oDefinition.className = "toolDefinition";
	oDefinition.innerHTML = sTheDefinition;
	oTooltip.appendChild(oDefinition);
	
	var oArrow = document.createElement("div");
	oArrow.setAttribute("id", "tooltipArrow");
	oTooltip.appendChild(oArrow);
		
	document.getElementsByTagName("body")[0].appendChild(oTooltip);		
	
	if (document.all) {
		var theSelects = document.getElementsByTagName("select");
		for ( i = 0; i < theSelects.length; i++ )
		{
			theSelects[i].style.visibility = "hidden";
		}
	}
	
	Locate(e);
	
}

function showTooltip(e)
{

	var iKey = this.id.match(/(\d+)/)[1];
	
	// the defined term that we found
	var sLinkedText = this.innerHTML;
	
	// the definition of that term
	var sTheDefinition = aDefinedTerms[iKey].definition;

	// create the DIV to show the definition
	var oTooltip = document.createElement("div");
	oTooltip.setAttribute("id", "popupTooltip");
	oTooltip.style.position = "absolute";
	
	var oDefinition = document.createElement("div");
	oDefinition.className = "toolDefinition";
	oDefinition.innerHTML = sTheDefinition;
	oTooltip.appendChild(oDefinition);
	
	var oArrow = document.createElement("div");
	oArrow.setAttribute("id", "tooltipArrow");
	oTooltip.appendChild(oArrow);
		
	document.getElementsByTagName("body")[0].appendChild(oTooltip);
	
	Locate(e);
}

function hideTooltip(e)
{
	var oPopup = document.getElementById("popupTooltip");
	if (oPopup.parentNode) {
		oPopup.parentNode.removeChild(oPopup);
	}
	if (document.all) {
		var theSelects = document.getElementsByTagName("select");
		for ( i = 0; i < theSelects.length; i++ )
		{
			theSelects[i].style.visibility = "visible";
		}
	}
}

function Locate(e){
	var posx = 0, posy = 0;
	if (e == null) 
	{
		e = window.event;
	}
	if (e.pageX || e.pageY)
	{
	    posx = e.pageX;
		posy = e.pageY;
	}
	else if	(e.clientX || e.clientY)
	{
	    if(document.documentElement.scrollTop)
		{
	        posx = e.clientX + document.documentElement.scrollLeft;
	        posy = e.clientY + document.documentElement.scrollTop;
	    }
		else 
		{
	        posx = e.clientX + document.body.scrollLeft;
	        posy = e.clientY + document.body.scrollTop;
	    }
	}
	
	var oPopup = document.getElementById("popupTooltip");
	if (oPopup) {
		oPopup.style.top = (posy - oPopup.offsetHeight - 8) + "px";
		oPopup.style.left = (posx - 20) + "px";
	}
}

function preventClick()
{
	return false;
}
