 /*
	This function is responsible to create links out of key pharses.
	It checks the existence of key phrases as well as contextual phrases
	inside the element, id of which is given in variable "idOfElementToModify".

	This function also takes care of case sensitive or cas insensetive search based on the
	value of variable "keyPhrasesCaseSensistive"

	This function also ignores the tags given by user in the "ignoreTags" array.
	To do this, it replaces the tags to ignore with certain pattern in the content of the
	desired element and then it process the content to place the links. Once the link placements
	is over, it replaces the patterns with original ignored tags. To accomplish this task it
	stores the temporarily igonred tags into "tagStorage" array.
*/
function findLink() {
    if (!document.getElementById(idOfElementToModify) || document.getElementById(idOfElementToModify) == null) {
        return false;
    }
    var tempDivText = document.getElementById(idOfElementToModify).innerHTML;
    //  create array to store ignore tag
    var tagStorage = new Array();
    var imgArray = new Array();
    //tempDivText = placeImg(tempDivText, imgArray);
    for (var i = 0; i < ignoreTags.length; i++) {
        tagStorage[i] = new Array();
        tempDivText = ingoreTag(ignoreTags[i], tagStorage[i], tempDivText);
    }
    var divText = "";
    var i = 0;
    // key phrases insensistive
    var capsDivText = '';
    if (!keyPhrasesCaseSensistive) {
        capsDivText = tempDivText.toUpperCase();
    }
    // find the contextual link
    for (var i = 0; i < phrases.length; i++) {
        if ((keyPhrasesCaseSensistive && tempDivText.indexOf(phrases[i][0]) >= 0) || (!keyPhrasesCaseSensistive && capsDivText.indexOf(phrases[i][0].toUpperCase()) >= 0)) {
            // create a link for eligible key phrases
            var prevIndex = -1;
            if (keyPhrasesCaseSensistive) {
                prevIndex = tempDivText.indexOf(phrases[i][0]);
            } else {
                prevIndex = capsDivText.indexOf(phrases[i][0].toUpperCase());
            }
            while (prevIndex != -1) {
                var origWord = tempDivText.substring(prevIndex, prevIndex + phrases[i][0].length);
                replaceString = "<a name='" + phrases[i][0] + "' href='" + phrases[i][1] + "' target='_top' >" + origWord + "</a>";
                if (phrases[i].length > 2) {
                  var showIcons = false;
                  switch(phrases[i][2]) {
                    case 1:
                      if(window.location.pathname.substr(0, 12).toLowerCase() == "/livescores/") showIcons = true;
                      else showIcons = false;
                      break;
                    case 2:
                      showIcons = true;
                      break;
                  }
                  if ( showIcons )
                  {
                    replaceString += "&nbsp;";
                    if (phrases[i][3] != undefined) replaceString += phrases[i][3];
                    if (phrases[i][4] != undefined) replaceString += phrases[i][4];
                    if (phrases[i][5] != undefined) replaceString += phrases[i][5];
                  }
                }
                if (navigator.appName.indexOf('Netscape') != -1) {
                    tempDivText = tempDivText.replace(phrases[i][0], replaceString, "i");
                } else {
                    tempDivText = tempDivText.replace(new RegExp(phrases[i][0], "i"), replaceString);
                }
                divText += tempDivText.substring(0, prevIndex + replaceString.length + 1);
                tempDivText = tempDivText.substring(prevIndex + replaceString.length + 1);
                if (!keyPhrasesCaseSensistive) {
                    capsDivText = tempDivText.toUpperCase();
                    prevIndex = capsDivText.indexOf(phrases[i][0].toUpperCase());
                } else {
                    prevIndex = tempDivText.indexOf(phrases[i][0]);
                }
            }
            tempDivText = divText + tempDivText;
            if (!keyPhrasesCaseSensistive) capsDivText = tempDivText.toUpperCase();
            divText = "";
        }
    }
    // replace ignored tag
    for (var i = 0; i < ignoreTags.length; i++) {
        tempDivText = placeTag(tempDivText, ignoreTags[i], tagStorage[i]);
    }
    document.getElementById(idOfElementToModify).innerHTML = tempDivText;
}

/*
	This function does the actual work of ignoring the tags.
	It replaces the content of a tag with a pattern related to that tag and returns
	the modified text as content to be processed for placing links.
*/
function ingoreTag(given, storageArray, tempDivText) {
    var i = 0;
    var textUpper = tempDivText.toUpperCase();
    var givenUpper = given.toUpperCase();
    var startIndex = textUpper.indexOf("<" + givenUpper);
    var endIndex;
    while (startIndex >= 0) {
        switch (givenUpper) {
            case "IMG":
                endIndex = textUpper.indexOf(">", startIndex) + 1;
                break;
            default:
                endIndex = textUpper.indexOf("</" + givenUpper + ">", startIndex) + givenUpper.length + 3;
                break;
        }
        storageArray[i++] = tempDivText.substring(startIndex, endIndex);
        tempDivText = tempDivText.substring(0, startIndex) + "##" + given + "##" + tempDivText.substring(endIndex);
        textUpper = tempDivText.toUpperCase();
        startIndex = textUpper.indexOf("<" + givenUpper, startIndex + 1);
    }
    return tempDivText;
}

/*
	This function is responsible for replacing the patterns with the original
	contents of ignored tags.
*/
function placeTag(tempDivText, given, storageArray) {
    var startIndex = 0;
    for (var i = 0; i < storageArray.length; i++) {
        startIndex = tempDivText.indexOf("##" + given + "##", startIndex);
        tempDivText = tempDivText.substring(0, startIndex) + storageArray[i] + tempDivText.substring(startIndex + given.length + 4);
    }
	return tempDivText;
}
function trackKeywordLinking(link, category, action) {
  _gat._getTrackerByName()._trackEvent(category, action);
}
