﻿//--------------------------------------------------------------------------------
// Glossary Tagger...
//--------------------------------------------------------------------------------

function glossaryTag(data, tagList)
{
    // Glossary Tag..

    // Create Tag Types Array From Tag List...

    var tagTypes = new Array();
    tagTypes = tagList.split(",");

    // Tag Type Index...
    var tagType = 0;

    // Iterate Through Tag Types...
    for (tagType = 0; tagType < tagTypes.length; tagType++)
    {
        // Get All Tags Of Tag Type On The Page...
        var tags = document.getElementsByTagName(tagTypes[tagType]);

        // Glossary Term Index...
        var term = 0;

        // Iterate Through All Terms In The Data Array...

        // JD --> WAS -->     for (term = 0; term < data.length; term++)
        // Was getting error: "Error: 'length' is null or not an object"
        for (term = 0; term < (data ? data.length : 0); term++)
        {
            // Tag Index...
            var index = 0;

            // Iterate Throught All Tags Of Tag Type...

            // JD --> WAS -->     for (index = 0; index < tags.length; index++)
            // Was getting error: "Error: 'length' is null or not an object"
            for (index = 0; index < (tags ? tags.length : 0); index++)
            {
                // Do Not Tag Tags With The 'title' or 'no_glossary_tag' CSS Class...
                if ((tags[index].className.search(/title/i) == -1) &&
                    (tags[index].className.search(/no_glossary_tag/i) == -1))
                {
                    // Build Regular Expression To Match The Term In Any Text Case As A Whole Word...
                    var reg = new RegExp("\\b" + data[term] + "\\b", 'gi');

                    // Run The RegEx...
                    // match Is An Array...
                    // match[0] -> The Matched Text...
                    // match[0].length -> The Length Of The Matched Text...
                    // match.index -> The Start Index Of The Matched Text...
                    // match.input -> The Searched Text...

                    var match = reg.exec(tags[index].innerHTML);

                    // Iterate Through The Text Looking For The Term...

                    while (match != null)
                    {
                        // String To Replace Any Matched Text With...
                        var middle = "<span class=\"glossary_tag\" title=\"Click for a definition\" >" +
                            match[0] + "</span>";

                        // Sub String The Searched Text Up To The Start Of The Matched Text...
                        var begin = tags[index].innerHTML.substring(0, match.index);

                        // Sub String The Text After The Matched Text...
                        var end = tags[index].innerHTML.substring(match.index + match[0].length);

                        // ----------

                        // Ensure No Terms Are Tagged Which Are Already Inside Existing Tags...

                        // Match Count...

                        var openerCount = 0;
                        var completeCount = 0;

                        // RegEx Of The Opening Of A Glossary Tag...
                        var openerReg = new RegExp('<span class="glossary_tag"', 'gim');

                        // RegEx Of A Complete Glossary Tag...
                        var completeReg = new RegExp('<span class="glossary_tag".*</span>', 'gim');
                        
                        // Search For First Matches...

                        var openerMatch = openerReg.exec(begin);
                        var completeMatch = completeReg.exec(begin);

                        while (openerMatch != null)
                        {
                            // Count All The Opener Matches...
                            openerCount += 1;

                            // Set The Point At Which The Search Resumes To The End Of The Located Text...
                            openerMatch.lastIndex = openerMatch.index + openerMatch.length;

                            // Find Next Complete Match....
                            openerMatch = openerReg.exec(begin);
                        }

                        while (completeMatch != null)
                        {
                            // Count All The Complete Matches...
                            completeCount += 1;

                            // Set The Point At Which The Search Resumes To The End Of The Located Text...
                            completeMatch.lastIndex = completeMatch.index + completeMatch.length;

                            // Find Next Complete Match....
                            completeMatch = completeReg.exec(begin);
                        }
                        
                        // ----------

                        if (openerCount > completeCount)
                        {
                            // Already Inside Tagged Text, DO NOT Tag...
                        
                            // Set The Tags Text To The Beggining Text Plus The Matched Text Plus The End Text...
                            tags[index].innerHTML = begin + match[0] + end;

                            // Set The Point At Which The Search Resumes To The End Of The Matched Text...
                            reg.lastIndex = (match.index + match.length);
                        }
                        else
                        {
                            // Outside Already Tagged Text, Tag...
                        
                            // Set The Tags Text To The Beggining Text Plus The Replacement Text Plus The End Text...
                            tags[index].innerHTML = begin + middle + end;

                            // Set The Point At Which The Search Resumes To The End Of The Replacement Text...
                            reg.lastIndex = (match.index + middle.length);
                        }

                        // Search For The Next Match...
                        match = reg.exec(tags[index].innerHTML);
                    }
                }
            }
        }
    }
}

//--------------------------------------------------------------------------------
