$(document).ready(function(){
   // hide all story boxes inside collapsed lists
   $(".storylist.collapsed .storybox.normal").hide();

   // Setting up WAI-ARIA in the DOM
   $("#content").attr("role", "main");
   $("#footer").attr("role", "contentinfo");
   $("#navigation").attr("role", "navigation");
   $("#search").attr("role", "search");
   $("#header").attr("role", "banner");
   $("a.archiveToggle.collapsed").attr({
      "role": "button",
      "aria-pressed": "false"
   });
   $("a.archiveToggle.expanded").attr({
      "role": "button",
      "aria-pressed": "true"
   })
   $(".storylist.collapsed,.storylist.expanded").attr({
      "aria-live": "polite",
      "aria-relevant": "all",
      "aria-atomic": "true"
   });

   // activate archive toggle links
   $("a.archiveToggle").hover(function() {
      $(this).addClass("hover");
   }, function() {
      $(this).removeClass("hover");
   }).click(function(event) {
      $this = $(this).blur().toggleClass("expanded");

      if ($this.attr("aria-pressed") == "true") {
         $this.attr("aria-pressed", "false").html('<span class="hidden">Alle <span lang="en">Stories</span> anzeigen</span>');
      } else {
         $this.attr("aria-pressed", "true").html('<span class="hidden">Nur Top-<span lang="en">Stories</span> anzeigen</span>');
      }
      // show/hide non-highlight stories
      $(".storybox.normal", $this.closest(".archiveDay")).slideToggle(500).parent(".storylist").toggleClass("collapsed");
      // refresh ivw pixel
      var $pixel = $("img#ivwCounter");
      var pixelSrc = $pixel.attr("src").replace("/toggle", "");
      $pixel.attr({
         "src": [
            pixelSrc.substring(0, pixelSrc.indexOf("?")),
            "/toggle",
            "?r=",
            escape(document.referrer),
            "&_=",
            Math.floor(Math.random() * 10e8)
         ].join(""),
         "width": 1,
         "height": 1,
         "alt": ""
      });
      return false;
   });

   // enable visitingcard links in forums
   $("a[rel='visitingcard']").click(function() {
      var href = $(this).attr("href");
      globalPopup(href, 440, 490);
      return false;
   });

   // Hover for the submitbutton
   $(".hoverButton").hover(function() {
      $(this).attr("src", $(this).attr("src").replace(/_\w*\.png/, "_hover.png"));
   }, function() {
      $(this).attr("src", $(this).attr("src").replace(/_\w*\.png/, "_normal.png"));
   });

   return;
});

/**
 * Generic popup method
 */
var globalPopup = function(url, wd, ht, options) {
   if (url != null) {
      if (!options) {
         options = "resizable=yes,status=yes,scrollbars=yes";
      }
      options += ",width=" + (wd || 400) + ",height=" + (ht || 300);
      window.open(url, "popup", options);
   }
   return;
};
