
/// ===========================================================================
/// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
/// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
/// PURPOSE.
/// ===========================================================================
/// 
/// Project:        MOSS Faceted Search
/// Author:         Leonid Lyublinski (leonidly@microsoft.com)
/// Company:        Microsoft Services
/// Date:           12/29/2007  Version:        2.0
///
/// ===========================================================================

function expandSection(sender, maxRows) {
    var uList = sender.previousSibling;

    // Make sure we are at the UL tag (Firefox's DOM returns an element for newlines whereas IE does not
    if (uList.tagName != "UL") {
        var childIndex = 0;

        while (uList != null && uList.tagName != "UL") {
            uList = sender.parentNode.childNodes[childIndex++];
        }
    }

    // Loop through each child, if its a LI then add one to the count and if we are above maxRows then toggle the class based on the anchor tag state
    var count = 0;
    var newClass = (sender.innerHTML == "See Less") ? "hideSearchFacet" : "showSearchFacet";

    for (var rowLoop = 0; rowLoop < uList.childNodes.length; rowLoop++) {
        var tag = uList.childNodes[rowLoop];

        if (tag.tagName == "LI" && ++count > maxRows) {
            tag.className = newClass;
        }
    }

    // Toggle the anchor state to reflect the new action
    sender.innerHTML = (sender.innerHTML == "See More") ? "See Less" : "See More";
}

function switchFooter(obj, name, totalResults) {
    try {
        var table = getFacetTableByObj(obj, name);
        if (table) {
            if (setFacetTable2(table, totalResults))
                obj.innerText = 'See More';
            else
                obj.innerText = 'See Less';
        }
    }
    catch (e) { }
    finally { return false; }
}

function getFacetTableByObj(obj, name) {
    // find the parent span with the right facet attribute
    var parent = obj.parentElement;
    while ((parent != null) && (parent.tagName != "span")) {
        parent = parent.parentElement;

        if (parent.getAttribute("facet") == name) {
            return parent;
        }
    }
}

function setFacetTable2(parentSpan, maxResults) {
    var base = parseInt(maxResults);
    var total = parentSpan.childNodes.length;
    var mode = parentSpan.childNodes[total - 1];
    var newMode = parentSpan.childNodes[1 + base].style.display;

    newMode = (parentSpan.childNodes[total - 2].style.display == "none") ? newMode = "inline-block" : newMode = "none";

    for (var i = base + 6; i < total - 1; i++) {
        var row = parentSpan.childNodes[i];

        row.style.display = newMode;
    }

    return newMode == "none";
}

function setFacetTable(tabl, expanded, maxResults) {
    var total = table.rows.length;

    if (expanded) {// then collapse and show last row with ...
        table.rows[total - 1].style.display = "block";
    } else {
        table.rows[total - 1].style.display = "none";
    }

    for (var i = parseInt(maxResults) + 1; i < total - 1; i++) {
        var row = table.rows[i];

        if (expanded) {// then collapse and hide all rows
            row.style.display = "none";
        } else {
            row.style.display = "block";
        }
    }
}

function getFacetTableByName(name) {
    var tables = document.body.getElementsByTagName("Table");
    for (var i = 0; i < tables.length; i++) {
        var table = tables[i];
        if (table.getAttribute("facet") == name) {
            return table;
        }
    }
}

function processFacetTables() {
    return;

    var tables = document.body.getElementsByTagName("span");
    for (var i = 0; i < tables.length; i++) {
        var table = tables[i];
        if (table.getAttribute("facet")) {
            var count = table.getAttribute("count");
            var totalresults = table.getAttribute("totalresults");

            setFacetTable2(table, totalresults);
        }
    }
}

function initFacetTables() {
    try {
        if (arFacetTables != undefined) {
            for (var i = 0; i < arFacetTables.length; i++) {
                var name = arFacetTables[i];
                var totalResults = arTotalResults[i];
                var table = getFacetTableByName(name);

                setFacetTable2(table, totalResults)
            }
        }
    } catch (e) {

    }
}

//*********** 2nd call ****************//  

function setProgress(url) {
    var images = document.images;
    for (var i = 0; i < images.length; i++) {
        var img = images[i];
        if (img.progress != null) {
            img.src = url;
        }
    }
}

function switchElement(id, toShow) {
    var el = document.getElementById(id);
    if (el != null) {
        if (toShow) {
            el.style.display = "inline";
        } else {
            el.style.display = "none";
        }
    }
}


function setElAttribute(id, attName, attValue) {
    var el = document.getElementById(id);
    el[attName] = attValue;
}

function addMatch(facetName, facetDisplayName, qs) {
    var match = prompt('Add additional match for ' + facetDisplayName, '[type the value]');
    if (!match) return false;
    var url = qs + ' ' + facetName + ':"' + encodeURI(match) + '"';
    GoToPageRelative(url);
}

