if (typeof(Prototype)!='undefined') {
  Object.extend(Try, {
    block : function() {
      var lambda        = arguments[0] || Prototype.emptyFunction,
          errorHandler  = arguments[1] || Prototype.emptyFunction;
      try {
        lambda();
        return true;
      } catch(e) {
        errorHandler.apply(e);
        return false;
      }
    }
  });
  Object.extend(Function.prototype, {
    schedule  : function() {
      var timeout = arguments[0] || 1;
      return new PeriodicalExecuter(this, timeout);
    }
  });
  var imgResize = function(element) {
    var modifier = (Object.isUndefined(arguments[1])) ? 1 : arguments[1];
    Try.block(function() {
      var size = element.getDimensions();
      if (typeof(modifier)=='number') {
        var width   = (modifier * size.width),
            height  = (modifier * size.height);
      } else {
        if (modifier.width) {
          if (modifier.height) {
            var width   = modifier.width,
                height  = modifier.height;
          } else {
            var width   = modifier.width,
                height  = ((modifier.width / size.width) * size.height).floor();
          }
        } else if (modifier.height) {
          var width   = ((modifier.height / size.height) * size.width).floor(),
              height  = modifier.height;
        }
      }
      if (typeof(width)!='undefined' && typeof(height)!='undefined') {
        element.setStyle({
          width   : width+'px',
          height  : height+'px'
        });
      }
    });
    return element;
  };
  Object.extend(Element.Methods.ByTag, {
    "IMG"  : Object.extend(Object.clone(Element.Methods), {
      resize : imgResize
    }),
    "DIV": Object.extend(Object.clone(Element.Methods), {
      centerize : function(element) {
        Try.block(function() {
          var element = $(element),
              width   = element.getWidth(),
              height  = element.getHeight();
          element
            .absolutize()
            .setStyle({
              left        : '50%',
              top         : '50%',
              marginLeft  : (-1 * (width / 2))+'px',
              marginTop   : (-1 * (height / 2))+'px'
            });
        });
        return element;
      }
    }),
    "SELECT":   Object.extend(Object.clone(Form.Element.Methods), {
      selected            : function(element) {
        return $A(element.options).detect(function(option) {
          return option.selected;
        });
      },
      selectOption  : function(element, value) {
        $A(element.options).each(function(option) {
          option.selected = (option.value==value) ? true : false;
        });
        return element;
      },
      addOptions    : function(element, options) {
        var map = Object.extend({
          text      : 'text',
          value     : 'value',
          selected  : 'selected'
        }, arguments[2] || {});
        element.reset();
        options.each(function(option) {
          element.options[element.options.length] = new Option(option[map.text], option[map.value], false, option[map.selected] || false);
        });
        return element;
      },
      reset       : function(element) {
        while (element.options.length) {
          element.options[0] = null;
        }
        return element;
      }
    })
  });
}
