// vk:


var VK = function(conf) {
    var that = {},
    conf = conf,
    create = function() {
    },
    _delete = function() {
    },
    updateableAdd = function(updateable) {
	updateables.push(updateable);
    },
    updateableRemove = function(updateable) {
	var i;
	for (i = 0; i < updateables.length; i++) {
	    if (updateables[i] == updateable) {
		updateables.splice(i, 1);
		break;
	    }
	}
    },
    updateables = [],
    updateUpdateables = function(args) {
	var i;
	for (i = 0; i < updateables.length; i++) {
	    updateables[i].update(that, args);
	}
    };
    that.updateableAdd = updateableAdd;
    that.updateableRemove = updateableRemove;
    that.updateUpdateables = updateUpdateables;
    create();
    return(that);
};


var catIdiEncode = function(idi) {
    return(idi.replace(/\./g, '[DOT]').replace(/,/g, '[KOMMA]').replace(/_/g, '[UNDERLINE]'));
}


var VKBasket = function(conf) {
    var that = VK(conf),
    hits = [],
    fill = function() {
	var sid, url, params;
	sid = VKSid();
	url = "searcher/fillBasket";
	params = {sid: sid, format: 'json'};
	that.updateUpdateables({hint: 'basket-filling'});
	$.ajax({url: url,
	        data: params,
	        type: 'POST',
		dataType: 'json',
		timeout: 60000,
		success: function(hitsPackage) {
		    var hit, hits, i;
		    if (hitsPackage.error) {
			alert(hitsPackage.error);
			that.updateUpdateables({hint: 'basket-err'});
			return;
		    }
		    hits = hitsPackage.hits;
		    for (i = 0; i < hits.length; i++) {
			hit = hits[i];
			hitAdd(conf.cats.catGetById(hit.cat_id), hit);
		    }
		    that.updateUpdateables({hint: 'basket-filled'});
		},
		error: function(xhr, status, exception) {
		    var error = true;
		    if (status != "error") {
			if (exception) {
			    alert('fill error, status="'+status+
				  '", exception="'+exception+'"');
			}
			else {
			    alert('fill error, status="'+status+'"');
			}
		    }
		    that.updateUpdateables({hint: 'basket-err'});
		}
	    }); 
    },
    clear = function() {
	hits = [];
	that.updateUpdateables({hint: 'basket-clear'});
    },
    create = function() {
	// fill();
    },
    hitAdd = function(cat, hit) {
	hits.push([cat, hit]);
	that.updateUpdateables({hint: 'basket-add',
		                hit: [cat, hit]});
    },
    hitRemove = function(cat, hit) {
	var i;
	for (i = 0; i < hits.length; i++) {
	    if ((cat == hits[i][0]) && (hit.idi == hits[i][1].idi)) {
		hits.splice(i, 1);
		that.updateUpdateables({hint: 'basket-remove',
			                hit: [cat, hit]});
		break;
	    }
	}
    },
    hitsGet = function() {
	return(hits);
    },
    isHitInBasket = function(cat, hit) {
	var i, is = false;
	for (i = 0; i < hits.length; i++) {
	    if ((cat == hits[i][0]) && (hit.idi == hits[i][1].idi)) {
		is = true;
		break;
	    }
	}
	return(is);
    },
    length = function() {
	return(hits.length);
    },
    urlGet = function() {
	var cat, catId, hit, i, id, idi, url;
	catId = "";
	url = "";	
	for (i = 0; i < hits.length; i++) {
	    cat = hits[i][0];
	    hit = hits[i][1];
	    idi = hit.idi;
	    if (catId != cat.id) {
		catId = cat.id;
		id = catId+'_'+catIdiEncode(hit.idi);
	    }
	    else {
		id = catIdiEncode(idi);
	    }
	    if (url != "") {
		url += '.';
	    }
	    url += encodeURIComponent(id);
	}
	if (url != "") {
	    url = "?m=basket&l=" + url;
	}
	return(url);
    };

    create();

    that.fill = fill;
    that.clear = clear;
    that.length = length;
    that.hitAdd = hitAdd;
    that.hitRemove = hitRemove;
    that.hitsGet = hitsGet;
    that.isHitInBasket = isHitInBasket;
    that.urlGet = urlGet;

    return(that);
};


var VKCat = function(conf) {
    var that = VK(conf),
    err = {},
    mats = null,
    hits = null,
    create = function() {
	var i;
	// alert("VKCat["+conf.id+"] create");
	if (conf["mats"]) {
	    for (i = 0; i < conf["mats"].length; i++) {
		if (mats == null) {
		    mats = {};
		}
		mats[conf["mats"][i]] = true;
	    }
	}
    },
    errorGet = function() {
	return(hits);
    },
    errorSet = function(err) {
	err = err;
	hits = null;
	that.updateUpdateables({hint: 'search-error'});
    },
    hasMats = function(_mats_) {
	var has = true, i, mat;
	if (_mats_ == null) {
	    return(true);
	}
	if (mats == null) {
	    if (_mats_.length > 0) {
		return(false);
	    }
	    else {
		return(true);
	    }
	}
	for (i = 0; i < _mats_.length; i++) {
	    mat = _mats_[i];
	    if (!mats[mat]) {
		has = false;
		break;
	    }
	}
	return(has);
    },
    hasLengthGet = function(mats) {
	return(hits ? hits.length : null);
    },
    hitFields = function() {
	/**/
	var hitFields = conf.conf.hitFields;
	if (!hitFields) {
	    hitFields = [
		['title', 'Titel'],
		['au', 'Autor'],
		['place', 'Verlagsort'],
		['pub', 'Verlag'],
		['date', 'Erscheinungsjahr'],
		['isbn', 'isbn'],
		['subj', 'Schlagwörter']
		];
	}
	return(hitFields);
	/**/
    },
    hitShortAlt = function() {
	return(conf.conf.hitShortAlt);
    },
    hitsLengthGet = function(_hits_) {
	return(hits ? ( hits.totalLength ? hits.totalLength : hits.length )
               : null);
    },
    hitsGet = function() {
	return(hits);
    },
    hitsSet = function(_hits_) {
	hits = _hits_;
	that.updateUpdateables({hint: 'hits'});
    },
    info = function() {
	return(conf.conf.info);
    },
    isSelected = function() { return(selected) },
    matsGet = function() {
	return(conf["mats"]);
    },
    nameGet = function() {
	return(conf["name"]);
    },
    nameShortGet = function() {
	return(conf.nameShort);
    },
    query,
    search = function(args) {
	var mats, q;
	if (conf.conf.isExtern) {
	    query = args["query"];
	    that.updateUpdateables({hint: 'query-extern', 'query': query});
	    // QueryHelp löschen:
	    conf["searcher"].updateUpdateables({hint: 'searching'}); 
	    return;
	}
	if (args["query"]) {
	    query = args["query"];
	    q = query.get();
	    if (!hasMats(q.mats)) {
		errorSet({msg: "Materialartsuche wird nicht unterstützt"});
		return;
	    }
	}
	hits = null;
	that.updateUpdateables({hint: 'searching'});
	conf["searcher"].search({cat: that,
				 query: query,
				 first: args["first"],
				 last: args["last"]});
    },
    select = function(sel) {
	if (selected != sel) {
	    selected = sel;
	    if (!isSelected()) {
		hits = null;
	    }
	    that.updateUpdateables({hint: 'cat select'});
	}
    },
    selected = conf["selected"],
    urlGet = function() {
	return(conf["url"]);
    };
    that.conf = conf.conf;
    that.hasMats = hasMats;
    that.errorGet = errorGet;
    that.errorSet = errorSet;
    that.hitFields = hitFields;
    that.hitShortAlt = hitShortAlt;
    that.id = conf["id"];
    that.isSelected = isSelected;
    that.nameGet = nameGet;
    that.nameShortGet = nameShortGet;
    that.hitsLengthGet = hitsLengthGet;
    that.hitsGet = hitsGet;
    that.hitsSet = hitsSet;
    that.matsGet = matsGet;
    that.search = search;
    that.select = select;
    that.urlGet = urlGet;
    that.info = info;
    create();
    return(that);
};


var VKCats = function(conf) {
    var that = VK(conf),
    cats,
    conf = conf,
    catGetById = function(id) {
	var cat = null, i;
	for (i = 0; i < cats.length; i++) {
	    cat = cats[i];
	    if (cat.id == id) {
		break;
	    }
	}
	return(cat);
    },
    catGetByPos = function(pos) {
	var cat = cats[pos];
	return(cat);
    },
    catsConfGet = function() {
	return(conf["cats"]);
    },
    catsTreeConfGet = function() {
	return(conf["tree"]);
    },
    create = function() {
	var cat, id;
	cats = [];
	for (id in conf["cats"]) {
	    if(conf["cats"].hasOwnProperty(id)) {
		// alert(id+': "'+conf["cats"][id].name+'"');
		cat = VKCat({id: id,
			     conf: conf["cats"][id],
			     name: conf["cats"][id].name,
			     nameShort: conf["cats"][id].nameShort,
			     url: conf["cats"][id].url,
			     searcher: conf["searcher"],
		             selected: conf["cats"][id].preselected,
			     mats: conf["cats"][id].mats});
		cats.push(cat);
	    }
	}
    },
    listGet = function() {
	return(cats);
    },
    search = function(args) {
	var cat, cats, i, query;
	query = args["query"];
	cats = query.catsGet();
	if (!cats) {
	    cats = selectedGet();
	}
	for (i = 0; i < cats.length; i++) {
	    cat = cats[i];
	    cat.search({query: query,
			first: args["first"],
			last: args["last"]});
	}
	that.updateUpdateables({hint: 'search', query: query});
    },
    selectedGet = function() {
	var cat, i, selected = [];
	for (i = 0; i < cats.length; i++) {
	    cat = cats[i];
	    if (cat.isSelected()) {
		selected.push(cat);
	    }
	}
	return(selected);
    },
    selectByIds = function(ids) {
	var cat, i, id;
	for (i = 0; i < cats.length; i++) {
	    cat = cats[i];
	    cat.select(false);
	}
	for (i = 0; i < ids.length; i++) {
	    id = ids[i];
	    cat = catGetById(id);
	    cat.select(true);
	}
    };
    that.catGetById = catGetById;
    that.catGetByPos = catGetByPos;
    that.catsConfGet = catsConfGet;
    that.catsTreeConfGet = catsTreeConfGet;
    that.listGet = listGet;
    that.search = search;
    that.selectByIds = selectByIds;
    that.selectedGet = selectedGet;
    create();
    return(that);
};


var VKCoins = function(conf) {
    var that = VK(conf),
    coinss,
    create = function() {
	coinss = {};
    },
    add = function(coins, vw) {
	var c;
	c = coinss[coins];
	if (c == null) {
	    c = {active: vw, inactive: []};
	    coinss[coins] = c;
	    vw.update(that, {hint: "active"});
	}
	else {
	    c.inactive.push(vw);
	    vw.update(that, {hint: "inactive"});
	}
	// alert("add coin "+ coin);
    },
    remove = function(coins, vw) {
	// alert("remove coin "+ coins);
	var c, i;
	c = coinss[coins];
	if (c != null) {
	    if (c.active == vw) {
		c.active = c.inactive.shift();
		if (c.active == null) {
		    delete(coinss[coins])
		}
		else {
		    c.active.update(that, {hint: "active"});
		}
	    }
	    else {
		for (i = 0; i < c.inactive.length; i++) {
		    if (c.inactive[i] == vw) {
			c.inactive.splice(i, 1);
			break;
		    }
		}
	    }
	}
    };

    that.add = add;
    that.remove = remove;

    create();

    return(that);
};


var VKMat = function(conf) {
    var that = VK(conf),
    conf = conf,
    mats = [],
    url2Mat = {
	"B": "book",
	"T": "periodical",
	"P": "olper",
	"O": "binary",
	"A": "article"
    },

    create = function() {
    },
    add = function(mat) {
	var i, found = false;
	for (i = 0; i < mats.length; i++) {	    
	    if (mat == mats[i]) {
		found = true;
		break;
	    }
	}
	if (!found) {
	    mats.push(mat);
	    mats.sort();
	    that.updateUpdateables({hint: "mat"});
	}
    },
    clear = function(mat) {
	if (mats.length > 0) {
	    mats = [];
	    that.updateUpdateables({hint: "mat"});
	}
    },
    matsFromUrlParam = function(param) {
	var i, id, mat, mats;
	mats = [];
	if ((param == null) || (param.length == 0)) {
	    return(mats);
	}
	for (i = 0; i < param.length; i++) {
	    id = param.substr(i,1);
	    mat = url2Mat[id];
	    if (mat) {
		mats.push(mat);
	    }
	}
	return(mats);
    },
    matsGet = function() {
	var i, mats2 = [];
	for (i = 0; i < mats.length; i++) {
	    mats2.push(mats[i]);
	}
	return(mats2);
    },
    matsSet = function(mats2) {
	mats = mats2;
	that.updateUpdateables({hint: "mat"});
    },
/**/
    matsSelGet = function() {
	var i, matsSel = {};
	for (i = 0; i < mats.length; i++) {	    
	    matsSel[mats[i]] = true;
	}
	return(matsSel);
    },
/**/    
    remove = function(mat) {
	var i, found = false;
	for (i = 0; i < mats.length; i++) {	    
	    if (mat == mats[i]) {
		found = true;
		break;
	    }
	}
	if (found) {
	    mats.splice(i,1);
	    that.updateUpdateables({hint: "mat"});
	}
    };
    that.add = add;
    that.matsFromUrlParam = matsFromUrlParam;
    that.remove = remove;
    that.clear = clear;
    that.create = create;
    that.matsGet = matsGet;
    that.matsSet = matsSet;
    create();
    return(that);
};


var VKQuery = function(conf) {
    var that = VK(conf),
    conf = conf,
    keys = null,
    vals = null,
    ops = null,
    mats = null,
    cats = null,
    mat2Url = {
	"book": "B",
	"periodical": "T",
	"olper": "P",
	"binary": "O",
	"article": "A"
    },

    normalize = function() {
	var i;
	for (i = 0; i < vals.length; i += 1) {
	    if (vals[i] == "") {
		keys.splice(i, 1);
		vals.splice(i, 1);
		ops.splice(i, 1);
		i--;
	    }
	}
	if (i > 0) {
	    if (ops.length > i - 1) {
		ops.splice(i - 1, ops.length - i + 1);
	    }
	}
    },

    create = function() {
	keys = conf["keys"];
	vals = conf["vals"];
	ops = conf["ops"];
	mats = conf["mats"];
	cats = conf["cats"];
	normalize();
    },

    displayStr = function(s) {
	var s2ds = {
	all: "Überall",
	person: "Person",
	title: "Titel",
	subject: "Schlagwort",
	id: "Nummer",
	date: "Jahr",
	and: "und",
	or: "oder",
	"and not": "und nicht"
	};
	return(s2ds[s]);
    };

    equalArrays = function(a, b) {
	var i;
	if ((a != null) && (b != null)) {
	    if (a.length != b.length) {
		return(false);		
	    }
	    for (i = 0; i < a.length; i++) {
		if (a[i] != b[i]) {
		    return(false);		
		}
	    }
	    return(true);
	}
	else if ((a == null) && (b == null)) {
	    return(true);
	}
	else {
	    return(false);
	}
    },

    catsGet = function() {
	return(cats);
    },

    equal = function(q) {
	var q = q.get();
	return(equalArrays(keys, q.keys) && equalArrays(vals, q.vals) && 
	       equalArrays(mats, q.mats) && equalArrays(ops, q.ops)
	       && equalArrays(cats, q.cats));
    },

    get = function() {
	return({keys: keys, vals: vals, ops: ops, mats: mats, cats: cats});
    },

    asDisplayStringGet = function() {
	var i, mat, str, first = true;
	str = "";
	for (i = 0; i < vals.length; i++) {
	    if (i == 0) {
		str = displayStr(keys[i])+': ( '+vals[i]+' )';
	    }
	    else {
		str = '( '+str+' ) '+displayStr(ops[i - 1])+
		    ' ( '+displayStr(keys[i])+': ( '+vals[i]+' ) )';
	    }
	}
	mat = that.matUrlParamGet();
	if (mat) {
	    str += ', Mat: '+mat;
	}
	/*
	str += ", "+cats.length+" Katalog";
	if (cats.length != 1) {
	    str += "e";
	}
	*/
	str += ", Katalog(e): ";
	for (i = 0; i < cats.length; i++) {
	    if (first) {
		first = false;
	    }
	    else {
		str += ', ';
	    }
	    str += cats[i].nameShortGet();
	}
	// alert(str);
	return(str);
    },

    asStringGet = function() {
	var i, str;
	str = "";
	for (i = 0; i < vals.length; i++) {
	    if (i == 0) {
		str = keys[i]+': ( '+vals[i]+' )';
	    }
	    else {
		str = '( '+str+' ) '+ops[i - 1]+
		      ' ( '+keys[i]+': ( '+vals[i]+' ) )';
	    }
	}
	// alert(str);
	return(str);
    },

    matUrlParamGet = function() {
	var i, param = "";
	if (mats) {
	    for (i = 0; i < mats.length; i++) {
		param += mat2Url[mats[i]];
	    }
	}
	return(param);
    },

    asURLGet = function() {
	var i, isSimple = true, mat, q, url;
	url = "?";
	for (i = 0; i < vals.length; i++) {
	    if (i == 0) {
		if (keys[0] == "all") {
		    q = encodeURIComponent(vals[i]);
		}
		else {
		    isSimple = false;
		}
	    }
	    else {
		isSimple = false;
		url += '&'+'o'+(i - 1)+'='+ops[i - 1];
		url += '&';
	    }
	    url += "k"+i+"="+keys[i]+'&v'+i+"="+
		encodeURIComponent(vals[i]);
	}
	if (mats.length > 0) {
	    isSimple = false;
	}
	if (isSimple) {
	    url = '?q='+q;
	}
	else {
	    mat = matUrlParamGet();
	    if (mat) {
		url += '&mat='+mat;
	    }
	}
	for (i = 0; i < cats.length; i++) {
	    url += '&'+'c='+cats[i].id;
	}
	return(url);
    },

    isSimple = function() {
	var is = true;
	if (vals.length != 1) {
	    is = false;
	}
	else if (keys[0] != "all") {
	    is = false;
	}
	else if ((mats != null) && (mats.length > 0)) {
	    is = false;
	}
	return(is);
    },

    matsGet = function() {
	return(mats);
    };

    that.asDisplayStringGet = asDisplayStringGet;
    that.asStringGet = asStringGet;
    that.asURLGet = asURLGet;
    that.catsGet = catsGet;
    that.equal = equal;
    that.get = get;
    that.create = create;
    that.equal = equal;
    that.isSimple = isSimple;
    that.matsGet = matsGet;
    that.matUrlParamGet = matUrlParamGet;

    create();

    return(that);
};


var VKQueryHistory = function(conf) {
    var that = VK(conf),
    conf = conf,
    queries = [],
    posSel = null,
    add = function(query) {
	var p = findQueryPos(query);
	/*
	that.updateUpdateables({hint: "query-history"});
	*/
	if (p > -1) {
	    query = queries[p];
	    posSel = p;
	    // alert("gefunden");
	    that.updateUpdateables({hint: "query-history", q: query,
			pos: posSel});
	}
	else {	    
	    queries.push(query);
	    posSel = queries.length - 1;
	    // alert("nicht gefunden");
	    that.updateUpdateables({hint: "query-history", q: query,
			pos: posSel});
	}
	return(query);
    },
    create = function() {
    },
    findQuery = function(q) {
	var i;
	for (i = 0; i < queries.length; i++) {
	    if (queries[i].equal(q)) {
		return(queries[i]);
	    }
	}
	return(null);
    },
    findQueryPos = function(q) {
	var i;
	for (i = 0; i < queries.length; i++) {
	    if (queries[i].equal(q)) {
		return(i);
	    }
	}
	return(-1);
    },
    queriesGet = function() {
	return(queries);
    },
    querySelectByPos = function(pos) {
	if ((0 <= pos) && (pos < queries.length)) {
	    posSel = pos;
	    that.updateUpdateables({hint: "query-history-select",
			pos: pos});
	}
    }
    sel = function() {
	var q = null;
	if ((posSel != null) && (queries.length)) {
	    q = queries[posSel];
	}
	return(q);
    },
    that.add = add;
    that.findQuery = findQuery;
    that.create = create;
    that.queriesGet = queriesGet;
    that.querySelectByPos = querySelectByPos;
    that.sel = sel;
    create();
    return(that);
};


var VKSearcher = function(conf) {
    var that = VK(conf),
    conf = conf,
    create = function() {
	// alert("VKSearcher create");
    },
    search = function(args) {
	var data, mat;
	that.updateUpdateables({hint: 'searching'});
	data = { c: args["cat"].id,
		 q: args["query"].asStringGet(),
		 qid: 1,
		 f: args["first"] - 1,
		 l: args["last"] - 1,
		 format: 'json'};
	mat = args["query"].matUrlParamGet();
	if (mat) {
	    data["mat"] = mat;
	}
	/*
	alert("Search for \""+args["query"].asStringGet()+"\" in cat \""+
	      args["cat"].id+"\"");
	*/
	$.ajax({url: conf["url"],
		type: 'GET',
		dataType: 'json',
		data: data,
		success: function(hits) {
		    // alert("success cat "+args["cat"].id+" "+hits.length);
		    /*
		    alert("success cat "+args["cat"].id+" "+
			  hits["hits"].length);
		    */
		    args["cat"].hitsSet(hits);
		},
		error: function(xhr, status, exception) {
		    // alert('cat-'+args["cat"].id+' error: "'+status+'"');
		    args["cat"].errorSet({status: status,
				          exception: exception});
		}}); 
    };
    that.search = search;
    create();
    return(that);
};


var VKSid = function() {
    var sid = $("html>head meta[name=sid]").attr("content");
    // alert('sid: "'+sid+'"');
    return(sid)
}


var opacUrlGet = function(hit) {
    var opac_url, opac_url_natde,
    natQuery = function(q) {
	return(q.replace('?', ' '));
    };
    if (hit.cat_id == 'teso') {
        opac_url = hit.opac_url;
        opac_url_natde = opac_url;
        opac_url_natde = opac_url_natde.replace(/\/\/gateway\.proquest\.com\//,
            '//gateway.proquest.com.proxy.nationallizenzen.de/');
        /*
        opac_url_natde = opac_url_natde.replace(/\/\/teso\.chadwyck\.co\.uk\//,
            '//teso.chadwyck.co.uk.proxy.nationallizenzen.de/');
        */
    }
    else if (hit.cat_id == 'nat_pao') {
	if (hit.ajId) {
            if (hit.idi.substr(3,1) == "a") {
		// article: (Bsp. pioa400105080142)
		opac_url = 'http://gateway.proquest.com/openurl?'+
		'url_ver=Z39.88-2004&res_dat=xri:pao:'+
		'&rft_dat=xri:pao:article:'+hit.ajId;
		opac_url_natde = 
		'http://pao.chadwyck.co.uk.proxy.nationallizenzen.de/'+
		'articles/displayItemFromId.do?'+
		'QueryType=articles&ItemID='+hit.ajId;
	    }
            else if (hit.idi.substr(3,1) == "z") {
		// journal: (Bsp. piozh372)
		opac_url = 'http://gateway.proquest.com/openurl?'+
		'url_ver=Z39.88-2004&res_dat=xri:pao:'+
		'&rft_dat=xri:pao:journal:'+hit.ajId;
		opac_url_natde = 
		'http://pao.chadwyck.co.uk.proxy.nationallizenzen.de/'+
		'journals/displayItemFromId.do?'+
		'QueryType=journals&ItemID='+hit.ajId;
	    }
	}
    }
    else if (hit.cat_id == 'nat_pio') {
	if (hit.ajId) {
            if (hit.idi.substr(3,1) == "a") {
		// article: (Bsp. pioa400105080142)
		opac_url = 'http://gateway.proquest.com/openurl?'+
		'url_ver=Z39.88-2004&res_dat=xri:pio:'+
		'&rft_dat=xri:pio:article:'+hit.ajId;
		opac_url_natde = 
		'http://pio.chadwyck.co.uk.proxy.nationallizenzen.de/'+
		'articles/displayItemFromId.do?'+
		'QueryType=articles&ItemID='+hit.ajId;
	    }
            else if (hit.idi.substr(3,1) == "z") {
		// journal: (Bsp. piozh372)
		opac_url = 'http://gateway.proquest.com/openurl?'+
		'url_ver=Z39.88-2004&res_dat=xri:pio:'+
		'&rft_dat=xri:pio:journal:'+hit.ajId;
		opac_url_natde = 
		'http://pio.chadwyck.co.uk.proxy.nationallizenzen.de/'+
		'journals/displayItemFromId.do?'+
		'QueryType=journals&ItemID='+hit.ajId;
	    }
	}
	else {
	    /* ajId kann fuer PIO-Artikel nicht sicher aus den Daten ermittelt 
	       werden:
	    */
            if (hit.idi.substr(3,1) == "a") {
		// article: (Bsp. pioah66100120015)

		opac_url = 'articles/executeSearch.do?Suchen=Suchen';
		if (hit.title) {
		    opac_url += '&searchTermTitle=' +
			encodeURIComponent(natQuery(hit.title));
		}
		if (hit.author) {
		    opac_url += '&searchTermAuthor=' +
			encodeURIComponent(hit.author);
		}
		if (hit.jtitle) {
		    opac_url += '&searchTermJnlTitle=' +
			encodeURIComponent(hit.jtitle);
		}
		if (hit.date) {
		    opac_url += '&yearFrom=' +
			encodeURIComponent(hit.date);
		}

		opac_url_natde = 
		'http://pio.chadwyck.co.uk.proxy.nationallizenzen.de/'+
		opac_url;
		opac_url = 'http://pio.chadwyck.co.uk/'+opac_url;
	    }
	    /* nicht noetig, da ajId fuer PIO-Zeitschriften sicher 
	       aus den Daten (idi) erzeugt werden kann:
            else if (hit.idi.substr(3,1) == "z") {
		// journal: (Bsp. pioz6402)

		if (hit.title) {
		    opac_url = 'journals/executeSearch.do?Suchen=Suchen';
		    opac_url += '&searchTermJnlTitle=' +
		    encodeURIComponent(hit.title);

		    opac_url_natde = 
		    'http://pio.chadwyck.co.uk.proxy.nationallizenzen.de/'+
		    opac_url;
		    opac_url = 'http://pio.chadwyck.co.uk/'+opac_url;
		}
	    }
	    */
	}
    }
    else {
        opac_url = hit.opac_url || null;
    }
    return({"opac_url": opac_url, "opac_url_natde": opac_url_natde})
}
