document.observe('dom:loaded', function(e) {
  
  // Glider für die Startseite
  if ($("Carousel")) {
    var my_glider = new Glider("Carousel", {
      duration: 0.5,
      autoGlide: true,
      frequency: 10
    });
    
    $$("#Carousel .section").each(function(element) {
      element.observe("click", function(event) {
        document.location = element.down("a").href;
      });
    });
  }
  
  // Suchfeld
  if ($("Search")) {
    var default_search_value = "Suchen…"
    $("Search").down("input").value = default_search_value;
    $("Search").down("input").observe("focus", function(event) {
      if (this.value == default_search_value) {
        this.value = "";
      }
    });
    $("Search").down("input").observe("blur", function(event) {
      if (this.value == "") {
        this.value = default_search_value;
      }
    });
  }
  
  // Anmeldung für Mein dvta
  if ($("Email")) {
    $("Email").value = "Ihre E-Mail-Adresse";
    $("Email").observe("focus", function(event) {
      if (this.value == "Ihre E-Mail-Adresse") {
        this.value = "";
      }
    });
    $("Email").observe("blur", function(event) {
      this.value = this.value || "Ihre E-Mail-Adresse";
    });
  }
  
  // Anmeldung für Mein dvta
  if ($("NewsletterEmail")) {
    $("NewsletterEmail").value = "Ihre E-Mail-Adresse";
    $("NewsletterEmail").observe("focus", function(event) {
      if (this.value == "Ihre E-Mail-Adresse") {
        this.value = "";
      }
    });
    $("NewsletterEmail").observe("blur", function(event) {
      this.value = this.value || "Ihre E-Mail-Adresse";
    });
  }
  
  // Akkordion für 3. Navigationsebene
  if ($("SubContent")) {
    var yumAccordion = new accordion("SubContent", {
      classNames : {
        toggle : "toggler",
        toggleActive : "selected",
        content : "element"
      }
    });
    
    if ($$("#Content .toggler a").first()) {
      var url = self.location.protocol + "//" + self.location.host + self.location.pathname + self.location.hash;
      $$("#Content .toggler a").each(function(element) {
        if (url == element.href) {
          yumAccordion.activate(element.up(".toggler"));
          element.up(".toggler").scrollTo();
        }
      });
    }
    
    if ($$("#Content .press_releases").first()) {
      yumAccordion.activate($$("#Content .press_releases").first().down(".toggler"));
    }
  }
  
  // Popup-Fenster zum Bearbeiten der Nav3-Ebene
  if ($$("#Content a.edit.popup")) {
    $$("#Content a.edit.popup").each(function(element) {
      element.observe("click", function(event) {
        window.open(element.href, element_popup_name(element), "width=850,height=620,scrollbars=yes,resizeable=yes");
        Event.stop(event);
      });
    });
  }
  
  // Mitgliedsanträge
  if ($("HomeAddress")) {
    if ($("MembershipsState").value !== "student") {
      $("HomeAddress").hide();
      $("SchoolAddress").hide();
    }
    
    $("MembershipsState").observe("change", function(event) {
      // alert($("MembershipsState").value);
      if (this.value === "student") {
        $("HomeAddress").show();
        $("SchoolAddress").show();
      } else {
        $("HomeAddress").hide();
        $("SchoolAddress").hide();
        $$("#HomeAddress input").each(function(element) {
          element.value = "";
        });
        $$("#SchoolAddress input").each(function(element) {
          element.value = "";
        });
      }
    });
  }
  
  // Seminar-Anmeldung
  if ($("BillingInformation")) {
    if ($("WorkshopRegistrationsSeparateBillingAddress").checked !== true) {
      $("BillingInformation").hide();
    }
    
    $("WorkshopRegistrationsSeparateBillingAddress").observe("click", function(event) {
      if (this.checked) {
        $("BillingInformation").show();
      } else {
        $("BillingInformation").hide();
        $$("#BillingInformation input").each(function(element) {
          element.value = "";
        });
      }
    });
  }
  
  // Kongress-Anmeldung
  if ($("CongressBillingInformation")) {
    if ($("CongressBookingsSeparateBillingAddress").checked !== true) {
      $("CongressBillingInformation").hide();
    }
    
    $("CongressBookingsSeparateBillingAddress").observe("click", function(event) {
      if (this.checked) {
        $("CongressBillingInformation").show();
      } else {
        $("CongressBillingInformation").hide();
        $$("#CongressBillingInformation input").each(function(element) {
          element.value = "";
        });
      }
    });
  }
  
  // Aufsummierung bei der Kongressanmeldung
  if ($$(".summableInput").first()) {
    $$('.summableInput').each(function(element) {
      Event.observe(element, 'click', function() {
        var sum1 = parseFloat(0);
        var sum2 = parseFloat(0);
        $$('.summableInput').each(function(innerelement) {
          if (innerelement.checked === true) {
            var value = $(innerelement.id + 'Hidden').getValue();
            values = value.split('|');
            if (values.length == 1) {
              values.push(values[0]);
            }
            sum1 = sum1 + parseFloat(values[0]);
            sum2 = sum2 + parseFloat(values[1]);
          }
        }); 
        if(sum1.toString().indexOf(',') == -1) sum1 = sum1 + ',00';
        if(sum2.toString().indexOf(',') == -1) sum2 = sum2 + ',00';
        $('congressBookingSum').innerHTML = sum1 + ' € (' + sum2 + ' € für Nichtmitglieder)';
      });
    });
  }
  
  // Suche zum Schulfinder
  if ($("MtaSchoolsCity")) {
    var default_school_search_value = "Name oder Ort";
    $("MtaSchoolsCity").value = $("MtaSchoolsCity").value || default_school_search_value;
    $("MtaSchoolsCity").observe("focus", function(event) {
      if (this.value == default_school_search_value) {
        this.value = "";
      }
    });
    $$('#Content .school_search .filter input').each(function(element) {
      Event.observe(element, 'click', function() {
        if ($("MtaSchoolsCity").value == default_school_search_value) {
          $("MtaSchoolsCity").value = "";
        }
        this.up("form").submit();
      });
    });
  }
  
  // Kongressanmeldung für „Tage der Funktion”
  if ($("CongressBookingsPackages22")) {
    $$(".summableInput").each(function(element) {
      element.observe("click", function(event) {
        if (element.checked) {
          $$(".summableInput").each(function(e) {
            if (element != e) {
              e.disable();
            }
          });
        } else {
          $$(".summableInput").each(function(e) {
            e.enable();
          });
        }
      });
    });
  }
  
  // Dokumente und Downloads
  if ($("SearchPhrase")) {
    $("SearchPhrase").observe("focus", function() {
      if ($("SearchPhrase").value == "Dokumententitel eingeben…") {
        $("SearchPhrase").value = "";
      }
    });
    $("SearchPhrase").observe("blur", function() {
      if ($("SearchPhrase").value == "") {
        $("SearchPhrase").value = "Dokumententitel eingeben…";
      }
    });
  }
});
