/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is LibX Firefox Extension.
 *
 * The Initial Developer of the Original Code is Annette Bailey (libx.org@gmail.com)
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developer and Virginia Tech. All Rights Reserved.
 *
 * Contributor(s): Godmar Back (godmar@gmail.com)
 *
 * ***** END LICENSE BLOCK ***** */

/* 
 *
 */

(function () 
{

// Support for Primo OPAC
function PrimoOPAC() { }

libxEnv.catalogClasses["primo"] = PrimoOPAC;

PrimoOPAC.prototype = new libxCatalog();

libxAddToPrototype(PrimoOPAC.prototype, {

    defaultpubdate: "all_items",
    defaultsearchmode: "contains",
    defaultlanguage: "all_items",
    defaultmaterial: "all_items",
    advsearchmode1: "vl(1UI0)",
    advsearchmode2: "vl(1UI1)",
    isbnneedsadvancedsearch: true,
    scps: "",
    searchmodevar: "vl(1UI0)",
    callnumkey: "lsr01",
    publisherkey: "lsr03",
    tab: "default_tab",

    xisbn: { opacid: "primo" },
    
	convert: function (stype) {
	    switch (stype) {
            case 'Y':  return "any";
            case 't':  return "title";
            case 'a':  return "creator";
            case 'd':  return "sub";
            case 'c':  return this.callnumkey;
            case 'usertag': return "usertag";
            case 'i':  return "isbn";
            case 'is': return "issn";
            case 'publisher':  return this.publisherkey;
	    }
	},

	makeSearch: function(stype, sterm) {
        // some Primo catalogs require advanced searches for ISBN
        if (this.isbnneedsadvancedsearch && (stype == 'i' || stype == 'is'))
            return this.makeAdvancedSearch([ { searchType: stype, searchTerms: sterm } ]);
        var url = this.url + "/primo_library/libweb/action/search.do?fn=go&ct=search"
                + "&vid=" + this.vid
                + "&mode=Basic"
                + "&indx=0"
                + "&dum=true"
                + "&vl(freeText0)=" + encodeURIComponent(sterm)
                + "&" + this.materialvar + "=" + this.defaultmaterial
                + "&" + this.searchmodevar + "=" + this.defaultsearchmode
                + "&" + this.searchvar + "=" + this.convert(stype)
                + "&scps=" + this.scps;
        return url;
    },
    
    makeAdvancedSearch: function(fields) {
        /* Note: Primo does not support Call Number searches in advanced mode. 
         * We ignore this case for now.  */
        var url = this.url + "/primo_library/libweb/action/search.do?frbg=&ct=search&indx=1";
        for(var i = 0; i < fields.length && i < 2; i++) {
            url += '&' + this['advsearchvar' + (i+1)] + '=' + this.convert(fields[i].searchType)
                 + '&' + this['advsearchmode' + (i+1)] + '=' + this.defaultsearchmode
                 + '&' + 'vl(freeText' + i + ')=' + encodeURIComponent(fields[i].searchTerms);
        }
        url += '&' + this.langvar + '=' + this.defaultlanguage
             + '&mode=Advanced'
             + '&vid=' + this.vid
             + '&scp.scps=' + this.scps
             + '&srt=' + this.defaultsort
             + '&tab=' + this.tab
             + '&dum=true'
             + "&" + this.advmaterialvar + "=" + this.defaultmaterial
             + "&" + this.publicationvar + "=" + this.defaultpubdate
             + '&fn=search';
        return url;
    }
});

})();
// vim: ts=4

