newjs 1.5.0 → 1.5.1

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