/** @namespace Functions for Bio-Research products site. */
var Bio = {
  /**
   * Function that loads other function after the body loads.
   */
  main : function () {
    Bio.normalizeHomeProductsHeight();
    Bio.confirmEmail();
    Bio.rollovers();
    Bio.toggleProducts();
  }, 

  /**
   * Make all product boxes on the home page the same height.
   */
  normalizeHomeProductsHeight : function () {
    if ($('learn')) {
      var els = $$('#learn div.product'); // All product divs
      // Find the tallest item
      var maxHeight = els.map(function(el) { return $(el).getHeight(); }).max();
      // Set all elements to the tallest height
      els.each(function(el) { $(el).setStyle({height: maxHeight + 'px'})}); 
    }
  },

  /**
   * On the contact form, do not let the user submit the form unless the 
   * email address matches the email confirmation address.
   */
  confirmEmail : function () {
    var form = "contact";
    var email1 = "email";
    var email2 = "email2";
    var submit = "submit";
    var obs = function () {
      if ($(email1).value == $(email2).value) { 
        $(submit).disabled = false; 
      } else {
        $(submit).disabled = true; 
      }
    };

    if ($(form) && $(email1) && $(email2) && $(submit)) {
      $(form).observe("submit", function (evt) {
        if ($F(email1).empty()) {
          evt.stop();
          alert("Email address is required.");
        } else if ($F(email1).empty() || $F(email2).empty() ||
            ($F(email1) != $F(email2))) {
          evt.stop();
          alert("Please confirm your email address.");
        }
      });
    }
  },

  /**
   * Set up rollovers using the Control.Modal library
   */
  rollovers : function () {
    if (typeof Control == "object") {
      $$("a.popup").each(function (a) {
        new Control.Modal(a, {
          hover : true,
          position : 'relative',
          offsetLeft : -10,
          offsetTop : 5
        });
        // disable clicking of links
        a.observe("click", function(evt) { evt.stop(); });
      });
    }
  },

    /**
    * Show/hide the products list on the contact form
    */
    toggleProducts : function toggleProducts() {
        var link = $('productLink');
        var list = $('productList');
        var closeLink = $('productCloseLink');
        var hasCheckedItems = false;
        var toggle = function toggle(evt) { // Toggling function
            evt.stop();
            list.toggle();
            link.toggleClassName("open");
        }

        if (link && list) {
            // If the list has a class error or any items checked, do not 
            // hide it
            hasCheckedItems = $$("#" + list.id + " input[type=checkbox]").any(
                function (checkbox) { return checkbox.checked; }
            ); 
            if (!hasCheckedItems) {
                if (!list.hasClassName("error")) { list.hide(); }
            }

            // Add a close link if there is not one
            if (!closeLink) {
                list.insert(new Element("a", {
                    href : "#",
                    id : "productCloseLink"
                }).update("click here to close this box"));
                closeLink = $('productCloseLink');
                closeLink.observe('click', function (evt) { 
                    toggle(evt);
                    list.form.scrollTo();
                });
            }

            link.observe("click", toggle);
        }
    }
};

// Run main function after DOM is loaded.
document.observe("dom:loaded", Bio.main);
