﻿ 
    function currentNodeData()
    {
        currentWord = '';
        text        = '';
        url         = '';
        keyword     = '';
        urlName     = '';
    }

 
 // Checks to see if the current wrod matches the word to change into a link
    // If so it flushes the Previous text into a new text node and create a link
    // node both of these are added to the output array and returns True. If not
    // it returns false.
    function ProcessWord(Data,outputArr)
    {
        var linkTitle = '';
        var currentWordLower = Data.currentWord.toLowerCase();
        var postText = "";
    
        if(currentWordLower == Data.keyword)
	    {
	        linkTitle = Data.currentWord;
	    }
        // Allows the following to be at the end of the word "s.! "
	    else if(currentWordLower.substr(0, currentWordLower.length-1) == Data.keyword)
	    {
	        var extraChar = currentWordLower.substr(currentWordLower.length-1,1);
	    
	        if (extraChar == 's')
                linkTitle = Data.currentWord;
                
	        else if (extraChar == ' ' || extraChar == '!' || extraChar == '.')
	        {
                linkTitle = Data.currentWord.substr(0, Data.currentWord.length-1);
                
                //Add the actual character after the link
                postText = Data.currentWord.substr(Data.currentWord.length-1,1);
            }
	    }
	    
	    if (linkTitle != '')
	    {
	        var a =  document.createElement('A'); 
	        outputArr[outputArr.length] = document.createTextNode(Data.text); 
	        outputArr[outputArr.length] = a;
	        a.innerHTML = linkTitle;
	        a.href = Data.url;
	        a.alt = Data.urlName;
	        
	        Data.text =  postText;
	        return true;
	    }
	    else 
	        return false;
    }
    
    function addLinks(node, keyword, url, urlName)
    {
        keyword =  keyword.toLowerCase();
        
        var Data = new currentNodeData();
        
        Data.keyword = keyword;
        Data.url = url;
        Data.urlName = urlName;
        
    
        if(node.childNodes.length > 0)
        {
            for(var i = 0; i != node.childNodes.length; i++)
            {
                if(node.childNodes[i].childNodes.length > 0 && node.childNodes[i].nodeName != "A")
                    addLinks(node.childNodes[i], keyword, url);
                else
                {
                    // .data is null for links.
                    if(node.childNodes[i].data != null)
                    {
                        Data.text = "";
                        var outputArr = [];
						var words = node.childNodes[i].data.split(' ');
						
						for(var j = 0; j != words.length; j++)
						{
						    Data.currentWord = words[j];
						
						    if (ProcessWord(Data,outputArr) == false)
						        Data.text += words[j];
						    
						    // A space has to be readded for each one that was taken away.
						    Data.text += ' ';
						}
						
						if (Data.text != "")
						{
						    Data.text =  Data.text.substr(0, Data.text.length-1);
                            outputArr[outputArr.length] = document.createTextNode(Data.text); 
		                }
						
						if (outputArr.length != 0)
						{
						    var innerLoop = 0;
						    
						    for (innerLoop = outputArr.length - 1; innerLoop >= 0; innerLoop--)
						    {
						        node.insertBefore(outputArr[innerLoop],node.childNodes[i].nextSibling);							    
						    }

                            node.removeChild(node.childNodes[i]);
						}
                    }
                }
            }
        }
    }