

/**
 * Redirect to the right index page.
 *
 ' @param An <input> element to get the search string from.
 */
function startSearch(input) {
    var search = input.value.toLowerCase();

    // FIXME: We're just using the complete index for testing,
    // convert to split index later.
    location.href = "../../search/search_index.html?" + search;

    return false;
}


/**
 * Redirect to the correct search page.
 * @param searchString
 */
function redirectSearch(searchString) {
    var letter = searchString.charAt(0);
    
    switch(letter) {
        case "a": location.replace("index_a.html?" + searchString); break;
        default: location.replace("index_other.html?" + searchString); break;
    }
}


/**
 * Constructor for the SearchDoc objects used to match search words to 
 * documents in searchWords.
 * 
 * @param ID The document ID.
 * @param title The document title.
 */
function SearchDoc(ID, title) {
     this.ID = ID;
     this.title = title;
     this.words = new Array();
}


/**
 * Constructor for the SearchCat objects used to sort search category matches.
 * 
 * @param ID The category ID.
 * @param title The category title.
 */
function SearchCat(ID, title) {
     this.ID = ID;
     this.title = title;
}


/**
 * Serach the 'categories' array for a match in a category, and display the results.
 * The array must be defined as a global variable (usually in the page
 * this function is called from), and the 'getCategoryName' function 
 * must be available to map IDs to names.
 *
 * @param searchString The word to search for.
 */
function searchCategories (searchString) {
    var index = new Array();
    searchString = unescape(searchString);

    // Search for matching words
    for (var i = 0; i < categories.length; i++) {
       // Only search for match from start
       if (categories[i][0].indexOf(searchString) == 0) {
           index.push(i);
       }
    }

    // Create a list of SearchCat objects, and filter out duplicates
    var catlist = new Array();
    for (var i = 0; i < index.length; i++) {
        for (var j = 1; j < categories[index[i]].length; j++) {
            // Search for existing category
            var found = false;
            var ij = categories[index[i]][j];
            for (var k = 0; k < catlist.length; k++) {
                if (catlist[k].ID == ij) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                var cat = new SearchCat(ij, getCategoryName(ij));
                catlist.push(cat);
            }
        }
    }


    // Sort and display results
    if (catlist.length > 0) {
       document.write("<h3>Følgende kategorier med ord som begynner p&aring; '" + searchString + "' ble funnet:</h3>");
       document.write("<ul class=\"category-match\">");
       for (var i = 0; i < catlist.length; i++) {
           document.write("<li><a href=\"../cat_" + catlist[i].ID + "/index.html\">" + 
                          catlist[i].title + "</a></li>");
       }
       document.write("</ul>");
   }
}


/**
 * Serach the 'words' array for a given word, and display the results.
 * The array must be defined as a global variable (usually in the page
 * this function is called from), and the 'getDocumentName' function 
 * (see 'documents.js') must be available to map document IDs to
 * document names.
 *
 * @param searchString The word to search for.
 */
function searchWords (searchString) {
    var row = -1;
    var indexof = new Array();
    searchString = unescape(searchString);

    d = new Date();
    searchstart = d.getTime();
    for (i = 0; i < words.length; i++) {
        if (words[i][0] == searchString) {
            row = i;
            break;
        }
    }
    d = new Date();
    searchend = d.getTime();

    // Search by indexof
    d = new Date();
    iofstart = d.getTime();
    var s;
    var j;
    for (i = 0; i < words.length; i++) {
       // Only search for match from start
       s = words[i][0];
       j = s.indexOf(searchString);
       if (j == 0) {
           indexof.push(i);
       }
    }
    d = new Date();
    iofend = d.getTime();



    d = new Date();
    presentstart = d.getTime();
    // Read indexof result
    var result = new Array();

    // For each matched word
    for (var i = 0; i < indexof.length; i++) {
        var r = indexof[i];
        // For each document hit
        for (var j = 1; j < words[r].length; j++) {
            // Search for existing SearchDoc
            doc = null;
            for (var k = 0; k < result.length; k++) {
                if (result[k].ID == words[r][j]) {
                    doc = result[k];
                    break;
                }
            }

            // Or add new SearchDoc if not found
            if (doc == null) {
                doc = new SearchDoc(words[r][j], getDocumentName(words[r][j]));
                result.push(doc);
            }

            // Add word to document
            doc.words.push(words[r][0]);
        }
    }


    result.sort(function(a,b) { if (a.title > b.title) { return 1; } else if (a.title < b.title) { return -1; } else {return 0;}});

    if (result.length > 0) {
        document.write("<h3>Følgende dokumentet med ord som begynner p&aring; '" + searchString + "' ble funnet:</h3>");
        document.write("<dl class=\"document-match\">");
        for (var i = 0; i < result.length; i++) {
            document.write("<dt><a href=\"../docs/doc_" + result[i].ID + "/index.html\">" + result[i].title + "</a>");
            document.write("<dd>" + result[i].words.join(", ") + "</dd></dt>");
        }
        document.write("</dl>");
    }
    else {
        document.write("<h3>Ingen dokumenter med ord som begynner p&aring; '" + searchString + "' ble funnet</h3>");
    }
    d = new Date();
    presentend = d.getTime();

}

