var contentAnimationTime = "fast";

toggleArrow = function (sender, content) {
  if (content.css("display") == "none") {
    sender.removeClass("icon_up");
    sender.addClass("icon_down");
  } else {
    sender.removeClass("icon_down");
    sender.addClass("icon_up");
  }
};

toggleContentArrow = function (sender, content) {
  if (content.css("display") == "none") {
    sender.removeClass("icon_up_content");
    sender.addClass("icon_down_content");
  } else {
    sender.removeClass("icon_down_content");
    sender.addClass("icon_up_content");
  }
};

initContentToggleArrows = function () {
  // initialize toggle arrows
  $("div#content table.content_table tr td a[class*=icon_]").each(function () {
    toggleContentArrow($(this), $(this).parent().parent().next(".toggle-content"));
  });
}

initTableContentToggle = function () {
  $("div#content table.content_table tr td a[class*=icon_]").click(function () {
    var sender = $(this);
    var content = $(this).parent().parent().next(".toggle-content");
    // längere Animation sehen in der Tabelle nicht gut aus
    content.toggle(0, function () {
      toggleContentArrow(sender, content);
    });
  });

  initContentToggleArrows();
};

initContentToggle = function () {
  $("div#content h2 a[href!=#top]").click(function () {
    // where did it came from?
    var sender = ($(this).hasClass("icon_down") || $(this).hasClass("icon_up")) ? $(this) : ($(this).next().is("span") ? $(this).next().next() : $(this).next());  // sorry for that...
    var content = $(this).parent().next();
    content.slideToggle(contentAnimationTime, function () {
      toggleArrow(sender, content);
    });
  });

  // initialize toggle arrows
  $("div#content h2").each(function () {
    toggleArrow($(this).children().last(), $(this).next());
  });

  // initialize quicklinks
  $("p.quicklinks a").click(function (event) {
    event.preventDefault();
    var id = $(this).attr("href");

    // cut leading #
    id = id.substr(1, id.length - 1);

    // scroll smoothly to content
    $('html, body').animate({
      scrollTop: $("#" + id).offset().top
    }, 800);

    // show content, if invisible
    var content = $("div#content h2#" + id + " a").parent().next();
    if (content.css("display") == "none") {
      var sender = $("div#content h2#" + id).has("a.icon_down") ? $("div#content h2#" + id + " a.icon_down") : $("div#content h2#" + id + " a.icon_up");
      content.slideToggle(contentAnimationTime, function () {
        toggleArrow(sender, content);
      });
    }
  });
};


// Popup Footer Menü
// http://webexpose.org/2006/12/28/jquery-pop-up-menu-tutorial/
var obj = null;

checkHover = function() {
  if (obj) {
    obj.find('ul').fadeOut('fast');
  }
};


$(document).ready(function () {
  // find table.content and add classes for backround-image workaround
  $("table.content_table thead th:first").addClass("first");
  $("table.content_table thead th:last").addClass("last");

  // behaviour of search-fiel
  $("div#search input.textbox").focus(function () {
    if ($(this).val() == "Suchbegriff") $(this).val("");
  });
  $("div#search input.textbox").blur(function () {
    if ($(this).val() == "") $(this).val("Suchbegriff");
  });

  // popup menu
  $('ul#bottom-navigation > li').hover(function () {
    if (obj) {
      obj.find('ul').fadeOut('fast');
      obj = null;
    }
    $(this).find('ul').fadeIn('fast');
  }, function () {
    obj = $(this);
    setTimeout("checkHover()", 400);
  });
});




