if (window.jQuery) {
  jQuery(document).ready(function() {
    // Remove all elements with class 'hide-by-jquery'.
    jQuery(".hide-by-jquery").remove();

    //Do a submit if its a form element and if it has been changed
    jQuery(".submit-on-click").click(function(){
      if (this.form)
        this.form.submit();
    });

    // Set in tables with classes "einfach auto_even" the class of every second
    // tr to "even"
    jQuery('table.einfach.auto_even tbody tr:even').addClass('even');

    if (jQuery('a.lightbox').lightBox) {
      // Select all links with lightbox class
      jQuery('a.lightbox').lightBox({
        FixedNavigation:true,
        imageLoading: LIGHTBOX_IMAGE_URL + '/lightbox-ico-loading.gif',
        imageBtnClose: LIGHTBOX_IMAGE_URL + '/lightbox-btn-close.gif',
        imageBtnPrev: LIGHTBOX_IMAGE_URL + '/lightbox-btn-prev.gif',
        imageBtnNext: LIGHTBOX_IMAGE_URL + '/lightbox-btn-next.gif'
      });
    }

    var i = 0;
    jQuery(".select-all").each(function() {
      i++;
      label = jQuery(this).attr("data-label") || "Select all";
      var input_selector = "input[type=checkbox]"
      if (jQuery(this).attr("data-input-class"))
        input_selector += "." + (jQuery(this).attr("data-input-class"));
      id = (jQuery(this).attr("data-input-class") || "select_all") + "_" + i;
      jQuery(this).replaceWith("<input type='checkbox' id='" + id + "' /> <label for='" + id + "' style='display: inline'>" + label + "</label>");
      jQuery("#" + id).click(function() {
        jQuery(input_selector).attr("checked", jQuery(this).attr('checked'));
      });
    })

    // -- expandable lists
    // open / close function
    clickFn = function () {
      var h = $(this); // The header
      if (h.hasClass("current")) {
        h.removeClass("current");
        h.next("div").hide("fast");
      }
      else {
        h.addClass("current");
        h.next("div").show("fast");
      }
      return false;
    };
    // register the function
    $(".expandable_list :header").click(clickFn);
    $(".expandable_list :header").keypress(function(e) {
      if (e.which == 13 || e.which == 32) {
        clickFn.call(this);
      }
    });
    // hide all not "current" expandable lists first
    $(".expandable_list :header").each(function() {
      if (!$(this).hasClass("current")) {
        $(this).addClass("current");
        $(this).click();
      }
    });

    // -- tabs
    if (jQuery(".ui-tabs").tabs) {
      jQuery(".ui-tabs").tabs();
    }

    // -- Bookmark-Link:
    if(window.opera) { // add a "rel" attrib if Opera 7+
      if ($("a.jqbookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set
        $("a.jqbookmark").attr("rel","sidebar");
      }
    }
    else if ((!window.sidebar || !window.sidebar.addPanel) && !window.external) {
      $("a.jqbookmark").remove(); // Do not display this link
    }
    $("a.jqbookmark").click(function(event){
      event.preventDefault(); // prevent the anchor tag from sending the user off to the link
      var url = this.href;

      if (window.sidebar && window.sidebar.addPanel) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(document.title, url,"");
      }
      else if( window.external ) { // IE Favorite
        window.external.AddFavorite(url, document.title);
      }
      else if(window.opera) { // Opera 7+
        return false; // do nothing - the rel="sidebar" should do the trick
      }
      else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
        alert('Unfortunately, this browser does not support the requested action,'
          + ' please bookmark this page manually.');
      }
      return false;
    });

    // -- Results switcher e.g. for the Homepage
    $("a.display_result_no").click(function(event){
      if ($(this).attr('result_id')) {
        $(".result").toggleClass("invisible", true);
        $("a.display_result_no").toggleClass("inactive", true);
        $("a.display_result_no").toggleClass("active", false);
        $("#" + $(this).attr('result_id')).toggleClass("invisible", false);
        $(this).toggleClass("inactive", false);
        $(this).toggleClass("active", true);
      }
    })
    if (jQuery("span.glossaryreference, a.glossaryreference").size() > 0) {
      jQuery.getJSON(jQuery("body").attr('data-glossary-json-url'), function(json) {
        var glossaryEntrys = new Array();
        jQuery.each(json, function(documentId, values) {
          if (values.text_short) {
            glossaryEntrys[documentId] = values.text_short.replace(/"/g, "&quot;").replace(/<[^>]*>/g, "");
          }
        });

        jQuery("span.glossaryreference, a.glossaryreference").each(function(i, element) {
          if (jQuery(element).attr('data-document-id')) {
            if (glossaryEntrys[jQuery(element).attr('data-document-id')]) {
              jQuery(element).replaceWith('<acronym title="' + glossaryEntrys[jQuery(element).attr('data-document-id')] + '">' + element.innerHTML + '</acronym>');
            }
          }
        });
      });
    };
    //swfobject stuff
    $(".flash").each(function() {
      var container = $(this);
      var json_string = container.attr("data-flash");
      container.flash(eval("(" + json_string + ")"));
    });
  //END swfobject stuff
  }); // end jQuery(document).ready
}

