win_gui 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +43 -0
  5. data/Rakefile +58 -0
  6. data/VERSION +1 -0
  7. data/book_code/early_success/bundle.rb +34 -0
  8. data/book_code/early_success/english.txt +1 -0
  9. data/book_code/early_success/jruby_basics.rb +47 -0
  10. data/book_code/early_success/windows_basics.rb +97 -0
  11. data/book_code/guessing/locknote.rb +379 -0
  12. data/book_code/guessing/monkeyshines.rb +14 -0
  13. data/book_code/guessing/note.rb +120 -0
  14. data/book_code/guessing/note_spec.rb +175 -0
  15. data/book_code/guessing/replay.rb +21 -0
  16. data/book_code/guessing/seed.rb +9 -0
  17. data/book_code/guessing/spec_helper.rb +69 -0
  18. data/book_code/guessing/windows_gui.rb +247 -0
  19. data/book_code/home_stretch/junquenote.rb +151 -0
  20. data/book_code/home_stretch/locknote.rb +180 -0
  21. data/book_code/home_stretch/note.rb +144 -0
  22. data/book_code/home_stretch/note_spec.rb +191 -0
  23. data/book_code/home_stretch/spec_helper.rb +55 -0
  24. data/book_code/home_stretch/swing_gui.rb +50 -0
  25. data/book_code/home_stretch/windows_gui.rb +232 -0
  26. data/book_code/junquenote/exports.sh +10 -0
  27. data/book_code/junquenote/jruby_mac.sh +10 -0
  28. data/book_code/junquenote/junquenote_app.rb +262 -0
  29. data/book_code/novite/Rakefile +10 -0
  30. data/book_code/novite/app/controllers/application.rb +18 -0
  31. data/book_code/novite/app/controllers/guests_controller.rb +28 -0
  32. data/book_code/novite/app/controllers/parties_controller.rb +77 -0
  33. data/book_code/novite/app/helpers/application_helper.rb +11 -0
  34. data/book_code/novite/app/helpers/guests_helper.rb +10 -0
  35. data/book_code/novite/app/helpers/parties_helper.rb +10 -0
  36. data/book_code/novite/app/models/guest.rb +11 -0
  37. data/book_code/novite/app/models/party.rb +32 -0
  38. data/book_code/novite/app/models/party_mailer.rb +19 -0
  39. data/book_code/novite/app/views/layouts/application.rhtml +44 -0
  40. data/book_code/novite/app/views/parties/new.html.erb +42 -0
  41. data/book_code/novite/app/views/parties/show.html.erb +43 -0
  42. data/book_code/novite/app/views/party_mailer/invite.erb +17 -0
  43. data/book_code/novite/config/boot.rb +117 -0
  44. data/book_code/novite/config/database.yml +19 -0
  45. data/book_code/novite/config/environment.rb +67 -0
  46. data/book_code/novite/config/environments/development.rb +29 -0
  47. data/book_code/novite/config/environments/production.rb +27 -0
  48. data/book_code/novite/config/environments/test.rb +30 -0
  49. data/book_code/novite/config/initializers/inflections.rb +18 -0
  50. data/book_code/novite/config/initializers/mime_types.rb +13 -0
  51. data/book_code/novite/config/routes.rb +47 -0
  52. data/book_code/novite/db/migrate/001_create_parties.rb +26 -0
  53. data/book_code/novite/db/migrate/002_create_guests.rb +23 -0
  54. data/book_code/novite/db/schema.rb +41 -0
  55. data/book_code/novite/log/empty.txt +0 -0
  56. data/book_code/novite/public/.htaccess +40 -0
  57. data/book_code/novite/public/404.html +38 -0
  58. data/book_code/novite/public/422.html +38 -0
  59. data/book_code/novite/public/500.html +38 -0
  60. data/book_code/novite/public/dispatch.cgi +10 -0
  61. data/book_code/novite/public/dispatch.fcgi +24 -0
  62. data/book_code/novite/public/dispatch.rb +18 -0
  63. data/book_code/novite/public/favicon.ico +0 -0
  64. data/book_code/novite/public/images/rails.png +0 -0
  65. data/book_code/novite/public/index.html +285 -0
  66. data/book_code/novite/public/javascripts/application.js +10 -0
  67. data/book_code/novite/public/javascripts/controls.js +971 -0
  68. data/book_code/novite/public/javascripts/dragdrop.js +980 -0
  69. data/book_code/novite/public/javascripts/effects.js +1128 -0
  70. data/book_code/novite/public/javascripts/prototype.js +4233 -0
  71. data/book_code/novite/public/robots.txt +5 -0
  72. data/book_code/novite/script/about +3 -0
  73. data/book_code/novite/script/console +3 -0
  74. data/book_code/novite/script/destroy +3 -0
  75. data/book_code/novite/script/generate +3 -0
  76. data/book_code/novite/script/performance/benchmarker +3 -0
  77. data/book_code/novite/script/performance/profiler +3 -0
  78. data/book_code/novite/script/performance/request +3 -0
  79. data/book_code/novite/script/plugin +3 -0
  80. data/book_code/novite/script/process/inspector +3 -0
  81. data/book_code/novite/script/process/reaper +3 -0
  82. data/book_code/novite/script/process/spawner +3 -0
  83. data/book_code/novite/script/runner +3 -0
  84. data/book_code/novite/script/server +3 -0
  85. data/book_code/novite/test/test_helper.rb +46 -0
  86. data/book_code/one_more_thing/applescript.rb +68 -0
  87. data/book_code/one_more_thing/note_spec.rb +50 -0
  88. data/book_code/one_more_thing/spec_helper.rb +17 -0
  89. data/book_code/one_more_thing/textedit-pure.rb +28 -0
  90. data/book_code/one_more_thing/textedit.applescript +26 -0
  91. data/book_code/one_more_thing/textedit.rb +32 -0
  92. data/book_code/one_more_thing/textnote.rb +87 -0
  93. data/book_code/simplify/junquenote.rb +48 -0
  94. data/book_code/simplify/locknote.rb +46 -0
  95. data/book_code/simplify/note.rb +35 -0
  96. data/book_code/simplify/note_spec.rb +28 -0
  97. data/book_code/simplify/swing_gui.rb +45 -0
  98. data/book_code/simplify/windows_gui.rb +232 -0
  99. data/book_code/simplify/windows_gui_spec.rb +35 -0
  100. data/book_code/story/invite.story +19 -0
  101. data/book_code/story/journal.txt +29 -0
  102. data/book_code/story/novite_stories.rb +156 -0
  103. data/book_code/story/party.rb +149 -0
  104. data/book_code/story/password.rb +61 -0
  105. data/book_code/story/password.story +26 -0
  106. data/book_code/story/rsvp.story +29 -0
  107. data/book_code/tables/TestTime.html +93 -0
  108. data/book_code/tables/TestTimeSample.html +63 -0
  109. data/book_code/tables/calculate_time.rb +39 -0
  110. data/book_code/tables/calculator.rb +108 -0
  111. data/book_code/tables/calculator_actions.rb +27 -0
  112. data/book_code/tables/calculator_spec.rb +47 -0
  113. data/book_code/tables/fit.rb +32 -0
  114. data/book_code/tables/matrix.rb +109 -0
  115. data/book_code/tables/pseudocode.rb +17 -0
  116. data/book_code/tubes/book_selenium.rb +67 -0
  117. data/book_code/tubes/book_watir.rb +60 -0
  118. data/book_code/tubes/dragdrop.html +81 -0
  119. data/book_code/tubes/html_capture.rb +33 -0
  120. data/book_code/tubes/joke_list.rb +67 -0
  121. data/book_code/tubes/list_spec.rb +41 -0
  122. data/book_code/tubes/search_spec.rb +32 -0
  123. data/book_code/tubes/selenium_example.rb +66 -0
  124. data/book_code/tubes/selenium_link.rb +23 -0
  125. data/book_code/tubes/web_server.rb +14 -0
  126. data/book_code/windows/wgui.rb +29 -0
  127. data/book_code/windows/wobj.rb +25 -0
  128. data/book_code/windows/wsh.rb +25 -0
  129. data/book_code/with_rspec/empty_spec.rb +13 -0
  130. data/book_code/with_rspec/junquenote.rb +60 -0
  131. data/book_code/with_rspec/locknote.rb +129 -0
  132. data/book_code/with_rspec/note_spec.rb +32 -0
  133. data/book_code/with_rspec/should_examples.rb +18 -0
  134. data/exp/exp.rb +6 -0
  135. data/exp/exp_encodings.rb +40 -0
  136. data/exp/exp_enum_windows.rb +60 -0
  137. data/exp/exp_quik.rb +38 -0
  138. data/exp/exp_wsh.rb +115 -0
  139. data/exp/old/windows_basics.rb +80 -0
  140. data/exp/old/wnote.rb +80 -0
  141. data/exp/old/wnote_spec.rb +20 -0
  142. data/features/step_definitions/win_gui_steps.rb +0 -0
  143. data/features/support/env.rb +4 -0
  144. data/features/win_gui.feature +9 -0
  145. data/lib/note/java/jemmy.jar +0 -0
  146. data/lib/note/java/jnote.rb +48 -0
  147. data/lib/note/java/jruby_basics.rb +37 -0
  148. data/lib/note/java/junquenote_app.rb +262 -0
  149. data/lib/note/java/note_spec.rb +20 -0
  150. data/lib/note/win/locknote.rb +19 -0
  151. data/lib/note.rb +15 -0
  152. data/lib/win_gui/constants.rb +66 -0
  153. data/lib/win_gui/string_extensions.rb +24 -0
  154. data/lib/win_gui/win_gui.rb +274 -0
  155. data/lib/win_gui/window.rb +70 -0
  156. data/lib/win_gui.rb +3 -0
  157. data/spec/note/win/locknote_spec.rb +7 -0
  158. data/spec/spec.opts +2 -0
  159. data/spec/spec_helper.rb +100 -0
  160. data/spec/test_apps/locknote/LockNote.exe +0 -0
  161. data/spec/win_gui/string_extensions_spec.rb +61 -0
  162. data/spec/win_gui/win_gui_spec.rb +733 -0
  163. data/spec/win_gui/window_spec.rb +124 -0
  164. metadata +251 -0
@@ -0,0 +1,4233 @@
1
+ /***
2
+ * Excerpted from "Scripted GUI Testing With Ruby",
3
+ * published by The Pragmatic Bookshelf.
4
+ * Copyrights apply to this code. It may not be used to create training material,
5
+ * courses, books, articles, and the like. Contact us if you are in doubt.
6
+ * We make no guarantees that this code is fit for any purpose.
7
+ * Visit http://www.pragmaticprogrammer.com/titles/idgtr for more book information.
8
+ ***/
9
+ /* Prototype JavaScript framework, version 1.6.0.1
10
+ * (c) 2005-2007 Sam Stephenson
11
+ *
12
+ * Prototype is freely distributable under the terms of an MIT-style license.
13
+ * For details, see the Prototype web site: http://www.prototypejs.org/
14
+ *
15
+ *--------------------------------------------------------------------------*/
16
+
17
+ var Prototype = {
18
+ Version: '1.6.0.1',
19
+
20
+ Browser: {
21
+ IE: !!(window.attachEvent && !window.opera),
22
+ Opera: !!window.opera,
23
+ WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
24
+ Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
25
+ MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
26
+ },
27
+
28
+ BrowserFeatures: {
29
+ XPath: !!document.evaluate,
30
+ ElementExtensions: !!window.HTMLElement,
31
+ SpecificElementExtensions:
32
+ document.createElement('div').__proto__ &&
33
+ document.createElement('div').__proto__ !==
34
+ document.createElement('form').__proto__
35
+ },
36
+
37
+ ScriptFragment: '<script[^>]*>([\\S\\s]*?)<\/script>',
38
+ JSONFilter: /^\/\*-secure-([\s\S]*)\*\/\s*$/,
39
+
40
+ emptyFunction: function() { },
41
+ K: function(x) { return x }
42
+ };
43
+
44
+ if (Prototype.Browser.MobileSafari)
45
+ Prototype.BrowserFeatures.SpecificElementExtensions = false;
46
+
47
+
48
+ /* Based on Alex Arnell's inheritance implementation. */
49
+ var Class = {
50
+ create: function() {
51
+ var parent = null, properties = $A(arguments);
52
+ if (Object.isFunction(properties[0]))
53
+ parent = properties.shift();
54
+
55
+ function klass() {
56
+ this.initialize.apply(this, arguments);
57
+ }
58
+
59
+ Object.extend(klass, Class.Methods);
60
+ klass.superclass = parent;
61
+ klass.subclasses = [];
62
+
63
+ if (parent) {
64
+ var subclass = function() { };
65
+ subclass.prototype = parent.prototype;
66
+ klass.prototype = new subclass;
67
+ parent.subclasses.push(klass);
68
+ }
69
+
70
+ for (var i = 0; i < properties.length; i++)
71
+ klass.addMethods(properties[i]);
72
+
73
+ if (!klass.prototype.initialize)
74
+ klass.prototype.initialize = Prototype.emptyFunction;
75
+
76
+ klass.prototype.constructor = klass;
77
+
78
+ return klass;
79
+ }
80
+ };
81
+
82
+ Class.Methods = {
83
+ addMethods: function(source) {
84
+ var ancestor = this.superclass && this.superclass.prototype;
85
+ var properties = Object.keys(source);
86
+
87
+ if (!Object.keys({ toString: true }).length)
88
+ properties.push("toString", "valueOf");
89
+
90
+ for (var i = 0, length = properties.length; i < length; i++) {
91
+ var property = properties[i], value = source[property];
92
+ if (ancestor && Object.isFunction(value) &&
93
+ value.argumentNames().first() == "$super") {
94
+ var method = value, value = Object.extend((function(m) {
95
+ return function() { return ancestor[m].apply(this, arguments) };
96
+ })(property).wrap(method), {
97
+ valueOf: function() { return method },
98
+ toString: function() { return method.toString() }
99
+ });
100
+ }
101
+ this.prototype[property] = value;
102
+ }
103
+
104
+ return this;
105
+ }
106
+ };
107
+
108
+ var Abstract = { };
109
+
110
+ Object.extend = function(destination, source) {
111
+ for (var property in source)
112
+ destination[property] = source[property];
113
+ return destination;
114
+ };
115
+
116
+ Object.extend(Object, {
117
+ inspect: function(object) {
118
+ try {
119
+ if (Object.isUndefined(object)) return 'undefined';
120
+ if (object === null) return 'null';
121
+ return object.inspect ? object.inspect() : object.toString();
122
+ } catch (e) {
123
+ if (e instanceof RangeError) return '...';
124
+ throw e;
125
+ }
126
+ },
127
+
128
+ toJSON: function(object) {
129
+ var type = typeof object;
130
+ switch (type) {
131
+ case 'undefined':
132
+ case 'function':
133
+ case 'unknown': return;
134
+ case 'boolean': return object.toString();
135
+ }
136
+
137
+ if (object === null) return 'null';
138
+ if (object.toJSON) return object.toJSON();
139
+ if (Object.isElement(object)) return;
140
+
141
+ var results = [];
142
+ for (var property in object) {
143
+ var value = Object.toJSON(object[property]);
144
+ if (!Object.isUndefined(value))
145
+ results.push(property.toJSON() + ': ' + value);
146
+ }
147
+
148
+ return '{' + results.join(', ') + '}';
149
+ },
150
+
151
+ toQueryString: function(object) {
152
+ return $H(object).toQueryString();
153
+ },
154
+
155
+ toHTML: function(object) {
156
+ return object && object.toHTML ? object.toHTML() : String.interpret(object);
157
+ },
158
+
159
+ keys: function(object) {
160
+ var keys = [];
161
+ for (var property in object)
162
+ keys.push(property);
163
+ return keys;
164
+ },
165
+
166
+ values: function(object) {
167
+ var values = [];
168
+ for (var property in object)
169
+ values.push(object[property]);
170
+ return values;
171
+ },
172
+
173
+ clone: function(object) {
174
+ return Object.extend({ }, object);
175
+ },
176
+
177
+ isElement: function(object) {
178
+ return object && object.nodeType == 1;
179
+ },
180
+
181
+ isArray: function(object) {
182
+ return object && object.constructor === Array;
183
+ },
184
+
185
+ isHash: function(object) {
186
+ return object instanceof Hash;
187
+ },
188
+
189
+ isFunction: function(object) {
190
+ return typeof object == "function";
191
+ },
192
+
193
+ isString: function(object) {
194
+ return typeof object == "string";
195
+ },
196
+
197
+ isNumber: function(object) {
198
+ return typeof object == "number";
199
+ },
200
+
201
+ isUndefined: function(object) {
202
+ return typeof object == "undefined";
203
+ }
204
+ });
205
+
206
+ Object.extend(Function.prototype, {
207
+ argumentNames: function() {
208
+ var names = this.toString().match(/^[\s\(]*function[^(]*\((.*?)\)/)[1].split(",").invoke("strip");
209
+ return names.length == 1 && !names[0] ? [] : names;
210
+ },
211
+
212
+ bind: function() {
213
+ if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this;
214
+ var __method = this, args = $A(arguments), object = args.shift();
215
+ return function() {
216
+ return __method.apply(object, args.concat($A(arguments)));
217
+ }
218
+ },
219
+
220
+ bindAsEventListener: function() {
221
+ var __method = this, args = $A(arguments), object = args.shift();
222
+ return function(event) {
223
+ return __method.apply(object, [event || window.event].concat(args));
224
+ }
225
+ },
226
+
227
+ curry: function() {
228
+ if (!arguments.length) return this;
229
+ var __method = this, args = $A(arguments);
230
+ return function() {
231
+ return __method.apply(this, args.concat($A(arguments)));
232
+ }
233
+ },
234
+
235
+ delay: function() {
236
+ var __method = this, args = $A(arguments), timeout = args.shift() * 1000;
237
+ return window.setTimeout(function() {
238
+ return __method.apply(__method, args);
239
+ }, timeout);
240
+ },
241
+
242
+ wrap: function(wrapper) {
243
+ var __method = this;
244
+ return function() {
245
+ return wrapper.apply(this, [__method.bind(this)].concat($A(arguments)));
246
+ }
247
+ },
248
+
249
+ methodize: function() {
250
+ if (this._methodized) return this._methodized;
251
+ var __method = this;
252
+ return this._methodized = function() {
253
+ return __method.apply(null, [this].concat($A(arguments)));
254
+ };
255
+ }
256
+ });
257
+
258
+ Function.prototype.defer = Function.prototype.delay.curry(0.01);
259
+
260
+ Date.prototype.toJSON = function() {
261
+ return '"' + this.getUTCFullYear() + '-' +
262
+ (this.getUTCMonth() + 1).toPaddedString(2) + '-' +
263
+ this.getUTCDate().toPaddedString(2) + 'T' +
264
+ this.getUTCHours().toPaddedString(2) + ':' +
265
+ this.getUTCMinutes().toPaddedString(2) + ':' +
266
+ this.getUTCSeconds().toPaddedString(2) + 'Z"';
267
+ };
268
+
269
+ var Try = {
270
+ these: function() {
271
+ var returnValue;
272
+
273
+ for (var i = 0, length = arguments.length; i < length; i++) {
274
+ var lambda = arguments[i];
275
+ try {
276
+ returnValue = lambda();
277
+ break;
278
+ } catch (e) { }
279
+ }
280
+
281
+ return returnValue;
282
+ }
283
+ };
284
+
285
+ RegExp.prototype.match = RegExp.prototype.test;
286
+
287
+ RegExp.escape = function(str) {
288
+ return String(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
289
+ };
290
+
291
+ /*--------------------------------------------------------------------------*/
292
+
293
+ var PeriodicalExecuter = Class.create({
294
+ initialize: function(callback, frequency) {
295
+ this.callback = callback;
296
+ this.frequency = frequency;
297
+ this.currentlyExecuting = false;
298
+
299
+ this.registerCallback();
300
+ },
301
+
302
+ registerCallback: function() {
303
+ this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
304
+ },
305
+
306
+ execute: function() {
307
+ this.callback(this);
308
+ },
309
+
310
+ stop: function() {
311
+ if (!this.timer) return;
312
+ clearInterval(this.timer);
313
+ this.timer = null;
314
+ },
315
+
316
+ onTimerEvent: function() {
317
+ if (!this.currentlyExecuting) {
318
+ try {
319
+ this.currentlyExecuting = true;
320
+ this.execute();
321
+ } finally {
322
+ this.currentlyExecuting = false;
323
+ }
324
+ }
325
+ }
326
+ });
327
+ Object.extend(String, {
328
+ interpret: function(value) {
329
+ return value == null ? '' : String(value);
330
+ },
331
+ specialChar: {
332
+ '\b': '\\b',
333
+ '\t': '\\t',
334
+ '\n': '\\n',
335
+ '\f': '\\f',
336
+ '\r': '\\r',
337
+ '\\': '\\\\'
338
+ }
339
+ });
340
+
341
+ Object.extend(String.prototype, {
342
+ gsub: function(pattern, replacement) {
343
+ var result = '', source = this, match;
344
+ replacement = arguments.callee.prepareReplacement(replacement);
345
+
346
+ while (source.length > 0) {
347
+ if (match = source.match(pattern)) {
348
+ result += source.slice(0, match.index);
349
+ result += String.interpret(replacement(match));
350
+ source = source.slice(match.index + match[0].length);
351
+ } else {
352
+ result += source, source = '';
353
+ }
354
+ }
355
+ return result;
356
+ },
357
+
358
+ sub: function(pattern, replacement, count) {
359
+ replacement = this.gsub.prepareReplacement(replacement);
360
+ count = Object.isUndefined(count) ? 1 : count;
361
+
362
+ return this.gsub(pattern, function(match) {
363
+ if (--count < 0) return match[0];
364
+ return replacement(match);
365
+ });
366
+ },
367
+
368
+ scan: function(pattern, iterator) {
369
+ this.gsub(pattern, iterator);
370
+ return String(this);
371
+ },
372
+
373
+ truncate: function(length, truncation) {
374
+ length = length || 30;
375
+ truncation = Object.isUndefined(truncation) ? '...' : truncation;
376
+ return this.length > length ?
377
+ this.slice(0, length - truncation.length) + truncation : String(this);
378
+ },
379
+
380
+ strip: function() {
381
+ return this.replace(/^\s+/, '').replace(/\s+$/, '');
382
+ },
383
+
384
+ stripTags: function() {
385
+ return this.replace(/<\/?[^>]+>/gi, '');
386
+ },
387
+
388
+ stripScripts: function() {
389
+ return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), '');
390
+ },
391
+
392
+ extractScripts: function() {
393
+ var matchAll = new RegExp(Prototype.ScriptFragment, 'img');
394
+ var matchOne = new RegExp(Prototype.ScriptFragment, 'im');
395
+ return (this.match(matchAll) || []).map(function(scriptTag) {
396
+ return (scriptTag.match(matchOne) || ['', ''])[1];
397
+ });
398
+ },
399
+
400
+ evalScripts: function() {
401
+ return this.extractScripts().map(function(script) { return eval(script) });
402
+ },
403
+
404
+ escapeHTML: function() {
405
+ var self = arguments.callee;
406
+ self.text.data = this;
407
+ return self.div.innerHTML;
408
+ },
409
+
410
+ unescapeHTML: function() {
411
+ var div = new Element('div');
412
+ div.innerHTML = this.stripTags();
413
+ return div.childNodes[0] ? (div.childNodes.length > 1 ?
414
+ $A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) :
415
+ div.childNodes[0].nodeValue) : '';
416
+ },
417
+
418
+ toQueryParams: function(separator) {
419
+ var match = this.strip().match(/([^?#]*)(#.*)?$/);
420
+ if (!match) return { };
421
+
422
+ return match[1].split(separator || '&').inject({ }, function(hash, pair) {
423
+ if ((pair = pair.split('='))[0]) {
424
+ var key = decodeURIComponent(pair.shift());
425
+ var value = pair.length > 1 ? pair.join('=') : pair[0];
426
+ if (value != undefined) value = decodeURIComponent(value);
427
+
428
+ if (key in hash) {
429
+ if (!Object.isArray(hash[key])) hash[key] = [hash[key]];
430
+ hash[key].push(value);
431
+ }
432
+ else hash[key] = value;
433
+ }
434
+ return hash;
435
+ });
436
+ },
437
+
438
+ toArray: function() {
439
+ return this.split('');
440
+ },
441
+
442
+ succ: function() {
443
+ return this.slice(0, this.length - 1) +
444
+ String.fromCharCode(this.charCodeAt(this.length - 1) + 1);
445
+ },
446
+
447
+ times: function(count) {
448
+ return count < 1 ? '' : new Array(count + 1).join(this);
449
+ },
450
+
451
+ camelize: function() {
452
+ var parts = this.split('-'), len = parts.length;
453
+ if (len == 1) return parts[0];
454
+
455
+ var camelized = this.charAt(0) == '-'
456
+ ? parts[0].charAt(0).toUpperCase() + parts[0].substring(1)
457
+ : parts[0];
458
+
459
+ for (var i = 1; i < len; i++)
460
+ camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1);
461
+
462
+ return camelized;
463
+ },
464
+
465
+ capitalize: function() {
466
+ return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
467
+ },
468
+
469
+ underscore: function() {
470
+ return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z\d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase();
471
+ },
472
+
473
+ dasherize: function() {
474
+ return this.gsub(/_/,'-');
475
+ },
476
+
477
+ inspect: function(useDoubleQuotes) {
478
+ var escapedString = this.gsub(/[\x00-\x1f\\]/, function(match) {
479
+ var character = String.specialChar[match[0]];
480
+ return character ? character : '\\u00' + match[0].charCodeAt().toPaddedString(2, 16);
481
+ });
482
+ if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"';
483
+ return "'" + escapedString.replace(/'/g, '\\\'') + "'";
484
+ },
485
+
486
+ toJSON: function() {
487
+ return this.inspect(true);
488
+ },
489
+
490
+ unfilterJSON: function(filter) {
491
+ return this.sub(filter || Prototype.JSONFilter, '#{1}');
492
+ },
493
+
494
+ isJSON: function() {
495
+ var str = this;
496
+ if (str.blank()) return false;
497
+ str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '');
498
+ return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str);
499
+ },
500
+
501
+ evalJSON: function(sanitize) {
502
+ var json = this.unfilterJSON();
503
+ try {
504
+ if (!sanitize || json.isJSON()) return eval('(' + json + ')');
505
+ } catch (e) { }
506
+ throw new SyntaxError('Badly formed JSON string: ' + this.inspect());
507
+ },
508
+
509
+ include: function(pattern) {
510
+ return this.indexOf(pattern) > -1;
511
+ },
512
+
513
+ startsWith: function(pattern) {
514
+ return this.indexOf(pattern) === 0;
515
+ },
516
+
517
+ endsWith: function(pattern) {
518
+ var d = this.length - pattern.length;
519
+ return d >= 0 && this.lastIndexOf(pattern) === d;
520
+ },
521
+
522
+ empty: function() {
523
+ return this == '';
524
+ },
525
+
526
+ blank: function() {
527
+ return /^\s*$/.test(this);
528
+ },
529
+
530
+ interpolate: function(object, pattern) {
531
+ return new Template(this, pattern).evaluate(object);
532
+ }
533
+ });
534
+
535
+ if (Prototype.Browser.WebKit || Prototype.Browser.IE) Object.extend(String.prototype, {
536
+ escapeHTML: function() {
537
+ return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
538
+ },
539
+ unescapeHTML: function() {
540
+ return this.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
541
+ }
542
+ });
543
+
544
+ String.prototype.gsub.prepareReplacement = function(replacement) {
545
+ if (Object.isFunction(replacement)) return replacement;
546
+ var template = new Template(replacement);
547
+ return function(match) { return template.evaluate(match) };
548
+ };
549
+
550
+ String.prototype.parseQuery = String.prototype.toQueryParams;
551
+
552
+ Object.extend(String.prototype.escapeHTML, {
553
+ div: document.createElement('div'),
554
+ text: document.createTextNode('')
555
+ });
556
+
557
+ with (String.prototype.escapeHTML) div.appendChild(text);
558
+
559
+ var Template = Class.create({
560
+ initialize: function(template, pattern) {
561
+ this.template = template.toString();
562
+ this.pattern = pattern || Template.Pattern;
563
+ },
564
+
565
+ evaluate: function(object) {
566
+ if (Object.isFunction(object.toTemplateReplacements))
567
+ object = object.toTemplateReplacements();
568
+
569
+ return this.template.gsub(this.pattern, function(match) {
570
+ if (object == null) return '';
571
+
572
+ var before = match[1] || '';
573
+ if (before == '\\') return match[2];
574
+
575
+ var ctx = object, expr = match[3];
576
+ var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/;
577
+ match = pattern.exec(expr);
578
+ if (match == null) return before;
579
+
580
+ while (match != null) {
581
+ var comp = match[1].startsWith('[') ? match[2].gsub('\\\\]', ']') : match[1];
582
+ ctx = ctx[comp];
583
+ if (null == ctx || '' == match[3]) break;
584
+ expr = expr.substring('[' == match[3] ? match[1].length : match[0].length);
585
+ match = pattern.exec(expr);
586
+ }
587
+
588
+ return before + String.interpret(ctx);
589
+ }.bind(this));
590
+ }
591
+ });
592
+ Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/;
593
+
594
+ var $break = { };
595
+
596
+ var Enumerable = {
597
+ each: function(iterator, context) {
598
+ var index = 0;
599
+ iterator = iterator.bind(context);
600
+ try {
601
+ this._each(function(value) {
602
+ iterator(value, index++);
603
+ });
604
+ } catch (e) {
605
+ if (e != $break) throw e;
606
+ }
607
+ return this;
608
+ },
609
+
610
+ eachSlice: function(number, iterator, context) {
611
+ iterator = iterator ? iterator.bind(context) : Prototype.K;
612
+ var index = -number, slices = [], array = this.toArray();
613
+ while ((index += number) < array.length)
614
+ slices.push(array.slice(index, index+number));
615
+ return slices.collect(iterator, context);
616
+ },
617
+
618
+ all: function(iterator, context) {
619
+ iterator = iterator ? iterator.bind(context) : Prototype.K;
620
+ var result = true;
621
+ this.each(function(value, index) {
622
+ result = result && !!iterator(value, index);
623
+ if (!result) throw $break;
624
+ });
625
+ return result;
626
+ },
627
+
628
+ any: function(iterator, context) {
629
+ iterator = iterator ? iterator.bind(context) : Prototype.K;
630
+ var result = false;
631
+ this.each(function(value, index) {
632
+ if (result = !!iterator(value, index))
633
+ throw $break;
634
+ });
635
+ return result;
636
+ },
637
+
638
+ collect: function(iterator, context) {
639
+ iterator = iterator ? iterator.bind(context) : Prototype.K;
640
+ var results = [];
641
+ this.each(function(value, index) {
642
+ results.push(iterator(value, index));
643
+ });
644
+ return results;
645
+ },
646
+
647
+ detect: function(iterator, context) {
648
+ iterator = iterator.bind(context);
649
+ var result;
650
+ this.each(function(value, index) {
651
+ if (iterator(value, index)) {
652
+ result = value;
653
+ throw $break;
654
+ }
655
+ });
656
+ return result;
657
+ },
658
+
659
+ findAll: function(iterator, context) {
660
+ iterator = iterator.bind(context);
661
+ var results = [];
662
+ this.each(function(value, index) {
663
+ if (iterator(value, index))
664
+ results.push(value);
665
+ });
666
+ return results;
667
+ },
668
+
669
+ grep: function(filter, iterator, context) {
670
+ iterator = iterator ? iterator.bind(context) : Prototype.K;
671
+ var results = [];
672
+
673
+ if (Object.isString(filter))
674
+ filter = new RegExp(filter);
675
+
676
+ this.each(function(value, index) {
677
+ if (filter.match(value))
678
+ results.push(iterator(value, index));
679
+ });
680
+ return results;
681
+ },
682
+
683
+ include: function(object) {
684
+ if (Object.isFunction(this.indexOf))
685
+ if (this.indexOf(object) != -1) return true;
686
+
687
+ var found = false;
688
+ this.each(function(value) {
689
+ if (value == object) {
690
+ found = true;
691
+ throw $break;
692
+ }
693
+ });
694
+ return found;
695
+ },
696
+
697
+ inGroupsOf: function(number, fillWith) {
698
+ fillWith = Object.isUndefined(fillWith) ? null : fillWith;
699
+ return this.eachSlice(number, function(slice) {
700
+ while(slice.length < number) slice.push(fillWith);
701
+ return slice;
702
+ });
703
+ },
704
+
705
+ inject: function(memo, iterator, context) {
706
+ iterator = iterator.bind(context);
707
+ this.each(function(value, index) {
708
+ memo = iterator(memo, value, index);
709
+ });
710
+ return memo;
711
+ },
712
+
713
+ invoke: function(method) {
714
+ var args = $A(arguments).slice(1);
715
+ return this.map(function(value) {
716
+ return value[method].apply(value, args);
717
+ });
718
+ },
719
+
720
+ max: function(iterator, context) {
721
+ iterator = iterator ? iterator.bind(context) : Prototype.K;
722
+ var result;
723
+ this.each(function(value, index) {
724
+ value = iterator(value, index);
725
+ if (result == null || value >= result)
726
+ result = value;
727
+ });
728
+ return result;
729
+ },
730
+
731
+ min: function(iterator, context) {
732
+ iterator = iterator ? iterator.bind(context) : Prototype.K;
733
+ var result;
734
+ this.each(function(value, index) {
735
+ value = iterator(value, index);
736
+ if (result == null || value < result)
737
+ result = value;
738
+ });
739
+ return result;
740
+ },
741
+
742
+ partition: function(iterator, context) {
743
+ iterator = iterator ? iterator.bind(context) : Prototype.K;
744
+ var trues = [], falses = [];
745
+ this.each(function(value, index) {
746
+ (iterator(value, index) ?
747
+ trues : falses).push(value);
748
+ });
749
+ return [trues, falses];
750
+ },
751
+
752
+ pluck: function(property) {
753
+ var results = [];
754
+ this.each(function(value) {
755
+ results.push(value[property]);
756
+ });
757
+ return results;
758
+ },
759
+
760
+ reject: function(iterator, context) {
761
+ iterator = iterator.bind(context);
762
+ var results = [];
763
+ this.each(function(value, index) {
764
+ if (!iterator(value, index))
765
+ results.push(value);
766
+ });
767
+ return results;
768
+ },
769
+
770
+ sortBy: function(iterator, context) {
771
+ iterator = iterator.bind(context);
772
+ return this.map(function(value, index) {
773
+ return {value: value, criteria: iterator(value, index)};
774
+ }).sort(function(left, right) {
775
+ var a = left.criteria, b = right.criteria;
776
+ return a < b ? -1 : a > b ? 1 : 0;
777
+ }).pluck('value');
778
+ },
779
+
780
+ toArray: function() {
781
+ return this.map();
782
+ },
783
+
784
+ zip: function() {
785
+ var iterator = Prototype.K, args = $A(arguments);
786
+ if (Object.isFunction(args.last()))
787
+ iterator = args.pop();
788
+
789
+ var collections = [this].concat(args).map($A);
790
+ return this.map(function(value, index) {
791
+ return iterator(collections.pluck(index));
792
+ });
793
+ },
794
+
795
+ size: function() {
796
+ return this.toArray().length;
797
+ },
798
+
799
+ inspect: function() {
800
+ return '#<Enumerable:' + this.toArray().inspect() + '>';
801
+ }
802
+ };
803
+
804
+ Object.extend(Enumerable, {
805
+ map: Enumerable.collect,
806
+ find: Enumerable.detect,
807
+ select: Enumerable.findAll,
808
+ filter: Enumerable.findAll,
809
+ member: Enumerable.include,
810
+ entries: Enumerable.toArray,
811
+ every: Enumerable.all,
812
+ some: Enumerable.any
813
+ });
814
+ function $A(iterable) {
815
+ if (!iterable) return [];
816
+ if (iterable.toArray) return iterable.toArray();
817
+ var length = iterable.length, results = new Array(length);
818
+ while (length--) results[length] = iterable[length];
819
+ return results;
820
+ }
821
+
822
+ if (Prototype.Browser.WebKit) {
823
+ function $A(iterable) {
824
+ if (!iterable) return [];
825
+ if (!(Object.isFunction(iterable) && iterable == '[object NodeList]') &&
826
+ iterable.toArray) return iterable.toArray();
827
+ var length = iterable.length, results = new Array(length);
828
+ while (length--) results[length] = iterable[length];
829
+ return results;
830
+ }
831
+ }
832
+
833
+ Array.from = $A;
834
+
835
+ Object.extend(Array.prototype, Enumerable);
836
+
837
+ if (!Array.prototype._reverse) Array.prototype._reverse = Array.prototype.reverse;
838
+
839
+ Object.extend(Array.prototype, {
840
+ _each: function(iterator) {
841
+ for (var i = 0, length = this.length; i < length; i++)
842
+ iterator(this[i]);
843
+ },
844
+
845
+ clear: function() {
846
+ this.length = 0;
847
+ return this;
848
+ },
849
+
850
+ first: function() {
851
+ return this[0];
852
+ },
853
+
854
+ last: function() {
855
+ return this[this.length - 1];
856
+ },
857
+
858
+ compact: function() {
859
+ return this.select(function(value) {
860
+ return value != null;
861
+ });
862
+ },
863
+
864
+ flatten: function() {
865
+ return this.inject([], function(array, value) {
866
+ return array.concat(Object.isArray(value) ?
867
+ value.flatten() : [value]);
868
+ });
869
+ },
870
+
871
+ without: function() {
872
+ var values = $A(arguments);
873
+ return this.select(function(value) {
874
+ return !values.include(value);
875
+ });
876
+ },
877
+
878
+ reverse: function(inline) {
879
+ return (inline !== false ? this : this.toArray())._reverse();
880
+ },
881
+
882
+ reduce: function() {
883
+ return this.length > 1 ? this : this[0];
884
+ },
885
+
886
+ uniq: function(sorted) {
887
+ return this.inject([], function(array, value, index) {
888
+ if (0 == index || (sorted ? array.last() != value : !array.include(value)))
889
+ array.push(value);
890
+ return array;
891
+ });
892
+ },
893
+
894
+ intersect: function(array) {
895
+ return this.uniq().findAll(function(item) {
896
+ return array.detect(function(value) { return item === value });
897
+ });
898
+ },
899
+
900
+ clone: function() {
901
+ return [].concat(this);
902
+ },
903
+
904
+ size: function() {
905
+ return this.length;
906
+ },
907
+
908
+ inspect: function() {
909
+ return '[' + this.map(Object.inspect).join(', ') + ']';
910
+ },
911
+
912
+ toJSON: function() {
913
+ var results = [];
914
+ this.each(function(object) {
915
+ var value = Object.toJSON(object);
916
+ if (!Object.isUndefined(value)) results.push(value);
917
+ });
918
+ return '[' + results.join(', ') + ']';
919
+ }
920
+ });
921
+
922
+ // use native browser JS 1.6 implementation if available
923
+ if (Object.isFunction(Array.prototype.forEach))
924
+ Array.prototype._each = Array.prototype.forEach;
925
+
926
+ if (!Array.prototype.indexOf) Array.prototype.indexOf = function(item, i) {
927
+ i || (i = 0);
928
+ var length = this.length;
929
+ if (i < 0) i = length + i;
930
+ for (; i < length; i++)
931
+ if (this[i] === item) return i;
932
+ return -1;
933
+ };
934
+
935
+ if (!Array.prototype.lastIndexOf) Array.prototype.lastIndexOf = function(item, i) {
936
+ i = isNaN(i) ? this.length : (i < 0 ? this.length + i : i) + 1;
937
+ var n = this.slice(0, i).reverse().indexOf(item);
938
+ return (n < 0) ? n : i - n - 1;
939
+ };
940
+
941
+ Array.prototype.toArray = Array.prototype.clone;
942
+
943
+ function $w(string) {
944
+ if (!Object.isString(string)) return [];
945
+ string = string.strip();
946
+ return string ? string.split(/\s+/) : [];
947
+ }
948
+
949
+ if (Prototype.Browser.Opera){
950
+ Array.prototype.concat = function() {
951
+ var array = [];
952
+ for (var i = 0, length = this.length; i < length; i++) array.push(this[i]);
953
+ for (var i = 0, length = arguments.length; i < length; i++) {
954
+ if (Object.isArray(arguments[i])) {
955
+ for (var j = 0, arrayLength = arguments[i].length; j < arrayLength; j++)
956
+ array.push(arguments[i][j]);
957
+ } else {
958
+ array.push(arguments[i]);
959
+ }
960
+ }
961
+ return array;
962
+ };
963
+ }
964
+ Object.extend(Number.prototype, {
965
+ toColorPart: function() {
966
+ return this.toPaddedString(2, 16);
967
+ },
968
+
969
+ succ: function() {
970
+ return this + 1;
971
+ },
972
+
973
+ times: function(iterator) {
974
+ $R(0, this, true).each(iterator);
975
+ return this;
976
+ },
977
+
978
+ toPaddedString: function(length, radix) {
979
+ var string = this.toString(radix || 10);
980
+ return '0'.times(length - string.length) + string;
981
+ },
982
+
983
+ toJSON: function() {
984
+ return isFinite(this) ? this.toString() : 'null';
985
+ }
986
+ });
987
+
988
+ $w('abs round ceil floor').each(function(method){
989
+ Number.prototype[method] = Math[method].methodize();
990
+ });
991
+ function $H(object) {
992
+ return new Hash(object);
993
+ };
994
+
995
+ var Hash = Class.create(Enumerable, (function() {
996
+
997
+ function toQueryPair(key, value) {
998
+ if (Object.isUndefined(value)) return key;
999
+ return key + '=' + encodeURIComponent(String.interpret(value));
1000
+ }
1001
+
1002
+ return {
1003
+ initialize: function(object) {
1004
+ this._object = Object.isHash(object) ? object.toObject() : Object.clone(object);
1005
+ },
1006
+
1007
+ _each: function(iterator) {
1008
+ for (var key in this._object) {
1009
+ var value = this._object[key], pair = [key, value];
1010
+ pair.key = key;
1011
+ pair.value = value;
1012
+ iterator(pair);
1013
+ }
1014
+ },
1015
+
1016
+ set: function(key, value) {
1017
+ return this._object[key] = value;
1018
+ },
1019
+
1020
+ get: function(key) {
1021
+ return this._object[key];
1022
+ },
1023
+
1024
+ unset: function(key) {
1025
+ var value = this._object[key];
1026
+ delete this._object[key];
1027
+ return value;
1028
+ },
1029
+
1030
+ toObject: function() {
1031
+ return Object.clone(this._object);
1032
+ },
1033
+
1034
+ keys: function() {
1035
+ return this.pluck('key');
1036
+ },
1037
+
1038
+ values: function() {
1039
+ return this.pluck('value');
1040
+ },
1041
+
1042
+ index: function(value) {
1043
+ var match = this.detect(function(pair) {
1044
+ return pair.value === value;
1045
+ });
1046
+ return match && match.key;
1047
+ },
1048
+
1049
+ merge: function(object) {
1050
+ return this.clone().update(object);
1051
+ },
1052
+
1053
+ update: function(object) {
1054
+ return new Hash(object).inject(this, function(result, pair) {
1055
+ result.set(pair.key, pair.value);
1056
+ return result;
1057
+ });
1058
+ },
1059
+
1060
+ toQueryString: function() {
1061
+ return this.map(function(pair) {
1062
+ var key = encodeURIComponent(pair.key), values = pair.value;
1063
+
1064
+ if (values && typeof values == 'object') {
1065
+ if (Object.isArray(values))
1066
+ return values.map(toQueryPair.curry(key)).join('&');
1067
+ }
1068
+ return toQueryPair(key, values);
1069
+ }).join('&');
1070
+ },
1071
+
1072
+ inspect: function() {
1073
+ return '#<Hash:{' + this.map(function(pair) {
1074
+ return pair.map(Object.inspect).join(': ');
1075
+ }).join(', ') + '}>';
1076
+ },
1077
+
1078
+ toJSON: function() {
1079
+ return Object.toJSON(this.toObject());
1080
+ },
1081
+
1082
+ clone: function() {
1083
+ return new Hash(this);
1084
+ }
1085
+ }
1086
+ })());
1087
+
1088
+ Hash.prototype.toTemplateReplacements = Hash.prototype.toObject;
1089
+ Hash.from = $H;
1090
+ var ObjectRange = Class.create(Enumerable, {
1091
+ initialize: function(start, end, exclusive) {
1092
+ this.start = start;
1093
+ this.end = end;
1094
+ this.exclusive = exclusive;
1095
+ },
1096
+
1097
+ _each: function(iterator) {
1098
+ var value = this.start;
1099
+ while (this.include(value)) {
1100
+ iterator(value);
1101
+ value = value.succ();
1102
+ }
1103
+ },
1104
+
1105
+ include: function(value) {
1106
+ if (value < this.start)
1107
+ return false;
1108
+ if (this.exclusive)
1109
+ return value < this.end;
1110
+ return value <= this.end;
1111
+ }
1112
+ });
1113
+
1114
+ var $R = function(start, end, exclusive) {
1115
+ return new ObjectRange(start, end, exclusive);
1116
+ };
1117
+
1118
+ var Ajax = {
1119
+ getTransport: function() {
1120
+ return Try.these(
1121
+ function() {return new XMLHttpRequest()},
1122
+ function() {return new ActiveXObject('Msxml2.XMLHTTP')},
1123
+ function() {return new ActiveXObject('Microsoft.XMLHTTP')}
1124
+ ) || false;
1125
+ },
1126
+
1127
+ activeRequestCount: 0
1128
+ };
1129
+
1130
+ Ajax.Responders = {
1131
+ responders: [],
1132
+
1133
+ _each: function(iterator) {
1134
+ this.responders._each(iterator);
1135
+ },
1136
+
1137
+ register: function(responder) {
1138
+ if (!this.include(responder))
1139
+ this.responders.push(responder);
1140
+ },
1141
+
1142
+ unregister: function(responder) {
1143
+ this.responders = this.responders.without(responder);
1144
+ },
1145
+
1146
+ dispatch: function(callback, request, transport, json) {
1147
+ this.each(function(responder) {
1148
+ if (Object.isFunction(responder[callback])) {
1149
+ try {
1150
+ responder[callback].apply(responder, [request, transport, json]);
1151
+ } catch (e) { }
1152
+ }
1153
+ });
1154
+ }
1155
+ };
1156
+
1157
+ Object.extend(Ajax.Responders, Enumerable);
1158
+
1159
+ Ajax.Responders.register({
1160
+ onCreate: function() { Ajax.activeRequestCount++ },
1161
+ onComplete: function() { Ajax.activeRequestCount-- }
1162
+ });
1163
+
1164
+ Ajax.Base = Class.create({
1165
+ initialize: function(options) {
1166
+ this.options = {
1167
+ method: 'post',
1168
+ asynchronous: true,
1169
+ contentType: 'application/x-www-form-urlencoded',
1170
+ encoding: 'UTF-8',
1171
+ parameters: '',
1172
+ evalJSON: true,
1173
+ evalJS: true
1174
+ };
1175
+ Object.extend(this.options, options || { });
1176
+
1177
+ this.options.method = this.options.method.toLowerCase();
1178
+
1179
+ if (Object.isString(this.options.parameters))
1180
+ this.options.parameters = this.options.parameters.toQueryParams();
1181
+ else if (Object.isHash(this.options.parameters))
1182
+ this.options.parameters = this.options.parameters.toObject();
1183
+ }
1184
+ });
1185
+
1186
+ Ajax.Request = Class.create(Ajax.Base, {
1187
+ _complete: false,
1188
+
1189
+ initialize: function($super, url, options) {
1190
+ $super(options);
1191
+ this.transport = Ajax.getTransport();
1192
+ this.request(url);
1193
+ },
1194
+
1195
+ request: function(url) {
1196
+ this.url = url;
1197
+ this.method = this.options.method;
1198
+ var params = Object.clone(this.options.parameters);
1199
+
1200
+ if (!['get', 'post'].include(this.method)) {
1201
+ // simulate other verbs over post
1202
+ params['_method'] = this.method;
1203
+ this.method = 'post';
1204
+ }
1205
+
1206
+ this.parameters = params;
1207
+
1208
+ if (params = Object.toQueryString(params)) {
1209
+ // when GET, append parameters to URL
1210
+ if (this.method == 'get')
1211
+ this.url += (this.url.include('?') ? '&' : '?') + params;
1212
+ else if (/Konqueror|Safari|KHTML/.test(navigator.userAgent))
1213
+ params += '&_=';
1214
+ }
1215
+
1216
+ try {
1217
+ var response = new Ajax.Response(this);
1218
+ if (this.options.onCreate) this.options.onCreate(response);
1219
+ Ajax.Responders.dispatch('onCreate', this, response);
1220
+
1221
+ this.transport.open(this.method.toUpperCase(), this.url,
1222
+ this.options.asynchronous);
1223
+
1224
+ if (this.options.asynchronous) this.respondToReadyState.bind(this).defer(1);
1225
+
1226
+ this.transport.onreadystatechange = this.onStateChange.bind(this);
1227
+ this.setRequestHeaders();
1228
+
1229
+ this.body = this.method == 'post' ? (this.options.postBody || params) : null;
1230
+ this.transport.send(this.body);
1231
+
1232
+ /* Force Firefox to handle ready state 4 for synchronous requests */
1233
+ if (!this.options.asynchronous && this.transport.overrideMimeType)
1234
+ this.onStateChange();
1235
+
1236
+ }
1237
+ catch (e) {
1238
+ this.dispatchException(e);
1239
+ }
1240
+ },
1241
+
1242
+ onStateChange: function() {
1243
+ var readyState = this.transport.readyState;
1244
+ if (readyState > 1 && !((readyState == 4) && this._complete))
1245
+ this.respondToReadyState(this.transport.readyState);
1246
+ },
1247
+
1248
+ setRequestHeaders: function() {
1249
+ var headers = {
1250
+ 'X-Requested-With': 'XMLHttpRequest',
1251
+ 'X-Prototype-Version': Prototype.Version,
1252
+ 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
1253
+ };
1254
+
1255
+ if (this.method == 'post') {
1256
+ headers['Content-type'] = this.options.contentType +
1257
+ (this.options.encoding ? '; charset=' + this.options.encoding : '');
1258
+
1259
+ /* Force "Connection: close" for older Mozilla browsers to work
1260
+ * around a bug where XMLHttpRequest sends an incorrect
1261
+ * Content-length header. See Mozilla Bugzilla #246651.
1262
+ */
1263
+ if (this.transport.overrideMimeType &&
1264
+ (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0,2005])[1] < 2005)
1265
+ headers['Connection'] = 'close';
1266
+ }
1267
+
1268
+ // user-defined headers
1269
+ if (typeof this.options.requestHeaders == 'object') {
1270
+ var extras = this.options.requestHeaders;
1271
+
1272
+ if (Object.isFunction(extras.push))
1273
+ for (var i = 0, length = extras.length; i < length; i += 2)
1274
+ headers[extras[i]] = extras[i+1];
1275
+ else
1276
+ $H(extras).each(function(pair) { headers[pair.key] = pair.value });
1277
+ }
1278
+
1279
+ for (var name in headers)
1280
+ this.transport.setRequestHeader(name, headers[name]);
1281
+ },
1282
+
1283
+ success: function() {
1284
+ var status = this.getStatus();
1285
+ return !status || (status >= 200 && status < 300);
1286
+ },
1287
+
1288
+ getStatus: function() {
1289
+ try {
1290
+ return this.transport.status || 0;
1291
+ } catch (e) { return 0 }
1292
+ },
1293
+
1294
+ respondToReadyState: function(readyState) {
1295
+ var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this);
1296
+
1297
+ if (state == 'Complete') {
1298
+ try {
1299
+ this._complete = true;
1300
+ (this.options['on' + response.status]
1301
+ || this.options['on' + (this.success() ? 'Success' : 'Failure')]
1302
+ || Prototype.emptyFunction)(response, response.headerJSON);
1303
+ } catch (e) {
1304
+ this.dispatchException(e);
1305
+ }
1306
+
1307
+ var contentType = response.getHeader('Content-type');
1308
+ if (this.options.evalJS == 'force'
1309
+ || (this.options.evalJS && contentType
1310
+ && contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i)))
1311
+ this.evalResponse();
1312
+ }
1313
+
1314
+ try {
1315
+ (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON);
1316
+ Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON);
1317
+ } catch (e) {
1318
+ this.dispatchException(e);
1319
+ }
1320
+
1321
+ if (state == 'Complete') {
1322
+ // avoid memory leak in MSIE: clean up
1323
+ this.transport.onreadystatechange = Prototype.emptyFunction;
1324
+ }
1325
+ },
1326
+
1327
+ getHeader: function(name) {
1328
+ try {
1329
+ return this.transport.getResponseHeader(name);
1330
+ } catch (e) { return null }
1331
+ },
1332
+
1333
+ evalResponse: function() {
1334
+ try {
1335
+ return eval((this.transport.responseText || '').unfilterJSON());
1336
+ } catch (e) {
1337
+ this.dispatchException(e);
1338
+ }
1339
+ },
1340
+
1341
+ dispatchException: function(exception) {
1342
+ (this.options.onException || Prototype.emptyFunction)(this, exception);
1343
+ Ajax.Responders.dispatch('onException', this, exception);
1344
+ }
1345
+ });
1346
+
1347
+ Ajax.Request.Events =
1348
+ ['Uninitialized', 'Loading', 'Loaded', 'Interactive', 'Complete'];
1349
+
1350
+ Ajax.Response = Class.create({
1351
+ initialize: function(request){
1352
+ this.request = request;
1353
+ var transport = this.transport = request.transport,
1354
+ readyState = this.readyState = transport.readyState;
1355
+
1356
+ if((readyState > 2 && !Prototype.Browser.IE) || readyState == 4) {
1357
+ this.status = this.getStatus();
1358
+ this.statusText = this.getStatusText();
1359
+ this.responseText = String.interpret(transport.responseText);
1360
+ this.headerJSON = this._getHeaderJSON();
1361
+ }
1362
+
1363
+ if(readyState == 4) {
1364
+ var xml = transport.responseXML;
1365
+ this.responseXML = Object.isUndefined(xml) ? null : xml;
1366
+ this.responseJSON = this._getResponseJSON();
1367
+ }
1368
+ },
1369
+
1370
+ status: 0,
1371
+ statusText: '',
1372
+
1373
+ getStatus: Ajax.Request.prototype.getStatus,
1374
+
1375
+ getStatusText: function() {
1376
+ try {
1377
+ return this.transport.statusText || '';
1378
+ } catch (e) { return '' }
1379
+ },
1380
+
1381
+ getHeader: Ajax.Request.prototype.getHeader,
1382
+
1383
+ getAllHeaders: function() {
1384
+ try {
1385
+ return this.getAllResponseHeaders();
1386
+ } catch (e) { return null }
1387
+ },
1388
+
1389
+ getResponseHeader: function(name) {
1390
+ return this.transport.getResponseHeader(name);
1391
+ },
1392
+
1393
+ getAllResponseHeaders: function() {
1394
+ return this.transport.getAllResponseHeaders();
1395
+ },
1396
+
1397
+ _getHeaderJSON: function() {
1398
+ var json = this.getHeader('X-JSON');
1399
+ if (!json) return null;
1400
+ json = decodeURIComponent(escape(json));
1401
+ try {
1402
+ return json.evalJSON(this.request.options.sanitizeJSON);
1403
+ } catch (e) {
1404
+ this.request.dispatchException(e);
1405
+ }
1406
+ },
1407
+
1408
+ _getResponseJSON: function() {
1409
+ var options = this.request.options;
1410
+ if (!options.evalJSON || (options.evalJSON != 'force' &&
1411
+ !(this.getHeader('Content-type') || '').include('application/json')) ||
1412
+ this.responseText.blank())
1413
+ return null;
1414
+ try {
1415
+ return this.responseText.evalJSON(options.sanitizeJSON);
1416
+ } catch (e) {
1417
+ this.request.dispatchException(e);
1418
+ }
1419
+ }
1420
+ });
1421
+
1422
+ Ajax.Updater = Class.create(Ajax.Request, {
1423
+ initialize: function($super, container, url, options) {
1424
+ this.container = {
1425
+ success: (container.success || container),
1426
+ failure: (container.failure || (container.success ? null : container))
1427
+ };
1428
+
1429
+ options = Object.clone(options);
1430
+ var onComplete = options.onComplete;
1431
+ options.onComplete = (function(response, json) {
1432
+ this.updateContent(response.responseText);
1433
+ if (Object.isFunction(onComplete)) onComplete(response, json);
1434
+ }).bind(this);
1435
+
1436
+ $super(url, options);
1437
+ },
1438
+
1439
+ updateContent: function(responseText) {
1440
+ var receiver = this.container[this.success() ? 'success' : 'failure'],
1441
+ options = this.options;
1442
+
1443
+ if (!options.evalScripts) responseText = responseText.stripScripts();
1444
+
1445
+ if (receiver = $(receiver)) {
1446
+ if (options.insertion) {
1447
+ if (Object.isString(options.insertion)) {
1448
+ var insertion = { }; insertion[options.insertion] = responseText;
1449
+ receiver.insert(insertion);
1450
+ }
1451
+ else options.insertion(receiver, responseText);
1452
+ }
1453
+ else receiver.update(responseText);
1454
+ }
1455
+ }
1456
+ });
1457
+
1458
+ Ajax.PeriodicalUpdater = Class.create(Ajax.Base, {
1459
+ initialize: function($super, container, url, options) {
1460
+ $super(options);
1461
+ this.onComplete = this.options.onComplete;
1462
+
1463
+ this.frequency = (this.options.frequency || 2);
1464
+ this.decay = (this.options.decay || 1);
1465
+
1466
+ this.updater = { };
1467
+ this.container = container;
1468
+ this.url = url;
1469
+
1470
+ this.start();
1471
+ },
1472
+
1473
+ start: function() {
1474
+ this.options.onComplete = this.updateComplete.bind(this);
1475
+ this.onTimerEvent();
1476
+ },
1477
+
1478
+ stop: function() {
1479
+ this.updater.options.onComplete = undefined;
1480
+ clearTimeout(this.timer);
1481
+ (this.onComplete || Prototype.emptyFunction).apply(this, arguments);
1482
+ },
1483
+
1484
+ updateComplete: function(response) {
1485
+ if (this.options.decay) {
1486
+ this.decay = (response.responseText == this.lastText ?
1487
+ this.decay * this.options.decay : 1);
1488
+
1489
+ this.lastText = response.responseText;
1490
+ }
1491
+ this.timer = this.onTimerEvent.bind(this).delay(this.decay * this.frequency);
1492
+ },
1493
+
1494
+ onTimerEvent: function() {
1495
+ this.updater = new Ajax.Updater(this.container, this.url, this.options);
1496
+ }
1497
+ });
1498
+ function $(element) {
1499
+ if (arguments.length > 1) {
1500
+ for (var i = 0, elements = [], length = arguments.length; i < length; i++)
1501
+ elements.push($(arguments[i]));
1502
+ return elements;
1503
+ }
1504
+ if (Object.isString(element))
1505
+ element = document.getElementById(element);
1506
+ return Element.extend(element);
1507
+ }
1508
+
1509
+ if (Prototype.BrowserFeatures.XPath) {
1510
+ document._getElementsByXPath = function(expression, parentElement) {
1511
+ var results = [];
1512
+ var query = document.evaluate(expression, $(parentElement) || document,
1513
+ null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
1514
+ for (var i = 0, length = query.snapshotLength; i < length; i++)
1515
+ results.push(Element.extend(query.snapshotItem(i)));
1516
+ return results;
1517
+ };
1518
+ }
1519
+
1520
+ /*--------------------------------------------------------------------------*/
1521
+
1522
+ if (!window.Node) var Node = { };
1523
+
1524
+ if (!Node.ELEMENT_NODE) {
1525
+ // DOM level 2 ECMAScript Language Binding
1526
+ Object.extend(Node, {
1527
+ ELEMENT_NODE: 1,
1528
+ ATTRIBUTE_NODE: 2,
1529
+ TEXT_NODE: 3,
1530
+ CDATA_SECTION_NODE: 4,
1531
+ ENTITY_REFERENCE_NODE: 5,
1532
+ ENTITY_NODE: 6,
1533
+ PROCESSING_INSTRUCTION_NODE: 7,
1534
+ COMMENT_NODE: 8,
1535
+ DOCUMENT_NODE: 9,
1536
+ DOCUMENT_TYPE_NODE: 10,
1537
+ DOCUMENT_FRAGMENT_NODE: 11,
1538
+ NOTATION_NODE: 12
1539
+ });
1540
+ }
1541
+
1542
+ (function() {
1543
+ var element = this.Element;
1544
+ this.Element = function(tagName, attributes) {
1545
+ attributes = attributes || { };
1546
+ tagName = tagName.toLowerCase();
1547
+ var cache = Element.cache;
1548
+ if (Prototype.Browser.IE && attributes.name) {
1549
+ tagName = '<' + tagName + ' name="' + attributes.name + '">';
1550
+ delete attributes.name;
1551
+ return Element.writeAttribute(document.createElement(tagName), attributes);
1552
+ }
1553
+ if (!cache[tagName]) cache[tagName] = Element.extend(document.createElement(tagName));
1554
+ return Element.writeAttribute(cache[tagName].cloneNode(false), attributes);
1555
+ };
1556
+ Object.extend(this.Element, element || { });
1557
+ }).call(window);
1558
+
1559
+ Element.cache = { };
1560
+
1561
+ Element.Methods = {
1562
+ visible: function(element) {
1563
+ return $(element).style.display != 'none';
1564
+ },
1565
+
1566
+ toggle: function(element) {
1567
+ element = $(element);
1568
+ Element[Element.visible(element) ? 'hide' : 'show'](element);
1569
+ return element;
1570
+ },
1571
+
1572
+ hide: function(element) {
1573
+ $(element).style.display = 'none';
1574
+ return element;
1575
+ },
1576
+
1577
+ show: function(element) {
1578
+ $(element).style.display = '';
1579
+ return element;
1580
+ },
1581
+
1582
+ remove: function(element) {
1583
+ element = $(element);
1584
+ element.parentNode.removeChild(element);
1585
+ return element;
1586
+ },
1587
+
1588
+ update: function(element, content) {
1589
+ element = $(element);
1590
+ if (content && content.toElement) content = content.toElement();
1591
+ if (Object.isElement(content)) return element.update().insert(content);
1592
+ content = Object.toHTML(content);
1593
+ element.innerHTML = content.stripScripts();
1594
+ content.evalScripts.bind(content).defer();
1595
+ return element;
1596
+ },
1597
+
1598
+ replace: function(element, content) {
1599
+ element = $(element);
1600
+ if (content && content.toElement) content = content.toElement();
1601
+ else if (!Object.isElement(content)) {
1602
+ content = Object.toHTML(content);
1603
+ var range = element.ownerDocument.createRange();
1604
+ range.selectNode(element);
1605
+ content.evalScripts.bind(content).defer();
1606
+ content = range.createContextualFragment(content.stripScripts());
1607
+ }
1608
+ element.parentNode.replaceChild(content, element);
1609
+ return element;
1610
+ },
1611
+
1612
+ insert: function(element, insertions) {
1613
+ element = $(element);
1614
+
1615
+ if (Object.isString(insertions) || Object.isNumber(insertions) ||
1616
+ Object.isElement(insertions) || (insertions && (insertions.toElement || insertions.toHTML)))
1617
+ insertions = {bottom:insertions};
1618
+
1619
+ var content, t, range;
1620
+
1621
+ for (position in insertions) {
1622
+ content = insertions[position];
1623
+ position = position.toLowerCase();
1624
+ t = Element._insertionTranslations[position];
1625
+
1626
+ if (content && content.toElement) content = content.toElement();
1627
+ if (Object.isElement(content)) {
1628
+ t.insert(element, content);
1629
+ continue;
1630
+ }
1631
+
1632
+ content = Object.toHTML(content);
1633
+
1634
+ range = element.ownerDocument.createRange();
1635
+ t.initializeRange(element, range);
1636
+ t.insert(element, range.createContextualFragment(content.stripScripts()));
1637
+
1638
+ content.evalScripts.bind(content).defer();
1639
+ }
1640
+
1641
+ return element;
1642
+ },
1643
+
1644
+ wrap: function(element, wrapper, attributes) {
1645
+ element = $(element);
1646
+ if (Object.isElement(wrapper))
1647
+ $(wrapper).writeAttribute(attributes || { });
1648
+ else if (Object.isString(wrapper)) wrapper = new Element(wrapper, attributes);
1649
+ else wrapper = new Element('div', wrapper);
1650
+ if (element.parentNode)
1651
+ element.parentNode.replaceChild(wrapper, element);
1652
+ wrapper.appendChild(element);
1653
+ return wrapper;
1654
+ },
1655
+
1656
+ inspect: function(element) {
1657
+ element = $(element);
1658
+ var result = '<' + element.tagName.toLowerCase();
1659
+ $H({'id': 'id', 'className': 'class'}).each(function(pair) {
1660
+ var property = pair.first(), attribute = pair.last();
1661
+ var value = (element[property] || '').toString();
1662
+ if (value) result += ' ' + attribute + '=' + value.inspect(true);
1663
+ });
1664
+ return result + '>';
1665
+ },
1666
+
1667
+ recursivelyCollect: function(element, property) {
1668
+ element = $(element);
1669
+ var elements = [];
1670
+ while (element = element[property])
1671
+ if (element.nodeType == 1)
1672
+ elements.push(Element.extend(element));
1673
+ return elements;
1674
+ },
1675
+
1676
+ ancestors: function(element) {
1677
+ return $(element).recursivelyCollect('parentNode');
1678
+ },
1679
+
1680
+ descendants: function(element) {
1681
+ return $(element).getElementsBySelector("*");
1682
+ },
1683
+
1684
+ firstDescendant: function(element) {
1685
+ element = $(element).firstChild;
1686
+ while (element && element.nodeType != 1) element = element.nextSibling;
1687
+ return $(element);
1688
+ },
1689
+
1690
+ immediateDescendants: function(element) {
1691
+ if (!(element = $(element).firstChild)) return [];
1692
+ while (element && element.nodeType != 1) element = element.nextSibling;
1693
+ if (element) return [element].concat($(element).nextSiblings());
1694
+ return [];
1695
+ },
1696
+
1697
+ previousSiblings: function(element) {
1698
+ return $(element).recursivelyCollect('previousSibling');
1699
+ },
1700
+
1701
+ nextSiblings: function(element) {
1702
+ return $(element).recursivelyCollect('nextSibling');
1703
+ },
1704
+
1705
+ siblings: function(element) {
1706
+ element = $(element);
1707
+ return element.previousSiblings().reverse().concat(element.nextSiblings());
1708
+ },
1709
+
1710
+ match: function(element, selector) {
1711
+ if (Object.isString(selector))
1712
+ selector = new Selector(selector);
1713
+ return selector.match($(element));
1714
+ },
1715
+
1716
+ up: function(element, expression, index) {
1717
+ element = $(element);
1718
+ if (arguments.length == 1) return $(element.parentNode);
1719
+ var ancestors = element.ancestors();
1720
+ return expression ? Selector.findElement(ancestors, expression, index) :
1721
+ ancestors[index || 0];
1722
+ },
1723
+
1724
+ down: function(element, expression, index) {
1725
+ element = $(element);
1726
+ if (arguments.length == 1) return element.firstDescendant();
1727
+ var descendants = element.descendants();
1728
+ return expression ? Selector.findElement(descendants, expression, index) :
1729
+ descendants[index || 0];
1730
+ },
1731
+
1732
+ previous: function(element, expression, index) {
1733
+ element = $(element);
1734
+ if (arguments.length == 1) return $(Selector.handlers.previousElementSibling(element));
1735
+ var previousSiblings = element.previousSiblings();
1736
+ return expression ? Selector.findElement(previousSiblings, expression, index) :
1737
+ previousSiblings[index || 0];
1738
+ },
1739
+
1740
+ next: function(element, expression, index) {
1741
+ element = $(element);
1742
+ if (arguments.length == 1) return $(Selector.handlers.nextElementSibling(element));
1743
+ var nextSiblings = element.nextSiblings();
1744
+ return expression ? Selector.findElement(nextSiblings, expression, index) :
1745
+ nextSiblings[index || 0];
1746
+ },
1747
+
1748
+ select: function() {
1749
+ var args = $A(arguments), element = $(args.shift());
1750
+ return Selector.findChildElements(element, args);
1751
+ },
1752
+
1753
+ adjacent: function() {
1754
+ var args = $A(arguments), element = $(args.shift());
1755
+ return Selector.findChildElements(element.parentNode, args).without(element);
1756
+ },
1757
+
1758
+ identify: function(element) {
1759
+ element = $(element);
1760
+ var id = element.readAttribute('id'), self = arguments.callee;
1761
+ if (id) return id;
1762
+ do { id = 'anonymous_element_' + self.counter++ } while ($(id));
1763
+ element.writeAttribute('id', id);
1764
+ return id;
1765
+ },
1766
+
1767
+ readAttribute: function(element, name) {
1768
+ element = $(element);
1769
+ if (Prototype.Browser.IE) {
1770
+ var t = Element._attributeTranslations.read;
1771
+ if (t.values[name]) return t.values[name](element, name);
1772
+ if (t.names[name]) name = t.names[name];
1773
+ if (name.include(':')) {
1774
+ return (!element.attributes || !element.attributes[name]) ? null :
1775
+ element.attributes[name].value;
1776
+ }
1777
+ }
1778
+ return element.getAttribute(name);
1779
+ },
1780
+
1781
+ writeAttribute: function(element, name, value) {
1782
+ element = $(element);
1783
+ var attributes = { }, t = Element._attributeTranslations.write;
1784
+
1785
+ if (typeof name == 'object') attributes = name;
1786
+ else attributes[name] = Object.isUndefined(value) ? true : value;
1787
+
1788
+ for (var attr in attributes) {
1789
+ name = t.names[attr] || attr;
1790
+ value = attributes[attr];
1791
+ if (t.values[attr]) name = t.values[attr](element, value);
1792
+ if (value === false || value === null)
1793
+ element.removeAttribute(name);
1794
+ else if (value === true)
1795
+ element.setAttribute(name, name);
1796
+ else element.setAttribute(name, value);
1797
+ }
1798
+ return element;
1799
+ },
1800
+
1801
+ getHeight: function(element) {
1802
+ return $(element).getDimensions().height;
1803
+ },
1804
+
1805
+ getWidth: function(element) {
1806
+ return $(element).getDimensions().width;
1807
+ },
1808
+
1809
+ classNames: function(element) {
1810
+ return new Element.ClassNames(element);
1811
+ },
1812
+
1813
+ hasClassName: function(element, className) {
1814
+ if (!(element = $(element))) return;
1815
+ var elementClassName = element.className;
1816
+ return (elementClassName.length > 0 && (elementClassName == className ||
1817
+ new RegExp("(^|\\s)" + className + "(\\s|$)").test(elementClassName)));
1818
+ },
1819
+
1820
+ addClassName: function(element, className) {
1821
+ if (!(element = $(element))) return;
1822
+ if (!element.hasClassName(className))
1823
+ element.className += (element.className ? ' ' : '') + className;
1824
+ return element;
1825
+ },
1826
+
1827
+ removeClassName: function(element, className) {
1828
+ if (!(element = $(element))) return;
1829
+ element.className = element.className.replace(
1830
+ new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ').strip();
1831
+ return element;
1832
+ },
1833
+
1834
+ toggleClassName: function(element, className) {
1835
+ if (!(element = $(element))) return;
1836
+ return element[element.hasClassName(className) ?
1837
+ 'removeClassName' : 'addClassName'](className);
1838
+ },
1839
+
1840
+ // removes whitespace-only text node children
1841
+ cleanWhitespace: function(element) {
1842
+ element = $(element);
1843
+ var node = element.firstChild;
1844
+ while (node) {
1845
+ var nextNode = node.nextSibling;
1846
+ if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
1847
+ element.removeChild(node);
1848
+ node = nextNode;
1849
+ }
1850
+ return element;
1851
+ },
1852
+
1853
+ empty: function(element) {
1854
+ return $(element).innerHTML.blank();
1855
+ },
1856
+
1857
+ descendantOf: function(element, ancestor) {
1858
+ element = $(element), ancestor = $(ancestor);
1859
+ var originalAncestor = ancestor;
1860
+
1861
+ if (element.compareDocumentPosition)
1862
+ return (element.compareDocumentPosition(ancestor) & 8) === 8;
1863
+
1864
+ if (element.sourceIndex && !Prototype.Browser.Opera) {
1865
+ var e = element.sourceIndex, a = ancestor.sourceIndex,
1866
+ nextAncestor = ancestor.nextSibling;
1867
+ if (!nextAncestor) {
1868
+ do { ancestor = ancestor.parentNode; }
1869
+ while (!(nextAncestor = ancestor.nextSibling) && ancestor.parentNode);
1870
+ }
1871
+ if (nextAncestor) return (e > a && e < nextAncestor.sourceIndex);
1872
+ }
1873
+
1874
+ while (element = element.parentNode)
1875
+ if (element == originalAncestor) return true;
1876
+ return false;
1877
+ },
1878
+
1879
+ scrollTo: function(element) {
1880
+ element = $(element);
1881
+ var pos = element.cumulativeOffset();
1882
+ window.scrollTo(pos[0], pos[1]);
1883
+ return element;
1884
+ },
1885
+
1886
+ getStyle: function(element, style) {
1887
+ element = $(element);
1888
+ style = style == 'float' ? 'cssFloat' : style.camelize();
1889
+ var value = element.style[style];
1890
+ if (!value) {
1891
+ var css = document.defaultView.getComputedStyle(element, null);
1892
+ value = css ? css[style] : null;
1893
+ }
1894
+ if (style == 'opacity') return value ? parseFloat(value) : 1.0;
1895
+ return value == 'auto' ? null : value;
1896
+ },
1897
+
1898
+ getOpacity: function(element) {
1899
+ return $(element).getStyle('opacity');
1900
+ },
1901
+
1902
+ setStyle: function(element, styles) {
1903
+ element = $(element);
1904
+ var elementStyle = element.style, match;
1905
+ if (Object.isString(styles)) {
1906
+ element.style.cssText += ';' + styles;
1907
+ return styles.include('opacity') ?
1908
+ element.setOpacity(styles.match(/opacity:\s*(\d?\.?\d*)/)[1]) : element;
1909
+ }
1910
+ for (var property in styles)
1911
+ if (property == 'opacity') element.setOpacity(styles[property]);
1912
+ else
1913
+ elementStyle[(property == 'float' || property == 'cssFloat') ?
1914
+ (Object.isUndefined(elementStyle.styleFloat) ? 'cssFloat' : 'styleFloat') :
1915
+ property] = styles[property];
1916
+
1917
+ return element;
1918
+ },
1919
+
1920
+ setOpacity: function(element, value) {
1921
+ element = $(element);
1922
+ element.style.opacity = (value == 1 || value === '') ? '' :
1923
+ (value < 0.00001) ? 0 : value;
1924
+ return element;
1925
+ },
1926
+
1927
+ getDimensions: function(element) {
1928
+ element = $(element);
1929
+ var display = $(element).getStyle('display');
1930
+ if (display != 'none' && display != null) // Safari bug
1931
+ return {width: element.offsetWidth, height: element.offsetHeight};
1932
+
1933
+ // All *Width and *Height properties give 0 on elements with display none,
1934
+ // so enable the element temporarily
1935
+ var els = element.style;
1936
+ var originalVisibility = els.visibility;
1937
+ var originalPosition = els.position;
1938
+ var originalDisplay = els.display;
1939
+ els.visibility = 'hidden';
1940
+ els.position = 'absolute';
1941
+ els.display = 'block';
1942
+ var originalWidth = element.clientWidth;
1943
+ var originalHeight = element.clientHeight;
1944
+ els.display = originalDisplay;
1945
+ els.position = originalPosition;
1946
+ els.visibility = originalVisibility;
1947
+ return {width: originalWidth, height: originalHeight};
1948
+ },
1949
+
1950
+ makePositioned: function(element) {
1951
+ element = $(element);
1952
+ var pos = Element.getStyle(element, 'position');
1953
+ if (pos == 'static' || !pos) {
1954
+ element._madePositioned = true;
1955
+ element.style.position = 'relative';
1956
+ // Opera returns the offset relative to the positioning context, when an
1957
+ // element is position relative but top and left have not been defined
1958
+ if (window.opera) {
1959
+ element.style.top = 0;
1960
+ element.style.left = 0;
1961
+ }
1962
+ }
1963
+ return element;
1964
+ },
1965
+
1966
+ undoPositioned: function(element) {
1967
+ element = $(element);
1968
+ if (element._madePositioned) {
1969
+ element._madePositioned = undefined;
1970
+ element.style.position =
1971
+ element.style.top =
1972
+ element.style.left =
1973
+ element.style.bottom =
1974
+ element.style.right = '';
1975
+ }
1976
+ return element;
1977
+ },
1978
+
1979
+ makeClipping: function(element) {
1980
+ element = $(element);
1981
+ if (element._overflow) return element;
1982
+ element._overflow = Element.getStyle(element, 'overflow') || 'auto';
1983
+ if (element._overflow !== 'hidden')
1984
+ element.style.overflow = 'hidden';
1985
+ return element;
1986
+ },
1987
+
1988
+ undoClipping: function(element) {
1989
+ element = $(element);
1990
+ if (!element._overflow) return element;
1991
+ element.style.overflow = element._overflow == 'auto' ? '' : element._overflow;
1992
+ element._overflow = null;
1993
+ return element;
1994
+ },
1995
+
1996
+ cumulativeOffset: function(element) {
1997
+ var valueT = 0, valueL = 0;
1998
+ do {
1999
+ valueT += element.offsetTop || 0;
2000
+ valueL += element.offsetLeft || 0;
2001
+ element = element.offsetParent;
2002
+ } while (element);
2003
+ return Element._returnOffset(valueL, valueT);
2004
+ },
2005
+
2006
+ positionedOffset: function(element) {
2007
+ var valueT = 0, valueL = 0;
2008
+ do {
2009
+ valueT += element.offsetTop || 0;
2010
+ valueL += element.offsetLeft || 0;
2011
+ element = element.offsetParent;
2012
+ if (element) {
2013
+ if (element.tagName == 'BODY') break;
2014
+ var p = Element.getStyle(element, 'position');
2015
+ if (p == 'relative' || p == 'absolute') break;
2016
+ }
2017
+ } while (element);
2018
+ return Element._returnOffset(valueL, valueT);
2019
+ },
2020
+
2021
+ absolutize: function(element) {
2022
+ element = $(element);
2023
+ if (element.getStyle('position') == 'absolute') return;
2024
+ // Position.prepare(); // To be done manually by Scripty when it needs it.
2025
+
2026
+ var offsets = element.positionedOffset();
2027
+ var top = offsets[1];
2028
+ var left = offsets[0];
2029
+ var width = element.clientWidth;
2030
+ var height = element.clientHeight;
2031
+
2032
+ element._originalLeft = left - parseFloat(element.style.left || 0);
2033
+ element._originalTop = top - parseFloat(element.style.top || 0);
2034
+ element._originalWidth = element.style.width;
2035
+ element._originalHeight = element.style.height;
2036
+
2037
+ element.style.position = 'absolute';
2038
+ element.style.top = top + 'px';
2039
+ element.style.left = left + 'px';
2040
+ element.style.width = width + 'px';
2041
+ element.style.height = height + 'px';
2042
+ return element;
2043
+ },
2044
+
2045
+ relativize: function(element) {
2046
+ element = $(element);
2047
+ if (element.getStyle('position') == 'relative') return;
2048
+ // Position.prepare(); // To be done manually by Scripty when it needs it.
2049
+
2050
+ element.style.position = 'relative';
2051
+ var top = parseFloat(element.style.top || 0) - (element._originalTop || 0);
2052
+ var left = parseFloat(element.style.left || 0) - (element._originalLeft || 0);
2053
+
2054
+ element.style.top = top + 'px';
2055
+ element.style.left = left + 'px';
2056
+ element.style.height = element._originalHeight;
2057
+ element.style.width = element._originalWidth;
2058
+ return element;
2059
+ },
2060
+
2061
+ cumulativeScrollOffset: function(element) {
2062
+ var valueT = 0, valueL = 0;
2063
+ do {
2064
+ valueT += element.scrollTop || 0;
2065
+ valueL += element.scrollLeft || 0;
2066
+ element = element.parentNode;
2067
+ } while (element);
2068
+ return Element._returnOffset(valueL, valueT);
2069
+ },
2070
+
2071
+ getOffsetParent: function(element) {
2072
+ if (element.offsetParent) return $(element.offsetParent);
2073
+ if (element == document.body) return $(element);
2074
+
2075
+ while ((element = element.parentNode) && element != document.body)
2076
+ if (Element.getStyle(element, 'position') != 'static')
2077
+ return $(element);
2078
+
2079
+ return $(document.body);
2080
+ },
2081
+
2082
+ viewportOffset: function(forElement) {
2083
+ var valueT = 0, valueL = 0;
2084
+
2085
+ var element = forElement;
2086
+ do {
2087
+ valueT += element.offsetTop || 0;
2088
+ valueL += element.offsetLeft || 0;
2089
+
2090
+ // Safari fix
2091
+ if (element.offsetParent == document.body &&
2092
+ Element.getStyle(element, 'position') == 'absolute') break;
2093
+
2094
+ } while (element = element.offsetParent);
2095
+
2096
+ element = forElement;
2097
+ do {
2098
+ if (!Prototype.Browser.Opera || element.tagName == 'BODY') {
2099
+ valueT -= element.scrollTop || 0;
2100
+ valueL -= element.scrollLeft || 0;
2101
+ }
2102
+ } while (element = element.parentNode);
2103
+
2104
+ return Element._returnOffset(valueL, valueT);
2105
+ },
2106
+
2107
+ clonePosition: function(element, source) {
2108
+ var options = Object.extend({
2109
+ setLeft: true,
2110
+ setTop: true,
2111
+ setWidth: true,
2112
+ setHeight: true,
2113
+ offsetTop: 0,
2114
+ offsetLeft: 0
2115
+ }, arguments[2] || { });
2116
+
2117
+ // find page position of source
2118
+ source = $(source);
2119
+ var p = source.viewportOffset();
2120
+
2121
+ // find coordinate system to use
2122
+ element = $(element);
2123
+ var delta = [0, 0];
2124
+ var parent = null;
2125
+ // delta [0,0] will do fine with position: fixed elements,
2126
+ // position:absolute needs offsetParent deltas
2127
+ if (Element.getStyle(element, 'position') == 'absolute') {
2128
+ parent = element.getOffsetParent();
2129
+ delta = parent.viewportOffset();
2130
+ }
2131
+
2132
+ // correct by body offsets (fixes Safari)
2133
+ if (parent == document.body) {
2134
+ delta[0] -= document.body.offsetLeft;
2135
+ delta[1] -= document.body.offsetTop;
2136
+ }
2137
+
2138
+ // set position
2139
+ if (options.setLeft) element.style.left = (p[0] - delta[0] + options.offsetLeft) + 'px';
2140
+ if (options.setTop) element.style.top = (p[1] - delta[1] + options.offsetTop) + 'px';
2141
+ if (options.setWidth) element.style.width = source.offsetWidth + 'px';
2142
+ if (options.setHeight) element.style.height = source.offsetHeight + 'px';
2143
+ return element;
2144
+ }
2145
+ };
2146
+
2147
+ Element.Methods.identify.counter = 1;
2148
+
2149
+ Object.extend(Element.Methods, {
2150
+ getElementsBySelector: Element.Methods.select,
2151
+ childElements: Element.Methods.immediateDescendants
2152
+ });
2153
+
2154
+ Element._attributeTranslations = {
2155
+ write: {
2156
+ names: {
2157
+ className: 'class',
2158
+ htmlFor: 'for'
2159
+ },
2160
+ values: { }
2161
+ }
2162
+ };
2163
+
2164
+
2165
+ if (!document.createRange || Prototype.Browser.Opera) {
2166
+ Element.Methods.insert = function(element, insertions) {
2167
+ element = $(element);
2168
+
2169
+ if (Object.isString(insertions) || Object.isNumber(insertions) ||
2170
+ Object.isElement(insertions) || (insertions && (insertions.toElement || insertions.toHTML)))
2171
+ insertions = { bottom: insertions };
2172
+
2173
+ var t = Element._insertionTranslations, content, position, pos, tagName;
2174
+
2175
+ for (position in insertions) {
2176
+ content = insertions[position];
2177
+ position = position.toLowerCase();
2178
+ pos = t[position];
2179
+
2180
+ if (content && content.toElement) content = content.toElement();
2181
+ if (Object.isElement(content)) {
2182
+ pos.insert(element, content);
2183
+ continue;
2184
+ }
2185
+
2186
+ content = Object.toHTML(content);
2187
+ tagName = ((position == 'before' || position == 'after')
2188
+ ? element.parentNode : element).tagName.toUpperCase();
2189
+
2190
+ if (t.tags[tagName]) {
2191
+ var fragments = Element._getContentFromAnonymousElement(tagName, content.stripScripts());
2192
+ if (position == 'top' || position == 'after') fragments.reverse();
2193
+ fragments.each(pos.insert.curry(element));
2194
+ }
2195
+ else element.insertAdjacentHTML(pos.adjacency, content.stripScripts());
2196
+
2197
+ content.evalScripts.bind(content).defer();
2198
+ }
2199
+
2200
+ return element;
2201
+ };
2202
+ }
2203
+
2204
+ if (Prototype.Browser.Opera) {
2205
+ Element.Methods.getStyle = Element.Methods.getStyle.wrap(
2206
+ function(proceed, element, style) {
2207
+ switch (style) {
2208
+ case 'left': case 'top': case 'right': case 'bottom':
2209
+ if (proceed(element, 'position') === 'static') return null;
2210
+ case 'height': case 'width':
2211
+ // returns '0px' for hidden elements; we want it to return null
2212
+ if (!Element.visible(element)) return null;
2213
+
2214
+ // returns the border-box dimensions rather than the content-box
2215
+ // dimensions, so we subtract padding and borders from the value
2216
+ var dim = parseInt(proceed(element, style), 10);
2217
+
2218
+ if (dim !== element['offset' + style.capitalize()])
2219
+ return dim + 'px';
2220
+
2221
+ var properties;
2222
+ if (style === 'height') {
2223
+ properties = ['border-top-width', 'padding-top',
2224
+ 'padding-bottom', 'border-bottom-width'];
2225
+ }
2226
+ else {
2227
+ properties = ['border-left-width', 'padding-left',
2228
+ 'padding-right', 'border-right-width'];
2229
+ }
2230
+ return properties.inject(dim, function(memo, property) {
2231
+ var val = proceed(element, property);
2232
+ return val === null ? memo : memo - parseInt(val, 10);
2233
+ }) + 'px';
2234
+ default: return proceed(element, style);
2235
+ }
2236
+ }
2237
+ );
2238
+
2239
+ Element.Methods.readAttribute = Element.Methods.readAttribute.wrap(
2240
+ function(proceed, element, attribute) {
2241
+ if (attribute === 'title') return element.title;
2242
+ return proceed(element, attribute);
2243
+ }
2244
+ );
2245
+ }
2246
+
2247
+ else if (Prototype.Browser.IE) {
2248
+ $w('positionedOffset getOffsetParent viewportOffset').each(function(method) {
2249
+ Element.Methods[method] = Element.Methods[method].wrap(
2250
+ function(proceed, element) {
2251
+ element = $(element);
2252
+ var position = element.getStyle('position');
2253
+ if (position != 'static') return proceed(element);
2254
+ element.setStyle({ position: 'relative' });
2255
+ var value = proceed(element);
2256
+ element.setStyle({ position: position });
2257
+ return value;
2258
+ }
2259
+ );
2260
+ });
2261
+
2262
+ Element.Methods.getStyle = function(element, style) {
2263
+ element = $(element);
2264
+ style = (style == 'float' || style == 'cssFloat') ? 'styleFloat' : style.camelize();
2265
+ var value = element.style[style];
2266
+ if (!value && element.currentStyle) value = element.currentStyle[style];
2267
+
2268
+ if (style == 'opacity') {
2269
+ if (value = (element.getStyle('filter') || '').match(/alpha\(opacity=(.*)\)/))
2270
+ if (value[1]) return parseFloat(value[1]) / 100;
2271
+ return 1.0;
2272
+ }
2273
+
2274
+ if (value == 'auto') {
2275
+ if ((style == 'width' || style == 'height') && (element.getStyle('display') != 'none'))
2276
+ return element['offset' + style.capitalize()] + 'px';
2277
+ return null;
2278
+ }
2279
+ return value;
2280
+ };
2281
+
2282
+ Element.Methods.setOpacity = function(element, value) {
2283
+ function stripAlpha(filter){
2284
+ return filter.replace(/alpha\([^\)]*\)/gi,'');
2285
+ }
2286
+ element = $(element);
2287
+ var currentStyle = element.currentStyle;
2288
+ if ((currentStyle && !currentStyle.hasLayout) ||
2289
+ (!currentStyle && element.style.zoom == 'normal'))
2290
+ element.style.zoom = 1;
2291
+
2292
+ var filter = element.getStyle('filter'), style = element.style;
2293
+ if (value == 1 || value === '') {
2294
+ (filter = stripAlpha(filter)) ?
2295
+ style.filter = filter : style.removeAttribute('filter');
2296
+ return element;
2297
+ } else if (value < 0.00001) value = 0;
2298
+ style.filter = stripAlpha(filter) +
2299
+ 'alpha(opacity=' + (value * 100) + ')';
2300
+ return element;
2301
+ };
2302
+
2303
+ Element._attributeTranslations = {
2304
+ read: {
2305
+ names: {
2306
+ 'class': 'className',
2307
+ 'for': 'htmlFor'
2308
+ },
2309
+ values: {
2310
+ _getAttr: function(element, attribute) {
2311
+ return element.getAttribute(attribute, 2);
2312
+ },
2313
+ _getAttrNode: function(element, attribute) {
2314
+ var node = element.getAttributeNode(attribute);
2315
+ return node ? node.value : "";
2316
+ },
2317
+ _getEv: function(element, attribute) {
2318
+ attribute = element.getAttribute(attribute);
2319
+ return attribute ? attribute.toString().slice(23, -2) : null;
2320
+ },
2321
+ _flag: function(element, attribute) {
2322
+ return $(element).hasAttribute(attribute) ? attribute : null;
2323
+ },
2324
+ style: function(element) {
2325
+ return element.style.cssText.toLowerCase();
2326
+ },
2327
+ title: function(element) {
2328
+ return element.title;
2329
+ }
2330
+ }
2331
+ }
2332
+ };
2333
+
2334
+ Element._attributeTranslations.write = {
2335
+ names: Object.clone(Element._attributeTranslations.read.names),
2336
+ values: {
2337
+ checked: function(element, value) {
2338
+ element.checked = !!value;
2339
+ },
2340
+
2341
+ style: function(element, value) {
2342
+ element.style.cssText = value ? value : '';
2343
+ }
2344
+ }
2345
+ };
2346
+
2347
+ Element._attributeTranslations.has = {};
2348
+
2349
+ $w('colSpan rowSpan vAlign dateTime accessKey tabIndex ' +
2350
+ 'encType maxLength readOnly longDesc').each(function(attr) {
2351
+ Element._attributeTranslations.write.names[attr.toLowerCase()] = attr;
2352
+ Element._attributeTranslations.has[attr.toLowerCase()] = attr;
2353
+ });
2354
+
2355
+ (function(v) {
2356
+ Object.extend(v, {
2357
+ href: v._getAttr,
2358
+ src: v._getAttr,
2359
+ type: v._getAttr,
2360
+ action: v._getAttrNode,
2361
+ disabled: v._flag,
2362
+ checked: v._flag,
2363
+ readonly: v._flag,
2364
+ multiple: v._flag,
2365
+ onload: v._getEv,
2366
+ onunload: v._getEv,
2367
+ onclick: v._getEv,
2368
+ ondblclick: v._getEv,
2369
+ onmousedown: v._getEv,
2370
+ onmouseup: v._getEv,
2371
+ onmouseover: v._getEv,
2372
+ onmousemove: v._getEv,
2373
+ onmouseout: v._getEv,
2374
+ onfocus: v._getEv,
2375
+ onblur: v._getEv,
2376
+ onkeypress: v._getEv,
2377
+ onkeydown: v._getEv,
2378
+ onkeyup: v._getEv,
2379
+ onsubmit: v._getEv,
2380
+ onreset: v._getEv,
2381
+ onselect: v._getEv,
2382
+ onchange: v._getEv
2383
+ });
2384
+ })(Element._attributeTranslations.read.values);
2385
+ }
2386
+
2387
+ else if (Prototype.Browser.Gecko && /rv:1\.8\.0/.test(navigator.userAgent)) {
2388
+ Element.Methods.setOpacity = function(element, value) {
2389
+ element = $(element);
2390
+ element.style.opacity = (value == 1) ? 0.999999 :
2391
+ (value === '') ? '' : (value < 0.00001) ? 0 : value;
2392
+ return element;
2393
+ };
2394
+ }
2395
+
2396
+ else if (Prototype.Browser.WebKit) {
2397
+ Element.Methods.setOpacity = function(element, value) {
2398
+ element = $(element);
2399
+ element.style.opacity = (value == 1 || value === '') ? '' :
2400
+ (value < 0.00001) ? 0 : value;
2401
+
2402
+ if (value == 1)
2403
+ if(element.tagName == 'IMG' && element.width) {
2404
+ element.width++; element.width--;
2405
+ } else try {
2406
+ var n = document.createTextNode(' ');
2407
+ element.appendChild(n);
2408
+ element.removeChild(n);
2409
+ } catch (e) { }
2410
+
2411
+ return element;
2412
+ };
2413
+
2414
+ // Safari returns margins on body which is incorrect if the child is absolutely
2415
+ // positioned. For performance reasons, redefine Element#cumulativeOffset for
2416
+ // KHTML/WebKit only.
2417
+ Element.Methods.cumulativeOffset = function(element) {
2418
+ var valueT = 0, valueL = 0;
2419
+ do {
2420
+ valueT += element.offsetTop || 0;
2421
+ valueL += element.offsetLeft || 0;
2422
+ if (element.offsetParent == document.body)
2423
+ if (Element.getStyle(element, 'position') == 'absolute') break;
2424
+
2425
+ element = element.offsetParent;
2426
+ } while (element);
2427
+
2428
+ return Element._returnOffset(valueL, valueT);
2429
+ };
2430
+ }
2431
+
2432
+ if (Prototype.Browser.IE || Prototype.Browser.Opera) {
2433
+ // IE and Opera are missing .innerHTML support for TABLE-related and SELECT elements
2434
+ Element.Methods.update = function(element, content) {
2435
+ element = $(element);
2436
+
2437
+ if (content && content.toElement) content = content.toElement();
2438
+ if (Object.isElement(content)) return element.update().insert(content);
2439
+
2440
+ content = Object.toHTML(content);
2441
+ var tagName = element.tagName.toUpperCase();
2442
+
2443
+ if (tagName in Element._insertionTranslations.tags) {
2444
+ $A(element.childNodes).each(function(node) { element.removeChild(node) });
2445
+ Element._getContentFromAnonymousElement(tagName, content.stripScripts())
2446
+ .each(function(node) { element.appendChild(node) });
2447
+ }
2448
+ else element.innerHTML = content.stripScripts();
2449
+
2450
+ content.evalScripts.bind(content).defer();
2451
+ return element;
2452
+ };
2453
+ }
2454
+
2455
+ if (document.createElement('div').outerHTML) {
2456
+ Element.Methods.replace = function(element, content) {
2457
+ element = $(element);
2458
+
2459
+ if (content && content.toElement) content = content.toElement();
2460
+ if (Object.isElement(content)) {
2461
+ element.parentNode.replaceChild(content, element);
2462
+ return element;
2463
+ }
2464
+
2465
+ content = Object.toHTML(content);
2466
+ var parent = element.parentNode, tagName = parent.tagName.toUpperCase();
2467
+
2468
+ if (Element._insertionTranslations.tags[tagName]) {
2469
+ var nextSibling = element.next();
2470
+ var fragments = Element._getContentFromAnonymousElement(tagName, content.stripScripts());
2471
+ parent.removeChild(element);
2472
+ if (nextSibling)
2473
+ fragments.each(function(node) { parent.insertBefore(node, nextSibling) });
2474
+ else
2475
+ fragments.each(function(node) { parent.appendChild(node) });
2476
+ }
2477
+ else element.outerHTML = content.stripScripts();
2478
+
2479
+ content.evalScripts.bind(content).defer();
2480
+ return element;
2481
+ };
2482
+ }
2483
+
2484
+ Element._returnOffset = function(l, t) {
2485
+ var result = [l, t];
2486
+ result.left = l;
2487
+ result.top = t;
2488
+ return result;
2489
+ };
2490
+
2491
+ Element._getContentFromAnonymousElement = function(tagName, html) {
2492
+ var div = new Element('div'), t = Element._insertionTranslations.tags[tagName];
2493
+ div.innerHTML = t[0] + html + t[1];
2494
+ t[2].times(function() { div = div.firstChild });
2495
+ return $A(div.childNodes);
2496
+ };
2497
+
2498
+ Element._insertionTranslations = {
2499
+ before: {
2500
+ adjacency: 'beforeBegin',
2501
+ insert: function(element, node) {
2502
+ element.parentNode.insertBefore(node, element);
2503
+ },
2504
+ initializeRange: function(element, range) {
2505
+ range.setStartBefore(element);
2506
+ }
2507
+ },
2508
+ top: {
2509
+ adjacency: 'afterBegin',
2510
+ insert: function(element, node) {
2511
+ element.insertBefore(node, element.firstChild);
2512
+ },
2513
+ initializeRange: function(element, range) {
2514
+ range.selectNodeContents(element);
2515
+ range.collapse(true);
2516
+ }
2517
+ },
2518
+ bottom: {
2519
+ adjacency: 'beforeEnd',
2520
+ insert: function(element, node) {
2521
+ element.appendChild(node);
2522
+ }
2523
+ },
2524
+ after: {
2525
+ adjacency: 'afterEnd',
2526
+ insert: function(element, node) {
2527
+ element.parentNode.insertBefore(node, element.nextSibling);
2528
+ },
2529
+ initializeRange: function(element, range) {
2530
+ range.setStartAfter(element);
2531
+ }
2532
+ },
2533
+ tags: {
2534
+ TABLE: ['<table>', '</table>', 1],
2535
+ TBODY: ['<table><tbody>', '</tbody></table>', 2],
2536
+ TR: ['<table><tbody><tr>', '</tr></tbody></table>', 3],
2537
+ TD: ['<table><tbody><tr><td>', '</td></tr></tbody></table>', 4],
2538
+ SELECT: ['<select>', '</select>', 1]
2539
+ }
2540
+ };
2541
+
2542
+ (function() {
2543
+ this.bottom.initializeRange = this.top.initializeRange;
2544
+ Object.extend(this.tags, {
2545
+ THEAD: this.tags.TBODY,
2546
+ TFOOT: this.tags.TBODY,
2547
+ TH: this.tags.TD
2548
+ });
2549
+ }).call(Element._insertionTranslations);
2550
+
2551
+ Element.Methods.Simulated = {
2552
+ hasAttribute: function(element, attribute) {
2553
+ attribute = Element._attributeTranslations.has[attribute] || attribute;
2554
+ var node = $(element).getAttributeNode(attribute);
2555
+ return node && node.specified;
2556
+ }
2557
+ };
2558
+
2559
+ Element.Methods.ByTag = { };
2560
+
2561
+ Object.extend(Element, Element.Methods);
2562
+
2563
+ if (!Prototype.BrowserFeatures.ElementExtensions &&
2564
+ document.createElement('div').__proto__) {
2565
+ window.HTMLElement = { };
2566
+ window.HTMLElement.prototype = document.createElement('div').__proto__;
2567
+ Prototype.BrowserFeatures.ElementExtensions = true;
2568
+ }
2569
+
2570
+ Element.extend = (function() {
2571
+ if (Prototype.BrowserFeatures.SpecificElementExtensions)
2572
+ return Prototype.K;
2573
+
2574
+ var Methods = { }, ByTag = Element.Methods.ByTag;
2575
+
2576
+ var extend = Object.extend(function(element) {
2577
+ if (!element || element._extendedByPrototype ||
2578
+ element.nodeType != 1 || element == window) return element;
2579
+
2580
+ var methods = Object.clone(Methods),
2581
+ tagName = element.tagName, property, value;
2582
+
2583
+ // extend methods for specific tags
2584
+ if (ByTag[tagName]) Object.extend(methods, ByTag[tagName]);
2585
+
2586
+ for (property in methods) {
2587
+ value = methods[property];
2588
+ if (Object.isFunction(value) && !(property in element))
2589
+ element[property] = value.methodize();
2590
+ }
2591
+
2592
+ element._extendedByPrototype = Prototype.emptyFunction;
2593
+ return element;
2594
+
2595
+ }, {
2596
+ refresh: function() {
2597
+ // extend methods for all tags (Safari doesn't need this)
2598
+ if (!Prototype.BrowserFeatures.ElementExtensions) {
2599
+ Object.extend(Methods, Element.Methods);
2600
+ Object.extend(Methods, Element.Methods.Simulated);
2601
+ }
2602
+ }
2603
+ });
2604
+
2605
+ extend.refresh();
2606
+ return extend;
2607
+ })();
2608
+
2609
+ Element.hasAttribute = function(element, attribute) {
2610
+ if (element.hasAttribute) return element.hasAttribute(attribute);
2611
+ return Element.Methods.Simulated.hasAttribute(element, attribute);
2612
+ };
2613
+
2614
+ Element.addMethods = function(methods) {
2615
+ var F = Prototype.BrowserFeatures, T = Element.Methods.ByTag;
2616
+
2617
+ if (!methods) {
2618
+ Object.extend(Form, Form.Methods);
2619
+ Object.extend(Form.Element, Form.Element.Methods);
2620
+ Object.extend(Element.Methods.ByTag, {
2621
+ "FORM": Object.clone(Form.Methods),
2622
+ "INPUT": Object.clone(Form.Element.Methods),
2623
+ "SELECT": Object.clone(Form.Element.Methods),
2624
+ "TEXTAREA": Object.clone(Form.Element.Methods)
2625
+ });
2626
+ }
2627
+
2628
+ if (arguments.length == 2) {
2629
+ var tagName = methods;
2630
+ methods = arguments[1];
2631
+ }
2632
+
2633
+ if (!tagName) Object.extend(Element.Methods, methods || { });
2634
+ else {
2635
+ if (Object.isArray(tagName)) tagName.each(extend);
2636
+ else extend(tagName);
2637
+ }
2638
+
2639
+ function extend(tagName) {
2640
+ tagName = tagName.toUpperCase();
2641
+ if (!Element.Methods.ByTag[tagName])
2642
+ Element.Methods.ByTag[tagName] = { };
2643
+ Object.extend(Element.Methods.ByTag[tagName], methods);
2644
+ }
2645
+
2646
+ function copy(methods, destination, onlyIfAbsent) {
2647
+ onlyIfAbsent = onlyIfAbsent || false;
2648
+ for (var property in methods) {
2649
+ var value = methods[property];
2650
+ if (!Object.isFunction(value)) continue;
2651
+ if (!onlyIfAbsent || !(property in destination))
2652
+ destination[property] = value.methodize();
2653
+ }
2654
+ }
2655
+
2656
+ function findDOMClass(tagName) {
2657
+ var klass;
2658
+ var trans = {
2659
+ "OPTGROUP": "OptGroup", "TEXTAREA": "TextArea", "P": "Paragraph",
2660
+ "FIELDSET": "FieldSet", "UL": "UList", "OL": "OList", "DL": "DList",
2661
+ "DIR": "Directory", "H1": "Heading", "H2": "Heading", "H3": "Heading",
2662
+ "H4": "Heading", "H5": "Heading", "H6": "Heading", "Q": "Quote",
2663
+ "INS": "Mod", "DEL": "Mod", "A": "Anchor", "IMG": "Image", "CAPTION":
2664
+ "TableCaption", "COL": "TableCol", "COLGROUP": "TableCol", "THEAD":
2665
+ "TableSection", "TFOOT": "TableSection", "TBODY": "TableSection", "TR":
2666
+ "TableRow", "TH": "TableCell", "TD": "TableCell", "FRAMESET":
2667
+ "FrameSet", "IFRAME": "IFrame"
2668
+ };
2669
+ if (trans[tagName]) klass = 'HTML' + trans[tagName] + 'Element';
2670
+ if (window[klass]) return window[klass];
2671
+ klass = 'HTML' + tagName + 'Element';
2672
+ if (window[klass]) return window[klass];
2673
+ klass = 'HTML' + tagName.capitalize() + 'Element';
2674
+ if (window[klass]) return window[klass];
2675
+
2676
+ window[klass] = { };
2677
+ window[klass].prototype = document.createElement(tagName).__proto__;
2678
+ return window[klass];
2679
+ }
2680
+
2681
+ if (F.ElementExtensions) {
2682
+ copy(Element.Methods, HTMLElement.prototype);
2683
+ copy(Element.Methods.Simulated, HTMLElement.prototype, true);
2684
+ }
2685
+
2686
+ if (F.SpecificElementExtensions) {
2687
+ for (var tag in Element.Methods.ByTag) {
2688
+ var klass = findDOMClass(tag);
2689
+ if (Object.isUndefined(klass)) continue;
2690
+ copy(T[tag], klass.prototype);
2691
+ }
2692
+ }
2693
+
2694
+ Object.extend(Element, Element.Methods);
2695
+ delete Element.ByTag;
2696
+
2697
+ if (Element.extend.refresh) Element.extend.refresh();
2698
+ Element.cache = { };
2699
+ };
2700
+
2701
+ document.viewport = {
2702
+ getDimensions: function() {
2703
+ var dimensions = { };
2704
+ var B = Prototype.Browser;
2705
+ $w('width height').each(function(d) {
2706
+ var D = d.capitalize();
2707
+ dimensions[d] = (B.WebKit && !document.evaluate) ? self['inner' + D] :
2708
+ (B.Opera) ? document.body['client' + D] : document.documentElement['client' + D];
2709
+ });
2710
+ return dimensions;
2711
+ },
2712
+
2713
+ getWidth: function() {
2714
+ return this.getDimensions().width;
2715
+ },
2716
+
2717
+ getHeight: function() {
2718
+ return this.getDimensions().height;
2719
+ },
2720
+
2721
+ getScrollOffsets: function() {
2722
+ return Element._returnOffset(
2723
+ window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
2724
+ window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop);
2725
+ }
2726
+ };
2727
+ /* Portions of the Selector class are derived from Jack Slocum’s DomQuery,
2728
+ * part of YUI-Ext version 0.40, distributed under the terms of an MIT-style
2729
+ * license. Please see http://www.yui-ext.com/ for more information. */
2730
+
2731
+ var Selector = Class.create({
2732
+ initialize: function(expression) {
2733
+ this.expression = expression.strip();
2734
+ this.compileMatcher();
2735
+ },
2736
+
2737
+ shouldUseXPath: function() {
2738
+ if (!Prototype.BrowserFeatures.XPath) return false;
2739
+
2740
+ var e = this.expression;
2741
+
2742
+ // Safari 3 chokes on :*-of-type and :empty
2743
+ if (Prototype.Browser.WebKit &&
2744
+ (e.include("-of-type") || e.include(":empty")))
2745
+ return false;
2746
+
2747
+ // XPath can't do namespaced attributes, nor can it read
2748
+ // the "checked" property from DOM nodes
2749
+ if ((/(\[[\w-]*?:|:checked)/).test(this.expression))
2750
+ return false;
2751
+
2752
+ return true;
2753
+ },
2754
+
2755
+ compileMatcher: function() {
2756
+ if (this.shouldUseXPath())
2757
+ return this.compileXPathMatcher();
2758
+
2759
+ var e = this.expression, ps = Selector.patterns, h = Selector.handlers,
2760
+ c = Selector.criteria, le, p, m;
2761
+
2762
+ if (Selector._cache[e]) {
2763
+ this.matcher = Selector._cache[e];
2764
+ return;
2765
+ }
2766
+
2767
+ this.matcher = ["this.matcher = function(root) {",
2768
+ "var r = root, h = Selector.handlers, c = false, n;"];
2769
+
2770
+ while (e && le != e && (/\S/).test(e)) {
2771
+ le = e;
2772
+ for (var i in ps) {
2773
+ p = ps[i];
2774
+ if (m = e.match(p)) {
2775
+ this.matcher.push(Object.isFunction(c[i]) ? c[i](m) :
2776
+ new Template(c[i]).evaluate(m));
2777
+ e = e.replace(m[0], '');
2778
+ break;
2779
+ }
2780
+ }
2781
+ }
2782
+
2783
+ this.matcher.push("return h.unique(n);\n}");
2784
+ eval(this.matcher.join('\n'));
2785
+ Selector._cache[this.expression] = this.matcher;
2786
+ },
2787
+
2788
+ compileXPathMatcher: function() {
2789
+ var e = this.expression, ps = Selector.patterns,
2790
+ x = Selector.xpath, le, m;
2791
+
2792
+ if (Selector._cache[e]) {
2793
+ this.xpath = Selector._cache[e]; return;
2794
+ }
2795
+
2796
+ this.matcher = ['.//*'];
2797
+ while (e && le != e && (/\S/).test(e)) {
2798
+ le = e;
2799
+ for (var i in ps) {
2800
+ if (m = e.match(ps[i])) {
2801
+ this.matcher.push(Object.isFunction(x[i]) ? x[i](m) :
2802
+ new Template(x[i]).evaluate(m));
2803
+ e = e.replace(m[0], '');
2804
+ break;
2805
+ }
2806
+ }
2807
+ }
2808
+
2809
+ this.xpath = this.matcher.join('');
2810
+ Selector._cache[this.expression] = this.xpath;
2811
+ },
2812
+
2813
+ findElements: function(root) {
2814
+ root = root || document;
2815
+ if (this.xpath) return document._getElementsByXPath(this.xpath, root);
2816
+ return this.matcher(root);
2817
+ },
2818
+
2819
+ match: function(element) {
2820
+ this.tokens = [];
2821
+
2822
+ var e = this.expression, ps = Selector.patterns, as = Selector.assertions;
2823
+ var le, p, m;
2824
+
2825
+ while (e && le !== e && (/\S/).test(e)) {
2826
+ le = e;
2827
+ for (var i in ps) {
2828
+ p = ps[i];
2829
+ if (m = e.match(p)) {
2830
+ // use the Selector.assertions methods unless the selector
2831
+ // is too complex.
2832
+ if (as[i]) {
2833
+ this.tokens.push([i, Object.clone(m)]);
2834
+ e = e.replace(m[0], '');
2835
+ } else {
2836
+ // reluctantly do a document-wide search
2837
+ // and look for a match in the array
2838
+ return this.findElements(document).include(element);
2839
+ }
2840
+ }
2841
+ }
2842
+ }
2843
+
2844
+ var match = true, name, matches;
2845
+ for (var i = 0, token; token = this.tokens[i]; i++) {
2846
+ name = token[0], matches = token[1];
2847
+ if (!Selector.assertions[name](element, matches)) {
2848
+ match = false; break;
2849
+ }
2850
+ }
2851
+
2852
+ return match;
2853
+ },
2854
+
2855
+ toString: function() {
2856
+ return this.expression;
2857
+ },
2858
+
2859
+ inspect: function() {
2860
+ return "#<Selector:" + this.expression.inspect() + ">";
2861
+ }
2862
+ });
2863
+
2864
+ Object.extend(Selector, {
2865
+ _cache: { },
2866
+
2867
+ xpath: {
2868
+ descendant: "//*",
2869
+ child: "/*",
2870
+ adjacent: "/following-sibling::*[1]",
2871
+ laterSibling: '/following-sibling::*',
2872
+ tagName: function(m) {
2873
+ if (m[1] == '*') return '';
2874
+ return "[local-name()='" + m[1].toLowerCase() +
2875
+ "' or local-name()='" + m[1].toUpperCase() + "']";
2876
+ },
2877
+ className: "[contains(concat(' ', @class, ' '), ' #{1} ')]",
2878
+ id: "[@id='#{1}']",
2879
+ attrPresence: function(m) {
2880
+ m[1] = m[1].toLowerCase();
2881
+ return new Template("[@#{1}]").evaluate(m);
2882
+ },
2883
+ attr: function(m) {
2884
+ m[1] = m[1].toLowerCase();
2885
+ m[3] = m[5] || m[6];
2886
+ return new Template(Selector.xpath.operators[m[2]]).evaluate(m);
2887
+ },
2888
+ pseudo: function(m) {
2889
+ var h = Selector.xpath.pseudos[m[1]];
2890
+ if (!h) return '';
2891
+ if (Object.isFunction(h)) return h(m);
2892
+ return new Template(Selector.xpath.pseudos[m[1]]).evaluate(m);
2893
+ },
2894
+ operators: {
2895
+ '=': "[@#{1}='#{3}']",
2896
+ '!=': "[@#{1}!='#{3}']",
2897
+ '^=': "[starts-with(@#{1}, '#{3}')]",
2898
+ '$=': "[substring(@#{1}, (string-length(@#{1}) - string-length('#{3}') + 1))='#{3}']",
2899
+ '*=': "[contains(@#{1}, '#{3}')]",
2900
+ '~=': "[contains(concat(' ', @#{1}, ' '), ' #{3} ')]",
2901
+ '|=': "[contains(concat('-', @#{1}, '-'), '-#{3}-')]"
2902
+ },
2903
+ pseudos: {
2904
+ 'first-child': '[not(preceding-sibling::*)]',
2905
+ 'last-child': '[not(following-sibling::*)]',
2906
+ 'only-child': '[not(preceding-sibling::* or following-sibling::*)]',
2907
+ 'empty': "[count(*) = 0 and (count(text()) = 0 or translate(text(), ' \t\r\n', '') = '')]",
2908
+ 'checked': "[@checked]",
2909
+ 'disabled': "[@disabled]",
2910
+ 'enabled': "[not(@disabled)]",
2911
+ 'not': function(m) {
2912
+ var e = m[6], p = Selector.patterns,
2913
+ x = Selector.xpath, le, v;
2914
+
2915
+ var exclusion = [];
2916
+ while (e && le != e && (/\S/).test(e)) {
2917
+ le = e;
2918
+ for (var i in p) {
2919
+ if (m = e.match(p[i])) {
2920
+ v = Object.isFunction(x[i]) ? x[i](m) : new Template(x[i]).evaluate(m);
2921
+ exclusion.push("(" + v.substring(1, v.length - 1) + ")");
2922
+ e = e.replace(m[0], '');
2923
+ break;
2924
+ }
2925
+ }
2926
+ }
2927
+ return "[not(" + exclusion.join(" and ") + ")]";
2928
+ },
2929
+ 'nth-child': function(m) {
2930
+ return Selector.xpath.pseudos.nth("(count(./preceding-sibling::*) + 1) ", m);
2931
+ },
2932
+ 'nth-last-child': function(m) {
2933
+ return Selector.xpath.pseudos.nth("(count(./following-sibling::*) + 1) ", m);
2934
+ },
2935
+ 'nth-of-type': function(m) {
2936
+ return Selector.xpath.pseudos.nth("position() ", m);
2937
+ },
2938
+ 'nth-last-of-type': function(m) {
2939
+ return Selector.xpath.pseudos.nth("(last() + 1 - position()) ", m);
2940
+ },
2941
+ 'first-of-type': function(m) {
2942
+ m[6] = "1"; return Selector.xpath.pseudos['nth-of-type'](m);
2943
+ },
2944
+ 'last-of-type': function(m) {
2945
+ m[6] = "1"; return Selector.xpath.pseudos['nth-last-of-type'](m);
2946
+ },
2947
+ 'only-of-type': function(m) {
2948
+ var p = Selector.xpath.pseudos; return p['first-of-type'](m) + p['last-of-type'](m);
2949
+ },
2950
+ nth: function(fragment, m) {
2951
+ var mm, formula = m[6], predicate;
2952
+ if (formula == 'even') formula = '2n+0';
2953
+ if (formula == 'odd') formula = '2n+1';
2954
+ if (mm = formula.match(/^(\d+)$/)) // digit only
2955
+ return '[' + fragment + "= " + mm[1] + ']';
2956
+ if (mm = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b
2957
+ if (mm[1] == "-") mm[1] = -1;
2958
+ var a = mm[1] ? Number(mm[1]) : 1;
2959
+ var b = mm[2] ? Number(mm[2]) : 0;
2960
+ predicate = "[((#{fragment} - #{b}) mod #{a} = 0) and " +
2961
+ "((#{fragment} - #{b}) div #{a} >= 0)]";
2962
+ return new Template(predicate).evaluate({
2963
+ fragment: fragment, a: a, b: b });
2964
+ }
2965
+ }
2966
+ }
2967
+ },
2968
+
2969
+ criteria: {
2970
+ tagName: 'n = h.tagName(n, r, "#{1}", c); c = false;',
2971
+ className: 'n = h.className(n, r, "#{1}", c); c = false;',
2972
+ id: 'n = h.id(n, r, "#{1}", c); c = false;',
2973
+ attrPresence: 'n = h.attrPresence(n, r, "#{1}"); c = false;',
2974
+ attr: function(m) {
2975
+ m[3] = (m[5] || m[6]);
2976
+ return new Template('n = h.attr(n, r, "#{1}", "#{3}", "#{2}"); c = false;').evaluate(m);
2977
+ },
2978
+ pseudo: function(m) {
2979
+ if (m[6]) m[6] = m[6].replace(/"/g, '\\"');
2980
+ return new Template('n = h.pseudo(n, "#{1}", "#{6}", r, c); c = false;').evaluate(m);
2981
+ },
2982
+ descendant: 'c = "descendant";',
2983
+ child: 'c = "child";',
2984
+ adjacent: 'c = "adjacent";',
2985
+ laterSibling: 'c = "laterSibling";'
2986
+ },
2987
+
2988
+ patterns: {
2989
+ // combinators must be listed first
2990
+ // (and descendant needs to be last combinator)
2991
+ laterSibling: /^\s*~\s*/,
2992
+ child: /^\s*>\s*/,
2993
+ adjacent: /^\s*\+\s*/,
2994
+ descendant: /^\s/,
2995
+
2996
+ // selectors follow
2997
+ tagName: /^\s*(\*|[\w\-]+)(\b|$)?/,
2998
+ id: /^#([\w\-\*]+)(\b|$)/,
2999
+ className: /^\.([\w\-\*]+)(\b|$)/,
3000
+ pseudo: /^:((first|last|nth|nth-last|only)(-child|-of-type)|empty|checked|(en|dis)abled|not)(\((.*?)\))?(\b|$|(?=\s)|(?=:))/,
3001
+ attrPresence: /^\[([\w]+)\]/,
3002
+ attr: /\[((?:[\w-]*:)?[\w-]+)\s*(?:([!^$*~|]?=)\s*((['"])([^\4]*?)\4|([^'"][^\]]*?)))?\]/
3003
+ },
3004
+
3005
+ // for Selector.match and Element#match
3006
+ assertions: {
3007
+ tagName: function(element, matches) {
3008
+ return matches[1].toUpperCase() == element.tagName.toUpperCase();
3009
+ },
3010
+
3011
+ className: function(element, matches) {
3012
+ return Element.hasClassName(element, matches[1]);
3013
+ },
3014
+
3015
+ id: function(element, matches) {
3016
+ return element.id === matches[1];
3017
+ },
3018
+
3019
+ attrPresence: function(element, matches) {
3020
+ return Element.hasAttribute(element, matches[1]);
3021
+ },
3022
+
3023
+ attr: function(element, matches) {
3024
+ var nodeValue = Element.readAttribute(element, matches[1]);
3025
+ return Selector.operators[matches[2]](nodeValue, matches[3]);
3026
+ }
3027
+ },
3028
+
3029
+ handlers: {
3030
+ // UTILITY FUNCTIONS
3031
+ // joins two collections
3032
+ concat: function(a, b) {
3033
+ for (var i = 0, node; node = b[i]; i++)
3034
+ a.push(node);
3035
+ return a;
3036
+ },
3037
+
3038
+ // marks an array of nodes for counting
3039
+ mark: function(nodes) {
3040
+ for (var i = 0, node; node = nodes[i]; i++)
3041
+ node._counted = true;
3042
+ return nodes;
3043
+ },
3044
+
3045
+ unmark: function(nodes) {
3046
+ for (var i = 0, node; node = nodes[i]; i++)
3047
+ node._counted = undefined;
3048
+ return nodes;
3049
+ },
3050
+
3051
+ // mark each child node with its position (for nth calls)
3052
+ // "ofType" flag indicates whether we're indexing for nth-of-type
3053
+ // rather than nth-child
3054
+ index: function(parentNode, reverse, ofType) {
3055
+ parentNode._counted = true;
3056
+ if (reverse) {
3057
+ for (var nodes = parentNode.childNodes, i = nodes.length - 1, j = 1; i >= 0; i--) {
3058
+ var node = nodes[i];
3059
+ if (node.nodeType == 1 && (!ofType || node._counted)) node.nodeIndex = j++;
3060
+ }
3061
+ } else {
3062
+ for (var i = 0, j = 1, nodes = parentNode.childNodes; node = nodes[i]; i++)
3063
+ if (node.nodeType == 1 && (!ofType || node._counted)) node.nodeIndex = j++;
3064
+ }
3065
+ },
3066
+
3067
+ // filters out duplicates and extends all nodes
3068
+ unique: function(nodes) {
3069
+ if (nodes.length == 0) return nodes;
3070
+ var results = [], n;
3071
+ for (var i = 0, l = nodes.length; i < l; i++)
3072
+ if (!(n = nodes[i])._counted) {
3073
+ n._counted = true;
3074
+ results.push(Element.extend(n));
3075
+ }
3076
+ return Selector.handlers.unmark(results);
3077
+ },
3078
+
3079
+ // COMBINATOR FUNCTIONS
3080
+ descendant: function(nodes) {
3081
+ var h = Selector.handlers;
3082
+ for (var i = 0, results = [], node; node = nodes[i]; i++)
3083
+ h.concat(results, node.getElementsByTagName('*'));
3084
+ return results;
3085
+ },
3086
+
3087
+ child: function(nodes) {
3088
+ var h = Selector.handlers;
3089
+ for (var i = 0, results = [], node; node = nodes[i]; i++) {
3090
+ for (var j = 0, child; child = node.childNodes[j]; j++)
3091
+ if (child.nodeType == 1 && child.tagName != '!') results.push(child);
3092
+ }
3093
+ return results;
3094
+ },
3095
+
3096
+ adjacent: function(nodes) {
3097
+ for (var i = 0, results = [], node; node = nodes[i]; i++) {
3098
+ var next = this.nextElementSibling(node);
3099
+ if (next) results.push(next);
3100
+ }
3101
+ return results;
3102
+ },
3103
+
3104
+ laterSibling: function(nodes) {
3105
+ var h = Selector.handlers;
3106
+ for (var i = 0, results = [], node; node = nodes[i]; i++)
3107
+ h.concat(results, Element.nextSiblings(node));
3108
+ return results;
3109
+ },
3110
+
3111
+ nextElementSibling: function(node) {
3112
+ while (node = node.nextSibling)
3113
+ if (node.nodeType == 1) return node;
3114
+ return null;
3115
+ },
3116
+
3117
+ previousElementSibling: function(node) {
3118
+ while (node = node.previousSibling)
3119
+ if (node.nodeType == 1) return node;
3120
+ return null;
3121
+ },
3122
+
3123
+ // TOKEN FUNCTIONS
3124
+ tagName: function(nodes, root, tagName, combinator) {
3125
+ tagName = tagName.toUpperCase();
3126
+ var results = [], h = Selector.handlers;
3127
+ if (nodes) {
3128
+ if (combinator) {
3129
+ // fastlane for ordinary descendant combinators
3130
+ if (combinator == "descendant") {
3131
+ for (var i = 0, node; node = nodes[i]; i++)
3132
+ h.concat(results, node.getElementsByTagName(tagName));
3133
+ return results;
3134
+ } else nodes = this[combinator](nodes);
3135
+ if (tagName == "*") return nodes;
3136
+ }
3137
+ for (var i = 0, node; node = nodes[i]; i++)
3138
+ if (node.tagName.toUpperCase() == tagName) results.push(node);
3139
+ return results;
3140
+ } else return root.getElementsByTagName(tagName);
3141
+ },
3142
+
3143
+ id: function(nodes, root, id, combinator) {
3144
+ var targetNode = $(id), h = Selector.handlers;
3145
+ if (!targetNode) return [];
3146
+ if (!nodes && root == document) return [targetNode];
3147
+ if (nodes) {
3148
+ if (combinator) {
3149
+ if (combinator == 'child') {
3150
+ for (var i = 0, node; node = nodes[i]; i++)
3151
+ if (targetNode.parentNode == node) return [targetNode];
3152
+ } else if (combinator == 'descendant') {
3153
+ for (var i = 0, node; node = nodes[i]; i++)
3154
+ if (Element.descendantOf(targetNode, node)) return [targetNode];
3155
+ } else if (combinator == 'adjacent') {
3156
+ for (var i = 0, node; node = nodes[i]; i++)
3157
+ if (Selector.handlers.previousElementSibling(targetNode) == node)
3158
+ return [targetNode];
3159
+ } else nodes = h[combinator](nodes);
3160
+ }
3161
+ for (var i = 0, node; node = nodes[i]; i++)
3162
+ if (node == targetNode) return [targetNode];
3163
+ return [];
3164
+ }
3165
+ return (targetNode && Element.descendantOf(targetNode, root)) ? [targetNode] : [];
3166
+ },
3167
+
3168
+ className: function(nodes, root, className, combinator) {
3169
+ if (nodes && combinator) nodes = this[combinator](nodes);
3170
+ return Selector.handlers.byClassName(nodes, root, className);
3171
+ },
3172
+
3173
+ byClassName: function(nodes, root, className) {
3174
+ if (!nodes) nodes = Selector.handlers.descendant([root]);
3175
+ var needle = ' ' + className + ' ';
3176
+ for (var i = 0, results = [], node, nodeClassName; node = nodes[i]; i++) {
3177
+ nodeClassName = node.className;
3178
+ if (nodeClassName.length == 0) continue;
3179
+ if (nodeClassName == className || (' ' + nodeClassName + ' ').include(needle))
3180
+ results.push(node);
3181
+ }
3182
+ return results;
3183
+ },
3184
+
3185
+ attrPresence: function(nodes, root, attr) {
3186
+ if (!nodes) nodes = root.getElementsByTagName("*");
3187
+ var results = [];
3188
+ for (var i = 0, node; node = nodes[i]; i++)
3189
+ if (Element.hasAttribute(node, attr)) results.push(node);
3190
+ return results;
3191
+ },
3192
+
3193
+ attr: function(nodes, root, attr, value, operator) {
3194
+ if (!nodes) nodes = root.getElementsByTagName("*");
3195
+ var handler = Selector.operators[operator], results = [];
3196
+ for (var i = 0, node; node = nodes[i]; i++) {
3197
+ var nodeValue = Element.readAttribute(node, attr);
3198
+ if (nodeValue === null) continue;
3199
+ if (handler(nodeValue, value)) results.push(node);
3200
+ }
3201
+ return results;
3202
+ },
3203
+
3204
+ pseudo: function(nodes, name, value, root, combinator) {
3205
+ if (nodes && combinator) nodes = this[combinator](nodes);
3206
+ if (!nodes) nodes = root.getElementsByTagName("*");
3207
+ return Selector.pseudos[name](nodes, value, root);
3208
+ }
3209
+ },
3210
+
3211
+ pseudos: {
3212
+ 'first-child': function(nodes, value, root) {
3213
+ for (var i = 0, results = [], node; node = nodes[i]; i++) {
3214
+ if (Selector.handlers.previousElementSibling(node)) continue;
3215
+ results.push(node);
3216
+ }
3217
+ return results;
3218
+ },
3219
+ 'last-child': function(nodes, value, root) {
3220
+ for (var i = 0, results = [], node; node = nodes[i]; i++) {
3221
+ if (Selector.handlers.nextElementSibling(node)) continue;
3222
+ results.push(node);
3223
+ }
3224
+ return results;
3225
+ },
3226
+ 'only-child': function(nodes, value, root) {
3227
+ var h = Selector.handlers;
3228
+ for (var i = 0, results = [], node; node = nodes[i]; i++)
3229
+ if (!h.previousElementSibling(node) && !h.nextElementSibling(node))
3230
+ results.push(node);
3231
+ return results;
3232
+ },
3233
+ 'nth-child': function(nodes, formula, root) {
3234
+ return Selector.pseudos.nth(nodes, formula, root);
3235
+ },
3236
+ 'nth-last-child': function(nodes, formula, root) {
3237
+ return Selector.pseudos.nth(nodes, formula, root, true);
3238
+ },
3239
+ 'nth-of-type': function(nodes, formula, root) {
3240
+ return Selector.pseudos.nth(nodes, formula, root, false, true);
3241
+ },
3242
+ 'nth-last-of-type': function(nodes, formula, root) {
3243
+ return Selector.pseudos.nth(nodes, formula, root, true, true);
3244
+ },
3245
+ 'first-of-type': function(nodes, formula, root) {
3246
+ return Selector.pseudos.nth(nodes, "1", root, false, true);
3247
+ },
3248
+ 'last-of-type': function(nodes, formula, root) {
3249
+ return Selector.pseudos.nth(nodes, "1", root, true, true);
3250
+ },
3251
+ 'only-of-type': function(nodes, formula, root) {
3252
+ var p = Selector.pseudos;
3253
+ return p['last-of-type'](p['first-of-type'](nodes, formula, root), formula, root);
3254
+ },
3255
+
3256
+ // handles the an+b logic
3257
+ getIndices: function(a, b, total) {
3258
+ if (a == 0) return b > 0 ? [b] : [];
3259
+ return $R(1, total).inject([], function(memo, i) {
3260
+ if (0 == (i - b) % a && (i - b) / a >= 0) memo.push(i);
3261
+ return memo;
3262
+ });
3263
+ },
3264
+
3265
+ // handles nth(-last)-child, nth(-last)-of-type, and (first|last)-of-type
3266
+ nth: function(nodes, formula, root, reverse, ofType) {
3267
+ if (nodes.length == 0) return [];
3268
+ if (formula == 'even') formula = '2n+0';
3269
+ if (formula == 'odd') formula = '2n+1';
3270
+ var h = Selector.handlers, results = [], indexed = [], m;
3271
+ h.mark(nodes);
3272
+ for (var i = 0, node; node = nodes[i]; i++) {
3273
+ if (!node.parentNode._counted) {
3274
+ h.index(node.parentNode, reverse, ofType);
3275
+ indexed.push(node.parentNode);
3276
+ }
3277
+ }
3278
+ if (formula.match(/^\d+$/)) { // just a number
3279
+ formula = Number(formula);
3280
+ for (var i = 0, node; node = nodes[i]; i++)
3281
+ if (node.nodeIndex == formula) results.push(node);
3282
+ } else if (m = formula.match(/^(-?\d*)?n(([+-])(\d+))?/)) { // an+b
3283
+ if (m[1] == "-") m[1] = -1;
3284
+ var a = m[1] ? Number(m[1]) : 1;
3285
+ var b = m[2] ? Number(m[2]) : 0;
3286
+ var indices = Selector.pseudos.getIndices(a, b, nodes.length);
3287
+ for (var i = 0, node, l = indices.length; node = nodes[i]; i++) {
3288
+ for (var j = 0; j < l; j++)
3289
+ if (node.nodeIndex == indices[j]) results.push(node);
3290
+ }
3291
+ }
3292
+ h.unmark(nodes);
3293
+ h.unmark(indexed);
3294
+ return results;
3295
+ },
3296
+
3297
+ 'empty': function(nodes, value, root) {
3298
+ for (var i = 0, results = [], node; node = nodes[i]; i++) {
3299
+ // IE treats comments as element nodes
3300
+ if (node.tagName == '!' || (node.firstChild && !node.innerHTML.match(/^\s*$/))) continue;
3301
+ results.push(node);
3302
+ }
3303
+ return results;
3304
+ },
3305
+
3306
+ 'not': function(nodes, selector, root) {
3307
+ var h = Selector.handlers, selectorType, m;
3308
+ var exclusions = new Selector(selector).findElements(root);
3309
+ h.mark(exclusions);
3310
+ for (var i = 0, results = [], node; node = nodes[i]; i++)
3311
+ if (!node._counted) results.push(node);
3312
+ h.unmark(exclusions);
3313
+ return results;
3314
+ },
3315
+
3316
+ 'enabled': function(nodes, value, root) {
3317
+ for (var i = 0, results = [], node; node = nodes[i]; i++)
3318
+ if (!node.disabled) results.push(node);
3319
+ return results;
3320
+ },
3321
+
3322
+ 'disabled': function(nodes, value, root) {
3323
+ for (var i = 0, results = [], node; node = nodes[i]; i++)
3324
+ if (node.disabled) results.push(node);
3325
+ return results;
3326
+ },
3327
+
3328
+ 'checked': function(nodes, value, root) {
3329
+ for (var i = 0, results = [], node; node = nodes[i]; i++)
3330
+ if (node.checked) results.push(node);
3331
+ return results;
3332
+ }
3333
+ },
3334
+
3335
+ operators: {
3336
+ '=': function(nv, v) { return nv == v; },
3337
+ '!=': function(nv, v) { return nv != v; },
3338
+ '^=': function(nv, v) { return nv.startsWith(v); },
3339
+ '$=': function(nv, v) { return nv.endsWith(v); },
3340
+ '*=': function(nv, v) { return nv.include(v); },
3341
+ '~=': function(nv, v) { return (' ' + nv + ' ').include(' ' + v + ' '); },
3342
+ '|=': function(nv, v) { return ('-' + nv.toUpperCase() + '-').include('-' + v.toUpperCase() + '-'); }
3343
+ },
3344
+
3345
+ matchElements: function(elements, expression) {
3346
+ var matches = new Selector(expression).findElements(), h = Selector.handlers;
3347
+ h.mark(matches);
3348
+ for (var i = 0, results = [], element; element = elements[i]; i++)
3349
+ if (element._counted) results.push(element);
3350
+ h.unmark(matches);
3351
+ return results;
3352
+ },
3353
+
3354
+ findElement: function(elements, expression, index) {
3355
+ if (Object.isNumber(expression)) {
3356
+ index = expression; expression = false;
3357
+ }
3358
+ return Selector.matchElements(elements, expression || '*')[index || 0];
3359
+ },
3360
+
3361
+ findChildElements: function(element, expressions) {
3362
+ var exprs = expressions.join(',');
3363
+ expressions = [];
3364
+ exprs.scan(/(([\w#:.~>+()\s-]+|\*|\[.*?\])+)\s*(,|$)/, function(m) {
3365
+ expressions.push(m[1].strip());
3366
+ });
3367
+ var results = [], h = Selector.handlers;
3368
+ for (var i = 0, l = expressions.length, selector; i < l; i++) {
3369
+ selector = new Selector(expressions[i].strip());
3370
+ h.concat(results, selector.findElements(element));
3371
+ }
3372
+ return (l > 1) ? h.unique(results) : results;
3373
+ }
3374
+ });
3375
+
3376
+ if (Prototype.Browser.IE) {
3377
+ // IE returns comment nodes on getElementsByTagName("*").
3378
+ // Filter them out.
3379
+ Selector.handlers.concat = function(a, b) {
3380
+ for (var i = 0, node; node = b[i]; i++)
3381
+ if (node.tagName !== "!") a.push(node);
3382
+ return a;
3383
+ };
3384
+ }
3385
+
3386
+ function $$() {
3387
+ return Selector.findChildElements(document, $A(arguments));
3388
+ }
3389
+ var Form = {
3390
+ reset: function(form) {
3391
+ $(form).reset();
3392
+ return form;
3393
+ },
3394
+
3395
+ serializeElements: function(elements, options) {
3396
+ if (typeof options != 'object') options = { hash: !!options };
3397
+ else if (Object.isUndefined(options.hash)) options.hash = true;
3398
+ var key, value, submitted = false, submit = options.submit;
3399
+
3400
+ var data = elements.inject({ }, function(result, element) {
3401
+ if (!element.disabled && element.name) {
3402
+ key = element.name; value = $(element).getValue();
3403
+ if (value != null && (element.type != 'submit' || (!submitted &&
3404
+ submit !== false && (!submit || key == submit) && (submitted = true)))) {
3405
+ if (key in result) {
3406
+ // a key is already present; construct an array of values
3407
+ if (!Object.isArray(result[key])) result[key] = [result[key]];
3408
+ result[key].push(value);
3409
+ }
3410
+ else result[key] = value;
3411
+ }
3412
+ }
3413
+ return result;
3414
+ });
3415
+
3416
+ return options.hash ? data : Object.toQueryString(data);
3417
+ }
3418
+ };
3419
+
3420
+ Form.Methods = {
3421
+ serialize: function(form, options) {
3422
+ return Form.serializeElements(Form.getElements(form), options);
3423
+ },
3424
+
3425
+ getElements: function(form) {
3426
+ return $A($(form).getElementsByTagName('*')).inject([],
3427
+ function(elements, child) {
3428
+ if (Form.Element.Serializers[child.tagName.toLowerCase()])
3429
+ elements.push(Element.extend(child));
3430
+ return elements;
3431
+ }
3432
+ );
3433
+ },
3434
+
3435
+ getInputs: function(form, typeName, name) {
3436
+ form = $(form);
3437
+ var inputs = form.getElementsByTagName('input');
3438
+
3439
+ if (!typeName && !name) return $A(inputs).map(Element.extend);
3440
+
3441
+ for (var i = 0, matchingInputs = [], length = inputs.length; i < length; i++) {
3442
+ var input = inputs[i];
3443
+ if ((typeName && input.type != typeName) || (name && input.name != name))
3444
+ continue;
3445
+ matchingInputs.push(Element.extend(input));
3446
+ }
3447
+
3448
+ return matchingInputs;
3449
+ },
3450
+
3451
+ disable: function(form) {
3452
+ form = $(form);
3453
+ Form.getElements(form).invoke('disable');
3454
+ return form;
3455
+ },
3456
+
3457
+ enable: function(form) {
3458
+ form = $(form);
3459
+ Form.getElements(form).invoke('enable');
3460
+ return form;
3461
+ },
3462
+
3463
+ findFirstElement: function(form) {
3464
+ var elements = $(form).getElements().findAll(function(element) {
3465
+ return 'hidden' != element.type && !element.disabled;
3466
+ });
3467
+ var firstByIndex = elements.findAll(function(element) {
3468
+ return element.hasAttribute('tabIndex') && element.tabIndex >= 0;
3469
+ }).sortBy(function(element) { return element.tabIndex }).first();
3470
+
3471
+ return firstByIndex ? firstByIndex : elements.find(function(element) {
3472
+ return ['input', 'select', 'textarea'].include(element.tagName.toLowerCase());
3473
+ });
3474
+ },
3475
+
3476
+ focusFirstElement: function(form) {
3477
+ form = $(form);
3478
+ form.findFirstElement().activate();
3479
+ return form;
3480
+ },
3481
+
3482
+ request: function(form, options) {
3483
+ form = $(form), options = Object.clone(options || { });
3484
+
3485
+ var params = options.parameters, action = form.readAttribute('action') || '';
3486
+ if (action.blank()) action = window.location.href;
3487
+ options.parameters = form.serialize(true);
3488
+
3489
+ if (params) {
3490
+ if (Object.isString(params)) params = params.toQueryParams();
3491
+ Object.extend(options.parameters, params);
3492
+ }
3493
+
3494
+ if (form.hasAttribute('method') && !options.method)
3495
+ options.method = form.method;
3496
+
3497
+ return new Ajax.Request(action, options);
3498
+ }
3499
+ };
3500
+
3501
+ /*--------------------------------------------------------------------------*/
3502
+
3503
+ Form.Element = {
3504
+ focus: function(element) {
3505
+ $(element).focus();
3506
+ return element;
3507
+ },
3508
+
3509
+ select: function(element) {
3510
+ $(element).select();
3511
+ return element;
3512
+ }
3513
+ };
3514
+
3515
+ Form.Element.Methods = {
3516
+ serialize: function(element) {
3517
+ element = $(element);
3518
+ if (!element.disabled && element.name) {
3519
+ var value = element.getValue();
3520
+ if (value != undefined) {
3521
+ var pair = { };
3522
+ pair[element.name] = value;
3523
+ return Object.toQueryString(pair);
3524
+ }
3525
+ }
3526
+ return '';
3527
+ },
3528
+
3529
+ getValue: function(element) {
3530
+ element = $(element);
3531
+ var method = element.tagName.toLowerCase();
3532
+ return Form.Element.Serializers[method](element);
3533
+ },
3534
+
3535
+ setValue: function(element, value) {
3536
+ element = $(element);
3537
+ var method = element.tagName.toLowerCase();
3538
+ Form.Element.Serializers[method](element, value);
3539
+ return element;
3540
+ },
3541
+
3542
+ clear: function(element) {
3543
+ $(element).value = '';
3544
+ return element;
3545
+ },
3546
+
3547
+ present: function(element) {
3548
+ return $(element).value != '';
3549
+ },
3550
+
3551
+ activate: function(element) {
3552
+ element = $(element);
3553
+ try {
3554
+ element.focus();
3555
+ if (element.select && (element.tagName.toLowerCase() != 'input' ||
3556
+ !['button', 'reset', 'submit'].include(element.type)))
3557
+ element.select();
3558
+ } catch (e) { }
3559
+ return element;
3560
+ },
3561
+
3562
+ disable: function(element) {
3563
+ element = $(element);
3564
+ element.blur();
3565
+ element.disabled = true;
3566
+ return element;
3567
+ },
3568
+
3569
+ enable: function(element) {
3570
+ element = $(element);
3571
+ element.disabled = false;
3572
+ return element;
3573
+ }
3574
+ };
3575
+
3576
+ /*--------------------------------------------------------------------------*/
3577
+
3578
+ var Field = Form.Element;
3579
+ var $F = Form.Element.Methods.getValue;
3580
+
3581
+ /*--------------------------------------------------------------------------*/
3582
+
3583
+ Form.Element.Serializers = {
3584
+ input: function(element, value) {
3585
+ switch (element.type.toLowerCase()) {
3586
+ case 'checkbox':
3587
+ case 'radio':
3588
+ return Form.Element.Serializers.inputSelector(element, value);
3589
+ default:
3590
+ return Form.Element.Serializers.textarea(element, value);
3591
+ }
3592
+ },
3593
+
3594
+ inputSelector: function(element, value) {
3595
+ if (Object.isUndefined(value)) return element.checked ? element.value : null;
3596
+ else element.checked = !!value;
3597
+ },
3598
+
3599
+ textarea: function(element, value) {
3600
+ if (Object.isUndefined(value)) return element.value;
3601
+ else element.value = value;
3602
+ },
3603
+
3604
+ select: function(element, index) {
3605
+ if (Object.isUndefined(index))
3606
+ return this[element.type == 'select-one' ?
3607
+ 'selectOne' : 'selectMany'](element);
3608
+ else {
3609
+ var opt, value, single = !Object.isArray(index);
3610
+ for (var i = 0, length = element.length; i < length; i++) {
3611
+ opt = element.options[i];
3612
+ value = this.optionValue(opt);
3613
+ if (single) {
3614
+ if (value == index) {
3615
+ opt.selected = true;
3616
+ return;
3617
+ }
3618
+ }
3619
+ else opt.selected = index.include(value);
3620
+ }
3621
+ }
3622
+ },
3623
+
3624
+ selectOne: function(element) {
3625
+ var index = element.selectedIndex;
3626
+ return index >= 0 ? this.optionValue(element.options[index]) : null;
3627
+ },
3628
+
3629
+ selectMany: function(element) {
3630
+ var values, length = element.length;
3631
+ if (!length) return null;
3632
+
3633
+ for (var i = 0, values = []; i < length; i++) {
3634
+ var opt = element.options[i];
3635
+ if (opt.selected) values.push(this.optionValue(opt));
3636
+ }
3637
+ return values;
3638
+ },
3639
+
3640
+ optionValue: function(opt) {
3641
+ // extend element because hasAttribute may not be native
3642
+ return Element.extend(opt).hasAttribute('value') ? opt.value : opt.text;
3643
+ }
3644
+ };
3645
+
3646
+ /*--------------------------------------------------------------------------*/
3647
+
3648
+ Abstract.TimedObserver = Class.create(PeriodicalExecuter, {
3649
+ initialize: function($super, element, frequency, callback) {
3650
+ $super(callback, frequency);
3651
+ this.element = $(element);
3652
+ this.lastValue = this.getValue();
3653
+ },
3654
+
3655
+ execute: function() {
3656
+ var value = this.getValue();
3657
+ if (Object.isString(this.lastValue) && Object.isString(value) ?
3658
+ this.lastValue != value : String(this.lastValue) != String(value)) {
3659
+ this.callback(this.element, value);
3660
+ this.lastValue = value;
3661
+ }
3662
+ }
3663
+ });
3664
+
3665
+ Form.Element.Observer = Class.create(Abstract.TimedObserver, {
3666
+ getValue: function() {
3667
+ return Form.Element.getValue(this.element);
3668
+ }
3669
+ });
3670
+
3671
+ Form.Observer = Class.create(Abstract.TimedObserver, {
3672
+ getValue: function() {
3673
+ return Form.serialize(this.element);
3674
+ }
3675
+ });
3676
+
3677
+ /*--------------------------------------------------------------------------*/
3678
+
3679
+ Abstract.EventObserver = Class.create({
3680
+ initialize: function(element, callback) {
3681
+ this.element = $(element);
3682
+ this.callback = callback;
3683
+
3684
+ this.lastValue = this.getValue();
3685
+ if (this.element.tagName.toLowerCase() == 'form')
3686
+ this.registerFormCallbacks();
3687
+ else
3688
+ this.registerCallback(this.element);
3689
+ },
3690
+
3691
+ onElementEvent: function() {
3692
+ var value = this.getValue();
3693
+ if (this.lastValue != value) {
3694
+ this.callback(this.element, value);
3695
+ this.lastValue = value;
3696
+ }
3697
+ },
3698
+
3699
+ registerFormCallbacks: function() {
3700
+ Form.getElements(this.element).each(this.registerCallback, this);
3701
+ },
3702
+
3703
+ registerCallback: function(element) {
3704
+ if (element.type) {
3705
+ switch (element.type.toLowerCase()) {
3706
+ case 'checkbox':
3707
+ case 'radio':
3708
+ Event.observe(element, 'click', this.onElementEvent.bind(this));
3709
+ break;
3710
+ default:
3711
+ Event.observe(element, 'change', this.onElementEvent.bind(this));
3712
+ break;
3713
+ }
3714
+ }
3715
+ }
3716
+ });
3717
+
3718
+ Form.Element.EventObserver = Class.create(Abstract.EventObserver, {
3719
+ getValue: function() {
3720
+ return Form.Element.getValue(this.element);
3721
+ }
3722
+ });
3723
+
3724
+ Form.EventObserver = Class.create(Abstract.EventObserver, {
3725
+ getValue: function() {
3726
+ return Form.serialize(this.element);
3727
+ }
3728
+ });
3729
+ if (!window.Event) var Event = { };
3730
+
3731
+ Object.extend(Event, {
3732
+ KEY_BACKSPACE: 8,
3733
+ KEY_TAB: 9,
3734
+ KEY_RETURN: 13,
3735
+ KEY_ESC: 27,
3736
+ KEY_LEFT: 37,
3737
+ KEY_UP: 38,
3738
+ KEY_RIGHT: 39,
3739
+ KEY_DOWN: 40,
3740
+ KEY_DELETE: 46,
3741
+ KEY_HOME: 36,
3742
+ KEY_END: 35,
3743
+ KEY_PAGEUP: 33,
3744
+ KEY_PAGEDOWN: 34,
3745
+ KEY_INSERT: 45,
3746
+
3747
+ cache: { },
3748
+
3749
+ relatedTarget: function(event) {
3750
+ var element;
3751
+ switch(event.type) {
3752
+ case 'mouseover': element = event.fromElement; break;
3753
+ case 'mouseout': element = event.toElement; break;
3754
+ default: return null;
3755
+ }
3756
+ return Element.extend(element);
3757
+ }
3758
+ });
3759
+
3760
+ Event.Methods = (function() {
3761
+ var isButton;
3762
+
3763
+ if (Prototype.Browser.IE) {
3764
+ var buttonMap = { 0: 1, 1: 4, 2: 2 };
3765
+ isButton = function(event, code) {
3766
+ return event.button == buttonMap[code];
3767
+ };
3768
+
3769
+ } else if (Prototype.Browser.WebKit) {
3770
+ isButton = function(event, code) {
3771
+ switch (code) {
3772
+ case 0: return event.which == 1 && !event.metaKey;
3773
+ case 1: return event.which == 1 && event.metaKey;
3774
+ default: return false;
3775
+ }
3776
+ };
3777
+
3778
+ } else {
3779
+ isButton = function(event, code) {
3780
+ return event.which ? (event.which === code + 1) : (event.button === code);
3781
+ };
3782
+ }
3783
+
3784
+ return {
3785
+ isLeftClick: function(event) { return isButton(event, 0) },
3786
+ isMiddleClick: function(event) { return isButton(event, 1) },
3787
+ isRightClick: function(event) { return isButton(event, 2) },
3788
+
3789
+ element: function(event) {
3790
+ var node = Event.extend(event).target;
3791
+ return Element.extend(node.nodeType == Node.TEXT_NODE ? node.parentNode : node);
3792
+ },
3793
+
3794
+ findElement: function(event, expression) {
3795
+ var element = Event.element(event);
3796
+ if (!expression) return element;
3797
+ var elements = [element].concat(element.ancestors());
3798
+ return Selector.findElement(elements, expression, 0);
3799
+ },
3800
+
3801
+ pointer: function(event) {
3802
+ return {
3803
+ x: event.pageX || (event.clientX +
3804
+ (document.documentElement.scrollLeft || document.body.scrollLeft)),
3805
+ y: event.pageY || (event.clientY +
3806
+ (document.documentElement.scrollTop || document.body.scrollTop))
3807
+ };
3808
+ },
3809
+
3810
+ pointerX: function(event) { return Event.pointer(event).x },
3811
+ pointerY: function(event) { return Event.pointer(event).y },
3812
+
3813
+ stop: function(event) {
3814
+ Event.extend(event);
3815
+ event.preventDefault();
3816
+ event.stopPropagation();
3817
+ event.stopped = true;
3818
+ }
3819
+ };
3820
+ })();
3821
+
3822
+ Event.extend = (function() {
3823
+ var methods = Object.keys(Event.Methods).inject({ }, function(m, name) {
3824
+ m[name] = Event.Methods[name].methodize();
3825
+ return m;
3826
+ });
3827
+
3828
+ if (Prototype.Browser.IE) {
3829
+ Object.extend(methods, {
3830
+ stopPropagation: function() { this.cancelBubble = true },
3831
+ preventDefault: function() { this.returnValue = false },
3832
+ inspect: function() { return "[object Event]" }
3833
+ });
3834
+
3835
+ return function(event) {
3836
+ if (!event) return false;
3837
+ if (event._extendedByPrototype) return event;
3838
+
3839
+ event._extendedByPrototype = Prototype.emptyFunction;
3840
+ var pointer = Event.pointer(event);
3841
+ Object.extend(event, {
3842
+ target: event.srcElement,
3843
+ relatedTarget: Event.relatedTarget(event),
3844
+ pageX: pointer.x,
3845
+ pageY: pointer.y
3846
+ });
3847
+ return Object.extend(event, methods);
3848
+ };
3849
+
3850
+ } else {
3851
+ Event.prototype = Event.prototype || document.createEvent("HTMLEvents").__proto__;
3852
+ Object.extend(Event.prototype, methods);
3853
+ return Prototype.K;
3854
+ }
3855
+ })();
3856
+
3857
+ Object.extend(Event, (function() {
3858
+ var cache = Event.cache;
3859
+
3860
+ function getEventID(element) {
3861
+ if (element._eventID) return element._eventID;
3862
+ arguments.callee.id = arguments.callee.id || 1;
3863
+ return element._eventID = ++arguments.callee.id;
3864
+ }
3865
+
3866
+ function getDOMEventName(eventName) {
3867
+ if (eventName && eventName.include(':')) return "dataavailable";
3868
+ return eventName;
3869
+ }
3870
+
3871
+ function getCacheForID(id) {
3872
+ return cache[id] = cache[id] || { };
3873
+ }
3874
+
3875
+ function getWrappersForEventName(id, eventName) {
3876
+ var c = getCacheForID(id);
3877
+ return c[eventName] = c[eventName] || [];
3878
+ }
3879
+
3880
+ function createWrapper(element, eventName, handler) {
3881
+ var id = getEventID(element);
3882
+ var c = getWrappersForEventName(id, eventName);
3883
+ if (c.pluck("handler").include(handler)) return false;
3884
+
3885
+ var wrapper = function(event) {
3886
+ if (!Event || !Event.extend ||
3887
+ (event.eventName && event.eventName != eventName))
3888
+ return false;
3889
+
3890
+ Event.extend(event);
3891
+ handler.call(element, event)
3892
+ };
3893
+
3894
+ wrapper.handler = handler;
3895
+ c.push(wrapper);
3896
+ return wrapper;
3897
+ }
3898
+
3899
+ function findWrapper(id, eventName, handler) {
3900
+ var c = getWrappersForEventName(id, eventName);
3901
+ return c.find(function(wrapper) { return wrapper.handler == handler });
3902
+ }
3903
+
3904
+ function destroyWrapper(id, eventName, handler) {
3905
+ var c = getCacheForID(id);
3906
+ if (!c[eventName]) return false;
3907
+ c[eventName] = c[eventName].without(findWrapper(id, eventName, handler));
3908
+ }
3909
+
3910
+ function destroyCache() {
3911
+ for (var id in cache)
3912
+ for (var eventName in cache[id])
3913
+ cache[id][eventName] = null;
3914
+ }
3915
+
3916
+ if (window.attachEvent) {
3917
+ window.attachEvent("onunload", destroyCache);
3918
+ }
3919
+
3920
+ return {
3921
+ observe: function(element, eventName, handler) {
3922
+ element = $(element);
3923
+ var name = getDOMEventName(eventName);
3924
+
3925
+ var wrapper = createWrapper(element, eventName, handler);
3926
+ if (!wrapper) return element;
3927
+
3928
+ if (element.addEventListener) {
3929
+ element.addEventListener(name, wrapper, false);
3930
+ } else {
3931
+ element.attachEvent("on" + name, wrapper);
3932
+ }
3933
+
3934
+ return element;
3935
+ },
3936
+
3937
+ stopObserving: function(element, eventName, handler) {
3938
+ element = $(element);
3939
+ var id = getEventID(element), name = getDOMEventName(eventName);
3940
+
3941
+ if (!handler && eventName) {
3942
+ getWrappersForEventName(id, eventName).each(function(wrapper) {
3943
+ element.stopObserving(eventName, wrapper.handler);
3944
+ });
3945
+ return element;
3946
+
3947
+ } else if (!eventName) {
3948
+ Object.keys(getCacheForID(id)).each(function(eventName) {
3949
+ element.stopObserving(eventName);
3950
+ });
3951
+ return element;
3952
+ }
3953
+
3954
+ var wrapper = findWrapper(id, eventName, handler);
3955
+ if (!wrapper) return element;
3956
+
3957
+ if (element.removeEventListener) {
3958
+ element.removeEventListener(name, wrapper, false);
3959
+ } else {
3960
+ element.detachEvent("on" + name, wrapper);
3961
+ }
3962
+
3963
+ destroyWrapper(id, eventName, handler);
3964
+
3965
+ return element;
3966
+ },
3967
+
3968
+ fire: function(element, eventName, memo) {
3969
+ element = $(element);
3970
+ if (element == document && document.createEvent && !element.dispatchEvent)
3971
+ element = document.documentElement;
3972
+
3973
+ if (document.createEvent) {
3974
+ var event = document.createEvent("HTMLEvents");
3975
+ event.initEvent("dataavailable", true, true);
3976
+ } else {
3977
+ var event = document.createEventObject();
3978
+ event.eventType = "ondataavailable";
3979
+ }
3980
+
3981
+ event.eventName = eventName;
3982
+ event.memo = memo || { };
3983
+
3984
+ if (document.createEvent) {
3985
+ element.dispatchEvent(event);
3986
+ } else {
3987
+ element.fireEvent(event.eventType, event);
3988
+ }
3989
+
3990
+ return Event.extend(event);
3991
+ }
3992
+ };
3993
+ })());
3994
+
3995
+ Object.extend(Event, Event.Methods);
3996
+
3997
+ Element.addMethods({
3998
+ fire: Event.fire,
3999
+ observe: Event.observe,
4000
+ stopObserving: Event.stopObserving
4001
+ });
4002
+
4003
+ Object.extend(document, {
4004
+ fire: Element.Methods.fire.methodize(),
4005
+ observe: Element.Methods.observe.methodize(),
4006
+ stopObserving: Element.Methods.stopObserving.methodize()
4007
+ });
4008
+
4009
+ (function() {
4010
+ /* Support for the DOMContentLoaded event is based on work by Dan Webb,
4011
+ Matthias Miller, Dean Edwards and John Resig. */
4012
+
4013
+ var timer, fired = false;
4014
+
4015
+ function fireContentLoadedEvent() {
4016
+ if (fired) return;
4017
+ if (timer) window.clearInterval(timer);
4018
+ document.fire("dom:loaded");
4019
+ fired = true;
4020
+ }
4021
+
4022
+ if (document.addEventListener) {
4023
+ if (Prototype.Browser.WebKit) {
4024
+ timer = window.setInterval(function() {
4025
+ if (/loaded|complete/.test(document.readyState))
4026
+ fireContentLoadedEvent();
4027
+ }, 0);
4028
+
4029
+ Event.observe(window, "load", fireContentLoadedEvent);
4030
+
4031
+ } else {
4032
+ document.addEventListener("DOMContentLoaded",
4033
+ fireContentLoadedEvent, false);
4034
+ }
4035
+
4036
+ } else {
4037
+ document.write("<script id=__onDOMContentLoaded defer src=//:><\/script>");
4038
+ $("__onDOMContentLoaded").onreadystatechange = function() {
4039
+ if (this.readyState == "complete") {
4040
+ this.onreadystatechange = null;
4041
+ fireContentLoadedEvent();
4042
+ }
4043
+ };
4044
+ }
4045
+ })();
4046
+ /*------------------------------- DEPRECATED -------------------------------*/
4047
+
4048
+ Hash.toQueryString = Object.toQueryString;
4049
+
4050
+ var Toggle = { display: Element.toggle };
4051
+
4052
+ Element.Methods.childOf = Element.Methods.descendantOf;
4053
+
4054
+ var Insertion = {
4055
+ Before: function(element, content) {
4056
+ return Element.insert(element, {before:content});
4057
+ },
4058
+
4059
+ Top: function(element, content) {
4060
+ return Element.insert(element, {top:content});
4061
+ },
4062
+
4063
+ Bottom: function(element, content) {
4064
+ return Element.insert(element, {bottom:content});
4065
+ },
4066
+
4067
+ After: function(element, content) {
4068
+ return Element.insert(element, {after:content});
4069
+ }
4070
+ };
4071
+
4072
+ var $continue = new Error('"throw $continue" is deprecated, use "return" instead');
4073
+
4074
+ // This should be moved to script.aculo.us; notice the deprecated methods
4075
+ // further below, that map to the newer Element methods.
4076
+ var Position = {
4077
+ // set to true if needed, warning: firefox performance problems
4078
+ // NOT neeeded for page scrolling, only if draggable contained in
4079
+ // scrollable elements
4080
+ includeScrollOffsets: false,
4081
+
4082
+ // must be called before calling withinIncludingScrolloffset, every time the
4083
+ // page is scrolled
4084
+ prepare: function() {
4085
+ this.deltaX = window.pageXOffset
4086
+ || document.documentElement.scrollLeft
4087
+ || document.body.scrollLeft
4088
+ || 0;
4089
+ this.deltaY = window.pageYOffset
4090
+ || document.documentElement.scrollTop
4091
+ || document.body.scrollTop
4092
+ || 0;
4093
+ },
4094
+
4095
+ // caches x/y coordinate pair to use with overlap
4096
+ within: function(element, x, y) {
4097
+ if (this.includeScrollOffsets)
4098
+ return this.withinIncludingScrolloffsets(element, x, y);
4099
+ this.xcomp = x;
4100
+ this.ycomp = y;
4101
+ this.offset = Element.cumulativeOffset(element);
4102
+
4103
+ return (y >= this.offset[1] &&
4104
+ y < this.offset[1] + element.offsetHeight &&
4105
+ x >= this.offset[0] &&
4106
+ x < this.offset[0] + element.offsetWidth);
4107
+ },
4108
+
4109
+ withinIncludingScrolloffsets: function(element, x, y) {
4110
+ var offsetcache = Element.cumulativeScrollOffset(element);
4111
+
4112
+ this.xcomp = x + offsetcache[0] - this.deltaX;
4113
+ this.ycomp = y + offsetcache[1] - this.deltaY;
4114
+ this.offset = Element.cumulativeOffset(element);
4115
+
4116
+ return (this.ycomp >= this.offset[1] &&
4117
+ this.ycomp < this.offset[1] + element.offsetHeight &&
4118
+ this.xcomp >= this.offset[0] &&
4119
+ this.xcomp < this.offset[0] + element.offsetWidth);
4120
+ },
4121
+
4122
+ // within must be called directly before
4123
+ overlap: function(mode, element) {
4124
+ if (!mode) return 0;
4125
+ if (mode == 'vertical')
4126
+ return ((this.offset[1] + element.offsetHeight) - this.ycomp) /
4127
+ element.offsetHeight;
4128
+ if (mode == 'horizontal')
4129
+ return ((this.offset[0] + element.offsetWidth) - this.xcomp) /
4130
+ element.offsetWidth;
4131
+ },
4132
+
4133
+ // Deprecation layer -- use newer Element methods now (1.5.2).
4134
+
4135
+ cumulativeOffset: Element.Methods.cumulativeOffset,
4136
+
4137
+ positionedOffset: Element.Methods.positionedOffset,
4138
+
4139
+ absolutize: function(element) {
4140
+ Position.prepare();
4141
+ return Element.absolutize(element);
4142
+ },
4143
+
4144
+ relativize: function(element) {
4145
+ Position.prepare();
4146
+ return Element.relativize(element);
4147
+ },
4148
+
4149
+ realOffset: Element.Methods.cumulativeScrollOffset,
4150
+
4151
+ offsetParent: Element.Methods.getOffsetParent,
4152
+
4153
+ page: Element.Methods.viewportOffset,
4154
+
4155
+ clone: function(source, target, options) {
4156
+ options = options || { };
4157
+ return Element.clonePosition(target, source, options);
4158
+ }
4159
+ };
4160
+
4161
+ /*--------------------------------------------------------------------------*/
4162
+
4163
+ if (!document.getElementsByClassName) document.getElementsByClassName = function(instanceMethods){
4164
+ function iter(name) {
4165
+ return name.blank() ? null : "[contains(concat(' ', @class, ' '), ' " + name + " ')]";
4166
+ }
4167
+
4168
+ instanceMethods.getElementsByClassName = Prototype.BrowserFeatures.XPath ?
4169
+ function(element, className) {
4170
+ className = className.toString().strip();
4171
+ var cond = /\s/.test(className) ? $w(className).map(iter).join('') : iter(className);
4172
+ return cond ? document._getElementsByXPath('.//*' + cond, element) : [];
4173
+ } : function(element, className) {
4174
+ className = className.toString().strip();
4175
+ var elements = [], classNames = (/\s/.test(className) ? $w(className) : null);
4176
+ if (!classNames && !className) return elements;
4177
+
4178
+ var nodes = $(element).getElementsByTagName('*');
4179
+ className = ' ' + className + ' ';
4180
+
4181
+ for (var i = 0, child, cn; child = nodes[i]; i++) {
4182
+ if (child.className && (cn = ' ' + child.className + ' ') && (cn.include(className) ||
4183
+ (classNames && classNames.all(function(name) {
4184
+ return !name.toString().blank() && cn.include(' ' + name + ' ');
4185
+ }))))
4186
+ elements.push(Element.extend(child));
4187
+ }
4188
+ return elements;
4189
+ };
4190
+
4191
+ return function(className, parentElement) {
4192
+ return $(parentElement || document.body).getElementsByClassName(className);
4193
+ };
4194
+ }(Element.Methods);
4195
+
4196
+ /*--------------------------------------------------------------------------*/
4197
+
4198
+ Element.ClassNames = Class.create();
4199
+ Element.ClassNames.prototype = {
4200
+ initialize: function(element) {
4201
+ this.element = $(element);
4202
+ },
4203
+
4204
+ _each: function(iterator) {
4205
+ this.element.className.split(/\s+/).select(function(name) {
4206
+ return name.length > 0;
4207
+ })._each(iterator);
4208
+ },
4209
+
4210
+ set: function(className) {
4211
+ this.element.className = className;
4212
+ },
4213
+
4214
+ add: function(classNameToAdd) {
4215
+ if (this.include(classNameToAdd)) return;
4216
+ this.set($A(this).concat(classNameToAdd).join(' '));
4217
+ },
4218
+
4219
+ remove: function(classNameToRemove) {
4220
+ if (!this.include(classNameToRemove)) return;
4221
+ this.set($A(this).without(classNameToRemove).join(' '));
4222
+ },
4223
+
4224
+ toString: function() {
4225
+ return $A(this).join(' ');
4226
+ }
4227
+ };
4228
+
4229
+ Object.extend(Element.ClassNames.prototype, Enumerable);
4230
+
4231
+ /*--------------------------------------------------------------------------*/
4232
+
4233
+ Element.addMethods();