javascript/prototype.js
author Tero Marttila <terom@fixme.fi>
Wed, 03 Jun 2009 18:37:12 +0300
branchold-taggr
changeset 37 5df1a18df815
parent 23 10841abbc01f
permissions -rw-r--r--
Old taggr code, never finished
23
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     1
/*  Prototype JavaScript framework, version 1.6.0.1
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     2
 *  (c) 2005-2007 Sam Stephenson
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     3
 *
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     4
 *  Prototype is freely distributable under the terms of an MIT-style license.
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     5
 *  For details, see the Prototype web site: http://www.prototypejs.org/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     6
 *
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     7
 *--------------------------------------------------------------------------*/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     8
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
     9
var Prototype = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    10
  Version: '1.6.0.1',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    11
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    12
  Browser: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    13
    IE:     !!(window.attachEvent && !window.opera),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    14
    Opera:  !!window.opera,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    15
    WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    16
    Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    17
    MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    18
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    19
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    20
  BrowserFeatures: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    21
    XPath: !!document.evaluate,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    22
    ElementExtensions: !!window.HTMLElement,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    23
    SpecificElementExtensions:
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    24
      document.createElement('div').__proto__ &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    25
      document.createElement('div').__proto__ !==
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    26
        document.createElement('form').__proto__
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    27
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    28
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    29
  ScriptFragment: '<script[^>]*>([\\S\\s]*?)<\/script>',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    30
  JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    31
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    32
  emptyFunction: function() { },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    33
  K: function(x) { return x }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    34
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    35
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    36
if (Prototype.Browser.MobileSafari)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    37
  Prototype.BrowserFeatures.SpecificElementExtensions = false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    38
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    39
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    40
/* Based on Alex Arnell's inheritance implementation. */
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    41
var Class = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    42
  create: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    43
    var parent = null, properties = $A(arguments);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    44
    if (Object.isFunction(properties[0]))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    45
      parent = properties.shift();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    46
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    47
    function klass() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    48
      this.initialize.apply(this, arguments);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    49
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    50
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    51
    Object.extend(klass, Class.Methods);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    52
    klass.superclass = parent;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    53
    klass.subclasses = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    54
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    55
    if (parent) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    56
      var subclass = function() { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    57
      subclass.prototype = parent.prototype;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    58
      klass.prototype = new subclass;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    59
      parent.subclasses.push(klass);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    60
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    61
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    62
    for (var i = 0; i < properties.length; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    63
      klass.addMethods(properties[i]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    64
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    65
    if (!klass.prototype.initialize)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    66
      klass.prototype.initialize = Prototype.emptyFunction;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    67
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    68
    klass.prototype.constructor = klass;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    69
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    70
    return klass;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    71
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    72
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    73
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    74
Class.Methods = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    75
  addMethods: function(source) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    76
    var ancestor   = this.superclass && this.superclass.prototype;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    77
    var properties = Object.keys(source);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    78
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    79
    if (!Object.keys({ toString: true }).length)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    80
      properties.push("toString", "valueOf");
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    81
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    82
    for (var i = 0, length = properties.length; i < length; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    83
      var property = properties[i], value = source[property];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    84
      if (ancestor && Object.isFunction(value) &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    85
          value.argumentNames().first() == "$super") {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    86
        var method = value, value = Object.extend((function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    87
          return function() { return ancestor[m].apply(this, arguments) };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    88
        })(property).wrap(method), {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    89
          valueOf:  function() { return method },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    90
          toString: function() { return method.toString() }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    91
        });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    92
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    93
      this.prototype[property] = value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    94
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    95
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    96
    return this;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    97
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    98
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
    99
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   100
var Abstract = { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   101
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   102
Object.extend = function(destination, source) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   103
  for (var property in source)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   104
    destination[property] = source[property];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   105
  return destination;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   106
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   107
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   108
Object.extend(Object, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   109
  inspect: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   110
    try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   111
      if (Object.isUndefined(object)) return 'undefined';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   112
      if (object === null) return 'null';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   113
      return object.inspect ? object.inspect() : object.toString();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   114
    } catch (e) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   115
      if (e instanceof RangeError) return '...';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   116
      throw e;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   117
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   118
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   119
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   120
  toJSON: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   121
    var type = typeof object;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   122
    switch (type) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   123
      case 'undefined':
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   124
      case 'function':
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   125
      case 'unknown': return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   126
      case 'boolean': return object.toString();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   127
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   128
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   129
    if (object === null) return 'null';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   130
    if (object.toJSON) return object.toJSON();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   131
    if (Object.isElement(object)) return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   132
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   133
    var results = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   134
    for (var property in object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   135
      var value = Object.toJSON(object[property]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   136
      if (!Object.isUndefined(value))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   137
        results.push(property.toJSON() + ': ' + value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   138
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   139
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   140
    return '{' + results.join(', ') + '}';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   141
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   142
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   143
  toQueryString: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   144
    return $H(object).toQueryString();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   145
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   146
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   147
  toHTML: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   148
    return object && object.toHTML ? object.toHTML() : String.interpret(object);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   149
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   150
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   151
  keys: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   152
    var keys = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   153
    for (var property in object)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   154
      keys.push(property);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   155
    return keys;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   156
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   157
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   158
  values: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   159
    var values = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   160
    for (var property in object)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   161
      values.push(object[property]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   162
    return values;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   163
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   164
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   165
  clone: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   166
    return Object.extend({ }, object);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   167
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   168
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   169
  isElement: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   170
    return object && object.nodeType == 1;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   171
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   172
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   173
  isArray: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   174
    return object && object.constructor === Array;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   175
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   176
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   177
  isHash: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   178
    return object instanceof Hash;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   179
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   180
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   181
  isFunction: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   182
    return typeof object == "function";
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   183
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   184
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   185
  isString: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   186
    return typeof object == "string";
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   187
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   188
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   189
  isNumber: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   190
    return typeof object == "number";
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   191
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   192
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   193
  isUndefined: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   194
    return typeof object == "undefined";
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   195
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   196
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   197
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   198
Object.extend(Function.prototype, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   199
  argumentNames: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   200
    var names = this.toString().match(/^[\s\(]*function[^(]*\((.*?)\)/)[1].split(",").invoke("strip");
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   201
    return names.length == 1 && !names[0] ? [] : names;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   202
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   203
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   204
  bind: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   205
    if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   206
    var __method = this, args = $A(arguments), object = args.shift();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   207
    return function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   208
      return __method.apply(object, args.concat($A(arguments)));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   209
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   210
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   211
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   212
  bindAsEventListener: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   213
    var __method = this, args = $A(arguments), object = args.shift();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   214
    return function(event) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   215
      return __method.apply(object, [event || window.event].concat(args));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   216
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   217
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   218
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   219
  curry: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   220
    if (!arguments.length) return this;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   221
    var __method = this, args = $A(arguments);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   222
    return function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   223
      return __method.apply(this, args.concat($A(arguments)));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   224
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   225
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   226
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   227
  delay: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   228
    var __method = this, args = $A(arguments), timeout = args.shift() * 1000;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   229
    return window.setTimeout(function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   230
      return __method.apply(__method, args);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   231
    }, timeout);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   232
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   233
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   234
  wrap: function(wrapper) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   235
    var __method = this;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   236
    return function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   237
      return wrapper.apply(this, [__method.bind(this)].concat($A(arguments)));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   238
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   239
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   240
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   241
  methodize: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   242
    if (this._methodized) return this._methodized;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   243
    var __method = this;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   244
    return this._methodized = function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   245
      return __method.apply(null, [this].concat($A(arguments)));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   246
    };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   247
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   248
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   249
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   250
Function.prototype.defer = Function.prototype.delay.curry(0.01);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   251
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   252
Date.prototype.toJSON = function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   253
  return '"' + this.getUTCFullYear() + '-' +
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   254
    (this.getUTCMonth() + 1).toPaddedString(2) + '-' +
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   255
    this.getUTCDate().toPaddedString(2) + 'T' +
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   256
    this.getUTCHours().toPaddedString(2) + ':' +
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   257
    this.getUTCMinutes().toPaddedString(2) + ':' +
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   258
    this.getUTCSeconds().toPaddedString(2) + 'Z"';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   259
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   260
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   261
var Try = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   262
  these: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   263
    var returnValue;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   264
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   265
    for (var i = 0, length = arguments.length; i < length; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   266
      var lambda = arguments[i];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   267
      try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   268
        returnValue = lambda();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   269
        break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   270
      } catch (e) { }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   271
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   272
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   273
    return returnValue;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   274
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   275
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   276
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   277
RegExp.prototype.match = RegExp.prototype.test;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   278
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   279
RegExp.escape = function(str) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   280
  return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   281
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   282
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   283
/*--------------------------------------------------------------------------*/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   284
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   285
var PeriodicalExecuter = Class.create({
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   286
  initialize: function(callback, frequency) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   287
    this.callback = callback;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   288
    this.frequency = frequency;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   289
    this.currentlyExecuting = false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   290
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   291
    this.registerCallback();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   292
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   293
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   294
  registerCallback: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   295
    this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   296
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   297
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   298
  execute: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   299
    this.callback(this);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   300
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   301
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   302
  stop: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   303
    if (!this.timer) return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   304
    clearInterval(this.timer);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   305
    this.timer = null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   306
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   307
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   308
  onTimerEvent: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   309
    if (!this.currentlyExecuting) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   310
      try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   311
        this.currentlyExecuting = true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   312
        this.execute();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   313
      } finally {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   314
        this.currentlyExecuting = false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   315
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   316
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   317
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   318
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   319
Object.extend(String, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   320
  interpret: function(value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   321
    return value == null ? '' : String(value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   322
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   323
  specialChar: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   324
    '\b': '\\b',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   325
    '\t': '\\t',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   326
    '\n': '\\n',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   327
    '\f': '\\f',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   328
    '\r': '\\r',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   329
    '\\': '\\\\'
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   330
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   331
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   332
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   333
Object.extend(String.prototype, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   334
  gsub: function(pattern, replacement) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   335
    var result = '', source = this, match;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   336
    replacement = arguments.callee.prepareReplacement(replacement);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   337
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   338
    while (source.length > 0) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   339
      if (match = source.match(pattern)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   340
        result += source.slice(0, match.index);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   341
        result += String.interpret(replacement(match));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   342
        source  = source.slice(match.index + match[0].length);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   343
      } else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   344
        result += source, source = '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   345
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   346
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   347
    return result;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   348
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   349
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   350
  sub: function(pattern, replacement, count) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   351
    replacement = this.gsub.prepareReplacement(replacement);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   352
    count = Object.isUndefined(count) ? 1 : count;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   353
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   354
    return this.gsub(pattern, function(match) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   355
      if (--count < 0) return match[0];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   356
      return replacement(match);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   357
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   358
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   359
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   360
  scan: function(pattern, iterator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   361
    this.gsub(pattern, iterator);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   362
    return String(this);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   363
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   364
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   365
  truncate: function(length, truncation) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   366
    length = length || 30;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   367
    truncation = Object.isUndefined(truncation) ? '...' : truncation;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   368
    return this.length > length ?
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   369
      this.slice(0, length - truncation.length) + truncation : String(this);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   370
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   371
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   372
  strip: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   373
    return this.replace(/^\s+/, '').replace(/\s+$/, '');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   374
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   375
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   376
  stripTags: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   377
    return this.replace(/<\/?[^>]+>/gi, '');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   378
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   379
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   380
  stripScripts: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   381
    return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), '');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   382
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   383
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   384
  extractScripts: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   385
    var matchAll = new RegExp(Prototype.ScriptFragment, 'img');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   386
    var matchOne = new RegExp(Prototype.ScriptFragment, 'im');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   387
    return (this.match(matchAll) || []).map(function(scriptTag) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   388
      return (scriptTag.match(matchOne) || ['', ''])[1];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   389
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   390
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   391
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   392
  evalScripts: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   393
    return this.extractScripts().map(function(script) { return eval(script) });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   394
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   395
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   396
  escapeHTML: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   397
    var self = arguments.callee;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   398
    self.text.data = this;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   399
    return self.div.innerHTML;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   400
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   401
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   402
  unescapeHTML: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   403
    var div = new Element('div');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   404
    div.innerHTML = this.stripTags();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   405
    return div.childNodes[0] ? (div.childNodes.length > 1 ?
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   406
      $A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   407
      div.childNodes[0].nodeValue) : '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   408
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   409
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   410
  toQueryParams: function(separator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   411
    var match = this.strip().match(/([^?#]*)(#.*)?$/);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   412
    if (!match) return { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   413
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   414
    return match[1].split(separator || '&').inject({ }, function(hash, pair) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   415
      if ((pair = pair.split('='))[0]) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   416
        var key = decodeURIComponent(pair.shift());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   417
        var value = pair.length > 1 ? pair.join('=') : pair[0];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   418
        if (value != undefined) value = decodeURIComponent(value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   419
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   420
        if (key in hash) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   421
          if (!Object.isArray(hash[key])) hash[key] = [hash[key]];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   422
          hash[key].push(value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   423
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   424
        else hash[key] = value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   425
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   426
      return hash;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   427
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   428
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   429
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   430
  toArray: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   431
    return this.split('');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   432
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   433
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   434
  succ: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   435
    return this.slice(0, this.length - 1) +
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   436
      String.fromCharCode(this.charCodeAt(this.length - 1) + 1);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   437
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   438
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   439
  times: function(count) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   440
    return count < 1 ? '' : new Array(count + 1).join(this);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   441
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   442
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   443
  camelize: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   444
    var parts = this.split('-'), len = parts.length;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   445
    if (len == 1) return parts[0];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   446
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   447
    var camelized = this.charAt(0) == '-'
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   448
      ? parts[0].charAt(0).toUpperCase() + parts[0].substring(1)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   449
      : parts[0];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   450
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   451
    for (var i = 1; i < len; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   452
      camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   453
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   454
    return camelized;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   455
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   456
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   457
  capitalize: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   458
    return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   459
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   460
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   461
  underscore: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   462
    return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z\d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   463
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   464
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   465
  dasherize: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   466
    return this.gsub(/_/,'-');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   467
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   468
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   469
  inspect: function(useDoubleQuotes) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   470
    var escapedString = this.gsub(/[\x00-\x1f\\]/, function(match) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   471
      var character = String.specialChar[match[0]];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   472
      return character ? character : '\\u00' + match[0].charCodeAt().toPaddedString(2, 16);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   473
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   474
    if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   475
    return "'" + escapedString.replace(/'/g, '\\\'') + "'";
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   476
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   477
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   478
  toJSON: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   479
    return this.inspect(true);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   480
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   481
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   482
  unfilterJSON: function(filter) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   483
    return this.sub(filter || Prototype.JSONFilter, '#{1}');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   484
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   485
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   486
  isJSON: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   487
    var str = this;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   488
    if (str.blank()) return false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   489
    str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   490
    return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   491
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   492
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   493
  evalJSON: function(sanitize) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   494
    var json = this.unfilterJSON();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   495
    try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   496
      if (!sanitize || json.isJSON()) return eval('(' + json + ')');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   497
    } catch (e) { }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   498
    throw new SyntaxError('Badly formed JSON string: ' + this.inspect());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   499
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   500
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   501
  include: function(pattern) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   502
    return this.indexOf(pattern) > -1;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   503
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   504
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   505
  startsWith: function(pattern) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   506
    return this.indexOf(pattern) === 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   507
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   508
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   509
  endsWith: function(pattern) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   510
    var d = this.length - pattern.length;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   511
    return d >= 0 && this.lastIndexOf(pattern) === d;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   512
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   513
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   514
  empty: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   515
    return this == '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   516
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   517
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   518
  blank: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   519
    return /^\s*$/.test(this);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   520
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   521
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   522
  interpolate: function(object, pattern) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   523
    return new Template(this, pattern).evaluate(object);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   524
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   525
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   526
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   527
if (Prototype.Browser.WebKit || Prototype.Browser.IE) Object.extend(String.prototype, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   528
  escapeHTML: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   529
    return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   530
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   531
  unescapeHTML: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   532
    return this.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   533
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   534
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   535
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   536
String.prototype.gsub.prepareReplacement = function(replacement) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   537
  if (Object.isFunction(replacement)) return replacement;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   538
  var template = new Template(replacement);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   539
  return function(match) { return template.evaluate(match) };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   540
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   541
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   542
String.prototype.parseQuery = String.prototype.toQueryParams;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   543
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   544
Object.extend(String.prototype.escapeHTML, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   545
  div:  document.createElement('div'),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   546
  text: document.createTextNode('')
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   547
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   548
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   549
with (String.prototype.escapeHTML) div.appendChild(text);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   550
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   551
var Template = Class.create({
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   552
  initialize: function(template, pattern) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   553
    this.template = template.toString();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   554
    this.pattern = pattern || Template.Pattern;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   555
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   556
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   557
  evaluate: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   558
    if (Object.isFunction(object.toTemplateReplacements))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   559
      object = object.toTemplateReplacements();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   560
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   561
    return this.template.gsub(this.pattern, function(match) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   562
      if (object == null) return '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   563
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   564
      var before = match[1] || '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   565
      if (before == '\\') return match[2];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   566
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   567
      var ctx = object, expr = match[3];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   568
      var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   569
      match = pattern.exec(expr);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   570
      if (match == null) return before;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   571
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   572
      while (match != null) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   573
        var comp = match[1].startsWith('[') ? match[2].gsub('\\\\]', ']') : match[1];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   574
        ctx = ctx[comp];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   575
        if (null == ctx || '' == match[3]) break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   576
        expr = expr.substring('[' == match[3] ? match[1].length : match[0].length);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   577
        match = pattern.exec(expr);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   578
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   579
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   580
      return before + String.interpret(ctx);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   581
    }.bind(this));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   582
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   583
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   584
Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   585
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   586
var $break = { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   587
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   588
var Enumerable = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   589
  each: function(iterator, context) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   590
    var index = 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   591
    iterator = iterator.bind(context);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   592
    try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   593
      this._each(function(value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   594
        iterator(value, index++);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   595
      });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   596
    } catch (e) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   597
      if (e != $break) throw e;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   598
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   599
    return this;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   600
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   601
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   602
  eachSlice: function(number, iterator, context) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   603
    iterator = iterator ? iterator.bind(context) : Prototype.K;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   604
    var index = -number, slices = [], array = this.toArray();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   605
    while ((index += number) < array.length)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   606
      slices.push(array.slice(index, index+number));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   607
    return slices.collect(iterator, context);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   608
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   609
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   610
  all: function(iterator, context) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   611
    iterator = iterator ? iterator.bind(context) : Prototype.K;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   612
    var result = true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   613
    this.each(function(value, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   614
      result = result && !!iterator(value, index);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   615
      if (!result) throw $break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   616
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   617
    return result;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   618
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   619
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   620
  any: function(iterator, context) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   621
    iterator = iterator ? iterator.bind(context) : Prototype.K;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   622
    var result = false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   623
    this.each(function(value, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   624
      if (result = !!iterator(value, index))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   625
        throw $break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   626
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   627
    return result;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   628
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   629
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   630
  collect: function(iterator, context) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   631
    iterator = iterator ? iterator.bind(context) : Prototype.K;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   632
    var results = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   633
    this.each(function(value, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   634
      results.push(iterator(value, index));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   635
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   636
    return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   637
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   638
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   639
  detect: function(iterator, context) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   640
    iterator = iterator.bind(context);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   641
    var result;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   642
    this.each(function(value, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   643
      if (iterator(value, index)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   644
        result = value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   645
        throw $break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   646
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   647
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   648
    return result;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   649
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   650
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   651
  findAll: function(iterator, context) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   652
    iterator = iterator.bind(context);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   653
    var results = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   654
    this.each(function(value, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   655
      if (iterator(value, index))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   656
        results.push(value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   657
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   658
    return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   659
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   660
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   661
  grep: function(filter, iterator, context) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   662
    iterator = iterator ? iterator.bind(context) : Prototype.K;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   663
    var results = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   664
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   665
    if (Object.isString(filter))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   666
      filter = new RegExp(filter);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   667
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   668
    this.each(function(value, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   669
      if (filter.match(value))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   670
        results.push(iterator(value, index));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   671
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   672
    return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   673
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   674
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   675
  include: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   676
    if (Object.isFunction(this.indexOf))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   677
      if (this.indexOf(object) != -1) return true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   678
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   679
    var found = false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   680
    this.each(function(value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   681
      if (value == object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   682
        found = true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   683
        throw $break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   684
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   685
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   686
    return found;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   687
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   688
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   689
  inGroupsOf: function(number, fillWith) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   690
    fillWith = Object.isUndefined(fillWith) ? null : fillWith;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   691
    return this.eachSlice(number, function(slice) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   692
      while(slice.length < number) slice.push(fillWith);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   693
      return slice;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   694
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   695
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   696
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   697
  inject: function(memo, iterator, context) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   698
    iterator = iterator.bind(context);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   699
    this.each(function(value, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   700
      memo = iterator(memo, value, index);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   701
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   702
    return memo;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   703
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   704
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   705
  invoke: function(method) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   706
    var args = $A(arguments).slice(1);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   707
    return this.map(function(value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   708
      return value[method].apply(value, args);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   709
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   710
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   711
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   712
  max: function(iterator, context) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   713
    iterator = iterator ? iterator.bind(context) : Prototype.K;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   714
    var result;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   715
    this.each(function(value, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   716
      value = iterator(value, index);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   717
      if (result == null || value >= result)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   718
        result = value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   719
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   720
    return result;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   721
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   722
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   723
  min: function(iterator, context) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   724
    iterator = iterator ? iterator.bind(context) : Prototype.K;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   725
    var result;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   726
    this.each(function(value, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   727
      value = iterator(value, index);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   728
      if (result == null || value < result)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   729
        result = value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   730
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   731
    return result;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   732
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   733
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   734
  partition: function(iterator, context) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   735
    iterator = iterator ? iterator.bind(context) : Prototype.K;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   736
    var trues = [], falses = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   737
    this.each(function(value, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   738
      (iterator(value, index) ?
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   739
        trues : falses).push(value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   740
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   741
    return [trues, falses];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   742
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   743
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   744
  pluck: function(property) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   745
    var results = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   746
    this.each(function(value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   747
      results.push(value[property]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   748
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   749
    return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   750
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   751
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   752
  reject: function(iterator, context) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   753
    iterator = iterator.bind(context);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   754
    var results = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   755
    this.each(function(value, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   756
      if (!iterator(value, index))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   757
        results.push(value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   758
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   759
    return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   760
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   761
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   762
  sortBy: function(iterator, context) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   763
    iterator = iterator.bind(context);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   764
    return this.map(function(value, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   765
      return {value: value, criteria: iterator(value, index)};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   766
    }).sort(function(left, right) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   767
      var a = left.criteria, b = right.criteria;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   768
      return a < b ? -1 : a > b ? 1 : 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   769
    }).pluck('value');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   770
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   771
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   772
  toArray: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   773
    return this.map();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   774
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   775
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   776
  zip: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   777
    var iterator = Prototype.K, args = $A(arguments);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   778
    if (Object.isFunction(args.last()))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   779
      iterator = args.pop();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   780
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   781
    var collections = [this].concat(args).map($A);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   782
    return this.map(function(value, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   783
      return iterator(collections.pluck(index));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   784
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   785
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   786
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   787
  size: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   788
    return this.toArray().length;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   789
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   790
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   791
  inspect: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   792
    return '#<Enumerable:' + this.toArray().inspect() + '>';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   793
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   794
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   795
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   796
Object.extend(Enumerable, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   797
  map:     Enumerable.collect,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   798
  find:    Enumerable.detect,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   799
  select:  Enumerable.findAll,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   800
  filter:  Enumerable.findAll,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   801
  member:  Enumerable.include,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   802
  entries: Enumerable.toArray,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   803
  every:   Enumerable.all,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   804
  some:    Enumerable.any
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   805
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   806
function $A(iterable) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   807
  if (!iterable) return [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   808
  if (iterable.toArray) return iterable.toArray();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   809
  var length = iterable.length || 0, results = new Array(length);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   810
  while (length--) results[length] = iterable[length];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   811
  return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   812
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   813
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   814
if (Prototype.Browser.WebKit) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   815
  function $A(iterable) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   816
    if (!iterable) return [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   817
    if (!(Object.isFunction(iterable) && iterable == '[object NodeList]') &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   818
        iterable.toArray) return iterable.toArray();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   819
    var length = iterable.length || 0, results = new Array(length);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   820
    while (length--) results[length] = iterable[length];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   821
    return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   822
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   823
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   824
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   825
Array.from = $A;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   826
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   827
Object.extend(Array.prototype, Enumerable);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   828
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   829
if (!Array.prototype._reverse) Array.prototype._reverse = Array.prototype.reverse;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   830
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   831
Object.extend(Array.prototype, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   832
  _each: function(iterator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   833
    for (var i = 0, length = this.length; i < length; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   834
      iterator(this[i]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   835
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   836
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   837
  clear: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   838
    this.length = 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   839
    return this;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   840
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   841
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   842
  first: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   843
    return this[0];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   844
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   845
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   846
  last: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   847
    return this[this.length - 1];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   848
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   849
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   850
  compact: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   851
    return this.select(function(value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   852
      return value != null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   853
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   854
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   855
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   856
  flatten: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   857
    return this.inject([], function(array, value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   858
      return array.concat(Object.isArray(value) ?
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   859
        value.flatten() : [value]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   860
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   861
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   862
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   863
  without: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   864
    var values = $A(arguments);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   865
    return this.select(function(value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   866
      return !values.include(value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   867
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   868
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   869
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   870
  reverse: function(inline) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   871
    return (inline !== false ? this : this.toArray())._reverse();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   872
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   873
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   874
  reduce: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   875
    return this.length > 1 ? this : this[0];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   876
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   877
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   878
  uniq: function(sorted) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   879
    return this.inject([], function(array, value, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   880
      if (0 == index || (sorted ? array.last() != value : !array.include(value)))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   881
        array.push(value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   882
      return array;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   883
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   884
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   885
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   886
  intersect: function(array) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   887
    return this.uniq().findAll(function(item) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   888
      return array.detect(function(value) { return item === value });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   889
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   890
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   891
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   892
  clone: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   893
    return [].concat(this);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   894
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   895
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   896
  size: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   897
    return this.length;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   898
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   899
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   900
  inspect: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   901
    return '[' + this.map(Object.inspect).join(', ') + ']';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   902
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   903
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   904
  toJSON: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   905
    var results = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   906
    this.each(function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   907
      var value = Object.toJSON(object);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   908
      if (!Object.isUndefined(value)) results.push(value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   909
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   910
    return '[' + results.join(', ') + ']';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   911
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   912
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   913
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   914
// use native browser JS 1.6 implementation if available
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   915
if (Object.isFunction(Array.prototype.forEach))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   916
  Array.prototype._each = Array.prototype.forEach;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   917
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   918
if (!Array.prototype.indexOf) Array.prototype.indexOf = function(item, i) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   919
  i || (i = 0);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   920
  var length = this.length;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   921
  if (i < 0) i = length + i;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   922
  for (; i < length; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   923
    if (this[i] === item) return i;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   924
  return -1;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   925
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   926
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   927
if (!Array.prototype.lastIndexOf) Array.prototype.lastIndexOf = function(item, i) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   928
  i = isNaN(i) ? this.length : (i < 0 ? this.length + i : i) + 1;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   929
  var n = this.slice(0, i).reverse().indexOf(item);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   930
  return (n < 0) ? n : i - n - 1;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   931
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   932
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   933
Array.prototype.toArray = Array.prototype.clone;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   934
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   935
function $w(string) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   936
  if (!Object.isString(string)) return [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   937
  string = string.strip();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   938
  return string ? string.split(/\s+/) : [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   939
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   940
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   941
if (Prototype.Browser.Opera){
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   942
  Array.prototype.concat = function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   943
    var array = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   944
    for (var i = 0, length = this.length; i < length; i++) array.push(this[i]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   945
    for (var i = 0, length = arguments.length; i < length; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   946
      if (Object.isArray(arguments[i])) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   947
        for (var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   948
          array.push(arguments[i][j]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   949
      } else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   950
        array.push(arguments[i]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   951
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   952
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   953
    return array;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   954
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   955
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   956
Object.extend(Number.prototype, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   957
  toColorPart: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   958
    return this.toPaddedString(2, 16);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   959
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   960
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   961
  succ: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   962
    return this + 1;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   963
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   964
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   965
  times: function(iterator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   966
    $R(0, this, true).each(iterator);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   967
    return this;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   968
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   969
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   970
  toPaddedString: function(length, radix) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   971
    var string = this.toString(radix || 10);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   972
    return '0'.times(length - string.length) + string;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   973
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   974
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   975
  toJSON: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   976
    return isFinite(this) ? this.toString() : 'null';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   977
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   978
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   979
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   980
$w('abs round ceil floor').each(function(method){
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   981
  Number.prototype[method] = Math[method].methodize();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   982
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   983
function $H(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   984
  return new Hash(object);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   985
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   986
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   987
var Hash = Class.create(Enumerable, (function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   988
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   989
  function toQueryPair(key, value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   990
    if (Object.isUndefined(value)) return key;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   991
    return key + '=' + encodeURIComponent(String.interpret(value));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   992
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   993
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   994
  return {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   995
    initialize: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   996
      this._object = Object.isHash(object) ? object.toObject() : Object.clone(object);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   997
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   998
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
   999
    _each: function(iterator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1000
      for (var key in this._object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1001
        var value = this._object[key], pair = [key, value];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1002
        pair.key = key;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1003
        pair.value = value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1004
        iterator(pair);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1005
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1006
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1007
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1008
    set: function(key, value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1009
      return this._object[key] = value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1010
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1011
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1012
    get: function(key) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1013
      return this._object[key];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1014
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1015
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1016
    unset: function(key) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1017
      var value = this._object[key];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1018
      delete this._object[key];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1019
      return value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1020
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1021
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1022
    toObject: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1023
      return Object.clone(this._object);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1024
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1025
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1026
    keys: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1027
      return this.pluck('key');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1028
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1029
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1030
    values: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1031
      return this.pluck('value');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1032
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1033
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1034
    index: function(value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1035
      var match = this.detect(function(pair) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1036
        return pair.value === value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1037
      });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1038
      return match && match.key;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1039
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1040
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1041
    merge: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1042
      return this.clone().update(object);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1043
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1044
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1045
    update: function(object) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1046
      return new Hash(object).inject(this, function(result, pair) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1047
        result.set(pair.key, pair.value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1048
        return result;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1049
      });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1050
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1051
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1052
    toQueryString: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1053
      return this.map(function(pair) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1054
        var key = encodeURIComponent(pair.key), values = pair.value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1055
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1056
        if (values && typeof values == 'object') {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1057
          if (Object.isArray(values))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1058
            return values.map(toQueryPair.curry(key)).join('&');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1059
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1060
        return toQueryPair(key, values);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1061
      }).join('&');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1062
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1063
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1064
    inspect: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1065
      return '#<Hash:{' + this.map(function(pair) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1066
        return pair.map(Object.inspect).join(': ');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1067
      }).join(', ') + '}>';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1068
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1069
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1070
    toJSON: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1071
      return Object.toJSON(this.toObject());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1072
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1073
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1074
    clone: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1075
      return new Hash(this);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1076
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1077
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1078
})());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1079
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1080
Hash.prototype.toTemplateReplacements = Hash.prototype.toObject;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1081
Hash.from = $H;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1082
var ObjectRange = Class.create(Enumerable, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1083
  initialize: function(start, end, exclusive) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1084
    this.start = start;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1085
    this.end = end;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1086
    this.exclusive = exclusive;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1087
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1088
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1089
  _each: function(iterator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1090
    var value = this.start;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1091
    while (this.include(value)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1092
      iterator(value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1093
      value = value.succ();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1094
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1095
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1096
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1097
  include: function(value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1098
    if (value < this.start)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1099
      return false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1100
    if (this.exclusive)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1101
      return value < this.end;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1102
    return value <= this.end;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1103
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1104
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1105
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1106
var $R = function(start, end, exclusive) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1107
  return new ObjectRange(start, end, exclusive);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1108
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1109
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1110
var Ajax = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1111
  getTransport: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1112
    return Try.these(
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1113
      function() {return new XMLHttpRequest()},
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1114
      function() {return new ActiveXObject('Msxml2.XMLHTTP')},
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1115
      function() {return new ActiveXObject('Microsoft.XMLHTTP')}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1116
    ) || false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1117
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1118
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1119
  activeRequestCount: 0
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1120
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1121
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1122
Ajax.Responders = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1123
  responders: [],
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1124
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1125
  _each: function(iterator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1126
    this.responders._each(iterator);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1127
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1128
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1129
  register: function(responder) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1130
    if (!this.include(responder))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1131
      this.responders.push(responder);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1132
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1133
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1134
  unregister: function(responder) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1135
    this.responders = this.responders.without(responder);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1136
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1137
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1138
  dispatch: function(callback, request, transport, json) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1139
    this.each(function(responder) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1140
      if (Object.isFunction(responder[callback])) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1141
        try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1142
          responder[callback].apply(responder, [request, transport, json]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1143
        } catch (e) { }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1144
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1145
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1146
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1147
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1148
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1149
Object.extend(Ajax.Responders, Enumerable);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1150
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1151
Ajax.Responders.register({
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1152
  onCreate:   function() { Ajax.activeRequestCount++ },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1153
  onComplete: function() { Ajax.activeRequestCount-- }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1154
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1155
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1156
Ajax.Base = Class.create({
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1157
  initialize: function(options) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1158
    this.options = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1159
      method:       'post',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1160
      asynchronous: true,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1161
      contentType:  'application/x-www-form-urlencoded',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1162
      encoding:     'UTF-8',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1163
      parameters:   '',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1164
      evalJSON:     true,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1165
      evalJS:       true
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1166
    };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1167
    Object.extend(this.options, options || { });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1168
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1169
    this.options.method = this.options.method.toLowerCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1170
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1171
    if (Object.isString(this.options.parameters))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1172
      this.options.parameters = this.options.parameters.toQueryParams();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1173
    else if (Object.isHash(this.options.parameters))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1174
      this.options.parameters = this.options.parameters.toObject();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1175
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1176
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1177
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1178
Ajax.Request = Class.create(Ajax.Base, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1179
  _complete: false,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1180
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1181
  initialize: function($super, url, options) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1182
    $super(options);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1183
    this.transport = Ajax.getTransport();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1184
    this.request(url);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1185
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1186
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1187
  request: function(url) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1188
    this.url = url;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1189
    this.method = this.options.method;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1190
    var params = Object.clone(this.options.parameters);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1191
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1192
    if (!['get', 'post'].include(this.method)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1193
      // simulate other verbs over post
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1194
      params['_method'] = this.method;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1195
      this.method = 'post';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1196
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1197
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1198
    this.parameters = params;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1199
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1200
    if (params = Object.toQueryString(params)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1201
      // when GET, append parameters to URL
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1202
      if (this.method == 'get')
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1203
        this.url += (this.url.include('?') ? '&' : '?') + params;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1204
      else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1205
        params += '&_=';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1206
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1207
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1208
    try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1209
      var response = new Ajax.Response(this);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1210
      if (this.options.onCreate) this.options.onCreate(response);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1211
      Ajax.Responders.dispatch('onCreate', this, response);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1212
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1213
      this.transport.open(this.method.toUpperCase(), this.url,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1214
        this.options.asynchronous);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1215
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1216
      if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1217
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1218
      this.transport.onreadystatechange = this.onStateChange.bind(this);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1219
      this.setRequestHeaders();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1220
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1221
      this.body = this.method == 'post' ? (this.options.postBody || params) : null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1222
      this.transport.send(this.body);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1223
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1224
      /* Force Firefox to handle ready state 4 for synchronous requests */
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1225
      if (!this.options.asynchronous && this.transport.overrideMimeType)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1226
        this.onStateChange();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1227
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1228
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1229
    catch (e) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1230
      this.dispatchException(e);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1231
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1232
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1233
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1234
  onStateChange: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1235
    var readyState = this.transport.readyState;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1236
    if (readyState > 1 && !((readyState == 4) && this._complete))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1237
      this.respondToReadyState(this.transport.readyState);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1238
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1239
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1240
  setRequestHeaders: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1241
    var headers = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1242
      'X-Requested-With': 'XMLHttpRequest',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1243
      'X-Prototype-Version': Prototype.Version,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1244
      'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1245
    };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1246
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1247
    if (this.method == 'post') {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1248
      headers['Content-type'] = this.options.contentType +
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1249
        (this.options.encoding ? '; charset=' + this.options.encoding : '');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1250
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1251
      /* Force "Connection: close" for older Mozilla browsers to work
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1252
       * around a bug where XMLHttpRequest sends an incorrect
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1253
       * Content-length header. See Mozilla Bugzilla #246651.
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1254
       */
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1255
      if (this.transport.overrideMimeType &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1256
          (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1257
            headers['Connection'] = 'close';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1258
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1259
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1260
    // user-defined headers
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1261
    if (typeof this.options.requestHeaders == 'object') {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1262
      var extras = this.options.requestHeaders;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1263
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1264
      if (Object.isFunction(extras.push))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1265
        for (var i = 0, length = extras.length; i < length; i += 2)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1266
          headers[extras[i]] = extras[i+1];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1267
      else
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1268
        $H(extras).each(function(pair) { headers[pair.key] = pair.value });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1269
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1270
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1271
    for (var name in headers)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1272
      this.transport.setRequestHeader(name, headers[name]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1273
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1274
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1275
  success: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1276
    var status = this.getStatus();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1277
    return !status || (status >= 200 && status < 300);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1278
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1279
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1280
  getStatus: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1281
    try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1282
      return this.transport.status || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1283
    } catch (e) { return 0 }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1284
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1285
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1286
  respondToReadyState: function(readyState) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1287
    var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1288
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1289
    if (state == 'Complete') {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1290
      try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1291
        this._complete = true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1292
        (this.options['on' + response.status]
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1293
         || this.options['on' + (this.success() ? 'Success' : 'Failure')]
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1294
         || Prototype.emptyFunction)(response, response.headerJSON);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1295
      } catch (e) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1296
        this.dispatchException(e);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1297
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1298
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1299
      var contentType = response.getHeader('Content-type');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1300
      if (this.options.evalJS == 'force'
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1301
          || (this.options.evalJS && contentType
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1302
          && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i)))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1303
        this.evalResponse();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1304
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1305
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1306
    try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1307
      (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1308
      Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1309
    } catch (e) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1310
      this.dispatchException(e);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1311
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1312
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1313
    if (state == 'Complete') {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1314
      // avoid memory leak in MSIE: clean up
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1315
      this.transport.onreadystatechange = Prototype.emptyFunction;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1316
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1317
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1318
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1319
  getHeader: function(name) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1320
    try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1321
      return this.transport.getResponseHeader(name) || null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1322
    } catch (e) { return null }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1323
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1324
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1325
  evalResponse: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1326
    try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1327
      return eval((this.transport.responseText || '').unfilterJSON());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1328
    } catch (e) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1329
      this.dispatchException(e);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1330
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1331
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1332
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1333
  dispatchException: function(exception) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1334
    (this.options.onException || Prototype.emptyFunction)(this, exception);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1335
    Ajax.Responders.dispatch('onException', this, exception);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1336
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1337
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1338
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1339
Ajax.Request.Events =
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1340
  ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1341
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1342
Ajax.Response = Class.create({
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1343
  initialize: function(request){
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1344
    this.request = request;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1345
    var transport  = this.transport  = request.transport,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1346
        readyState = this.readyState = transport.readyState;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1347
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1348
    if((readyState > 2 && !Prototype.Browser.IE) || readyState == 4) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1349
      this.status       = this.getStatus();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1350
      this.statusText   = this.getStatusText();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1351
      this.responseText = String.interpret(transport.responseText);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1352
      this.headerJSON   = this._getHeaderJSON();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1353
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1354
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1355
    if(readyState == 4) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1356
      var xml = transport.responseXML;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1357
      this.responseXML  = Object.isUndefined(xml) ? null : xml;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1358
      this.responseJSON = this._getResponseJSON();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1359
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1360
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1361
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1362
  status:      0,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1363
  statusText: '',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1364
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1365
  getStatus: Ajax.Request.prototype.getStatus,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1366
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1367
  getStatusText: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1368
    try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1369
      return this.transport.statusText || '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1370
    } catch (e) { return '' }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1371
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1372
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1373
  getHeader: Ajax.Request.prototype.getHeader,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1374
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1375
  getAllHeaders: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1376
    try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1377
      return this.getAllResponseHeaders();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1378
    } catch (e) { return null }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1379
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1380
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1381
  getResponseHeader: function(name) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1382
    return this.transport.getResponseHeader(name);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1383
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1384
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1385
  getAllResponseHeaders: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1386
    return this.transport.getAllResponseHeaders();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1387
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1388
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1389
  _getHeaderJSON: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1390
    var json = this.getHeader('X-JSON');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1391
    if (!json) return null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1392
    json = decodeURIComponent(escape(json));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1393
    try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1394
      return json.evalJSON(this.request.options.sanitizeJSON);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1395
    } catch (e) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1396
      this.request.dispatchException(e);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1397
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1398
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1399
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1400
  _getResponseJSON: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1401
    var options = this.request.options;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1402
    if (!options.evalJSON || (options.evalJSON != 'force' &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1403
      !(this.getHeader('Content-type') || '').include('application/json')) ||
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1404
        this.responseText.blank())
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1405
          return null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1406
    try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1407
      return this.responseText.evalJSON(options.sanitizeJSON);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1408
    } catch (e) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1409
      this.request.dispatchException(e);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1410
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1411
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1412
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1413
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1414
Ajax.Updater = Class.create(Ajax.Request, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1415
  initialize: function($super, container, url, options) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1416
    this.container = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1417
      success: (container.success || container),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1418
      failure: (container.failure || (container.success ? null : container))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1419
    };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1420
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1421
    options = Object.clone(options);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1422
    var onComplete = options.onComplete;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1423
    options.onComplete = (function(response, json) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1424
      this.updateContent(response.responseText);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1425
      if (Object.isFunction(onComplete)) onComplete(response, json);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1426
    }).bind(this);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1427
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1428
    $super(url, options);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1429
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1430
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1431
  updateContent: function(responseText) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1432
    var receiver = this.container[this.success() ? 'success' : 'failure'],
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1433
        options = this.options;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1434
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1435
    if (!options.evalScripts) responseText = responseText.stripScripts();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1436
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1437
    if (receiver = $(receiver)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1438
      if (options.insertion) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1439
        if (Object.isString(options.insertion)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1440
          var insertion = { }; insertion[options.insertion] = responseText;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1441
          receiver.insert(insertion);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1442
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1443
        else options.insertion(receiver, responseText);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1444
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1445
      else receiver.update(responseText);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1446
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1447
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1448
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1449
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1450
Ajax.PeriodicalUpdater = Class.create(Ajax.Base, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1451
  initialize: function($super, container, url, options) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1452
    $super(options);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1453
    this.onComplete = this.options.onComplete;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1454
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1455
    this.frequency = (this.options.frequency || 2);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1456
    this.decay = (this.options.decay || 1);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1457
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1458
    this.updater = { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1459
    this.container = container;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1460
    this.url = url;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1461
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1462
    this.start();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1463
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1464
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1465
  start: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1466
    this.options.onComplete = this.updateComplete.bind(this);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1467
    this.onTimerEvent();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1468
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1469
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1470
  stop: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1471
    this.updater.options.onComplete = undefined;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1472
    clearTimeout(this.timer);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1473
    (this.onComplete || Prototype.emptyFunction).apply(this, arguments);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1474
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1475
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1476
  updateComplete: function(response) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1477
    if (this.options.decay) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1478
      this.decay = (response.responseText == this.lastText ?
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1479
        this.decay * this.options.decay : 1);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1480
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1481
      this.lastText = response.responseText;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1482
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1483
    this.timer = this.onTimerEvent.bind(this).delay(this.decay * this.frequency);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1484
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1485
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1486
  onTimerEvent: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1487
    this.updater = new Ajax.Updater(this.container, this.url, this.options);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1488
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1489
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1490
function $(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1491
  if (arguments.length > 1) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1492
    for (var i = 0, elements = [], length = arguments.length; i < length; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1493
      elements.push($(arguments[i]));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1494
    return elements;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1495
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1496
  if (Object.isString(element))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1497
    element = document.getElementById(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1498
  return Element.extend(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1499
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1500
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1501
if (Prototype.BrowserFeatures.XPath) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1502
  document._getElementsByXPath = function(expression, parentElement) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1503
    var results = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1504
    var query = document.evaluate(expression, $(parentElement) || document,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1505
      null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1506
    for (var i = 0, length = query.snapshotLength; i < length; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1507
      results.push(Element.extend(query.snapshotItem(i)));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1508
    return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1509
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1510
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1511
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1512
/*--------------------------------------------------------------------------*/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1513
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1514
if (!window.Node) var Node = { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1515
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1516
if (!Node.ELEMENT_NODE) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1517
  // DOM level 2 ECMAScript Language Binding
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1518
  Object.extend(Node, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1519
    ELEMENT_NODE: 1,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1520
    ATTRIBUTE_NODE: 2,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1521
    TEXT_NODE: 3,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1522
    CDATA_SECTION_NODE: 4,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1523
    ENTITY_REFERENCE_NODE: 5,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1524
    ENTITY_NODE: 6,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1525
    PROCESSING_INSTRUCTION_NODE: 7,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1526
    COMMENT_NODE: 8,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1527
    DOCUMENT_NODE: 9,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1528
    DOCUMENT_TYPE_NODE: 10,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1529
    DOCUMENT_FRAGMENT_NODE: 11,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1530
    NOTATION_NODE: 12
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1531
  });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1532
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1533
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1534
(function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1535
  var element = this.Element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1536
  this.Element = function(tagName, attributes) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1537
    attributes = attributes || { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1538
    tagName = tagName.toLowerCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1539
    var cache = Element.cache;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1540
    if (Prototype.Browser.IE && attributes.name) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1541
      tagName = '<' + tagName + ' name="' + attributes.name + '">';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1542
      delete attributes.name;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1543
      return Element.writeAttribute(document.createElement(tagName), attributes);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1544
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1545
    if (!cache[tagName]) cache[tagName] = Element.extend(document.createElement(tagName));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1546
    return Element.writeAttribute(cache[tagName].cloneNode(false), attributes);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1547
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1548
  Object.extend(this.Element, element || { });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1549
}).call(window);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1550
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1551
Element.cache = { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1552
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1553
Element.Methods = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1554
  visible: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1555
    return $(element).style.display != 'none';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1556
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1557
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1558
  toggle: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1559
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1560
    Element[Element.visible(element) ? 'hide' : 'show'](element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1561
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1562
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1563
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1564
  hide: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1565
    $(element).style.display = 'none';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1566
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1567
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1568
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1569
  show: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1570
    $(element).style.display = '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1571
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1572
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1573
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1574
  remove: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1575
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1576
    element.parentNode.removeChild(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1577
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1578
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1579
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1580
  update: function(element, content) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1581
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1582
    if (content && content.toElement) content = content.toElement();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1583
    if (Object.isElement(content)) return element.update().insert(content);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1584
    content = Object.toHTML(content);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1585
    element.innerHTML = content.stripScripts();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1586
    content.evalScripts.bind(content).defer();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1587
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1588
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1589
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1590
  replace: function(element, content) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1591
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1592
    if (content && content.toElement) content = content.toElement();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1593
    else if (!Object.isElement(content)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1594
      content = Object.toHTML(content);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1595
      var range = element.ownerDocument.createRange();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1596
      range.selectNode(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1597
      content.evalScripts.bind(content).defer();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1598
      content = range.createContextualFragment(content.stripScripts());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1599
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1600
    element.parentNode.replaceChild(content, element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1601
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1602
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1603
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1604
  insert: function(element, insertions) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1605
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1606
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1607
    if (Object.isString(insertions) || Object.isNumber(insertions) ||
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1608
        Object.isElement(insertions) || (insertions && (insertions.toElement || insertions.toHTML)))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1609
          insertions = {bottom:insertions};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1610
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1611
    var content, insert, tagName, childNodes;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1612
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1613
    for (position in insertions) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1614
      content  = insertions[position];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1615
      position = position.toLowerCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1616
      insert = Element._insertionTranslations[position];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1617
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1618
      if (content && content.toElement) content = content.toElement();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1619
      if (Object.isElement(content)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1620
        insert(element, content);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1621
        continue;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1622
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1623
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1624
      content = Object.toHTML(content);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1625
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1626
      tagName = ((position == 'before' || position == 'after')
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1627
        ? element.parentNode : element).tagName.toUpperCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1628
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1629
      childNodes = Element._getContentFromAnonymousElement(tagName, content.stripScripts());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1630
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1631
      if (position == 'top' || position == 'after') childNodes.reverse();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1632
      childNodes.each(insert.curry(element));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1633
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1634
      content.evalScripts.bind(content).defer();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1635
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1636
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1637
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1638
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1639
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1640
  wrap: function(element, wrapper, attributes) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1641
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1642
    if (Object.isElement(wrapper))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1643
      $(wrapper).writeAttribute(attributes || { });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1644
    else if (Object.isString(wrapper)) wrapper = new Element(wrapper, attributes);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1645
    else wrapper = new Element('div', wrapper);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1646
    if (element.parentNode)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1647
      element.parentNode.replaceChild(wrapper, element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1648
    wrapper.appendChild(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1649
    return wrapper;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1650
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1651
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1652
  inspect: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1653
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1654
    var result = '<' + element.tagName.toLowerCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1655
    $H({'id': 'id', 'className': 'class'}).each(function(pair) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1656
      var property = pair.first(), attribute = pair.last();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1657
      var value = (element[property] || '').toString();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1658
      if (value) result += ' ' + attribute + '=' + value.inspect(true);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1659
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1660
    return result + '>';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1661
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1662
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1663
  recursivelyCollect: function(element, property) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1664
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1665
    var elements = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1666
    while (element = element[property])
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1667
      if (element.nodeType == 1)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1668
        elements.push(Element.extend(element));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1669
    return elements;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1670
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1671
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1672
  ancestors: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1673
    return $(element).recursivelyCollect('parentNode');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1674
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1675
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1676
  descendants: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1677
    return $(element).getElementsBySelector("*");
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1678
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1679
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1680
  firstDescendant: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1681
    element = $(element).firstChild;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1682
    while (element && element.nodeType != 1) element = element.nextSibling;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1683
    return $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1684
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1685
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1686
  immediateDescendants: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1687
    if (!(element = $(element).firstChild)) return [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1688
    while (element && element.nodeType != 1) element = element.nextSibling;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1689
    if (element) return [element].concat($(element).nextSiblings());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1690
    return [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1691
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1692
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1693
  previousSiblings: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1694
    return $(element).recursivelyCollect('previousSibling');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1695
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1696
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1697
  nextSiblings: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1698
    return $(element).recursivelyCollect('nextSibling');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1699
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1700
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1701
  siblings: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1702
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1703
    return element.previousSiblings().reverse().concat(element.nextSiblings());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1704
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1705
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1706
  match: function(element, selector) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1707
    if (Object.isString(selector))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1708
      selector = new Selector(selector);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1709
    return selector.match($(element));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1710
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1711
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1712
  up: function(element, expression, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1713
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1714
    if (arguments.length == 1) return $(element.parentNode);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1715
    var ancestors = element.ancestors();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1716
    return Object.isNumber(expression) ? ancestors[expression] :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1717
      Selector.findElement(ancestors, expression, index);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1718
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1719
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1720
  down: function(element, expression, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1721
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1722
    if (arguments.length == 1) return element.firstDescendant();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1723
    return Object.isNumber(expression) ? element.descendants()[expression] :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1724
      element.select(expression)[index || 0];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1725
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1726
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1727
  previous: function(element, expression, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1728
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1729
    if (arguments.length == 1) return $(Selector.handlers.previousElementSibling(element));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1730
    var previousSiblings = element.previousSiblings();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1731
    return Object.isNumber(expression) ? previousSiblings[expression] :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1732
      Selector.findElement(previousSiblings, expression, index);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1733
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1734
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1735
  next: function(element, expression, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1736
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1737
    if (arguments.length == 1) return $(Selector.handlers.nextElementSibling(element));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1738
    var nextSiblings = element.nextSiblings();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1739
    return Object.isNumber(expression) ? nextSiblings[expression] :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1740
      Selector.findElement(nextSiblings, expression, index);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1741
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1742
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1743
  select: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1744
    var args = $A(arguments), element = $(args.shift());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1745
    return Selector.findChildElements(element, args);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1746
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1747
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1748
  adjacent: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1749
    var args = $A(arguments), element = $(args.shift());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1750
    return Selector.findChildElements(element.parentNode, args).without(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1751
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1752
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1753
  identify: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1754
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1755
    var id = element.readAttribute('id'), self = arguments.callee;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1756
    if (id) return id;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1757
    do { id = 'anonymous_element_' + self.counter++ } while ($(id));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1758
    element.writeAttribute('id', id);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1759
    return id;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1760
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1761
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1762
  readAttribute: function(element, name) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1763
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1764
    if (Prototype.Browser.IE) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1765
      var t = Element._attributeTranslations.read;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1766
      if (t.values[name]) return t.values[name](element, name);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1767
      if (t.names[name]) name = t.names[name];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1768
      if (name.include(':')) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1769
        return (!element.attributes || !element.attributes[name]) ? null :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1770
         element.attributes[name].value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1771
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1772
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1773
    return element.getAttribute(name);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1774
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1775
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1776
  writeAttribute: function(element, name, value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1777
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1778
    var attributes = { }, t = Element._attributeTranslations.write;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1779
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1780
    if (typeof name == 'object') attributes = name;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1781
    else attributes[name] = Object.isUndefined(value) ? true : value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1782
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1783
    for (var attr in attributes) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1784
      name = t.names[attr] || attr;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1785
      value = attributes[attr];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1786
      if (t.values[attr]) name = t.values[attr](element, value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1787
      if (value === false || value === null)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1788
        element.removeAttribute(name);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1789
      else if (value === true)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1790
        element.setAttribute(name, name);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1791
      else element.setAttribute(name, value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1792
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1793
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1794
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1795
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1796
  getHeight: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1797
    return $(element).getDimensions().height;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1798
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1799
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1800
  getWidth: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1801
    return $(element).getDimensions().width;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1802
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1803
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1804
  classNames: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1805
    return new Element.ClassNames(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1806
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1807
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1808
  hasClassName: function(element, className) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1809
    if (!(element = $(element))) return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1810
    var elementClassName = element.className;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1811
    return (elementClassName.length > 0 && (elementClassName == className ||
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1812
      new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName)));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1813
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1814
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1815
  addClassName: function(element, className) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1816
    if (!(element = $(element))) return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1817
    if (!element.hasClassName(className))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1818
      element.className += (element.className ? ' ' : '') + className;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1819
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1820
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1821
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1822
  removeClassName: function(element, className) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1823
    if (!(element = $(element))) return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1824
    element.className = element.className.replace(
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1825
      new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ').strip();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1826
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1827
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1828
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1829
  toggleClassName: function(element, className) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1830
    if (!(element = $(element))) return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1831
    return element[element.hasClassName(className) ?
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1832
      'removeClassName' : 'addClassName'](className);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1833
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1834
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1835
  // removes whitespace-only text node children
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1836
  cleanWhitespace: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1837
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1838
    var node = element.firstChild;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1839
    while (node) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1840
      var nextNode = node.nextSibling;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1841
      if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1842
        element.removeChild(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1843
      node = nextNode;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1844
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1845
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1846
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1847
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1848
  empty: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1849
    return $(element).innerHTML.blank();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1850
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1851
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1852
  descendantOf: function(element, ancestor) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1853
    element = $(element), ancestor = $(ancestor);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1854
    var originalAncestor = ancestor;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1855
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1856
    if (element.compareDocumentPosition)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1857
      return (element.compareDocumentPosition(ancestor) & 8) === 8;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1858
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1859
    if (element.sourceIndex && !Prototype.Browser.Opera) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1860
      var e = element.sourceIndex, a = ancestor.sourceIndex,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1861
       nextAncestor = ancestor.nextSibling;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1862
      if (!nextAncestor) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1863
        do { ancestor = ancestor.parentNode; }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1864
        while (!(nextAncestor = ancestor.nextSibling) && ancestor.parentNode);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1865
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1866
      if (nextAncestor) return (e > a && e < nextAncestor.sourceIndex);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1867
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1868
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1869
    while (element = element.parentNode)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1870
      if (element == originalAncestor) return true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1871
    return false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1872
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1873
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1874
  scrollTo: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1875
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1876
    var pos = element.cumulativeOffset();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1877
    window.scrollTo(pos[0], pos[1]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1878
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1879
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1880
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1881
  getStyle: function(element, style) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1882
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1883
    style = style == 'float' ? 'cssFloat' : style.camelize();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1884
    var value = element.style[style];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1885
    if (!value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1886
      var css = document.defaultView.getComputedStyle(element, null);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1887
      value = css ? css[style] : null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1888
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1889
    if (style == 'opacity') return value ? parseFloat(value) : 1.0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1890
    return value == 'auto' ? null : value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1891
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1892
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1893
  getOpacity: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1894
    return $(element).getStyle('opacity');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1895
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1896
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1897
  setStyle: function(element, styles) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1898
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1899
    var elementStyle = element.style, match;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1900
    if (Object.isString(styles)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1901
      element.style.cssText += ';' + styles;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1902
      return styles.include('opacity') ?
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1903
        element.setOpacity(styles.match(/opacity:\s*(\d?\.?\d*)/)[1]) : element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1904
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1905
    for (var property in styles)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1906
      if (property == 'opacity') element.setOpacity(styles[property]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1907
      else
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1908
        elementStyle[(property == 'float' || property == 'cssFloat') ?
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1909
          (Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1910
            property] = styles[property];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1911
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1912
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1913
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1914
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1915
  setOpacity: function(element, value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1916
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1917
    element.style.opacity = (value == 1 || value === '') ? '' :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1918
      (value < 0.00001) ? 0 : value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1919
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1920
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1921
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1922
  getDimensions: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1923
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1924
    var display = $(element).getStyle('display');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1925
    if (display != 'none' && display != null) // Safari bug
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1926
      return {width: element.offsetWidth, height: element.offsetHeight};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1927
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1928
    // All *Width and *Height properties give 0 on elements with display none,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1929
    // so enable the element temporarily
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1930
    var els = element.style;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1931
    var originalVisibility = els.visibility;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1932
    var originalPosition = els.position;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1933
    var originalDisplay = els.display;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1934
    els.visibility = 'hidden';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1935
    els.position = 'absolute';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1936
    els.display = 'block';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1937
    var originalWidth = element.clientWidth;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1938
    var originalHeight = element.clientHeight;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1939
    els.display = originalDisplay;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1940
    els.position = originalPosition;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1941
    els.visibility = originalVisibility;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1942
    return {width: originalWidth, height: originalHeight};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1943
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1944
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1945
  makePositioned: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1946
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1947
    var pos = Element.getStyle(element, 'position');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1948
    if (pos == 'static' || !pos) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1949
      element._madePositioned = true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1950
      element.style.position = 'relative';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1951
      // Opera returns the offset relative to the positioning context, when an
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1952
      // element is position relative but top and left have not been defined
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1953
      if (window.opera) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1954
        element.style.top = 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1955
        element.style.left = 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1956
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1957
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1958
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1959
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1960
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1961
  undoPositioned: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1962
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1963
    if (element._madePositioned) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1964
      element._madePositioned = undefined;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1965
      element.style.position =
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1966
        element.style.top =
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1967
        element.style.left =
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1968
        element.style.bottom =
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1969
        element.style.right = '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1970
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1971
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1972
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1973
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1974
  makeClipping: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1975
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1976
    if (element._overflow) return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1977
    element._overflow = Element.getStyle(element, 'overflow') || 'auto';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1978
    if (element._overflow !== 'hidden')
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1979
      element.style.overflow = 'hidden';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1980
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1981
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1982
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1983
  undoClipping: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1984
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1985
    if (!element._overflow) return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1986
    element.style.overflow = element._overflow == 'auto' ? '' : element._overflow;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1987
    element._overflow = null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1988
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1989
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1990
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1991
  cumulativeOffset: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1992
    var valueT = 0, valueL = 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1993
    do {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1994
      valueT += element.offsetTop  || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1995
      valueL += element.offsetLeft || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1996
      element = element.offsetParent;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1997
    } while (element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1998
    return Element._returnOffset(valueL, valueT);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  1999
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2000
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2001
  positionedOffset: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2002
    var valueT = 0, valueL = 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2003
    do {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2004
      valueT += element.offsetTop  || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2005
      valueL += element.offsetLeft || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2006
      element = element.offsetParent;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2007
      if (element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2008
        if (element.tagName == 'BODY') break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2009
        var p = Element.getStyle(element, 'position');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2010
        if (p == 'relative' || p == 'absolute') break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2011
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2012
    } while (element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2013
    return Element._returnOffset(valueL, valueT);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2014
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2015
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2016
  absolutize: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2017
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2018
    if (element.getStyle('position') == 'absolute') return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2019
    // Position.prepare(); // To be done manually by Scripty when it needs it.
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2020
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2021
    var offsets = element.positionedOffset();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2022
    var top     = offsets[1];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2023
    var left    = offsets[0];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2024
    var width   = element.clientWidth;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2025
    var height  = element.clientHeight;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2026
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2027
    element._originalLeft   = left - parseFloat(element.style.left  || 0);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2028
    element._originalTop    = top  - parseFloat(element.style.top || 0);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2029
    element._originalWidth  = element.style.width;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2030
    element._originalHeight = element.style.height;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2031
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2032
    element.style.position = 'absolute';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2033
    element.style.top    = top + 'px';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2034
    element.style.left   = left + 'px';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2035
    element.style.width  = width + 'px';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2036
    element.style.height = height + 'px';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2037
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2038
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2039
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2040
  relativize: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2041
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2042
    if (element.getStyle('position') == 'relative') return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2043
    // Position.prepare(); // To be done manually by Scripty when it needs it.
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2044
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2045
    element.style.position = 'relative';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2046
    var top  = parseFloat(element.style.top  || 0) - (element._originalTop || 0);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2047
    var left = parseFloat(element.style.left || 0) - (element._originalLeft || 0);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2048
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2049
    element.style.top    = top + 'px';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2050
    element.style.left   = left + 'px';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2051
    element.style.height = element._originalHeight;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2052
    element.style.width  = element._originalWidth;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2053
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2054
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2055
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2056
  cumulativeScrollOffset: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2057
    var valueT = 0, valueL = 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2058
    do {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2059
      valueT += element.scrollTop  || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2060
      valueL += element.scrollLeft || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2061
      element = element.parentNode;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2062
    } while (element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2063
    return Element._returnOffset(valueL, valueT);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2064
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2065
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2066
  getOffsetParent: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2067
    if (element.offsetParent) return $(element.offsetParent);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2068
    if (element == document.body) return $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2069
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2070
    while ((element = element.parentNode) && element != document.body)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2071
      if (Element.getStyle(element, 'position') != 'static')
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2072
        return $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2073
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2074
    return $(document.body);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2075
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2076
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2077
  viewportOffset: function(forElement) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2078
    var valueT = 0, valueL = 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2079
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2080
    var element = forElement;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2081
    do {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2082
      valueT += element.offsetTop  || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2083
      valueL += element.offsetLeft || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2084
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2085
      // Safari fix
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2086
      if (element.offsetParent == document.body &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2087
        Element.getStyle(element, 'position') == 'absolute') break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2088
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2089
    } while (element = element.offsetParent);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2090
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2091
    element = forElement;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2092
    do {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2093
      if (!Prototype.Browser.Opera || element.tagName == 'BODY') {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2094
        valueT -= element.scrollTop  || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2095
        valueL -= element.scrollLeft || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2096
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2097
    } while (element = element.parentNode);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2098
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2099
    return Element._returnOffset(valueL, valueT);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2100
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2101
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2102
  clonePosition: function(element, source) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2103
    var options = Object.extend({
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2104
      setLeft:    true,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2105
      setTop:     true,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2106
      setWidth:   true,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2107
      setHeight:  true,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2108
      offsetTop:  0,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2109
      offsetLeft: 0
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2110
    }, arguments[2] || { });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2111
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2112
    // find page position of source
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2113
    source = $(source);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2114
    var p = source.viewportOffset();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2115
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2116
    // find coordinate system to use
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2117
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2118
    var delta = [0, 0];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2119
    var parent = null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2120
    // delta [0,0] will do fine with position: fixed elements,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2121
    // position:absolute needs offsetParent deltas
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2122
    if (Element.getStyle(element, 'position') == 'absolute') {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2123
      parent = element.getOffsetParent();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2124
      delta = parent.viewportOffset();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2125
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2126
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2127
    // correct by body offsets (fixes Safari)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2128
    if (parent == document.body) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2129
      delta[0] -= document.body.offsetLeft;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2130
      delta[1] -= document.body.offsetTop;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2131
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2132
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2133
    // set position
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2134
    if (options.setLeft)   element.style.left  = (p[0] - delta[0] + options.offsetLeft) + 'px';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2135
    if (options.setTop)    element.style.top   = (p[1] - delta[1] + options.offsetTop) + 'px';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2136
    if (options.setWidth)  element.style.width = source.offsetWidth + 'px';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2137
    if (options.setHeight) element.style.height = source.offsetHeight + 'px';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2138
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2139
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2140
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2141
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2142
Element.Methods.identify.counter = 1;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2143
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2144
Object.extend(Element.Methods, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2145
  getElementsBySelector: Element.Methods.select,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2146
  childElements: Element.Methods.immediateDescendants
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2147
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2148
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2149
Element._attributeTranslations = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2150
  write: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2151
    names: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2152
      className: 'class',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2153
      htmlFor:   'for'
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2154
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2155
    values: { }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2156
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2157
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2158
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2159
if (Prototype.Browser.Opera) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2160
  Element.Methods.getStyle = Element.Methods.getStyle.wrap(
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2161
    function(proceed, element, style) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2162
      switch (style) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2163
        case 'left': case 'top': case 'right': case 'bottom':
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2164
          if (proceed(element, 'position') === 'static') return null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2165
        case 'height': case 'width':
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2166
          // returns '0px' for hidden elements; we want it to return null
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2167
          if (!Element.visible(element)) return null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2168
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2169
          // returns the border-box dimensions rather than the content-box
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2170
          // dimensions, so we subtract padding and borders from the value
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2171
          var dim = parseInt(proceed(element, style), 10);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2172
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2173
          if (dim !== element['offset' + style.capitalize()])
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2174
            return dim + 'px';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2175
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2176
          var properties;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2177
          if (style === 'height') {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2178
            properties = ['border-top-width', 'padding-top',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2179
             'padding-bottom', 'border-bottom-width'];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2180
          }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2181
          else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2182
            properties = ['border-left-width', 'padding-left',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2183
             'padding-right', 'border-right-width'];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2184
          }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2185
          return properties.inject(dim, function(memo, property) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2186
            var val = proceed(element, property);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2187
            return val === null ? memo : memo - parseInt(val, 10);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2188
          }) + 'px';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2189
        default: return proceed(element, style);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2190
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2191
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2192
  );
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2193
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2194
  Element.Methods.readAttribute = Element.Methods.readAttribute.wrap(
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2195
    function(proceed, element, attribute) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2196
      if (attribute === 'title') return element.title;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2197
      return proceed(element, attribute);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2198
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2199
  );
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2200
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2201
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2202
else if (Prototype.Browser.IE) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2203
  $w('positionedOffset getOffsetParent viewportOffset').each(function(method) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2204
    Element.Methods[method] = Element.Methods[method].wrap(
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2205
      function(proceed, element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2206
        element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2207
        var position = element.getStyle('position');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2208
        if (position != 'static') return proceed(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2209
        element.setStyle({ position: 'relative' });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2210
        var value = proceed(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2211
        element.setStyle({ position: position });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2212
        return value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2213
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2214
    );
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2215
  });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2216
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2217
  Element.Methods.getStyle = function(element, style) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2218
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2219
    style = (style == 'float' || style == 'cssFloat') ? 'styleFloat' : style.camelize();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2220
    var value = element.style[style];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2221
    if (!value && element.currentStyle) value = element.currentStyle[style];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2222
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2223
    if (style == 'opacity') {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2224
      if (value = (element.getStyle('filter') || '').match(/alpha\(opacity=(.*)\)/))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2225
        if (value[1]) return parseFloat(value[1]) / 100;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2226
      return 1.0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2227
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2228
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2229
    if (value == 'auto') {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2230
      if ((style == 'width' || style == 'height') && (element.getStyle('display') != 'none'))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2231
        return element['offset' + style.capitalize()] + 'px';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2232
      return null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2233
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2234
    return value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2235
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2236
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2237
  Element.Methods.setOpacity = function(element, value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2238
    function stripAlpha(filter){
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2239
      return filter.replace(/alpha\([^\)]*\)/gi,'');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2240
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2241
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2242
    var currentStyle = element.currentStyle;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2243
    if ((currentStyle && !currentStyle.hasLayout) ||
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2244
      (!currentStyle && element.style.zoom == 'normal'))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2245
        element.style.zoom = 1;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2246
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2247
    var filter = element.getStyle('filter'), style = element.style;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2248
    if (value == 1 || value === '') {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2249
      (filter = stripAlpha(filter)) ?
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2250
        style.filter = filter : style.removeAttribute('filter');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2251
      return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2252
    } else if (value < 0.00001) value = 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2253
    style.filter = stripAlpha(filter) +
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2254
      'alpha(opacity=' + (value * 100) + ')';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2255
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2256
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2257
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2258
  Element._attributeTranslations = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2259
    read: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2260
      names: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2261
        'class': 'className',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2262
        'for':   'htmlFor'
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2263
      },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2264
      values: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2265
        _getAttr: function(element, attribute) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2266
          return element.getAttribute(attribute, 2);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2267
        },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2268
        _getAttrNode: function(element, attribute) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2269
          var node = element.getAttributeNode(attribute);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2270
          return node ? node.value : "";
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2271
        },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2272
        _getEv: function(element, attribute) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2273
          attribute = element.getAttribute(attribute);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2274
          return attribute ? attribute.toString().slice(23, -2) : null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2275
        },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2276
        _flag: function(element, attribute) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2277
          return $(element).hasAttribute(attribute) ? attribute : null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2278
        },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2279
        style: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2280
          return element.style.cssText.toLowerCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2281
        },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2282
        title: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2283
          return element.title;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2284
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2285
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2286
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2287
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2288
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2289
  Element._attributeTranslations.write = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2290
    names: Object.clone(Element._attributeTranslations.read.names),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2291
    values: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2292
      checked: function(element, value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2293
        element.checked = !!value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2294
      },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2295
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2296
      style: function(element, value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2297
        element.style.cssText = value ? value : '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2298
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2299
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2300
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2301
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2302
  Element._attributeTranslations.has = {};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2303
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2304
  $w('colSpan rowSpan vAlign dateTime accessKey tabIndex ' +
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2305
      'encType maxLength readOnly longDesc').each(function(attr) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2306
    Element._attributeTranslations.write.names[attr.toLowerCase()] = attr;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2307
    Element._attributeTranslations.has[attr.toLowerCase()] = attr;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2308
  });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2309
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2310
  (function(v) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2311
    Object.extend(v, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2312
      href:        v._getAttr,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2313
      src:         v._getAttr,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2314
      type:        v._getAttr,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2315
      action:      v._getAttrNode,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2316
      disabled:    v._flag,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2317
      checked:     v._flag,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2318
      readonly:    v._flag,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2319
      multiple:    v._flag,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2320
      onload:      v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2321
      onunload:    v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2322
      onclick:     v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2323
      ondblclick:  v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2324
      onmousedown: v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2325
      onmouseup:   v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2326
      onmouseover: v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2327
      onmousemove: v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2328
      onmouseout:  v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2329
      onfocus:     v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2330
      onblur:      v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2331
      onkeypress:  v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2332
      onkeydown:   v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2333
      onkeyup:     v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2334
      onsubmit:    v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2335
      onreset:     v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2336
      onselect:    v._getEv,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2337
      onchange:    v._getEv
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2338
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2339
  })(Element._attributeTranslations.read.values);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2340
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2341
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2342
else if (Prototype.Browser.Gecko && /rv:1\.8\.0/.test(navigator.userAgent)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2343
  Element.Methods.setOpacity = function(element, value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2344
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2345
    element.style.opacity = (value == 1) ? 0.999999 :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2346
      (value === '') ? '' : (value < 0.00001) ? 0 : value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2347
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2348
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2349
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2350
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2351
else if (Prototype.Browser.WebKit) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2352
  Element.Methods.setOpacity = function(element, value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2353
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2354
    element.style.opacity = (value == 1 || value === '') ? '' :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2355
      (value < 0.00001) ? 0 : value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2356
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2357
    if (value == 1)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2358
      if(element.tagName == 'IMG' && element.width) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2359
        element.width++; element.width--;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2360
      } else try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2361
        var n = document.createTextNode(' ');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2362
        element.appendChild(n);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2363
        element.removeChild(n);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2364
      } catch (e) { }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2365
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2366
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2367
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2368
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2369
  // Safari returns margins on body which is incorrect if the child is absolutely
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2370
  // positioned.  For performance reasons, redefine Element#cumulativeOffset for
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2371
  // KHTML/WebKit only.
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2372
  Element.Methods.cumulativeOffset = function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2373
    var valueT = 0, valueL = 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2374
    do {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2375
      valueT += element.offsetTop  || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2376
      valueL += element.offsetLeft || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2377
      if (element.offsetParent == document.body)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2378
        if (Element.getStyle(element, 'position') == 'absolute') break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2379
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2380
      element = element.offsetParent;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2381
    } while (element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2382
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2383
    return Element._returnOffset(valueL, valueT);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2384
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2385
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2386
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2387
if (Prototype.Browser.IE || Prototype.Browser.Opera) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2388
  // IE and Opera are missing .innerHTML support for TABLE-related and SELECT elements
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2389
  Element.Methods.update = function(element, content) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2390
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2391
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2392
    if (content && content.toElement) content = content.toElement();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2393
    if (Object.isElement(content)) return element.update().insert(content);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2394
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2395
    content = Object.toHTML(content);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2396
    var tagName = element.tagName.toUpperCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2397
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2398
    if (tagName in Element._insertionTranslations.tags) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2399
      $A(element.childNodes).each(function(node) { element.removeChild(node) });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2400
      Element._getContentFromAnonymousElement(tagName, content.stripScripts())
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2401
        .each(function(node) { element.appendChild(node) });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2402
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2403
    else element.innerHTML = content.stripScripts();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2404
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2405
    content.evalScripts.bind(content).defer();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2406
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2407
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2408
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2409
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2410
if (document.createElement('div').outerHTML) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2411
  Element.Methods.replace = function(element, content) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2412
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2413
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2414
    if (content && content.toElement) content = content.toElement();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2415
    if (Object.isElement(content)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2416
      element.parentNode.replaceChild(content, element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2417
      return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2418
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2419
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2420
    content = Object.toHTML(content);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2421
    var parent = element.parentNode, tagName = parent.tagName.toUpperCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2422
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2423
    if (Element._insertionTranslations.tags[tagName]) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2424
      var nextSibling = element.next();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2425
      var fragments = Element._getContentFromAnonymousElement(tagName, content.stripScripts());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2426
      parent.removeChild(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2427
      if (nextSibling)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2428
        fragments.each(function(node) { parent.insertBefore(node, nextSibling) });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2429
      else
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2430
        fragments.each(function(node) { parent.appendChild(node) });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2431
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2432
    else element.outerHTML = content.stripScripts();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2433
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2434
    content.evalScripts.bind(content).defer();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2435
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2436
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2437
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2438
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2439
Element._returnOffset = function(l, t) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2440
  var result = [l, t];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2441
  result.left = l;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2442
  result.top = t;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2443
  return result;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2444
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2445
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2446
Element._getContentFromAnonymousElement = function(tagName, html) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2447
  var div = new Element('div'), t = Element._insertionTranslations.tags[tagName];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2448
  if (t) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2449
    div.innerHTML = t[0] + html + t[1];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2450
    t[2].times(function() { div = div.firstChild });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2451
  } else div.innerHTML = html;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2452
  return $A(div.childNodes);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2453
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2454
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2455
Element._insertionTranslations = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2456
  before: function(element, node) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2457
    element.parentNode.insertBefore(node, element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2458
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2459
  top: function(element, node) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2460
    element.insertBefore(node, element.firstChild);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2461
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2462
  bottom: function(element, node) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2463
    element.appendChild(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2464
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2465
  after: function(element, node) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2466
    element.parentNode.insertBefore(node, element.nextSibling);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2467
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2468
  tags: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2469
    TABLE:  ['<table>',                '</table>',                   1],
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2470
    TBODY:  ['<table><tbody>',         '</tbody></table>',           2],
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2471
    TR:     ['<table><tbody><tr>',     '</tr></tbody></table>',      3],
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2472
    TD:     ['<table><tbody><tr><td>', '</td></tr></tbody></table>', 4],
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2473
    SELECT: ['<select>',               '</select>',                  1]
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2474
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2475
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2476
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2477
(function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2478
  Object.extend(this.tags, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2479
    THEAD: this.tags.TBODY,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2480
    TFOOT: this.tags.TBODY,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2481
    TH:    this.tags.TD
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2482
  });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2483
}).call(Element._insertionTranslations);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2484
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2485
Element.Methods.Simulated = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2486
  hasAttribute: function(element, attribute) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2487
    attribute = Element._attributeTranslations.has[attribute] || attribute;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2488
    var node = $(element).getAttributeNode(attribute);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2489
    return node && node.specified;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2490
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2491
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2492
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2493
Element.Methods.ByTag = { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2494
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2495
Object.extend(Element, Element.Methods);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2496
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2497
if (!Prototype.BrowserFeatures.ElementExtensions &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2498
    document.createElement('div').__proto__) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2499
  window.HTMLElement = { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2500
  window.HTMLElement.prototype = document.createElement('div').__proto__;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2501
  Prototype.BrowserFeatures.ElementExtensions = true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2502
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2503
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2504
Element.extend = (function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2505
  if (Prototype.BrowserFeatures.SpecificElementExtensions)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2506
    return Prototype.K;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2507
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2508
  var Methods = { }, ByTag = Element.Methods.ByTag;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2509
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2510
  var extend = Object.extend(function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2511
    if (!element || element._extendedByPrototype ||
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2512
        element.nodeType != 1 || element == window) return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2513
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2514
    var methods = Object.clone(Methods),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2515
      tagName = element.tagName, property, value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2516
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2517
    // extend methods for specific tags
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2518
    if (ByTag[tagName]) Object.extend(methods, ByTag[tagName]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2519
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2520
    for (property in methods) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2521
      value = methods[property];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2522
      if (Object.isFunction(value) && !(property in element))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2523
        element[property] = value.methodize();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2524
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2525
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2526
    element._extendedByPrototype = Prototype.emptyFunction;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2527
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2528
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2529
  }, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2530
    refresh: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2531
      // extend methods for all tags (Safari doesn't need this)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2532
      if (!Prototype.BrowserFeatures.ElementExtensions) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2533
        Object.extend(Methods, Element.Methods);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2534
        Object.extend(Methods, Element.Methods.Simulated);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2535
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2536
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2537
  });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2538
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2539
  extend.refresh();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2540
  return extend;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2541
})();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2542
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2543
Element.hasAttribute = function(element, attribute) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2544
  if (element.hasAttribute) return element.hasAttribute(attribute);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2545
  return Element.Methods.Simulated.hasAttribute(element, attribute);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2546
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2547
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2548
Element.addMethods = function(methods) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2549
  var F = Prototype.BrowserFeatures, T = Element.Methods.ByTag;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2550
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2551
  if (!methods) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2552
    Object.extend(Form, Form.Methods);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2553
    Object.extend(Form.Element, Form.Element.Methods);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2554
    Object.extend(Element.Methods.ByTag, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2555
      "FORM":     Object.clone(Form.Methods),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2556
      "INPUT":    Object.clone(Form.Element.Methods),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2557
      "SELECT":   Object.clone(Form.Element.Methods),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2558
      "TEXTAREA": Object.clone(Form.Element.Methods)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2559
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2560
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2561
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2562
  if (arguments.length == 2) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2563
    var tagName = methods;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2564
    methods = arguments[1];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2565
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2566
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2567
  if (!tagName) Object.extend(Element.Methods, methods || { });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2568
  else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2569
    if (Object.isArray(tagName)) tagName.each(extend);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2570
    else extend(tagName);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2571
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2572
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2573
  function extend(tagName) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2574
    tagName = tagName.toUpperCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2575
    if (!Element.Methods.ByTag[tagName])
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2576
      Element.Methods.ByTag[tagName] = { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2577
    Object.extend(Element.Methods.ByTag[tagName], methods);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2578
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2579
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2580
  function copy(methods, destination, onlyIfAbsent) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2581
    onlyIfAbsent = onlyIfAbsent || false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2582
    for (var property in methods) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2583
      var value = methods[property];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2584
      if (!Object.isFunction(value)) continue;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2585
      if (!onlyIfAbsent || !(property in destination))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2586
        destination[property] = value.methodize();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2587
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2588
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2589
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2590
  function findDOMClass(tagName) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2591
    var klass;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2592
    var trans = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2593
      "OPTGROUP": "OptGroup", "TEXTAREA": "TextArea", "P": "Paragraph",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2594
      "FIELDSET": "FieldSet", "UL": "UList", "OL": "OList", "DL": "DList",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2595
      "DIR": "Directory", "H1": "Heading", "H2": "Heading", "H3": "Heading",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2596
      "H4": "Heading", "H5": "Heading", "H6": "Heading", "Q": "Quote",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2597
      "INS": "Mod", "DEL": "Mod", "A": "Anchor", "IMG": "Image", "CAPTION":
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2598
      "TableCaption", "COL": "TableCol", "COLGROUP": "TableCol", "THEAD":
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2599
      "TableSection", "TFOOT": "TableSection", "TBODY": "TableSection", "TR":
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2600
      "TableRow", "TH": "TableCell", "TD": "TableCell", "FRAMESET":
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2601
      "FrameSet", "IFRAME": "IFrame"
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2602
    };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2603
    if (trans[tagName]) klass = 'HTML' + trans[tagName] + 'Element';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2604
    if (window[klass]) return window[klass];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2605
    klass = 'HTML' + tagName + 'Element';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2606
    if (window[klass]) return window[klass];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2607
    klass = 'HTML' + tagName.capitalize() + 'Element';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2608
    if (window[klass]) return window[klass];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2609
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2610
    window[klass] = { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2611
    window[klass].prototype = document.createElement(tagName).__proto__;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2612
    return window[klass];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2613
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2614
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2615
  if (F.ElementExtensions) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2616
    copy(Element.Methods, HTMLElement.prototype);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2617
    copy(Element.Methods.Simulated, HTMLElement.prototype, true);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2618
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2619
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2620
  if (F.SpecificElementExtensions) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2621
    for (var tag in Element.Methods.ByTag) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2622
      var klass = findDOMClass(tag);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2623
      if (Object.isUndefined(klass)) continue;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2624
      copy(T[tag], klass.prototype);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2625
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2626
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2627
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2628
  Object.extend(Element, Element.Methods);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2629
  delete Element.ByTag;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2630
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2631
  if (Element.extend.refresh) Element.extend.refresh();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2632
  Element.cache = { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2633
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2634
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2635
document.viewport = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2636
  getDimensions: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2637
    var dimensions = { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2638
    var B = Prototype.Browser;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2639
    $w('width height').each(function(d) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2640
      var D = d.capitalize();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2641
      dimensions[d] = (B.WebKit && !document.evaluate) ? self['inner' + D] :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2642
        (B.Opera) ? document.body['client' + D] : document.documentElement['client' + D];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2643
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2644
    return dimensions;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2645
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2646
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2647
  getWidth: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2648
    return this.getDimensions().width;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2649
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2650
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2651
  getHeight: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2652
    return this.getDimensions().height;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2653
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2654
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2655
  getScrollOffsets: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2656
    return Element._returnOffset(
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2657
      window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2658
      window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2659
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2660
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2661
/* Portions of the Selector class are derived from Jack Slocum’s DomQuery,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2662
 * part of YUI-Ext version 0.40, distributed under the terms of an MIT-style
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2663
 * license.  Please see http://www.yui-ext.com/ for more information. */
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2664
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2665
var Selector = Class.create({
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2666
  initialize: function(expression) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2667
    this.expression = expression.strip();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2668
    this.compileMatcher();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2669
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2670
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2671
  shouldUseXPath: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2672
    if (!Prototype.BrowserFeatures.XPath) return false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2673
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2674
    var e = this.expression;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2675
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2676
    // Safari 3 chokes on :*-of-type and :empty
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2677
    if (Prototype.Browser.WebKit &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2678
     (e.include("-of-type") || e.include(":empty")))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2679
      return false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2680
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2681
    // XPath can't do namespaced attributes, nor can it read
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2682
    // the "checked" property from DOM nodes
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2683
    if ((/(\[[\w-]*?:|:checked)/).test(this.expression))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2684
      return false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2685
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2686
    return true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2687
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2688
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2689
  compileMatcher: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2690
    if (this.shouldUseXPath())
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2691
      return this.compileXPathMatcher();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2692
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2693
    var e = this.expression, ps = Selector.patterns, h = Selector.handlers,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2694
        c = Selector.criteria, le, p, m;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2695
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2696
    if (Selector._cache[e]) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2697
      this.matcher = Selector._cache[e];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2698
      return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2699
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2700
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2701
    this.matcher = ["this.matcher = function(root) {",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2702
                    "var r = root, h = Selector.handlers, c = false, n;"];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2703
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2704
    while (e && le != e && (/\S/).test(e)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2705
      le = e;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2706
      for (var i in ps) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2707
        p = ps[i];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2708
        if (m = e.match(p)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2709
          this.matcher.push(Object.isFunction(c[i]) ? c[i](m) :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2710
    	      new Template(c[i]).evaluate(m));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2711
          e = e.replace(m[0], '');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2712
          break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2713
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2714
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2715
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2716
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2717
    this.matcher.push("return h.unique(n);\n}");
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2718
    eval(this.matcher.join('\n'));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2719
    Selector._cache[this.expression] = this.matcher;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2720
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2721
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2722
  compileXPathMatcher: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2723
    var e = this.expression, ps = Selector.patterns,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2724
        x = Selector.xpath, le, m;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2725
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2726
    if (Selector._cache[e]) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2727
      this.xpath = Selector._cache[e]; return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2728
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2729
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2730
    this.matcher = ['.//*'];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2731
    while (e && le != e && (/\S/).test(e)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2732
      le = e;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2733
      for (var i in ps) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2734
        if (m = e.match(ps[i])) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2735
          this.matcher.push(Object.isFunction(x[i]) ? x[i](m) :
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2736
            new Template(x[i]).evaluate(m));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2737
          e = e.replace(m[0], '');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2738
          break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2739
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2740
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2741
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2742
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2743
    this.xpath = this.matcher.join('');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2744
    Selector._cache[this.expression] = this.xpath;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2745
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2746
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2747
  findElements: function(root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2748
    root = root || document;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2749
    if (this.xpath) return document._getElementsByXPath(this.xpath, root);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2750
    return this.matcher(root);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2751
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2752
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2753
  match: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2754
    this.tokens = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2755
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2756
    var e = this.expression, ps = Selector.patterns, as = Selector.assertions;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2757
    var le, p, m;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2758
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2759
    while (e && le !== e && (/\S/).test(e)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2760
      le = e;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2761
      for (var i in ps) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2762
        p = ps[i];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2763
        if (m = e.match(p)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2764
          // use the Selector.assertions methods unless the selector
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2765
          // is too complex.
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2766
          if (as[i]) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2767
            this.tokens.push([i, Object.clone(m)]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2768
            e = e.replace(m[0], '');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2769
          } else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2770
            // reluctantly do a document-wide search
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2771
            // and look for a match in the array
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2772
            return this.findElements(document).include(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2773
          }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2774
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2775
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2776
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2777
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2778
    var match = true, name, matches;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2779
    for (var i = 0, token; token = this.tokens[i]; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2780
      name = token[0], matches = token[1];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2781
      if (!Selector.assertions[name](element, matches)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2782
        match = false; break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2783
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2784
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2785
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2786
    return match;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2787
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2788
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2789
  toString: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2790
    return this.expression;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2791
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2792
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2793
  inspect: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2794
    return "#<Selector:" + this.expression.inspect() + ">";
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2795
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2796
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2797
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2798
Object.extend(Selector, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2799
  _cache: { },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2800
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2801
  xpath: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2802
    descendant:   "//*",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2803
    child:        "/*",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2804
    adjacent:     "/following-sibling::*[1]",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2805
    laterSibling: '/following-sibling::*',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2806
    tagName:      function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2807
      if (m[1] == '*') return '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2808
      return "[local-name()='" + m[1].toLowerCase() +
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2809
             "' or local-name()='" + m[1].toUpperCase() + "']";
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2810
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2811
    className:    "[contains(concat(' ', @class, ' '), ' #{1} ')]",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2812
    id:           "[@id='#{1}']",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2813
    attrPresence: function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2814
      m[1] = m[1].toLowerCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2815
      return new Template("[@#{1}]").evaluate(m);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2816
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2817
    attr: function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2818
      m[1] = m[1].toLowerCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2819
      m[3] = m[5] || m[6];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2820
      return new Template(Selector.xpath.operators[m[2]]).evaluate(m);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2821
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2822
    pseudo: function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2823
      var h = Selector.xpath.pseudos[m[1]];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2824
      if (!h) return '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2825
      if (Object.isFunction(h)) return h(m);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2826
      return new Template(Selector.xpath.pseudos[m[1]]).evaluate(m);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2827
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2828
    operators: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2829
      '=':  "[@#{1}='#{3}']",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2830
      '!=': "[@#{1}!='#{3}']",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2831
      '^=': "[starts-with(@#{1}, '#{3}')]",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2832
      '$=': "[substring(@#{1}, (string-length(@#{1}) - string-length('#{3}') + 1))='#{3}']",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2833
      '*=': "[contains(@#{1}, '#{3}')]",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2834
      '~=': "[contains(concat(' ', @#{1}, ' '), ' #{3} ')]",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2835
      '|=': "[contains(concat('-', @#{1}, '-'), '-#{3}-')]"
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2836
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2837
    pseudos: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2838
      'first-child': '[not(preceding-sibling::*)]',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2839
      'last-child':  '[not(following-sibling::*)]',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2840
      'only-child':  '[not(preceding-sibling::* or following-sibling::*)]',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2841
      'empty':       "[count(*) = 0 and (count(text()) = 0 or translate(text(), ' \t\r\n', '') = '')]",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2842
      'checked':     "[@checked]",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2843
      'disabled':    "[@disabled]",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2844
      'enabled':     "[not(@disabled)]",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2845
      'not': function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2846
        var e = m[6], p = Selector.patterns,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2847
            x = Selector.xpath, le, v;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2848
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2849
        var exclusion = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2850
        while (e && le != e && (/\S/).test(e)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2851
          le = e;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2852
          for (var i in p) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2853
            if (m = e.match(p[i])) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2854
              v = Object.isFunction(x[i]) ? x[i](m) : new Template(x[i]).evaluate(m);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2855
              exclusion.push("(" + v.substring(1, v.length - 1) + ")");
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2856
              e = e.replace(m[0], '');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2857
              break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2858
            }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2859
          }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2860
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2861
        return "[not(" + exclusion.join(" and ") + ")]";
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2862
      },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2863
      'nth-child':      function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2864
        return Selector.xpath.pseudos.nth("(count(./preceding-sibling::*) + 1) ", m);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2865
      },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2866
      'nth-last-child': function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2867
        return Selector.xpath.pseudos.nth("(count(./following-sibling::*) + 1) ", m);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2868
      },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2869
      'nth-of-type':    function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2870
        return Selector.xpath.pseudos.nth("position() ", m);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2871
      },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2872
      'nth-last-of-type': function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2873
        return Selector.xpath.pseudos.nth("(last() + 1 - position()) ", m);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2874
      },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2875
      'first-of-type':  function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2876
        m[6] = "1"; return Selector.xpath.pseudos['nth-of-type'](m);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2877
      },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2878
      'last-of-type':   function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2879
        m[6] = "1"; return Selector.xpath.pseudos['nth-last-of-type'](m);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2880
      },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2881
      'only-of-type':   function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2882
        var p = Selector.xpath.pseudos; return p['first-of-type'](m) + p['last-of-type'](m);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2883
      },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2884
      nth: function(fragment, m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2885
        var mm, formula = m[6], predicate;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2886
        if (formula == 'even') formula = '2n+0';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2887
        if (formula == 'odd')  formula = '2n+1';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2888
        if (mm = formula.match(/^(\d+)$/)) // digit only
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2889
          return '[' + fragment + "= " + mm[1] + ']';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2890
        if (mm = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2891
          if (mm[1] == "-") mm[1] = -1;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2892
          var a = mm[1] ? Number(mm[1]) : 1;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2893
          var b = mm[2] ? Number(mm[2]) : 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2894
          predicate = "[((#{fragment} - #{b}) mod #{a} = 0) and " +
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2895
          "((#{fragment} - #{b}) div #{a} >= 0)]";
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2896
          return new Template(predicate).evaluate({
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2897
            fragment: fragment, a: a, b: b });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2898
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2899
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2900
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2901
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2902
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2903
  criteria: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2904
    tagName:      'n = h.tagName(n, r, "#{1}", c);   c = false;',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2905
    className:    'n = h.className(n, r, "#{1}", c); c = false;',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2906
    id:           'n = h.id(n, r, "#{1}", c);        c = false;',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2907
    attrPresence: 'n = h.attrPresence(n, r, "#{1}"); c = false;',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2908
    attr: function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2909
      m[3] = (m[5] || m[6]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2910
      return new Template('n = h.attr(n, r, "#{1}", "#{3}", "#{2}"); c = false;').evaluate(m);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2911
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2912
    pseudo: function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2913
      if (m[6]) m[6] = m[6].replace(/"/g, '\\"');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2914
      return new Template('n = h.pseudo(n, "#{1}", "#{6}", r, c); c = false;').evaluate(m);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2915
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2916
    descendant:   'c = "descendant";',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2917
    child:        'c = "child";',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2918
    adjacent:     'c = "adjacent";',
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2919
    laterSibling: 'c = "laterSibling";'
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2920
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2921
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2922
  patterns: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2923
    // combinators must be listed first
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2924
    // (and descendant needs to be last combinator)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2925
    laterSibling: /^\s*~\s*/,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2926
    child:        /^\s*>\s*/,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2927
    adjacent:     /^\s*\+\s*/,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2928
    descendant:   /^\s/,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2929
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2930
    // selectors follow
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2931
    tagName:      /^\s*(\*|[\w\-]+)(\b|$)?/,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2932
    id:           /^#([\w\-\*]+)(\b|$)/,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2933
    className:    /^\.([\w\-\*]+)(\b|$)/,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2934
    pseudo:
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2935
/^:((first|last|nth|nth-last|only)(-child|-of-type)|empty|checked|(en|dis)abled|not)(\((.*?)\))?(\b|$|(?=\s|[:+~>]))/,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2936
    attrPresence: /^\[([\w]+)\]/,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2937
    attr:         /\[((?:[\w-]*:)?[\w-]+)\s*(?:([!^$*~|]?=)\s*((['"])([^\4]*?)\4|([^'"][^\]]*?)))?\]/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2938
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2939
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2940
  // for Selector.match and Element#match
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2941
  assertions: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2942
    tagName: function(element, matches) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2943
      return matches[1].toUpperCase() == element.tagName.toUpperCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2944
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2945
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2946
    className: function(element, matches) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2947
      return Element.hasClassName(element, matches[1]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2948
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2949
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2950
    id: function(element, matches) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2951
      return element.id === matches[1];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2952
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2953
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2954
    attrPresence: function(element, matches) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2955
      return Element.hasAttribute(element, matches[1]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2956
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2957
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2958
    attr: function(element, matches) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2959
      var nodeValue = Element.readAttribute(element, matches[1]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2960
      return Selector.operators[matches[2]](nodeValue, matches[3]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2961
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2962
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2963
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2964
  handlers: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2965
    // UTILITY FUNCTIONS
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2966
    // joins two collections
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2967
    concat: function(a, b) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2968
      for (var i = 0, node; node = b[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2969
        a.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2970
      return a;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2971
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2972
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2973
    // marks an array of nodes for counting
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2974
    mark: function(nodes) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2975
      for (var i = 0, node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2976
        node._counted = true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2977
      return nodes;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2978
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2979
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2980
    unmark: function(nodes) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2981
      for (var i = 0, node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2982
        node._counted = undefined;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2983
      return nodes;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2984
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2985
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2986
    // mark each child node with its position (for nth calls)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2987
    // "ofType" flag indicates whether we're indexing for nth-of-type
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2988
    // rather than nth-child
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2989
    index: function(parentNode, reverse, ofType) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2990
      parentNode._counted = true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2991
      if (reverse) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2992
        for (var nodes = parentNode.childNodes, i = nodes.length - 1, j = 1; i >= 0; i--) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2993
          var node = nodes[i];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2994
          if (node.nodeType == 1 && (!ofType || node._counted)) node.nodeIndex = j++;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2995
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2996
      } else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2997
        for (var i = 0, j = 1, nodes = parentNode.childNodes; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2998
          if (node.nodeType == 1 && (!ofType || node._counted)) node.nodeIndex = j++;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  2999
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3000
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3001
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3002
    // filters out duplicates and extends all nodes
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3003
    unique: function(nodes) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3004
      if (nodes.length == 0) return nodes;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3005
      var results = [], n;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3006
      for (var i = 0, l = nodes.length; i < l; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3007
        if (!(n = nodes[i])._counted) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3008
          n._counted = true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3009
          results.push(Element.extend(n));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3010
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3011
      return Selector.handlers.unmark(results);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3012
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3013
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3014
    // COMBINATOR FUNCTIONS
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3015
    descendant: function(nodes) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3016
      var h = Selector.handlers;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3017
      for (var i = 0, results = [], node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3018
        h.concat(results, node.getElementsByTagName('*'));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3019
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3020
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3021
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3022
    child: function(nodes) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3023
      var h = Selector.handlers;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3024
      for (var i = 0, results = [], node; node = nodes[i]; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3025
        for (var j = 0, child; child = node.childNodes[j]; j++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3026
          if (child.nodeType == 1 && child.tagName != '!') results.push(child);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3027
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3028
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3029
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3030
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3031
    adjacent: function(nodes) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3032
      for (var i = 0, results = [], node; node = nodes[i]; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3033
        var next = this.nextElementSibling(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3034
        if (next) results.push(next);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3035
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3036
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3037
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3038
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3039
    laterSibling: function(nodes) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3040
      var h = Selector.handlers;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3041
      for (var i = 0, results = [], node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3042
        h.concat(results, Element.nextSiblings(node));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3043
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3044
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3045
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3046
    nextElementSibling: function(node) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3047
      while (node = node.nextSibling)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3048
	      if (node.nodeType == 1) return node;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3049
      return null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3050
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3051
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3052
    previousElementSibling: function(node) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3053
      while (node = node.previousSibling)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3054
        if (node.nodeType == 1) return node;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3055
      return null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3056
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3057
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3058
    // TOKEN FUNCTIONS
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3059
    tagName: function(nodes, root, tagName, combinator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3060
      var uTagName = tagName.toUpperCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3061
      var results = [], h = Selector.handlers;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3062
      if (nodes) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3063
        if (combinator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3064
          // fastlane for ordinary descendant combinators
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3065
          if (combinator == "descendant") {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3066
            for (var i = 0, node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3067
              h.concat(results, node.getElementsByTagName(tagName));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3068
            return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3069
          } else nodes = this[combinator](nodes);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3070
          if (tagName == "*") return nodes;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3071
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3072
        for (var i = 0, node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3073
          if (node.tagName.toUpperCase() === uTagName) results.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3074
        return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3075
      } else return root.getElementsByTagName(tagName);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3076
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3077
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3078
    id: function(nodes, root, id, combinator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3079
      var targetNode = $(id), h = Selector.handlers;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3080
      if (!targetNode) return [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3081
      if (!nodes && root == document) return [targetNode];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3082
      if (nodes) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3083
        if (combinator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3084
          if (combinator == 'child') {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3085
            for (var i = 0, node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3086
              if (targetNode.parentNode == node) return [targetNode];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3087
          } else if (combinator == 'descendant') {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3088
            for (var i = 0, node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3089
              if (Element.descendantOf(targetNode, node)) return [targetNode];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3090
          } else if (combinator == 'adjacent') {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3091
            for (var i = 0, node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3092
              if (Selector.handlers.previousElementSibling(targetNode) == node)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3093
                return [targetNode];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3094
          } else nodes = h[combinator](nodes);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3095
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3096
        for (var i = 0, node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3097
          if (node == targetNode) return [targetNode];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3098
        return [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3099
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3100
      return (targetNode && Element.descendantOf(targetNode, root)) ? [targetNode] : [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3101
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3102
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3103
    className: function(nodes, root, className, combinator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3104
      if (nodes && combinator) nodes = this[combinator](nodes);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3105
      return Selector.handlers.byClassName(nodes, root, className);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3106
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3107
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3108
    byClassName: function(nodes, root, className) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3109
      if (!nodes) nodes = Selector.handlers.descendant([root]);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3110
      var needle = ' ' + className + ' ';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3111
      for (var i = 0, results = [], node, nodeClassName; node = nodes[i]; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3112
        nodeClassName = node.className;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3113
        if (nodeClassName.length == 0) continue;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3114
        if (nodeClassName == className || (' ' + nodeClassName + ' ').include(needle))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3115
          results.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3116
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3117
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3118
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3119
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3120
    attrPresence: function(nodes, root, attr) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3121
      if (!nodes) nodes = root.getElementsByTagName("*");
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3122
      var results = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3123
      for (var i = 0, node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3124
        if (Element.hasAttribute(node, attr)) results.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3125
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3126
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3127
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3128
    attr: function(nodes, root, attr, value, operator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3129
      if (!nodes) nodes = root.getElementsByTagName("*");
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3130
      var handler = Selector.operators[operator], results = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3131
      for (var i = 0, node; node = nodes[i]; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3132
        var nodeValue = Element.readAttribute(node, attr);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3133
        if (nodeValue === null) continue;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3134
        if (handler(nodeValue, value)) results.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3135
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3136
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3137
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3138
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3139
    pseudo: function(nodes, name, value, root, combinator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3140
      if (nodes && combinator) nodes = this[combinator](nodes);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3141
      if (!nodes) nodes = root.getElementsByTagName("*");
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3142
      return Selector.pseudos[name](nodes, value, root);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3143
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3144
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3145
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3146
  pseudos: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3147
    'first-child': function(nodes, value, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3148
      for (var i = 0, results = [], node; node = nodes[i]; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3149
        if (Selector.handlers.previousElementSibling(node)) continue;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3150
          results.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3151
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3152
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3153
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3154
    'last-child': function(nodes, value, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3155
      for (var i = 0, results = [], node; node = nodes[i]; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3156
        if (Selector.handlers.nextElementSibling(node)) continue;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3157
          results.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3158
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3159
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3160
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3161
    'only-child': function(nodes, value, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3162
      var h = Selector.handlers;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3163
      for (var i = 0, results = [], node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3164
        if (!h.previousElementSibling(node) && !h.nextElementSibling(node))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3165
          results.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3166
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3167
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3168
    'nth-child':        function(nodes, formula, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3169
      return Selector.pseudos.nth(nodes, formula, root);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3170
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3171
    'nth-last-child':   function(nodes, formula, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3172
      return Selector.pseudos.nth(nodes, formula, root, true);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3173
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3174
    'nth-of-type':      function(nodes, formula, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3175
      return Selector.pseudos.nth(nodes, formula, root, false, true);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3176
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3177
    'nth-last-of-type': function(nodes, formula, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3178
      return Selector.pseudos.nth(nodes, formula, root, true, true);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3179
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3180
    'first-of-type':    function(nodes, formula, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3181
      return Selector.pseudos.nth(nodes, "1", root, false, true);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3182
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3183
    'last-of-type':     function(nodes, formula, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3184
      return Selector.pseudos.nth(nodes, "1", root, true, true);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3185
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3186
    'only-of-type':     function(nodes, formula, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3187
      var p = Selector.pseudos;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3188
      return p['last-of-type'](p['first-of-type'](nodes, formula, root), formula, root);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3189
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3190
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3191
    // handles the an+b logic
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3192
    getIndices: function(a, b, total) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3193
      if (a == 0) return b > 0 ? [b] : [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3194
      return $R(1, total).inject([], function(memo, i) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3195
        if (0 == (i - b) % a && (i - b) / a >= 0) memo.push(i);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3196
        return memo;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3197
      });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3198
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3199
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3200
    // handles nth(-last)-child, nth(-last)-of-type, and (first|last)-of-type
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3201
    nth: function(nodes, formula, root, reverse, ofType) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3202
      if (nodes.length == 0) return [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3203
      if (formula == 'even') formula = '2n+0';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3204
      if (formula == 'odd')  formula = '2n+1';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3205
      var h = Selector.handlers, results = [], indexed = [], m;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3206
      h.mark(nodes);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3207
      for (var i = 0, node; node = nodes[i]; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3208
        if (!node.parentNode._counted) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3209
          h.index(node.parentNode, reverse, ofType);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3210
          indexed.push(node.parentNode);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3211
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3212
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3213
      if (formula.match(/^\d+$/)) { // just a number
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3214
        formula = Number(formula);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3215
        for (var i = 0, node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3216
          if (node.nodeIndex == formula) results.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3217
      } else if (m = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3218
        if (m[1] == "-") m[1] = -1;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3219
        var a = m[1] ? Number(m[1]) : 1;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3220
        var b = m[2] ? Number(m[2]) : 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3221
        var indices = Selector.pseudos.getIndices(a, b, nodes.length);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3222
        for (var i = 0, node, l = indices.length; node = nodes[i]; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3223
          for (var j = 0; j < l; j++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3224
            if (node.nodeIndex == indices[j]) results.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3225
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3226
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3227
      h.unmark(nodes);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3228
      h.unmark(indexed);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3229
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3230
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3231
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3232
    'empty': function(nodes, value, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3233
      for (var i = 0, results = [], node; node = nodes[i]; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3234
        // IE treats comments as element nodes
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3235
        if (node.tagName == '!' || (node.firstChild && !node.innerHTML.match(/^\s*$/))) continue;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3236
        results.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3237
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3238
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3239
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3240
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3241
    'not': function(nodes, selector, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3242
      var h = Selector.handlers, selectorType, m;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3243
      var exclusions = new Selector(selector).findElements(root);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3244
      h.mark(exclusions);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3245
      for (var i = 0, results = [], node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3246
        if (!node._counted) results.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3247
      h.unmark(exclusions);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3248
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3249
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3250
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3251
    'enabled': function(nodes, value, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3252
      for (var i = 0, results = [], node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3253
        if (!node.disabled) results.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3254
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3255
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3256
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3257
    'disabled': function(nodes, value, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3258
      for (var i = 0, results = [], node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3259
        if (node.disabled) results.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3260
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3261
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3262
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3263
    'checked': function(nodes, value, root) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3264
      for (var i = 0, results = [], node; node = nodes[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3265
        if (node.checked) results.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3266
      return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3267
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3268
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3269
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3270
  operators: {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3271
    '=':  function(nv, v) { return nv == v; },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3272
    '!=': function(nv, v) { return nv != v; },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3273
    '^=': function(nv, v) { return nv.startsWith(v); },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3274
    '$=': function(nv, v) { return nv.endsWith(v); },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3275
    '*=': function(nv, v) { return nv.include(v); },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3276
    '~=': function(nv, v) { return (' ' + nv + ' ').include(' ' + v + ' '); },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3277
    '|=': function(nv, v) { return ('-' + nv.toUpperCase() + '-').include('-' + v.toUpperCase() + '-'); }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3278
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3279
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3280
  matchElements: function(elements, expression) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3281
    var matches = new Selector(expression).findElements(), h = Selector.handlers;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3282
    h.mark(matches);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3283
    for (var i = 0, results = [], element; element = elements[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3284
      if (element._counted) results.push(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3285
    h.unmark(matches);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3286
    return results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3287
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3288
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3289
  findElement: function(elements, expression, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3290
    if (Object.isNumber(expression)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3291
      index = expression; expression = false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3292
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3293
    return Selector.matchElements(elements, expression || '*')[index || 0];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3294
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3295
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3296
  findChildElements: function(element, expressions) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3297
    var exprs = expressions.join(',');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3298
    expressions = [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3299
    exprs.scan(/(([\w#:.~>+()\s-]+|\*|\[.*?\])+)\s*(,|$)/, function(m) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3300
      expressions.push(m[1].strip());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3301
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3302
    var results = [], h = Selector.handlers;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3303
    for (var i = 0, l = expressions.length, selector; i < l; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3304
      selector = new Selector(expressions[i].strip());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3305
      h.concat(results, selector.findElements(element));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3306
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3307
    return (l > 1) ? h.unique(results) : results;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3308
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3309
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3310
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3311
if (Prototype.Browser.IE) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3312
  // IE returns comment nodes on getElementsByTagName("*").
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3313
  // Filter them out.
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3314
  Selector.handlers.concat = function(a, b) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3315
    for (var i = 0, node; node = b[i]; i++)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3316
      if (node.tagName !== "!") a.push(node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3317
    return a;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3318
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3319
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3320
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3321
function $$() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3322
  return Selector.findChildElements(document, $A(arguments));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3323
}
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3324
var Form = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3325
  reset: function(form) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3326
    $(form).reset();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3327
    return form;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3328
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3329
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3330
  serializeElements: function(elements, options) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3331
    if (typeof options != 'object') options = { hash: !!options };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3332
    else if (Object.isUndefined(options.hash)) options.hash = true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3333
    var key, value, submitted = false, submit = options.submit;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3334
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3335
    var data = elements.inject({ }, function(result, element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3336
      if (!element.disabled && element.name) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3337
        key = element.name; value = $(element).getValue();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3338
        if (value != null && (element.type != 'submit' || (!submitted &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3339
            submit !== false && (!submit || key == submit) && (submitted = true)))) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3340
          if (key in result) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3341
            // a key is already present; construct an array of values
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3342
            if (!Object.isArray(result[key])) result[key] = [result[key]];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3343
            result[key].push(value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3344
          }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3345
          else result[key] = value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3346
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3347
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3348
      return result;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3349
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3350
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3351
    return options.hash ? data : Object.toQueryString(data);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3352
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3353
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3354
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3355
Form.Methods = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3356
  serialize: function(form, options) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3357
    return Form.serializeElements(Form.getElements(form), options);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3358
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3359
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3360
  getElements: function(form) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3361
    return $A($(form).getElementsByTagName('*')).inject([],
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3362
      function(elements, child) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3363
        if (Form.Element.Serializers[child.tagName.toLowerCase()])
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3364
          elements.push(Element.extend(child));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3365
        return elements;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3366
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3367
    );
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3368
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3369
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3370
  getInputs: function(form, typeName, name) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3371
    form = $(form);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3372
    var inputs = form.getElementsByTagName('input');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3373
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3374
    if (!typeName && !name) return $A(inputs).map(Element.extend);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3375
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3376
    for (var i = 0, matchingInputs = [], length = inputs.length; i < length; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3377
      var input = inputs[i];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3378
      if ((typeName && input.type != typeName) || (name && input.name != name))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3379
        continue;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3380
      matchingInputs.push(Element.extend(input));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3381
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3382
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3383
    return matchingInputs;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3384
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3385
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3386
  disable: function(form) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3387
    form = $(form);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3388
    Form.getElements(form).invoke('disable');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3389
    return form;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3390
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3391
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3392
  enable: function(form) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3393
    form = $(form);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3394
    Form.getElements(form).invoke('enable');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3395
    return form;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3396
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3397
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3398
  findFirstElement: function(form) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3399
    var elements = $(form).getElements().findAll(function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3400
      return 'hidden' != element.type && !element.disabled;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3401
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3402
    var firstByIndex = elements.findAll(function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3403
      return element.hasAttribute('tabIndex') && element.tabIndex >= 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3404
    }).sortBy(function(element) { return element.tabIndex }).first();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3405
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3406
    return firstByIndex ? firstByIndex : elements.find(function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3407
      return ['input', 'select', 'textarea'].include(element.tagName.toLowerCase());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3408
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3409
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3410
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3411
  focusFirstElement: function(form) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3412
    form = $(form);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3413
    form.findFirstElement().activate();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3414
    return form;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3415
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3416
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3417
  request: function(form, options) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3418
    form = $(form), options = Object.clone(options || { });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3419
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3420
    var params = options.parameters, action = form.readAttribute('action') || '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3421
    if (action.blank()) action = window.location.href;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3422
    options.parameters = form.serialize(true);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3423
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3424
    if (params) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3425
      if (Object.isString(params)) params = params.toQueryParams();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3426
      Object.extend(options.parameters, params);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3427
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3428
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3429
    if (form.hasAttribute('method') && !options.method)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3430
      options.method = form.method;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3431
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3432
    return new Ajax.Request(action, options);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3433
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3434
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3435
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3436
/*--------------------------------------------------------------------------*/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3437
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3438
Form.Element = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3439
  focus: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3440
    $(element).focus();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3441
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3442
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3443
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3444
  select: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3445
    $(element).select();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3446
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3447
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3448
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3449
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3450
Form.Element.Methods = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3451
  serialize: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3452
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3453
    if (!element.disabled && element.name) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3454
      var value = element.getValue();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3455
      if (value != undefined) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3456
        var pair = { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3457
        pair[element.name] = value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3458
        return Object.toQueryString(pair);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3459
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3460
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3461
    return '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3462
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3463
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3464
  getValue: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3465
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3466
    var method = element.tagName.toLowerCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3467
    return Form.Element.Serializers[method](element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3468
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3469
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3470
  setValue: function(element, value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3471
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3472
    var method = element.tagName.toLowerCase();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3473
    Form.Element.Serializers[method](element, value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3474
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3475
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3476
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3477
  clear: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3478
    $(element).value = '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3479
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3480
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3481
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3482
  present: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3483
    return $(element).value != '';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3484
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3485
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3486
  activate: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3487
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3488
    try {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3489
      element.focus();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3490
      if (element.select && (element.tagName.toLowerCase() != 'input' ||
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3491
          !['button', 'reset', 'submit'].include(element.type)))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3492
        element.select();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3493
    } catch (e) { }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3494
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3495
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3496
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3497
  disable: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3498
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3499
    element.blur();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3500
    element.disabled = true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3501
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3502
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3503
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3504
  enable: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3505
    element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3506
    element.disabled = false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3507
    return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3508
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3509
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3510
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3511
/*--------------------------------------------------------------------------*/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3512
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3513
var Field = Form.Element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3514
var $F = Form.Element.Methods.getValue;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3515
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3516
/*--------------------------------------------------------------------------*/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3517
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3518
Form.Element.Serializers = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3519
  input: function(element, value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3520
    switch (element.type.toLowerCase()) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3521
      case 'checkbox':
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3522
      case 'radio':
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3523
        return Form.Element.Serializers.inputSelector(element, value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3524
      default:
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3525
        return Form.Element.Serializers.textarea(element, value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3526
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3527
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3528
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3529
  inputSelector: function(element, value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3530
    if (Object.isUndefined(value)) return element.checked ? element.value : null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3531
    else element.checked = !!value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3532
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3533
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3534
  textarea: function(element, value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3535
    if (Object.isUndefined(value)) return element.value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3536
    else element.value = value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3537
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3538
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3539
  select: function(element, index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3540
    if (Object.isUndefined(index))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3541
      return this[element.type == 'select-one' ?
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3542
        'selectOne' : 'selectMany'](element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3543
    else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3544
      var opt, value, single = !Object.isArray(index);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3545
      for (var i = 0, length = element.length; i < length; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3546
        opt = element.options[i];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3547
        value = this.optionValue(opt);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3548
        if (single) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3549
          if (value == index) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3550
            opt.selected = true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3551
            return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3552
          }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3553
        }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3554
        else opt.selected = index.include(value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3555
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3556
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3557
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3558
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3559
  selectOne: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3560
    var index = element.selectedIndex;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3561
    return index >= 0 ? this.optionValue(element.options[index]) : null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3562
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3563
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3564
  selectMany: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3565
    var values, length = element.length;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3566
    if (!length) return null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3567
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3568
    for (var i = 0, values = []; i < length; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3569
      var opt = element.options[i];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3570
      if (opt.selected) values.push(this.optionValue(opt));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3571
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3572
    return values;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3573
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3574
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3575
  optionValue: function(opt) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3576
    // extend element because hasAttribute may not be native
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3577
    return Element.extend(opt).hasAttribute('value') ? opt.value : opt.text;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3578
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3579
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3580
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3581
/*--------------------------------------------------------------------------*/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3582
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3583
Abstract.TimedObserver = Class.create(PeriodicalExecuter, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3584
  initialize: function($super, element, frequency, callback) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3585
    $super(callback, frequency);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3586
    this.element   = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3587
    this.lastValue = this.getValue();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3588
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3589
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3590
  execute: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3591
    var value = this.getValue();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3592
    if (Object.isString(this.lastValue) && Object.isString(value) ?
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3593
        this.lastValue != value : String(this.lastValue) != String(value)) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3594
      this.callback(this.element, value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3595
      this.lastValue = value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3596
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3597
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3598
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3599
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3600
Form.Element.Observer = Class.create(Abstract.TimedObserver, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3601
  getValue: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3602
    return Form.Element.getValue(this.element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3603
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3604
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3605
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3606
Form.Observer = Class.create(Abstract.TimedObserver, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3607
  getValue: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3608
    return Form.serialize(this.element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3609
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3610
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3611
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3612
/*--------------------------------------------------------------------------*/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3613
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3614
Abstract.EventObserver = Class.create({
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3615
  initialize: function(element, callback) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3616
    this.element  = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3617
    this.callback = callback;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3618
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3619
    this.lastValue = this.getValue();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3620
    if (this.element.tagName.toLowerCase() == 'form')
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3621
      this.registerFormCallbacks();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3622
    else
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3623
      this.registerCallback(this.element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3624
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3625
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3626
  onElementEvent: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3627
    var value = this.getValue();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3628
    if (this.lastValue != value) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3629
      this.callback(this.element, value);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3630
      this.lastValue = value;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3631
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3632
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3633
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3634
  registerFormCallbacks: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3635
    Form.getElements(this.element).each(this.registerCallback, this);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3636
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3637
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3638
  registerCallback: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3639
    if (element.type) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3640
      switch (element.type.toLowerCase()) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3641
        case 'checkbox':
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3642
        case 'radio':
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3643
          Event.observe(element, 'click', this.onElementEvent.bind(this));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3644
          break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3645
        default:
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3646
          Event.observe(element, 'change', this.onElementEvent.bind(this));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3647
          break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3648
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3649
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3650
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3651
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3652
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3653
Form.Element.EventObserver = Class.create(Abstract.EventObserver, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3654
  getValue: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3655
    return Form.Element.getValue(this.element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3656
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3657
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3658
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3659
Form.EventObserver = Class.create(Abstract.EventObserver, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3660
  getValue: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3661
    return Form.serialize(this.element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3662
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3663
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3664
if (!window.Event) var Event = { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3665
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3666
Object.extend(Event, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3667
  KEY_BACKSPACE: 8,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3668
  KEY_TAB:       9,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3669
  KEY_RETURN:   13,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3670
  KEY_ESC:      27,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3671
  KEY_LEFT:     37,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3672
  KEY_UP:       38,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3673
  KEY_RIGHT:    39,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3674
  KEY_DOWN:     40,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3675
  KEY_DELETE:   46,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3676
  KEY_HOME:     36,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3677
  KEY_END:      35,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3678
  KEY_PAGEUP:   33,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3679
  KEY_PAGEDOWN: 34,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3680
  KEY_INSERT:   45,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3681
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3682
  cache: { },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3683
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3684
  relatedTarget: function(event) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3685
    var element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3686
    switch(event.type) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3687
      case 'mouseover': element = event.fromElement; break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3688
      case 'mouseout':  element = event.toElement;   break;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3689
      default: return null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3690
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3691
    return Element.extend(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3692
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3693
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3694
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3695
Event.Methods = (function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3696
  var isButton;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3697
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3698
  if (Prototype.Browser.IE) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3699
    var buttonMap = { 0: 1, 1: 4, 2: 2 };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3700
    isButton = function(event, code) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3701
      return event.button == buttonMap[code];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3702
    };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3703
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3704
  } else if (Prototype.Browser.WebKit) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3705
    isButton = function(event, code) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3706
      switch (code) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3707
        case 0: return event.which == 1 && !event.metaKey;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3708
        case 1: return event.which == 1 && event.metaKey;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3709
        default: return false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3710
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3711
    };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3712
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3713
  } else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3714
    isButton = function(event, code) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3715
      return event.which ? (event.which === code + 1) : (event.button === code);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3716
    };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3717
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3718
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3719
  return {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3720
    isLeftClick:   function(event) { return isButton(event, 0) },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3721
    isMiddleClick: function(event) { return isButton(event, 1) },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3722
    isRightClick:  function(event) { return isButton(event, 2) },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3723
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3724
    element: function(event) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3725
      var node = Event.extend(event).target;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3726
      return Element.extend(node.nodeType == Node.TEXT_NODE ? node.parentNode : node);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3727
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3728
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3729
    findElement: function(event, expression) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3730
      var element = Event.element(event);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3731
      if (!expression) return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3732
      var elements = [element].concat(element.ancestors());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3733
      return Selector.findElement(elements, expression, 0);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3734
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3735
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3736
    pointer: function(event) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3737
      return {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3738
        x: event.pageX || (event.clientX +
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3739
          (document.documentElement.scrollLeft || document.body.scrollLeft)),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3740
        y: event.pageY || (event.clientY +
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3741
          (document.documentElement.scrollTop || document.body.scrollTop))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3742
      };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3743
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3744
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3745
    pointerX: function(event) { return Event.pointer(event).x },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3746
    pointerY: function(event) { return Event.pointer(event).y },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3747
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3748
    stop: function(event) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3749
      Event.extend(event);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3750
      event.preventDefault();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3751
      event.stopPropagation();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3752
      event.stopped = true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3753
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3754
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3755
})();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3756
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3757
Event.extend = (function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3758
  var methods = Object.keys(Event.Methods).inject({ }, function(m, name) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3759
    m[name] = Event.Methods[name].methodize();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3760
    return m;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3761
  });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3762
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3763
  if (Prototype.Browser.IE) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3764
    Object.extend(methods, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3765
      stopPropagation: function() { this.cancelBubble = true },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3766
      preventDefault:  function() { this.returnValue = false },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3767
      inspect: function() { return "[object Event]" }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3768
    });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3769
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3770
    return function(event) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3771
      if (!event) return false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3772
      if (event._extendedByPrototype) return event;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3773
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3774
      event._extendedByPrototype = Prototype.emptyFunction;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3775
      var pointer = Event.pointer(event);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3776
      Object.extend(event, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3777
        target: event.srcElement,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3778
        relatedTarget: Event.relatedTarget(event),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3779
        pageX:  pointer.x,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3780
        pageY:  pointer.y
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3781
      });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3782
      return Object.extend(event, methods);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3783
    };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3784
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3785
  } else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3786
    Event.prototype = Event.prototype || document.createEvent("HTMLEvents").__proto__;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3787
    Object.extend(Event.prototype, methods);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3788
    return Prototype.K;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3789
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3790
})();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3791
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3792
Object.extend(Event, (function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3793
  var cache = Event.cache;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3794
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3795
  function getEventID(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3796
    if (element._eventID) return element._eventID;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3797
    arguments.callee.id = arguments.callee.id || 1;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3798
    return element._eventID = ++arguments.callee.id;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3799
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3800
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3801
  function getDOMEventName(eventName) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3802
    if (eventName && eventName.include(':')) return "dataavailable";
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3803
    return eventName;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3804
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3805
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3806
  function getCacheForID(id) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3807
    return cache[id] = cache[id] || { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3808
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3809
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3810
  function getWrappersForEventName(id, eventName) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3811
    var c = getCacheForID(id);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3812
    return c[eventName] = c[eventName] || [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3813
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3814
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3815
  function createWrapper(element, eventName, handler) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3816
    var id = getEventID(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3817
    var c = getWrappersForEventName(id, eventName);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3818
    if (c.pluck("handler").include(handler)) return false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3819
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3820
    var wrapper = function(event) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3821
      if (!Event || !Event.extend ||
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3822
        (event.eventName && event.eventName != eventName))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3823
          return false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3824
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3825
      Event.extend(event);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3826
      handler.call(element, event);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3827
    };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3828
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3829
    wrapper.handler = handler;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3830
    c.push(wrapper);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3831
    return wrapper;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3832
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3833
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3834
  function findWrapper(id, eventName, handler) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3835
    var c = getWrappersForEventName(id, eventName);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3836
    return c.find(function(wrapper) { return wrapper.handler == handler });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3837
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3838
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3839
  function destroyWrapper(id, eventName, handler) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3840
    var c = getCacheForID(id);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3841
    if (!c[eventName]) return false;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3842
    c[eventName] = c[eventName].without(findWrapper(id, eventName, handler));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3843
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3844
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3845
  function destroyCache() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3846
    for (var id in cache)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3847
      for (var eventName in cache[id])
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3848
        cache[id][eventName] = null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3849
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3850
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3851
  if (window.attachEvent) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3852
    window.attachEvent("onunload", destroyCache);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3853
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3854
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3855
  return {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3856
    observe: function(element, eventName, handler) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3857
      element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3858
      var name = getDOMEventName(eventName);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3859
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3860
      var wrapper = createWrapper(element, eventName, handler);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3861
      if (!wrapper) return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3862
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3863
      if (element.addEventListener) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3864
        element.addEventListener(name, wrapper, false);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3865
      } else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3866
        element.attachEvent("on" + name, wrapper);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3867
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3868
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3869
      return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3870
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3871
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3872
    stopObserving: function(element, eventName, handler) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3873
      element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3874
      var id = getEventID(element), name = getDOMEventName(eventName);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3875
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3876
      if (!handler && eventName) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3877
        getWrappersForEventName(id, eventName).each(function(wrapper) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3878
          element.stopObserving(eventName, wrapper.handler);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3879
        });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3880
        return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3881
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3882
      } else if (!eventName) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3883
        Object.keys(getCacheForID(id)).each(function(eventName) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3884
          element.stopObserving(eventName);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3885
        });
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3886
        return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3887
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3888
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3889
      var wrapper = findWrapper(id, eventName, handler);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3890
      if (!wrapper) return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3891
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3892
      if (element.removeEventListener) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3893
        element.removeEventListener(name, wrapper, false);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3894
      } else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3895
        element.detachEvent("on" + name, wrapper);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3896
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3897
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3898
      destroyWrapper(id, eventName, handler);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3899
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3900
      return element;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3901
    },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3902
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3903
    fire: function(element, eventName, memo) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3904
      element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3905
      if (element == document && document.createEvent && !element.dispatchEvent)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3906
        element = document.documentElement;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3907
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3908
      var event;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3909
      if (document.createEvent) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3910
        event = document.createEvent("HTMLEvents");
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3911
        event.initEvent("dataavailable", true, true);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3912
      } else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3913
        event = document.createEventObject();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3914
        event.eventType = "ondataavailable";
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3915
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3916
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3917
      event.eventName = eventName;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3918
      event.memo = memo || { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3919
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3920
      if (document.createEvent) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3921
        element.dispatchEvent(event);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3922
      } else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3923
        element.fireEvent(event.eventType, event);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3924
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3925
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3926
      return Event.extend(event);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3927
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3928
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3929
})());
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3930
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3931
Object.extend(Event, Event.Methods);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3932
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3933
Element.addMethods({
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3934
  fire:          Event.fire,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3935
  observe:       Event.observe,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3936
  stopObserving: Event.stopObserving
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3937
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3938
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3939
Object.extend(document, {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3940
  fire:          Element.Methods.fire.methodize(),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3941
  observe:       Element.Methods.observe.methodize(),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3942
  stopObserving: Element.Methods.stopObserving.methodize(),
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3943
  loaded:        false
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3944
});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3945
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3946
(function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3947
  /* Support for the DOMContentLoaded event is based on work by Dan Webb,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3948
     Matthias Miller, Dean Edwards and John Resig. */
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3949
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3950
  var timer;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3951
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3952
  function fireContentLoadedEvent() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3953
    if (document.loaded) return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3954
    if (timer) window.clearInterval(timer);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3955
    document.fire("dom:loaded");
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3956
    document.loaded = true;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3957
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3958
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3959
  if (document.addEventListener) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3960
    if (Prototype.Browser.WebKit) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3961
      timer = window.setInterval(function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3962
        if (/loaded|complete/.test(document.readyState))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3963
          fireContentLoadedEvent();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3964
      }, 0);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3965
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3966
      Event.observe(window, "load", fireContentLoadedEvent);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3967
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3968
    } else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3969
      document.addEventListener("DOMContentLoaded",
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3970
        fireContentLoadedEvent, false);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3971
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3972
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3973
  } else {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3974
    document.write("<script id=__onDOMContentLoaded defer src=//:><\/script>");
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3975
    $("__onDOMContentLoaded").onreadystatechange = function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3976
      if (this.readyState == "complete") {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3977
        this.onreadystatechange = null;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3978
        fireContentLoadedEvent();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3979
      }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3980
    };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3981
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3982
})();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3983
/*------------------------------- DEPRECATED -------------------------------*/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3984
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3985
Hash.toQueryString = Object.toQueryString;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3986
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3987
var Toggle = { display: Element.toggle };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3988
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3989
Element.Methods.childOf = Element.Methods.descendantOf;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3990
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3991
var Insertion = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3992
  Before: function(element, content) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3993
    return Element.insert(element, {before:content});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3994
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3995
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3996
  Top: function(element, content) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3997
    return Element.insert(element, {top:content});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3998
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  3999
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4000
  Bottom: function(element, content) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4001
    return Element.insert(element, {bottom:content});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4002
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4003
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4004
  After: function(element, content) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4005
    return Element.insert(element, {after:content});
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4006
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4007
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4008
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4009
var $continue = new Error('"throw $continue" is deprecated, use "return" instead');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4010
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4011
// This should be moved to script.aculo.us; notice the deprecated methods
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4012
// further below, that map to the newer Element methods.
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4013
var Position = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4014
  // set to true if needed, warning: firefox performance problems
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4015
  // NOT neeeded for page scrolling, only if draggable contained in
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4016
  // scrollable elements
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4017
  includeScrollOffsets: false,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4018
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4019
  // must be called before calling withinIncludingScrolloffset, every time the
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4020
  // page is scrolled
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4021
  prepare: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4022
    this.deltaX =  window.pageXOffset
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4023
                || document.documentElement.scrollLeft
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4024
                || document.body.scrollLeft
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4025
                || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4026
    this.deltaY =  window.pageYOffset
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4027
                || document.documentElement.scrollTop
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4028
                || document.body.scrollTop
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4029
                || 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4030
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4031
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4032
  // caches x/y coordinate pair to use with overlap
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4033
  within: function(element, x, y) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4034
    if (this.includeScrollOffsets)
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4035
      return this.withinIncludingScrolloffsets(element, x, y);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4036
    this.xcomp = x;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4037
    this.ycomp = y;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4038
    this.offset = Element.cumulativeOffset(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4039
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4040
    return (y >= this.offset[1] &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4041
            y <  this.offset[1] + element.offsetHeight &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4042
            x >= this.offset[0] &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4043
            x <  this.offset[0] + element.offsetWidth);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4044
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4045
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4046
  withinIncludingScrolloffsets: function(element, x, y) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4047
    var offsetcache = Element.cumulativeScrollOffset(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4048
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4049
    this.xcomp = x + offsetcache[0] - this.deltaX;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4050
    this.ycomp = y + offsetcache[1] - this.deltaY;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4051
    this.offset = Element.cumulativeOffset(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4052
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4053
    return (this.ycomp >= this.offset[1] &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4054
            this.ycomp <  this.offset[1] + element.offsetHeight &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4055
            this.xcomp >= this.offset[0] &&
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4056
            this.xcomp <  this.offset[0] + element.offsetWidth);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4057
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4058
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4059
  // within must be called directly before
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4060
  overlap: function(mode, element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4061
    if (!mode) return 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4062
    if (mode == 'vertical')
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4063
      return ((this.offset[1] + element.offsetHeight) - this.ycomp) /
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4064
        element.offsetHeight;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4065
    if (mode == 'horizontal')
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4066
      return ((this.offset[0] + element.offsetWidth) - this.xcomp) /
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4067
        element.offsetWidth;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4068
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4069
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4070
  // Deprecation layer -- use newer Element methods now (1.5.2).
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4071
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4072
  cumulativeOffset: Element.Methods.cumulativeOffset,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4073
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4074
  positionedOffset: Element.Methods.positionedOffset,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4075
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4076
  absolutize: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4077
    Position.prepare();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4078
    return Element.absolutize(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4079
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4080
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4081
  relativize: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4082
    Position.prepare();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4083
    return Element.relativize(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4084
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4085
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4086
  realOffset: Element.Methods.cumulativeScrollOffset,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4087
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4088
  offsetParent: Element.Methods.getOffsetParent,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4089
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4090
  page: Element.Methods.viewportOffset,
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4091
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4092
  clone: function(source, target, options) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4093
    options = options || { };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4094
    return Element.clonePosition(target, source, options);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4095
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4096
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4097
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4098
/*--------------------------------------------------------------------------*/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4099
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4100
if (!document.getElementsByClassName) document.getElementsByClassName = function(instanceMethods){
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4101
  function iter(name) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4102
    return name.blank() ? null : "[contains(concat(' ', @class, ' '), ' " + name + " ')]";
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4103
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4104
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4105
  instanceMethods.getElementsByClassName = Prototype.BrowserFeatures.XPath ?
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4106
  function(element, className) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4107
    className = className.toString().strip();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4108
    var cond = /\s/.test(className) ? $w(className).map(iter).join('') : iter(className);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4109
    return cond ? document._getElementsByXPath('.//*' + cond, element) : [];
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4110
  } : function(element, className) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4111
    className = className.toString().strip();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4112
    var elements = [], classNames = (/\s/.test(className) ? $w(className) : null);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4113
    if (!classNames && !className) return elements;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4114
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4115
    var nodes = $(element).getElementsByTagName('*');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4116
    className = ' ' + className + ' ';
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4117
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4118
    for (var i = 0, child, cn; child = nodes[i]; i++) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4119
      if (child.className && (cn = ' ' + child.className + ' ') && (cn.include(className) ||
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4120
          (classNames && classNames.all(function(name) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4121
            return !name.toString().blank() && cn.include(' ' + name + ' ');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4122
          }))))
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4123
        elements.push(Element.extend(child));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4124
    }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4125
    return elements;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4126
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4127
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4128
  return function(className, parentElement) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4129
    return $(parentElement || document.body).getElementsByClassName(className);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4130
  };
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4131
}(Element.Methods);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4132
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4133
/*--------------------------------------------------------------------------*/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4134
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4135
Element.ClassNames = Class.create();
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4136
Element.ClassNames.prototype = {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4137
  initialize: function(element) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4138
    this.element = $(element);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4139
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4140
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4141
  _each: function(iterator) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4142
    this.element.className.split(/\s+/).select(function(name) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4143
      return name.length > 0;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4144
    })._each(iterator);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4145
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4146
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4147
  set: function(className) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4148
    this.element.className = className;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4149
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4150
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4151
  add: function(classNameToAdd) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4152
    if (this.include(classNameToAdd)) return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4153
    this.set($A(this).concat(classNameToAdd).join(' '));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4154
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4155
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4156
  remove: function(classNameToRemove) {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4157
    if (!this.include(classNameToRemove)) return;
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4158
    this.set($A(this).without(classNameToRemove).join(' '));
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4159
  },
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4160
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4161
  toString: function() {
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4162
    return $A(this).join(' ');
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4163
  }
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4164
};
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4165
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4166
Object.extend(Element.ClassNames.prototype, Enumerable);
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4167
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4168
/*--------------------------------------------------------------------------*/
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4169
10841abbc01f taggr2, which is starting to shape up
terom
parents:
diff changeset
  4170
Element.addMethods();