function openPDFWin(url) {
  window.open(url, '', 'resizable');
}

function openPopup(link) {
  url = link.href;
  options = link.getAttribute("data-popup-options") || "";
  window.open(url, '', options);
}

var Nav = {
  preloadImages: [],

  init: function() {
    $("#navigation a").each(function() {
      var img = $(this).find("img")[0];
      $(this).bind("mouseover", { img: img }, Nav.over);
      $(this).bind("mouseout", { img: img }, Nav.out);
      Nav.preloadOverImg(img);
    });
  },

  over: function(event) {
    event.data.img.src = event.data.img.src.replace("normal/", "over/");
  },

  out: function(event) {
    event.data.img.src = event.data.img.src.replace("over/", "normal/");
  },

  preloadOverImg: function(img) {
    var overImg = new Image();
    overImg.src = img.src.replace("normal/", "over/");
    this.preloadImages.push(overImg);
  }
};

var Overlays = {
  init: function() {
    $(function() { 
      $(".hall_of_fame a[rel=#overlay]").overlay({ 
        expose: { 
          color: "#000",
          opacity: 0.5
        },
        top: "center",
        effect: "apple",
        speed: 300,
        onBeforeLoad: function() { 
          var wrap = this.getContent().find(".contentWrap"); 
          wrap.load(this.getTrigger().attr("href") + " #hall_of_fame_content"); 
        } 
      }); 

      $(".gallery a[rel=#overlay]").overlay({ 
        expose: { 
          color: "#000",
          opacity: 0.5
        },
        top: "center",
        effect: "apple",
        speed: 300,
        onBeforeLoad: function() { 
          var wrap = this.getContent().find(".contentWrap"); 
          wrap[0].innerHTML = "<img src='" + this.getTrigger().attr("href") + "'>"; 
        },
        onLoad: function() {
          Overlays.initPrevAndNextLinks(this.getContent().find(".contentWrap img")[0], this.getTrigger());
        } 
      }); 
    });
  },
  
  initPrevAndNextLinks: function(imageEl, triggerLink) {
    var prev = triggerLink.closest("li").prev().find("a[rel=#overlay]");
    var next = triggerLink.closest("li").next().find("a[rel=#overlay]");
    if (prev[0]) {
      $("#previous-link").show().unbind("click").click(function(ev) {
        imageEl.src = prev[0].href;
        Overlays.initPrevAndNextLinks(imageEl, prev)
        ev.preventDefault();  
      });
    } else {
      $("#previous-link").hide();
    }
    if (next[0]) {
      $("#next-link").show().unbind("click").click(function(ev) {
        imageEl.src = next[0].href;
        Overlays.initPrevAndNextLinks(imageEl, next)
        ev.preventDefault();  
      });
    } else {
      $("#next-link").hide();
    }
  }
};

var RegistrationForm = {
  selectedFormElement: null,
  errorOverlay: null,

  init: function() {
    if (jQuery.browser.msie) this.cssHoverCompatibilityForIE();
    this.addRequiredClassToLabels();
    this.initErrorOverlay();
    this.initJQueryValidator();
    $("#schultyp").bind("change", this.toggleSchultypAlternativ);
    $("#new_captcha").replaceWith("<a href='javascript:RegistrationForm.newCaptcha()'>Neuen Code erzeugen</a>")
  },

  initErrorOverlay: function() {
    $("#error_overlay").appendTo("body"); // Overlay muss außerhalb des absolut positionierten #contentbox-Elements sein.
    this.errorOverlay = $("#error_overlay").overlay({
      api: true, 
      top: "30%", 
      absolute: msie6 ? false : true,
      expose: { color: '#333', loadSpeed: 200, opacity: 0.9 },
      onBeforeLoad: function() { 
        if (msie6) $("select").hide(); 
        $("#email_invalid").toggle($("input[name='email']").closest("span.input_wrap").hasClass("error"));
        $("#homepage_invalid").toggle($("input[name='homepage']").closest("span.input_wrap").hasClass("error"));
        $("#captcha_invalid").toggle($("input[name='captcha']").closest("span.input_wrap").hasClass("error"));
      },
      onLoad:  function() { if (!msie6) $("#error_overlay").css({ position: "fixed" }); },
      onClose: function() { if (msie6) $("select").show(); }
    });
  },

  initJQueryValidator: function() {
    jQuery.validator.messages.required = " ";
    jQuery.validator.addMethod("email_surrounding_whitespace_allowed", function(value, element) { 
      return this.optional(element) || /^ *[a-z0-9_.!%-]+@([a-z0-9_!%-]+\.)+[a-z0-9_!%-][a-z0-9_!%-]+ *$/.test(value.toLowerCase()); 
    }, "");
    jQuery.validator.addMethod("url_surrounding_whitespace_allowed", function(value, element) { 
			return this.optional(element) || /^ *(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)? *$/i.test(value);
    }, "");
    $("#registration_form").validate({
      highlight:   function(element, errorClass) { $(element.form).find("label[for=" + element.id + "]").addClass(errorClass); $(element).closest("span.input_wrap").addClass(errorClass);  },
      unhighlight: function(element, errorClass) { $(element.form).find("label[for=" + element.id + "]").removeClass(errorClass); $(element).closest("span.input_wrap").removeClass(errorClass) },
      errorElement: "em",
      invalidHandler: function(event, validator) { RegistrationForm.errorOverlay.load() },
      focusInvalid: false,
      rules: {
        captcha: { remote: "/check-captcha.php" }
      }
    });
  },

  addRequiredClassToLabels: function() {
    $("#registration_form .required").each(function() {
      $("#registration_form label[for=" + this.id + "]").addClass("required")
    });
  },

  cssHoverCompatibilityForIE: function() {
    $("#registration_form input, #registration_form select, #registration_form textarea, #registration_form div.radiobutton").
      bind("mouseover", function() { $(this).addClass("hover") }).
      bind("mouseout", function() { if (RegistrationForm.selectedFormElement != this) $(this).removeClass("hover") }).
      bind("focus", function() { RegistrationForm.selectedFormElement = this; $(this).addClass("hover") }).
      bind("blur", function() { $(this).removeClass("hover") });
  },

  toggleSchultypAlternativ: function() {
    if ($("#schultyp").val() == "Sonstiges …")
      $("#schultyp_alternativ_group").show("fast");
    else
      $("#schultyp_alternativ_group").hide("fast");
  },

  newCaptcha: function() {
    $("#captcha_img")[0].src = "/captcha.php?" + new Date().getTime();
    $("#captcha_img").hide().bind("load", function() { $(this).show() });
  }

};

var msie6 = jQuery.browser.msie && jQuery.browser.version == "6.0";

$(document).ready(function() {
  Nav.init();
  Overlays.init();
  $("a.pdflink").bind("click", function() { openPDFWin(this.href); return false } );
  $("a.popuplink").bind("click", function() { openPopup(this); return false } );
  if(typeof sIFR == "function" && !msie6) {
    sIFR.replaceElement("h1", named({ sFlashSrc: "/_swf/myriad-pro-regular.swf", sColor: "#84005c", sWmode: "transparent" }));
  };
  if ($("#registration_form")[0]) RegistrationForm.init();
});




