text: // Generated by CoffeeScript 1.6.3
  /*
   * @author Robert Kolatzek
   * @version 1.0
   *     The MIT License (MIT)
   *
   *  Copyright (c) 2015 Dr. Robert Kolatzek
   *
   *  Permission is hereby granted, free of charge, to any person obtaining a copy
   *  of this software and associated documentation files (the "Software"), to deal
   *  in the Software without restriction, including without limitation the rights
   *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   *  copies of the Software, and to permit persons to whom the Software is
   *  furnished to do so, subject to the following conditions:
   *
   *  The above copyright notice and this permission notice shall be included in
   *  all copies or substantial portions of the Software.
   *
   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
   *  THE SOFTWARE.
  */
  
  var $, ajaxGnd, appendSwdItem, coffeePrintf, emptySwdList, exactSearch, getItemInfo, gndData, gndTimeoutWarning, gndXhr, gndXhrType, infoGndTimeout, listGndTypes, openGndType, removeGngItem, searchGND, searchGndItemById, searchGndTimeout, takeGndItem, zeigeRecord, _SEPARATOR_,
    __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  
  $ = jQuery;
  
  gndData = {};
  
  gndXhr = {};
  
  gndXhrType = 'exact';
  
  _SEPARATOR_ = ',';
  
  $(function() {
    var i, imFeld, str, wortArr, _i, _len;
    imFeld = $.trim($(swdId).val());
    wortArr = imFeld.split("" + _SEPARATOR_);
    for (_i = 0, _len = wortArr.length; _i < _len; _i++) {
      i = wortArr[_i];
      if ($.trim(i) !== "") {
        str = coffeePrintf.apply(null, [
          schlagwortSpanselected, {
            schlagwort: $.trim(i.replace("<", "&lt;").replace("\"", "&quote;")),
            'swdWortClass': swdWortClass
          }
        ]);
        $(swdDivSammlungId).append($(str));
      }
    }
    $("." + swdWortClass).unbind('click');
    $("." + swdWortClass).bind('click', removeGngItem);
    $(swdErsatzNachrichtId).empty();
    $(gndFormHtml).insertAfter(gndDivInsertAfterId);
    $(gndDivInsertAfterId).append($("<input type='text' id='" + (swdId.replace('#', '')) + "' name='" + (swdId.replace('#', '')) + "'/>"));
    if (exactSearchId.length > 0) {
      $(exactSearchId).bind('click', exactSearch);
    }
    $("<br/><div id='swd_div_sammlung'>" + schlagwortUebernehmen + "</div>").insertAfter(insertswdCollectionAfterId);
    $(swdId).val(appendSwdItem(swdId, ''));
    $(xgndSearchFormId).fadeIn('slow');
    $("form" + xgndSearchFormId).unbind('submit').bind('submit', exactSearch);
    $(partSearchButtonId).unbind('click').bind('click', searchGND);
    if (typeof window.onpopstate !== "undefined") {
      window.onpopstate = function(event) {
        var GNDcoffeineState;
        GNDcoffeineState = event.state;
        if (typeof GNDcoffeineState['swdsuche'] !== 'undefined') {
          $(suchwortId).val(GNDcoffeineState['swdsuche']);
          $('html, body').animate({
            scrollTop: $(xgndSearchFormId).offset().top
          }, 100);
          searchGND();
        }
        return true;
      };
    }
    return true;
  });
  
  searchGND = function() {
    try {
      event.preventDefault();
    } catch (_error) {}
    gndData = {};
    $(gndsDiv).html(bitteWarten);
    gndXhrType = 'part';
    ajaxGnd("pica.sw", $(suchwortId).val(), "pica.tbs=\"s\"", "listGndTypes", gndTimeoutWarning);
    return false;
  };
  
  listGndTypes = function(txt) {
    var elem, elems, i, str, typ, _ref;
    window.clearTimeout(window.gndLoadTimeout);
    $(gndsDiv).empty();
    if (gndXhrType === 'exact') {
      $(gndsDiv).html(exactSearchResultMsg);
    } else if (gndXhrType === 'part') {
      $(gndsDiv).html(fuzzySearchResultMsg);
    }
    if (typeof window.history.pushState !== "undefined") {
      document.title = "" + (document.title.split('| ' + documentHistoryItem)[0]) + " | " + documentHistoryItem + " " + ($(suchwortId).val());
      history.pushState({
        swdsuche: $(suchwortId).val()
      }, "" + documentHistoryItem + " " + ($(suchwortId).val()), "#" + encodeURIComponent($(suchwortId).val()));
    }
    $(xgndSearchFormId).css('cursor', 'default');
    if (txt) {
      txt.forEach(function(n) {
        var typ;
        typ = n.Typ;
        if (!gndData[typ]) {
          return gndData[typ] = [n];
        } else {
          return gndData[typ] = gndData[typ].concat(n);
        }
      });
      for (typ in gndData) {
        i = gndData[typ];
        elems = {};
        _ref = gndData[typ];
        for (i in _ref) {
          elem = _ref[i];
          elems[elem.Ansetzung] = elem;
        }
        gndData[typ] = elems;
      }
      for (typ in gndData) {
        i = gndData[typ];
        str = coffeePrintf.apply(null, [
          zeigeListeDerGruppeTyp, {
            "typ": typ
          }
        ]);
        $(gndsDiv).append($("<fieldset class='gnd_typ' data-typ='" + typ + "' id='fieldset_" + typ + "'><legend><a href='#' title='" + str + "'>" + typ + " (" + (Object.keys(i).length) + ")</a></legend></fieldset>"));
        $('.gnd_typ a').unbind('click');
        $('.gnd_typ a').bind('click', openGndType);
        if (gndXhrType === 'exact') {
          $("#fieldset_" + typ + " a").click();
        }
      }
    } else {
      $(gndsDiv).append($(emptyResultWarning));
    }
    return false;
  };
  
  openGndType = function() {
    var gndSortedType, gndSortedTypeObjects, i, k, obj, typ, _i, _len, _ref;
    typ = $(this).parent().parent().data('typ');
    $("#fieldset_" + typ).css('cursor', 'wait');
    if (typeof $(this).data('closed') === "undefined" || $(this).data('closed') === true) {
      $(this).data('closed', false);
      $(this).parent().parent().append($("<ul id='gnd_list_" + typ + "' class='gnd_list'></ul>"));
      if (gndData[typ]) {
        gndSortedType = (function() {
          var _results;
          _results = [];
          for (k in gndData[typ]) {
            _results.push(k);
          }
          return _results;
        })();
        gndSortedType.sort();
        gndSortedTypeObjects = {};
        _ref = gndData[typ];
        for (i in _ref) {
          obj = _ref[i];
          gndSortedTypeObjects[obj.Ansetzung] = obj;
        }
        for (_i = 0, _len = gndSortedType.length; _i < _len; _i++) {
          k = gndSortedType[_i];
          obj = gndSortedTypeObjects[k];
          $("#gnd_list_" + typ).append($("<li id='gnd_list_item_" + obj.PPN + "' data-gndppn='" + obj.PPN + "' data-gndwort='" + obj.Ansetzung + "' class='gnd_list_item'><a href='#' class='takeGndItem' title='" + schlagwortInFundListe + "'>" + obj.Ansetzung + "</a> <a href='#' class='getItemInfo' title='" + detailsHinweis + "'>" + Details + "</a> </li>"));
        }
        $('.gnd_list_item a').unbind('click');
        $('.gnd_list_item a.takeGndItem').bind('click', takeGndItem);
        $('.gnd_list_item a.getItemInfo').bind('click', getItemInfo);
      }
    } else {
      $(this).data('closed', true);
      $("#gnd_list_" + typ).remove();
    }
    $("#fieldset_" + typ).css('cursor', 'default');
    return false;
  };
  
  takeGndItem = function() {
    var i, imFeld, str, wort, wortArr, _i, _len;
    wort = $(this).parent().data('gndwort');
    imFeld = $.trim($(swdId).val());
    wortArr = imFeld.split("" + _SEPARATOR_);
    for (_i = 0, _len = wortArr.length; _i < _len; _i++) {
      i = wortArr[_i];
      if ($.trim(wort) === $.trim(i)) {
        return false;
      }
    }
    $(swdId).val(appendSwdItem(swdId, wort));
    str = coffeePrintf.apply(null, [
      schlagwortSpanselected, {
        schlagwort: wort.replace('<', '&lt;').replace("\"", "&quote;"),
        'swdWortClass': swdWortClass
      }
    ]);
    $(swdDivSammlungId).append($(str));
    $("." + swdWortClass).unbind('click');
    $("." + swdWortClass).bind('click', removeGngItem);
    return false;
  };
  
  getItemInfo = function(event) {
    var ppn;
    try {
      event.preventDefault();
    } catch (_error) {}
    ppn = $(this).parent().data('gndppn');
    if (typeof $(this).data('closed') === 'undefined' || $(this).data('closed') === 1) {
      $(this).data('closed', 0);
      ajaxGnd("pica.ppn", ppn, encodeURIComponent('pica.tbs="s" and '), "zeigeRecord", infoGndTimeout);
    } else {
      $(this).data('closed', 1);
      $("#gnd_list_item_info_" + ppn).remove();
    }
    return false;
  };
  
  removeGngItem = function() {
    var r, swdArr, swdNewArr, swds, w, wort, _i, _len;
    wort = $(this).data('wort');
    swdArr = $(swdId).val().split(" " + _SEPARATOR_ + " ");
    swdNewArr = [];
    for (_i = 0, _len = swdArr.length; _i < _len; _i++) {
      w = swdArr[_i];
      if (__indexOf.call(swdNewArr, w) < 0 && $.trim(w) !== $.trim(wort)) {
        r = new RegExp("$\s*" + _SEPARATOR_ + "\s*$", "g");
        swdNewArr.push($.trim(w).replace(r, ''));
      }
    }
    swds = swdNewArr.join(" " + _SEPARATOR_ + " ");
    $(swdId).val(swds);
    return $(this).remove();
  };
  
  emptySwdList = function() {
    $(swdId).val('');
    return $(swdDivSammlungId).empty();
  };
  
  zeigeRecord = function(txt) {
    var i, liste, n, str1, tabelle, v, verweiseliste, _i, _len, _ref, _results;
    window.clearTimeout(window.gndLoadTimeout);
    $(xgndSearchFormId).css('cursor', 'default');
    $("#gnd_list_item_" + txt[0].PPN).append($("<div id='gnd_list_item_info_" + txt[0].PPN + "' class='gnd_list_item_info'></div>"));
    str1 = coffeePrintf.apply(null, [
      detailsFuerSchlagwort, {
        schlagwort: txt[0].Ansetzung
      }
    ]);
    tabelle = $("<table class='gnd_list_item_info_table'><caption>" + str1 + "</caption><thead><tr><td>" + Typ + "</td><td>" + Inhalt + "</td></tr></thead><tbody></tbody></table>");
    $("#gnd_list_item_info_" + txt[0].PPN).append(tabelle);
    _ref = txt[0];
    _results = [];
    for (n in _ref) {
      v = _ref[n];
      if (n === 'Verweise' && typeof v !== 'undefined' && v.length > 0) {
        verweiseliste = $("<tr></tr>");
        verweiseliste.append("<td>" + n + "</td>");
        liste = '';
        for (_i = 0, _len = v.length; _i < _len; _i++) {
          i = v[_i];
          if (i.feldname !== "") {
            liste += " <b>" + i.feldname + "</b>: ";
          }
          if (typeof i.ppn !== 'undefined') {
            liste += "<i><a href='#' class='item_link' data-ppn='" + i.ppn + "'>" + i.linkname + "</a></i><br>";
          } else {
            liste += "<i>" + i.linkname + "</i><br>";
          }
        }
        verweiseliste.append($("<td>" + liste + "</td>"));
        $("#gnd_list_item_info_" + txt[0].PPN + " > table > tbody ").append(verweiseliste);
        $('.item_link').unbind('click');
        _results.push($('.item_link').bind('click', searchGndItemById));
      } else if (n === 'Synonyme' && v !== null && v.length > 0) {
        _results.push($("#gnd_list_item_info_" + txt[0].PPN + " > table > tbody ").append($("<tr><td>" + n + "</td><td>" + (v.join('; ')) + "</td></tr>")));
      } else if (n !== 'Typ' && n !== 'Ansetzung' && n !== 'PPN' && v !== null && v.length > 0) {
        _results.push($("#gnd_list_item_info_" + txt[0].PPN + " > table > tbody ").append($("<tr><td>" + n + "</td><td>" + v + "</td></tr>")));
      } else {
        _results.push(void 0);
      }
    }
    return _results;
  };
  
  searchGndItemById = function() {
    var ppn;
    $(suchwortId).val($(this).text());
    $('html, body').animate({
      scrollTop: $(xgndSearchFormId).offset().top
    }, 100);
    gndData = {};
    ppn = $(this).data('ppn');
    $(gndsDiv).html(bitteWarten);
    gndXhrType = 'exact';
    ajaxGnd("pica.ppn", ppn, encodeURIComponent('pica.tbs="s" and '), "listGndTypes", gndTimeoutWarning);
    return false;
  };
  
  exactSearch = function() {
    try {
      event.preventDefault();
    } catch (_error) {}
    gndData = {};
    $(gndsDiv).html(bitteWarten);
    gndXhrType = 'exact';
    ajaxGnd("pica.swr", $(suchwortId).val(), 'pica.tbs="s"', "listGndTypes", gndTimeoutWarning);
    return false;
  };
  
  searchGndTimeout = function(jqXHR, textStatus, errorThrown) {
    return $(gndsDiv).html(dataErrorWarning);
  };
  
  gndTimeoutWarning = function() {
    return $(gndsDiv).html(timeoutWarning);
  };
  
  infoGndTimeout = function() {
    return $(this).append($("" + timeoutWarning));
  };
  
  ajaxGnd = function(searchtype, suchwort, suchoptionen, callbackFunction, gndTimeoutWarningFunction) {
    $(xgndSearchFormId).css('cursor', 'wait');
    gndXhr = $.ajax({
      "url": 'https://xgnd.bsz-bw.de/Anfrage',
      "data": {
        "suchfeld": searchtype,
        "suchwort": encodeURIComponent(suchwort),
        "suchfilter": "000000",
        suchoptionen: suchoptionen,
        "jsonpCallback": callbackFunction
      },
      "dataType": "jsonp",
      "jsonpCallback": callbackFunction,
      "timeout": gndSearchTimeout,
      "error": "searchGndTimeout"
    });
    return window.gndLoadTimeout = window.setTimeout(gndTimeoutWarningFunction, gndSearchTimeout);
  };
  
  coffeePrintf = function(string, values) {
    var r, _i, _len, _ref;
    _ref = string.match(/#\{([^\}]+)\}/ig);
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      r = _ref[_i];
      string = string.replace(r, "" + values[r.replace('#{', '').replace('}', '')]);
    }
    return string;
  };
  
  appendSwdItem = function(swdId, item) {
    var swdArr, swdNewArr, swdlist, w, _i, _len, _ref;
    swdlist = $.trim($(swdId).val());
    swdArr = swdlist.split(" " + _SEPARATOR_ + " ");
    swdNewArr = [];
    for (_i = 0, _len = swdArr.length; _i < _len; _i++) {
      w = swdArr[_i];
      if ((_ref = $.trim(w), __indexOf.call(swdNewArr, _ref) < 0) && $.trim(w) !== '') {
        swdNewArr.push(w);
      }
    }
    if ($.trim(item) !== '' && __indexOf.call(swdNewArr, item) < 0) {
      swdNewArr.push(item);
    }
    return swdNewArr.join(" " + _SEPARATOR_ + " ");
  };
  