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