/*
 * ML adapted use of jquery.autocomplete.js,
 *     see: jquery.autocomplete.xml.js
 *
 * License: http://docs.jquery.com/Licensing
 *
 */


$().ready(function() {

    // This one does the list display. FormatResult does the value selected.
    function formatItem(data)
    {
        return data.text;
    };

    if ($("#country")) {
        $("#country").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'countryId',      htmlId:'countryId' },
                              { xmlNode:'subnational1Id', htmlId:'subnational1Id' },
                              { xmlNode:'subnational2Id', htmlId:'subnational2Id' },
                              { xmlNode:'subnational1',   htmlId:'subnational1' },
                              { xmlNode:'subnational2',   htmlId:'subnational2' } ],
            extraParams   : { type:'country' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#country1")) {
        $("#country1").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'countryId',      htmlId:'countryId1' },
                              { xmlNode:'subnational1Id', htmlId:'subnational1Id1' },
                              { xmlNode:'subnational2Id', htmlId:'subnational2Id1' },
                              { xmlNode:'subnational1',   htmlId:'subnational11' },
                              { xmlNode:'subnational2',   htmlId:'subnational21' } ],
            extraParams   : { type:'country' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#country2")) {
        $("#country2").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'countryId',      htmlId:'countryId2' },
                              { xmlNode:'subnational1Id', htmlId:'subnational1Id2' },
                              { xmlNode:'subnational2Id', htmlId:'subnational2Id2' },
                              { xmlNode:'subnational1',   htmlId:'subnational12' },
                              { xmlNode:'subnational2',   htmlId:'subnational22' } ],
            extraParams   : { type:'country' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#catalogedBy")) {
        $("#catalogedBy").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'archivistId',   htmlId:'catalogedById' } ],
            extraParams   : { type:'archivist' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 0,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#digitizedBy")) {
        $("#digitizedBy").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'archivistId',   htmlId:'digitizedById' } ],
            extraParams   : { type:'archivist' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 0,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#editedBy")) {
        $("#editedBy").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'archivistId',   htmlId:'editedById' } ],
            extraParams   : { type:'archivist' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 0,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#geophys1")) {
        $("#geophys1").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'geophys1Id',   htmlId:'geophys1Id' },
                              { xmlNode:'geophys2Id',   htmlId:'geophys2Id' },
                              { xmlNode:'geophys3Id',   htmlId:'geophys3Id' },
                              { xmlNode:'geophys2',     htmlId:'geophys2' },
                              { xmlNode:'geophys3',     htmlId:'geophys3' } ],
            extraParams   : { type:'geophys1' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#geophys2")) {
        $("#geophys2").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'geophys1Id',   htmlId:'geophys1Id' },
                              { xmlNode:'geophys2Id',   htmlId:'geophys2Id' },
                              { xmlNode:'geophys3Id',   htmlId:'geophys3Id' },
                              { xmlNode:'geophys1',     htmlId:'geophys1' },
                              { xmlNode:'geophys3',     htmlId:'geophys3' } ],
            extraParams   : { type:'geophys2' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#geophys3")) {
        $("#geophys3").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'geophys1Id',   htmlId:'geophys1Id' },
                              { xmlNode:'geophys2Id',   htmlId:'geophys2Id' },
                              { xmlNode:'geophys3Id',   htmlId:'geophys3Id' },
                              { xmlNode:'geophys1',     htmlId:'geophys1' },
                              { xmlNode:'geophys2',     htmlId:'geophys2' } ],
            extraParams   : { type:'geophys3' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#recordist")) {
        $("#recordist").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'recordistId',   htmlId:'recordistId' } ],
            extraParams   : { type:'recordist' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 2,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#recordist2")) {
        $("#recordist2").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'recordistId',   htmlId:'recordist2Id' } ],
            extraParams   : { type:'recordist' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 2,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#searchCatalogedBy")) {
        $("#searchCatalogedBy").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'archivistId',   htmlId:'searchCatalogedById' } ],
            extraParams   : { type:'archivist' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 0,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#searchCountry")) {
        $("#searchCountry").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'countryId',      htmlId:'searchCountryId' },
                              { xmlNode:'subnational1Id', htmlId:'searchSubnational1Id' },
                              { xmlNode:'subnational2Id', htmlId:'searchSubnational2Id' },
                              { xmlNode:'subnational1',   htmlId:'searchSubnational1' },
                              { xmlNode:'subnational2',   htmlId:'searchSubnational2' } ],
            extraParams   : { type:'country' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#searchGeophys1")) {
        $("#searchGeophys1").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'geophys1Id',   htmlId:'searchGeophys1Id' },
                              { xmlNode:'geophys2Id',   htmlId:'searchGeophys2Id' },
                              { xmlNode:'geophys3Id',   htmlId:'searchGeophys3Id' },
                              { xmlNode:'geophys2',     htmlId:'searchGeophys2' },
                              { xmlNode:'geophys3',     htmlId:'searchGeophys3' } ],
            extraParams   : { type:'geophys1' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#searchGeophys2")) {
        $("#searchGeophys2").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'geophys1Id',   htmlId:'searchGeophys1Id' },
                              { xmlNode:'geophys2Id',   htmlId:'searchGeophys2Id' },
                              { xmlNode:'geophys3Id',   htmlId:'searchGeophys3Id' },
                              { xmlNode:'geophys1',     htmlId:'searchGeophys1' },
                              { xmlNode:'geophys3',     htmlId:'searchGeophys3' } ],
            extraParams   : { type:'geophys2' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#searchGeophys3")) {
        $("#searchGeophys3").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'geophys1Id',   htmlId:'searchGeophys1Id' },
                              { xmlNode:'geophys2Id',   htmlId:'searchGeophys2Id' },
                              { xmlNode:'geophys3Id',   htmlId:'searchGeophys3Id' },
                              { xmlNode:'geophys1',     htmlId:'searchGeophys1' },
                              { xmlNode:'geophys2',     htmlId:'searchGeophys2' } ],
            extraParams   : { type:'geophys3' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#searchRecordist")) {
        $("#searchRecordist").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'recordistId',   htmlId:'searchRecordistId' } ],
            extraParams   : { type:'recordist' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 2,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#searchSubnational1")) {
        $("#searchSubnational1").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'countryId',      htmlId:'searchCountryId' },
                              { xmlNode:'subnational1Id', htmlId:'searchSubnational1Id' },
                              { xmlNode:'subnational2Id', htmlId:'searchSubnational2Id' },
                              { xmlNode:'country',        htmlId:'searchCountry' },
                              { xmlNode:'subnational2',   htmlId:'searchSubnational2' } ],
            extraParams   : { type:'subnational1' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#searchSubnational2")) {
        $("#searchSubnational2").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'countryId',      htmlId:'searchCountryId' },
                              { xmlNode:'subnational1Id', htmlId:'searchSubnational1Id' },
                              { xmlNode:'subnational2Id', htmlId:'searchSubnational2Id' },
                              { xmlNode:'country',        htmlId:'searchCountry' },
                              { xmlNode:'subnational1',   htmlId:'searchSubnational1' } ],
            extraParams   : { type:'subnational2' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }


    if ($("#searchTaxonCommon")) {
        $("#searchTaxonCommon").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'taxonId',        htmlId:'searchTaxonId' },
                              { xmlNode:'taxon',          htmlId:'searchTaxon' } ],
            extraParams   : { type:'taxonCommon' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 4,
            scrollHeight  : 200,
            width         : 350
        });
    }

    if ($("#searchTaxon")) {
        $("#searchTaxon").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'taxonId',         htmlId:'searchTaxonId' },
                              { xmlNode:'taxonCommon',     htmlId:'searchTaxonCommon' } ],
            extraParams   : { type:'taxon' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 4,
            scrollHeight  : 200,
            width         : 350
        });
    }

    if ($("#splicedBy")) {
        $("#splicedBy").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'archivistId',   htmlId:'splicedById' } ],
            extraParams   : { type:'archivist' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#subnational1")) {
        $("#subnational1").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'countryId',      htmlId:'countryId' },
                              { xmlNode:'subnational1Id', htmlId:'subnational1Id' },
                              { xmlNode:'subnational2Id', htmlId:'subnational2Id' },
                              { xmlNode:'country',        htmlId:'country' },
                              { xmlNode:'subnational2',   htmlId:'subnational2' } ],
            extraParams   : { type:'subnational1' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#subnational11")) {
        $("#subnational11").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'countryId',      htmlId:'countryId1' },
                              { xmlNode:'subnational1Id', htmlId:'subnational1Id1' },
                              { xmlNode:'subnational2Id', htmlId:'subnational2Id1' },
                              { xmlNode:'country',        htmlId:'country1' },
                              { xmlNode:'subnational2',   htmlId:'subnational21' } ],
            extraParams   : { type:'subnational1' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#subnational12")) {
        $("#subnational12").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'countryId',      htmlId:'countryId2' },
                              { xmlNode:'subnational1Id', htmlId:'subnational1Id2' },
                              { xmlNode:'subnational2Id', htmlId:'subnational2Id2' },
                              { xmlNode:'country',        htmlId:'country2' },
                              { xmlNode:'subnational2',   htmlId:'subnational22' } ],
            extraParams   : { type:'subnational1' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#subnational2")) {
        $("#subnational2").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'countryId',      htmlId:'countryId' },
                              { xmlNode:'subnational1Id', htmlId:'subnational1Id' },
                              { xmlNode:'subnational2Id', htmlId:'subnational2Id' },
                              { xmlNode:'country',        htmlId:'country' },
                              { xmlNode:'subnational1',   htmlId:'subnational1' } ],
            extraParams   : { type:'subnational2' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#subnational21")) {
        $("#subnational21").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'countryId',      htmlId:'countryId1' },
                              { xmlNode:'subnational1Id', htmlId:'subnational1Id1' },
                              { xmlNode:'subnational2Id', htmlId:'subnational2Id1' },
                              { xmlNode:'country',        htmlId:'country1' },
                              { xmlNode:'subnational1',   htmlId:'subnational11' } ],
            extraParams   : { type:'subnational2' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#subnational22")) {
        $("#subnational22").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'countryId',      htmlId:'countryId2' },
                              { xmlNode:'subnational1Id', htmlId:'subnational1Id2' },
                              { xmlNode:'subnational2Id', htmlId:'subnational2Id2' },
                              { xmlNode:'country',        htmlId:'country2' },
                              { xmlNode:'subnational1',   htmlId:'subnational12' } ],
            extraParams   : { type:'subnational2' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 1,
            scrollHeight  : 200,
            width         : 250
        });
    }

    if ($("#taxonCommon")) {
        $("#taxonCommon").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'taxonId',        htmlId:'taxonId' },
                              { xmlNode:'taxon',          htmlId:'taxon' } ],
            extraParams   : { type:'taxonCommon' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 4,
            scrollHeight  : 200,
            width         : 350
        });
    }

    if ($("#taxon")) {
        $("#taxon").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'taxonId',         htmlId:'taxonId' },
                              { xmlNode:'taxonCommon',     htmlId:'taxonCommon' } ],
            extraParams   : { type:'taxon' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 3,
            scrollHeight  : 200,
            width         : 350
        });
    }

    if ($("#taxonCommon1")) {
        $("#taxonCommon1").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'taxonId',        htmlId:'taxonId1' },
                              { xmlNode:'taxon',          htmlId:'taxon1' } ],
            extraParams   : { type:'taxonCommon' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 4,
            scrollHeight  : 200,
            width         : 350
        });
    }

    if ($("#taxon1")) {
        $("#taxon1").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'taxonId',         htmlId:'taxonId1' },
                              { xmlNode:'taxonCommon',     htmlId:'taxonCommon1' } ],
            extraParams   : { type:'taxon' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 4,
            scrollHeight  : 200,
            width         : 350
        });
    }

    if ($("#taxonCommon2")) {
        $("#taxonCommon2").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'taxonId',        htmlId:'taxonId2' },
                              { xmlNode:'taxon',          htmlId:'taxon2' } ],
            extraParams   : { type:'taxonCommon' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 4,
            scrollHeight  : 200,
            width         : 350
        });
    }

    if ($("#taxon2")) {
        $("#taxon2").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'taxonId',         htmlId:'taxonId2' },
                              { xmlNode:'taxonCommon',     htmlId:'taxonCommon2' } ],
            extraParams   : { type:'taxon' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 4,
            scrollHeight  : 200,
            width         : 350
        });
    }



    if ($("#taxonCommon3")) {
        $("#taxonCommon3").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'taxonId',        htmlId:'taxonId3' },
                              { xmlNode:'taxon',          htmlId:'taxon3' } ],
            extraParams   : { type:'taxonCommon' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 4,
            scrollHeight  : 200,
            width         : 350
        });
    }

    if ($("#taxon3")) {
        $("#taxon3").autocomplete("/ws/ac", {
            delay         : 500,
            extraOutputMap: [ { xmlNode:'taxonId',         htmlId:'taxonId3' },
                              { xmlNode:'taxonCommon',     htmlId:'taxonCommon3' } ],
            extraParams   : { type:'taxon' },
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 4,
            scrollHeight  : 200,
            width         : 350
        });
    }


/*
    if ($("#test")) {
        $("#test").autocomplete("/ws/ac", {
            cacheLength   : 1,
            extraParams   : { type:'recordist' },
            delay         : 500,
            extraParamsMap: [ { htmlId:'catalogedDttm',   paramName:'country'},
                              { htmlId:'catalogedByName', paramName:'subnational1'} ],
            extraOutputMap: [ { xmlNode:'id1',   htmlId:'catalogedDttm'},
                              { xmlNode:'name1', htmlId:'catalogedByName'},
                              { xmlNode:'id2',   htmlId:'catalogedDttm'},
                              { xmlNode:'name2', htmlId:'catalogedDttm'},
                              { xmlNode:'id3',   htmlId:'catalogedDttm'},
                              { xmlNode:'name3', htmlId:'dvdNum'} ],
            formatItem    : formatItem,
            limit         : 50,
            minChars      : 3,
            scrollHeight  : 200,
            selectFirst   : false,
            width         : 250
        }).result(function(data, value) {
            ; // nothing here
        });
    }
*/


});

