﻿/*	navClasses.js

This file sets the first major word of a main navigation link's text as a class
attribute for the link element.  It splits the text string on whitespace and will
act on the first acceptable chunk of text, converting it to lowercase and setting
it as the link element's secondary classname.
	
Prerequisites:
*	Links must already be assigned a class attribute value (in this case, "navmenu")
*	Variable 'className' must be set to the above class attribute value
		
Features:
*	First major word is converted to lowercase and added as an additional class
for the link element
			
** Example: "Business Services" would have a class attribute value of "navmenu business"
			
*	Numeric values are filtered out and will not be used as class names
		
** Example: "2008 Sponsors" would have a class attribute value of "navmenu sponsors"
			
*	"Fluff" words such as pronouns, articles, and "be" verbs are filtered and will not
be used as class names
			
** Example: "Our Story" would have a class attribute value of "navmenu story"
			
*	The first and final links will addtionally be given a tertiary classes of "first" and 
"last" respectively, so that any special padding, borders, or margins can be omitted for
these elements.
			
Notes:
*	Comments in code refer to the line above
*/

function addContentBasedClasses(className) {
    // Primary function to add class name(s) to navigation links
    var elemArray = new Array;
    elemArray = $("." + className);
    // Gather all main navigation link elements into an array
    for (var i = 0; i < elemArray.length; i++) {
        // Loop through links
        var elemText = elemArray[i].childNodes[0].nodeValue.toLowerCase();
        // Get link text and convert to lowercase
        var classSet = false;
        // Set a flag that this link has not yet been assigned a class value
        for (var j = 0; j < elemText.split(" ").length; j++) {
            // Loop through space-delimited chunks of text
            if (!classSet) {
                // Perform the following steps if a class has not yet been set for this link
                var elemClass = elemText.split(" ")[j];
                // Select the next available chunk of text
                if ((!checkNumeric(elemClass)) && (!checkFluffWords(elemClass))) {
                    // Check that chunk of text is not a number or a "throw-away" word
                    $(elemArray[i]).addClass(elemClass);
                    // Add secondary class name to the current link
                    classSet = true;
                    // Update flag to indicate a secondary class has been set
                    if (i == (0)) $(elemArray[i]).addClass("first");
                    // Add a tertiary "first" class to the first link
                    if (i == (elemArray.length - 1)) $(elemArray[i]).addClass("last");
                    // Add a tertiary "last" class to the last link
                }
            }
        }
    }
}
function checkNumeric(value) {
    // Function to check if a passed-in string is a number
    var anum = /(^\d+$)|(^\d+\.\d+$)/
    // Setup a regular expression to check for integer and float-point numbers
    if (anum.test(value)) {
        // Test the passed-in string against the regular expression
        return true;
        // If we find a match return back true, indicating we've found a number
    }
    return false;
    // If we do not find a match return back false, indicating we've not found a number
}

function checkFluffWords(word) {
    // Function to check if a passed-in string is a "throw-away" word
    var fluffWords = getFluffWords();
    // Build a multi-dimensional array of possible "throw-away" words (pronouns, articles, "be" verbs)
    for (var i = 0; i < fluffWords.length; i++) {
        // Loop through first tier of "throw-away" words array
        var fluffArray = fluffWords[i];
        for (var j = 0; j < fluffArray.length; j++) {
            // Loop through second tier of "throw-away" words array (e.x. pronouns)
            var compareWord = fluffArray[j];
            // Select the next "throw-away" word to compare to the passed-in string
            fluff = new RegExp("^" + compareWord + "$", "i");
            // Create a regular expression that checks for a case-insensitive match of the "throw-away" word
            if (fluff.test(word)) {
                // If an exact match to a throw-away word is found...
                return true;
                // Return back a true value, indicating our passed-in string is a "throw-away" word
            }
        }
    }
    return false;
    // If we find no exact matches for a "throw-away" word return back a value of false
}

function getFluffWords() {
    // Function to return an array of pre-defined "throw-away" words
    var pronouns = new Array;
    pronouns[0] = "i";
    pronouns[1] = "you";
    pronouns[2] = "he";
    pronouns[3] = "she";
    pronouns[4] = "it";
    pronouns[5] = "we";
    pronouns[6] = "they";
    pronouns[7] = "this";
    pronouns[8] = "these";
    pronouns[9] = "those";
    pronouns[10] = "him";
    pronouns[11] = "her";
    pronouns[12] = "our";
    pronouns[13] = "their";
    pronouns[14] = "his";
    pronouns[15] = "hers";
    pronouns[16] = "ours";
    pronouns[17] = "theirs";
    var articles = new Array;
    articles[0] = "a";
    articles[1] = "an";
    articles[2] = "the";
    var beVerbs = new Array;
    beVerbs[0] = "be";
    beVerbs[1] = "being";
    beVerbs[2] = "been";
    beVerbs[3] = "am";
    beVerbs[4] = "is";
    beVerbs[5] = "are";
    beVerbs[6] = "was";
    beVerbs[7] = "were";
    var connectorVerbs = new Array;
    connectorVerbs[0] = "to";
    var prepositions = new Array;
    prepositions[0] = "with";
    prepositions[1] = "without";
    prepositions[2] = "over";
    prepositions[3] = "under";
    prepositions[4] = "in";
    prepositions[5] = "out";
    prepositions[6] = "ahead";
    prepositions[7] = "behind";
    prepositions[8] = "above";
    prepositions[9] = "below";
    var fluffWords = new Array;
    fluffWords[0] = pronouns;
    fluffWords[1] = articles;
    fluffWords[2] = beVerbs;
    fluffWords[3] = connectorVerbs;
    fluffWords[4] = prepositions;
    return fluffWords;
}
