3 MooTools - My Object Oriented JavaScript Tools.
9 Copyright (c) 2006-2008 [Valerio Proietti](http://mad4milk.net/).
12 [The MooTools production team](http://mootools.net/developers/).
15 - Class implementation inspired by [Base.js](http://dean.edwards.name/weblog/2006/03/base/) Copyright (c) 2006 Dean Edwards, [GNU Lesser General Public License](http://opensource.org/licenses/lgpl-license.php)
16 - Some functionality inspired by [Prototype.js](http://prototypejs.org) Copyright (c) 2005-2007 Sam Stephenson, [MIT License](http://opensource.org/licenses/mit-license.php)
21 'build': 'f0491d62fbb7e906789aa3733d6a67d43e5af7c9'
24 var Native = function(options){
25 options = options || {};
26 var name = options.name;
27 var legacy = options.legacy;
28 var protect = options.protect;
29 var methods = options.implement;
30 var generics = options.generics;
31 var initialize = options.initialize;
32 var afterImplement = options.afterImplement || function(){};
33 var object = initialize || legacy;
34 generics = generics !== false;
36 object.constructor = Native;
37 object.$family = {name: 'native'};
38 if (legacy && initialize) object.prototype = legacy.prototype;
39 object.prototype.constructor = object;
42 var family = name.toLowerCase();
43 object.prototype.$family = {name: family};
44 Native.typize(object, family);
47 var add = function(obj, name, method, force){
48 if (!protect || force || !obj.prototype[name]) obj.prototype[name] = method;
49 if (generics) Native.genericize(obj, name, protect);
50 afterImplement.call(obj, name, method);
54 object.alias = function(a1, a2, a3){
55 if (typeof a1 == 'string'){
56 if ((a1 = this.prototype[a1])) return add(this, a2, a1, a3);
58 for (var a in a1) this.alias(a, a1[a], a2);
62 object.implement = function(a1, a2, a3){
63 if (typeof a1 == 'string') return add(this, a1, a2, a3);
64 for (var p in a1) add(this, p, a1[p], a2);
68 if (methods) object.implement(methods);
73 Native.genericize = function(object, property, check){
74 if ((!check || !object[property]) && typeof object.prototype[property] == 'function') object[property] = function(){
75 var args = Array.prototype.slice.call(arguments);
76 return object.prototype[property].apply(args.shift(), args);
80 Native.implement = function(objects, properties){
81 for (var i = 0, l = objects.length; i < l; i++) objects[i].implement(properties);
84 Native.typize = function(object, family){
85 if (!object.type) object.type = function(item){
86 return ($type(item) === family);
91 var natives = {'Array': Array, 'Date': Date, 'Function': Function, 'Number': Number, 'RegExp': RegExp, 'String': String};
92 for (var n in natives) new Native({name: n, initialize: natives[n], protect: true});
94 var types = {'boolean': Boolean, 'native': Native, 'object': Object};
95 for (var t in types) Native.typize(types[t], t);
98 'Array': ["concat", "indexOf", "join", "lastIndexOf", "pop", "push", "reverse", "shift", "slice", "sort", "splice", "toString", "unshift", "valueOf"],
99 'String': ["charAt", "charCodeAt", "concat", "indexOf", "lastIndexOf", "match", "replace", "search", "slice", "split", "substr", "substring", "toLowerCase", "toUpperCase", "valueOf"]
101 for (var g in generics){
102 for (var i = generics[g].length; i--;) Native.genericize(window[g], generics[g][i], true);
106 var Hash = new Native({
110 initialize: function(object){
111 if ($type(object) == 'hash') object = $unlink(object.getClean());
112 for (var key in object) this[key] = object[key];
120 forEach: function(fn, bind){
121 for (var key in this){
122 if (this.hasOwnProperty(key)) fn.call(bind, this[key], key, this);
126 getClean: function(){
128 for (var key in this){
129 if (this.hasOwnProperty(key)) clean[key] = this[key];
134 getLength: function(){
136 for (var key in this){
137 if (this.hasOwnProperty(key)) length++;
144 Hash.alias('forEach', 'each');
148 forEach: function(fn, bind){
149 for (var i = 0, l = this.length; i < l; i++) fn.call(bind, this[i], i, this);
154 Array.alias('forEach', 'each');
156 function $A(iterable){
158 var l = iterable.length, array = new Array(l);
159 while (l--) array[l] = iterable[l];
162 return Array.prototype.slice.call(iterable);
165 function $arguments(i){
172 return !!(obj || obj === 0);
175 function $clear(timer){
177 clearInterval(timer);
181 function $defined(obj){
182 return (obj != undefined);
185 function $each(iterable, fn, bind){
186 var type = $type(iterable);
187 ((type == 'arguments' || type == 'collection' || type == 'array') ? Array : Hash).each(iterable, fn, bind);
192 function $extend(original, extended){
193 for (var key in (extended || {})) original[key] = extended[key];
198 return new Hash(object);
201 function $lambda(value){
202 return (typeof value == 'function') ? value : function(){
208 var args = Array.slice(arguments);
210 return $mixin.apply(null, args);
213 function $mixin(mix){
214 for (var i = 1, l = arguments.length; i < l; i++){
215 var object = arguments[i];
216 if ($type(object) != 'object') continue;
217 for (var key in object){
218 var op = object[key], mp = mix[key];
219 mix[key] = (mp && $type(op) == 'object' && $type(mp) == 'object') ? $mixin(mp, op) : $unlink(op);
226 for (var i = 0, l = arguments.length; i < l; i++){
227 if (arguments[i] != undefined) return arguments[i];
232 function $random(min, max){
233 return Math.floor(Math.random() * (max - min + 1) + min);
236 function $splat(obj){
237 var type = $type(obj);
238 return (type) ? ((type != 'array' && type != 'arguments') ? [obj] : obj) : [];
241 var $time = Date.now || function(){
246 for (var i = 0, l = arguments.length; i < l; i++){
248 return arguments[i]();
255 if (obj == undefined) return false;
256 if (obj.$family) return (obj.$family.name == 'number' && !isFinite(obj)) ? false : obj.$family.name;
258 switch (obj.nodeType){
259 case 1: return 'element';
260 case 3: return (/\S/).test(obj.nodeValue) ? 'textnode' : 'whitespace';
262 } else if (typeof obj.length == 'number'){
263 if (obj.callee) return 'arguments';
264 else if (obj.item) return 'collection';
269 function $unlink(object){
271 switch ($type(object)){
274 for (var p in object) unlinked[p] = $unlink(object[p]);
277 unlinked = new Hash(object);
281 for (var i = 0, l = object.length; i < l; i++) unlinked[i] = $unlink(object[i]);
283 default: return object;
291 The Browser Core. Contains Browser initialization, Window and Document, and the Browser Hash.
297 var Browser = $merge({
299 Engine: {name: 'unknown', version: 0},
301 Platform: {name: (window.orientation != undefined) ? 'ipod' : (navigator.platform.match(/mac|win|linux/i) || ['other'])[0].toLowerCase()},
303 Features: {xpath: !!(document.evaluate), air: !!(window.runtime), query: !!(document.querySelector)},
310 return (!window.opera) ? false : ((arguments.callee.caller) ? 960 : ((document.getElementsByClassName) ? 950 : 925));
314 return (!window.ActiveXObject) ? false : ((window.XMLHttpRequest) ? 5 : 4);
318 return (navigator.taintEnabled) ? false : ((Browser.Features.xpath) ? ((Browser.Features.query) ? 525 : 420) : 419);
322 return (document.getBoxObjectFor == undefined) ? false : ((document.getElementsByClassName) ? 19 : 18);
329 Browser.Platform[Browser.Platform.name] = true;
331 Browser.detect = function(){
333 for (var engine in this.Engines){
334 var version = this.Engines[engine]();
336 this.Engine = {name: engine, version: version};
337 this.Engine[engine] = this.Engine[engine + version] = true;
342 return {name: engine, version: version};
348 Browser.Request = function(){
349 return $try(function(){
350 return new XMLHttpRequest();
352 return new ActiveXObject('MSXML2.XMLHTTP');
356 Browser.Features.xhr = !!(Browser.Request());
358 Browser.Plugins.Flash = (function(){
359 var version = ($try(function(){
360 return navigator.plugins['Shockwave Flash'].description;
362 return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version');
363 }) || '0 r0').match(/\d+/g);
364 return {version: parseInt(version[0] || 0 + '.' + version[1], 10) || 0, build: parseInt(version[2], 10) || 0};
367 function $exec(text){
368 if (!text) return text;
369 if (window.execScript){
370 window.execScript(text);
372 var script = document.createElement('script');
373 script.setAttribute('type', 'text/javascript');
374 script[(Browser.Engine.webkit && Browser.Engine.version < 420) ? 'innerText' : 'text'] = text;
375 document.head.appendChild(script);
376 document.head.removeChild(script);
383 var $uid = (Browser.Engine.trident) ? function(item){
384 return (item.uid || (item.uid = [Native.UID++]))[0];
386 return item.uid || (item.uid = Native.UID++);
389 var Window = new Native({
393 legacy: (Browser.Engine.trident) ? null: window.Window,
395 initialize: function(win){
398 win.Element = $empty;
399 if (Browser.Engine.webkit) win.document.createElement("iframe"); //fixes safari 2
400 win.Element.prototype = (Browser.Engine.webkit) ? window["[[DOMElement.prototype]]"] : {};
402 win.document.window = win;
403 return $extend(win, Window.Prototype);
406 afterImplement: function(property, value){
407 window[property] = Window.Prototype[property] = value;
412 Window.Prototype = {$family: {name: 'window'}};
416 var Document = new Native({
420 legacy: (Browser.Engine.trident) ? null: window.Document,
422 initialize: function(doc){
424 doc.head = doc.getElementsByTagName('head')[0];
425 doc.html = doc.getElementsByTagName('html')[0];
426 if (Browser.Engine.trident && Browser.Engine.version <= 4) $try(function(){
427 doc.execCommand("BackgroundImageCache", false, true);
429 if (Browser.Engine.trident) doc.window.attachEvent('onunload', function() {
430 doc.window.detachEvent('onunload', arguments.callee);
431 doc.head = doc.html = doc.window = null;
433 return $extend(doc, Document.Prototype);
436 afterImplement: function(property, value){
437 document[property] = Document.Prototype[property] = value;
442 Document.Prototype = {$family: {name: 'document'}};
444 new Document(document);
449 Contains Array Prototypes like each, contains, and erase.
457 every: function(fn, bind){
458 for (var i = 0, l = this.length; i < l; i++){
459 if (!fn.call(bind, this[i], i, this)) return false;
464 filter: function(fn, bind){
466 for (var i = 0, l = this.length; i < l; i++){
467 if (fn.call(bind, this[i], i, this)) results.push(this[i]);
473 return this.filter($defined);
476 indexOf: function(item, from){
477 var len = this.length;
478 for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++){
479 if (this[i] === item) return i;
484 map: function(fn, bind){
486 for (var i = 0, l = this.length; i < l; i++) results[i] = fn.call(bind, this[i], i, this);
490 some: function(fn, bind){
491 for (var i = 0, l = this.length; i < l; i++){
492 if (fn.call(bind, this[i], i, this)) return true;
497 associate: function(keys){
498 var obj = {}, length = Math.min(this.length, keys.length);
499 for (var i = 0; i < length; i++) obj[keys[i]] = this[i];
503 link: function(object){
505 for (var i = 0, l = this.length; i < l; i++){
506 for (var key in object){
507 if (object[key](this[i])){
508 result[key] = this[i];
517 contains: function(item, from){
518 return this.indexOf(item, from) != -1;
521 extend: function(array){
522 for (var i = 0, j = array.length; i < j; i++) this.push(array[i]);
527 return (this.length) ? this[this.length - 1] : null;
530 getRandom: function(){
531 return (this.length) ? this[$random(0, this.length - 1)] : null;
534 include: function(item){
535 if (!this.contains(item)) this.push(item);
539 combine: function(array){
540 for (var i = 0, l = array.length; i < l; i++) this.include(array[i]);
544 erase: function(item){
545 for (var i = this.length; i--; i){
546 if (this[i] === item) this.splice(i, 1);
558 for (var i = 0, l = this.length; i < l; i++){
559 var type = $type(this[i]);
561 array = array.concat((type == 'array' || type == 'collection' || type == 'arguments') ? Array.flatten(this[i]) : this[i]);
566 hexToRgb: function(array){
567 if (this.length != 3) return null;
568 var rgb = this.map(function(value){
569 if (value.length == 1) value += value;
570 return value.toInt(16);
572 return (array) ? rgb : 'rgb(' + rgb + ')';
575 rgbToHex: function(array){
576 if (this.length < 3) return null;
577 if (this.length == 4 && this[3] == 0 && !array) return 'transparent';
579 for (var i = 0; i < 3; i++){
580 var bit = (this[i] - 0).toString(16);
581 hex.push((bit.length == 1) ? '0' + bit : bit);
583 return (array) ? hex : '#' + hex.join('');
591 Contains Function Prototypes like create, bind, pass, and delay.
599 extend: function(properties){
600 for (var property in properties) this[property] = properties[property];
604 create: function(options){
606 options = options || {};
607 return function(event){
608 var args = options.arguments;
609 args = (args != undefined) ? $splat(args) : Array.slice(arguments, (options.event) ? 1 : 0);
610 if (options.event) args = [event || window.event].extend(args);
611 var returns = function(){
612 return self.apply(options.bind || null, args);
614 if (options.delay) return setTimeout(returns, options.delay);
615 if (options.periodical) return setInterval(returns, options.periodical);
616 if (options.attempt) return $try(returns);
621 run: function(args, bind){
622 return this.apply(bind, $splat(args));
625 pass: function(args, bind){
626 return this.create({bind: bind, arguments: args});
629 bind: function(bind, args){
630 return this.create({bind: bind, arguments: args});
633 bindWithEvent: function(bind, args){
634 return this.create({bind: bind, arguments: args, event: true});
637 attempt: function(args, bind){
638 return this.create({bind: bind, arguments: args, attempt: true})();
641 delay: function(delay, bind, args){
642 return this.create({bind: bind, arguments: args, delay: delay})();
645 periodical: function(periodical, bind, args){
646 return this.create({bind: bind, arguments: args, periodical: periodical})();
654 Contains Number Prototypes like limit, round, times, and ceil.
662 limit: function(min, max){
663 return Math.min(max, Math.max(min, this));
666 round: function(precision){
667 precision = Math.pow(10, precision || 0);
668 return Math.round(this * precision) / precision;
671 times: function(fn, bind){
672 for (var i = 0; i < this; i++) fn.call(bind, i, this);
676 return parseFloat(this);
679 toInt: function(base){
680 return parseInt(this, base || 10);
685 Number.alias('times', 'each');
689 math.each(function(name){
690 if (!Number[name]) methods[name] = function(){
691 return Math[name].apply(null, [this].concat($A(arguments)));
694 Number.implement(methods);
695 })(['abs', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'exp', 'floor', 'log', 'max', 'min', 'pow', 'sin', 'sqrt', 'tan']);
700 Contains String Prototypes like camelCase, capitalize, test, and toInt.
708 test: function(regex, params){
709 return ((typeof regex == 'string') ? new RegExp(regex, params) : regex).test(this);
712 contains: function(string, separator){
713 return (separator) ? (separator + this + separator).indexOf(separator + string + separator) > -1 : this.indexOf(string) > -1;
717 return this.replace(/^\s+|\s+$/g, '');
721 return this.replace(/\s+/g, ' ').trim();
724 camelCase: function(){
725 return this.replace(/-\D/g, function(match){
726 return match.charAt(1).toUpperCase();
730 hyphenate: function(){
731 return this.replace(/[A-Z]/g, function(match){
732 return ('-' + match.charAt(0).toLowerCase());
736 capitalize: function(){
737 return this.replace(/\b[a-z]/g, function(match){
738 return match.toUpperCase();
742 escapeRegExp: function(){
743 return this.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1');
746 toInt: function(base){
747 return parseInt(this, base || 10);
751 return parseFloat(this);
754 hexToRgb: function(array){
755 var hex = this.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);
756 return (hex) ? hex.slice(1).hexToRgb(array) : null;
759 rgbToHex: function(array){
760 var rgb = this.match(/\d{1,3}/g);
761 return (rgb) ? rgb.rgbToHex(array) : null;
764 stripScripts: function(option){
766 var text = this.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function(){
767 scripts += arguments[1] + '\n';
770 if (option === true) $exec(scripts);
771 else if ($type(option) == 'function') option(scripts, text);
775 substitute: function(object, regexp){
776 return this.replace(regexp || (/\\?\{([^{}]+)\}/g), function(match, name){
777 if (match.charAt(0) == '\\') return match.slice(1);
778 return (object[name] != undefined) ? object[name] : '';
787 Contains Hash Prototypes. Provides a means for overcoming the JavaScript practical impossibility of extending native Objects.
795 has: Object.prototype.hasOwnProperty,
797 keyOf: function(value){
798 for (var key in this){
799 if (this.hasOwnProperty(key) && this[key] === value) return key;
804 hasValue: function(value){
805 return (Hash.keyOf(this, value) !== null);
808 extend: function(properties){
809 Hash.each(properties, function(value, key){
810 Hash.set(this, key, value);
815 combine: function(properties){
816 Hash.each(properties, function(value, key){
817 Hash.include(this, key, value);
822 erase: function(key){
823 if (this.hasOwnProperty(key)) delete this[key];
828 return (this.hasOwnProperty(key)) ? this[key] : null;
831 set: function(key, value){
832 if (!this[key] || this.hasOwnProperty(key)) this[key] = value;
837 Hash.each(this, function(value, key){
843 include: function(key, value){
844 if (this[key] == undefined) this[key] = value;
848 map: function(fn, bind){
849 var results = new Hash;
850 Hash.each(this, function(value, key){
851 results.set(key, fn.call(bind, value, key, this));
856 filter: function(fn, bind){
857 var results = new Hash;
858 Hash.each(this, function(value, key){
859 if (fn.call(bind, value, key, this)) results.set(key, value);
864 every: function(fn, bind){
865 for (var key in this){
866 if (this.hasOwnProperty(key) && !fn.call(bind, this[key], key)) return false;
871 some: function(fn, bind){
872 for (var key in this){
873 if (this.hasOwnProperty(key) && fn.call(bind, this[key], key)) return true;
880 Hash.each(this, function(value, key){
886 getValues: function(){
888 Hash.each(this, function(value){
894 toQueryString: function(base){
895 var queryString = [];
896 Hash.each(this, function(value, key){
897 if (base) key = base + '[' + key + ']';
899 switch ($type(value)){
900 case 'object': result = Hash.toQueryString(value, key); break;
903 value.each(function(val, i){
906 result = Hash.toQueryString(qs, key);
908 default: result = key + '=' + encodeURIComponent(value);
910 if (value != undefined) queryString.push(result);
913 return queryString.join('&');
918 Hash.alias({keyOf: 'indexOf', hasValue: 'contains'});
923 Contains the Event Native, to make the event object completely crossbrowser.
929 var Event = new Native({
933 initialize: function(event, win){
935 var doc = win.document;
936 event = event || win.event;
937 if (event.$extended) return event;
938 this.$extended = true;
939 var type = event.type;
940 var target = event.target || event.srcElement;
941 while (target && target.nodeType == 3) target = target.parentNode;
943 if (type.test(/key/)){
944 var code = event.which || event.keyCode;
945 var key = Event.Keys.keyOf(code);
946 if (type == 'keydown'){
947 var fKey = code - 111;
948 if (fKey > 0 && fKey < 13) key = 'f' + fKey;
950 key = key || String.fromCharCode(code).toLowerCase();
951 } else if (type.match(/(click|mouse|menu)/i)){
952 doc = (!doc.compatMode || doc.compatMode == 'CSS1Compat') ? doc.html : doc.body;
954 x: event.pageX || event.clientX + doc.scrollLeft,
955 y: event.pageY || event.clientY + doc.scrollTop
958 x: (event.pageX) ? event.pageX - win.pageXOffset : event.clientX,
959 y: (event.pageY) ? event.pageY - win.pageYOffset : event.clientY
961 if (type.match(/DOMMouseScroll|mousewheel/)){
962 var wheel = (event.wheelDelta) ? event.wheelDelta / 120 : -(event.detail || 0) / 3;
964 var rightClick = (event.which == 3) || (event.button == 2);
966 if (type.match(/over|out/)){
968 case 'mouseover': related = event.relatedTarget || event.fromElement; break;
969 case 'mouseout': related = event.relatedTarget || event.toElement;
972 while (related && related.nodeType == 3) related = related.parentNode;
974 }).create({attempt: Browser.Engine.gecko})()) related = false;
978 return $extend(this, {
984 rightClick: rightClick,
988 relatedTarget: related,
994 shift: event.shiftKey,
995 control: event.ctrlKey,
1003 Event.Keys = new Hash({
1019 return this.stopPropagation().preventDefault();
1022 stopPropagation: function(){
1023 if (this.event.stopPropagation) this.event.stopPropagation();
1024 else this.event.cancelBubble = true;
1028 preventDefault: function(){
1029 if (this.event.preventDefault) this.event.preventDefault();
1030 else this.event.returnValue = false;
1039 Contains the Class Function for easily creating, extending, and implementing reusable Classes.
1045 function Class(params){
1047 if (params instanceof Function) params = {initialize: params};
1049 var newClass = function(){
1051 if (newClass._prototyping) return this;
1052 this._current = $empty;
1053 var value = (this.initialize) ? this.initialize.apply(this, arguments) : this;
1054 delete this._current; delete this.caller;
1058 newClass.implement(params);
1060 newClass.constructor = Class;
1061 newClass.prototype.constructor = newClass;
1067 Function.prototype.protect = function(){
1068 this._protected = true;
1072 Object.reset = function(object, key){
1075 for (var p in object) Object.reset(object, p);
1081 switch ($type(object[key])){
1083 var F = function(){};
1084 F.prototype = object[key];
1086 object[key] = Object.reset(i);
1088 case 'array': object[key] = $unlink(object[key]); break;
1095 new Native({name: 'Class', initialize: Class}).extend({
1097 instantiate: function(F){
1098 F._prototyping = true;
1100 delete F._prototyping;
1104 wrap: function(self, key, method){
1105 if (method._origin) method = method._origin;
1108 if (method._protected && this._current == null) throw new Error('The method "' + key + '" cannot be called.');
1109 var caller = this.caller, current = this._current;
1110 this.caller = current; this._current = arguments.callee;
1111 var result = method.apply(this, arguments);
1112 this._current = current; this.caller = caller;
1114 }.extend({_owner: self, _origin: method, _name: key});
1122 implement: function(key, value){
1124 if ($type(key) == 'object'){
1125 for (var p in key) this.implement(p, key[p]);
1129 var mutator = Class.Mutators[key];
1132 value = mutator.call(this, value);
1133 if (value == null) return this;
1136 var proto = this.prototype;
1138 switch ($type(value)){
1141 if (value._hidden) return this;
1142 proto[key] = Class.wrap(this, key, value);
1146 var previous = proto[key];
1147 if ($type(previous) == 'object') $mixin(previous, value);
1148 else proto[key] = $unlink(value);
1152 proto[key] = $unlink(value);
1155 default: proto[key] = value;
1167 Extends: function(parent){
1169 this.parent = parent;
1170 this.prototype = Class.instantiate(parent);
1172 this.implement('parent', function(){
1173 var name = this.caller._name, previous = this.caller._owner.parent.prototype[name];
1174 if (!previous) throw new Error('The method "' + name + '" has no parent.');
1175 return previous.apply(this, arguments);
1180 Implements: function(items){
1181 $splat(items).each(function(item){
1182 if (item instanceof Function) item = Class.instantiate(item);
1183 this.implement(item);
1192 Script: Class.Extras.js
1193 Contains Utility Classes that can be implemented into your own Classes to ease the execution of many common tasks.
1199 var Chain = new Class({
1204 this.$chain.extend(Array.flatten(arguments));
1208 callChain: function(){
1209 return (this.$chain.length) ? this.$chain.shift().apply(this, arguments) : false;
1212 clearChain: function(){
1213 this.$chain.empty();
1219 var Events = new Class({
1223 addEvent: function(type, fn, internal){
1224 type = Events.removeOn(type);
1226 this.$events[type] = this.$events[type] || [];
1227 this.$events[type].include(fn);
1228 if (internal) fn.internal = true;
1233 addEvents: function(events){
1234 for (var type in events) this.addEvent(type, events[type]);
1238 fireEvent: function(type, args, delay){
1239 type = Events.removeOn(type);
1240 if (!this.$events || !this.$events[type]) return this;
1241 this.$events[type].each(function(fn){
1242 fn.create({'bind': this, 'delay': delay, 'arguments': args})();
1247 removeEvent: function(type, fn){
1248 type = Events.removeOn(type);
1249 if (!this.$events[type]) return this;
1250 if (!fn.internal) this.$events[type].erase(fn);
1254 removeEvents: function(events){
1256 if ($type(events) == 'object'){
1257 for (type in events) this.removeEvent(type, events[type]);
1260 if (events) events = Events.removeOn(events);
1261 for (type in this.$events){
1262 if (events && events != type) continue;
1263 var fns = this.$events[type];
1264 for (var i = fns.length; i--; i) this.removeEvent(type, fns[i]);
1271 Events.removeOn = function(string){
1272 return string.replace(/^on([A-Z])/, function(full, first) {
1273 return first.toLowerCase();
1277 var Options = new Class({
1279 setOptions: function(){
1280 this.options = $merge.run([this.options].extend(arguments));
1281 if (!this.addEvent) return this;
1282 for (var option in this.options){
1283 if ($type(this.options[option]) != 'function' || !(/^on[A-Z]/).test(option)) continue;
1284 this.addEvent(option, this.options[option]);
1285 delete this.options[option];
1295 One of the most important items in MooTools. Contains the dollar function, the dollars function, and an handful of cross-browser,
1296 time-saver methods to let you easily work with HTML Elements.
1302 var Element = new Native({
1306 legacy: window.Element,
1308 initialize: function(tag, props){
1309 var konstructor = Element.Constructors.get(tag);
1310 if (konstructor) return konstructor(props);
1311 if (typeof tag == 'string') return document.newElement(tag, props);
1312 return $(tag).set(props);
1315 afterImplement: function(key, value){
1316 Element.Prototype[key] = value;
1317 if (Array[key]) return;
1318 Elements.implement(key, function(){
1319 var items = [], elements = true;
1320 for (var i = 0, j = this.length; i < j; i++){
1321 var returns = this[i][key].apply(this[i], arguments);
1322 items.push(returns);
1323 if (elements) elements = ($type(returns) == 'element');
1325 return (elements) ? new Elements(items) : items;
1331 Element.Prototype = {$family: {name: 'element'}};
1333 Element.Constructors = new Hash;
1335 var IFrame = new Native({
1341 initialize: function(){
1342 var params = Array.link(arguments, {properties: Object.type, iframe: $defined});
1343 var props = params.properties || {};
1344 var iframe = $(params.iframe) || false;
1345 var onload = props.onload || $empty;
1346 delete props.onload;
1347 props.id = props.name = $pick(props.id, props.name, iframe.id, iframe.name, 'IFrame_' + $time());
1348 iframe = new Element(iframe || 'iframe', props);
1349 var onFrameLoad = function(){
1350 var host = $try(function(){
1351 return iframe.contentWindow.location.host;
1353 if (host && host == window.location.host){
1354 var win = new Window(iframe.contentWindow);
1355 new Document(iframe.contentWindow.document);
1356 $extend(win.Element.prototype, Element.Prototype);
1358 onload.call(iframe.contentWindow, iframe.contentWindow.document);
1360 (window.frames[props.id]) ? onFrameLoad() : iframe.addListener('load', onFrameLoad);
1366 var Elements = new Native({
1368 initialize: function(elements, options){
1369 options = $extend({ddup: true, cash: true}, options);
1370 elements = elements || [];
1371 if (options.ddup || options.cash){
1372 var uniques = {}, returned = [];
1373 for (var i = 0, l = elements.length; i < l; i++){
1374 var el = $.element(elements[i], !options.cash);
1376 if (uniques[el.uid]) continue;
1377 uniques[el.uid] = true;
1381 elements = returned;
1383 return (options.cash) ? $extend(elements, this) : elements;
1388 Elements.implement({
1390 filter: function(filter, bind){
1391 if (!filter) return this;
1392 return new Elements(Array.filter(this, (typeof filter == 'string') ? function(item){
1393 return item.match(filter);
1399 Document.implement({
1401 newElement: function(tag, props){
1402 if (Browser.Engine.trident && props){
1403 ['name', 'type', 'checked'].each(function(attribute){
1404 if (!props[attribute]) return;
1405 tag += ' ' + attribute + '="' + props[attribute] + '"';
1406 if (attribute != 'checked') delete props[attribute];
1408 tag = '<' + tag + '>';
1410 return $.element(this.createElement(tag)).set(props);
1413 newTextNode: function(text){
1414 return this.createTextNode(text);
1417 getDocument: function(){
1421 getWindow: function(){
1429 $: function(el, nocash){
1430 if (el && el.$family && el.uid) return el;
1431 var type = $type(el);
1432 return ($[type]) ? $[type](el, nocash, this.document) : null;
1435 $$: function(selector){
1436 if (arguments.length == 1 && typeof selector == 'string') return this.document.getElements(selector);
1438 var args = Array.flatten(arguments);
1439 for (var i = 0, l = args.length; i < l; i++){
1441 switch ($type(item)){
1442 case 'element': elements.push(item); break;
1443 case 'string': elements.extend(this.document.getElements(item, true));
1446 return new Elements(elements);
1449 getDocument: function(){
1450 return this.document;
1453 getWindow: function(){
1459 $.string = function(id, nocash, doc){
1460 id = doc.getElementById(id);
1461 return (id) ? $.element(id, nocash) : null;
1464 $.element = function(el, nocash){
1466 if (!nocash && !el.$family && !(/^object|embed$/i).test(el.tagName)){
1467 var proto = Element.Prototype;
1468 for (var p in proto) el[p] = proto[p];
1473 $.object = function(obj, nocash, doc){
1474 if (obj.toElement) return $.element(obj.toElement(doc), nocash);
1478 $.textnode = $.whitespace = $.window = $.document = $arguments(0);
1480 Native.implement([Element, Document], {
1482 getElement: function(selector, nocash){
1483 return $(this.getElements(selector, true)[0] || null, nocash);
1486 getElements: function(tags, nocash){
1487 tags = tags.split(',');
1489 var ddup = (tags.length > 1);
1490 tags.each(function(tag){
1491 var partial = this.getElementsByTagName(tag.trim());
1492 (ddup) ? elements.extend(partial) : elements = partial;
1494 return new Elements(elements, {ddup: ddup, cash: !nocash});
1501 var collected = {}, storage = {};
1502 var props = {input: 'checked', option: 'selected', textarea: (Browser.Engine.webkit && Browser.Engine.version < 420) ? 'innerHTML' : 'value'};
1504 var get = function(uid){
1505 return (storage[uid] || (storage[uid] = {}));
1508 var clean = function(item, retain){
1511 if (Browser.Engine.trident){
1512 if (item.clearAttributes){
1513 var clone = retain && item.cloneNode(false);
1514 item.clearAttributes();
1515 if (clone) item.mergeAttributes(clone);
1516 } else if (item.removeEvents){
1517 item.removeEvents();
1519 if ((/object/i).test(item.tagName)){
1520 for (var p in item){
1521 if (typeof item[p] == 'function') item[p] = $empty;
1523 Element.dispose(item);
1527 collected[uid] = storage[uid] = null;
1530 var purge = function(){
1531 Hash.each(collected, clean);
1532 if (Browser.Engine.trident) $A(document.getElementsByTagName('object')).each(clean);
1533 if (window.CollectGarbage) CollectGarbage();
1534 collected = storage = null;
1537 var walk = function(element, walk, start, match, all, nocash){
1538 var el = element[start || walk];
1541 if (el.nodeType == 1 && (!match || Element.match(el, match))){
1542 if (!all) return $(el, nocash);
1547 return (all) ? new Elements(elements, {ddup: false, cash: !nocash}) : null;
1551 'html': 'innerHTML',
1552 'class': 'className',
1554 'text': (Browser.Engine.trident || (Browser.Engine.webkit && Browser.Engine.version < 420)) ? 'innerText' : 'textContent'
1556 var bools = ['compact', 'nowrap', 'ismap', 'declare', 'noshade', 'checked', 'disabled', 'readonly', 'multiple', 'selected', 'noresize', 'defer'];
1557 var camels = ['value', 'accessKey', 'cellPadding', 'cellSpacing', 'colSpan', 'frameBorder', 'maxLength', 'readOnly', 'rowSpan', 'tabIndex', 'useMap'];
1559 bools = bools.associate(bools);
1561 Hash.extend(attributes, bools);
1562 Hash.extend(attributes, camels.associate(camels.map(String.toLowerCase)));
1566 before: function(context, element){
1567 if (element.parentNode) element.parentNode.insertBefore(context, element);
1570 after: function(context, element){
1571 if (!element.parentNode) return;
1572 var next = element.nextSibling;
1573 (next) ? element.parentNode.insertBefore(context, next) : element.parentNode.appendChild(context);
1576 bottom: function(context, element){
1577 element.appendChild(context);
1580 top: function(context, element){
1581 var first = element.firstChild;
1582 (first) ? element.insertBefore(context, first) : element.appendChild(context);
1587 inserters.inside = inserters.bottom;
1589 Hash.each(inserters, function(inserter, where){
1591 where = where.capitalize();
1593 Element.implement('inject' + where, function(el){
1594 inserter(this, $(el, true));
1598 Element.implement('grab' + where, function(el){
1599 inserter($(el, true), this);
1607 set: function(prop, value){
1608 switch ($type(prop)){
1610 for (var p in prop) this.set(p, prop[p]);
1613 var property = Element.Properties.get(prop);
1614 (property && property.set) ? property.set.apply(this, Array.slice(arguments, 1)) : this.setProperty(prop, value);
1619 get: function(prop){
1620 var property = Element.Properties.get(prop);
1621 return (property && property.get) ? property.get.apply(this, Array.slice(arguments, 1)) : this.getProperty(prop);
1624 erase: function(prop){
1625 var property = Element.Properties.get(prop);
1626 (property && property.erase) ? property.erase.apply(this) : this.removeProperty(prop);
1630 setProperty: function(attribute, value){
1631 var key = attributes[attribute];
1632 if (value == undefined) return this.removeProperty(attribute);
1633 if (key && bools[attribute]) value = !!value;
1634 (key) ? this[key] = value : this.setAttribute(attribute, '' + value);
1638 setProperties: function(attributes){
1639 for (var attribute in attributes) this.setProperty(attribute, attributes[attribute]);
1643 getProperty: function(attribute){
1644 var key = attributes[attribute];
1645 var value = (key) ? this[key] : this.getAttribute(attribute, 2);
1646 return (bools[attribute]) ? !!value : (key) ? value : value || null;
1649 getProperties: function(){
1650 var args = $A(arguments);
1651 return args.map(this.getProperty, this).associate(args);
1654 removeProperty: function(attribute){
1655 var key = attributes[attribute];
1656 (key) ? this[key] = (key && bools[attribute]) ? false : '' : this.removeAttribute(attribute);
1660 removeProperties: function(){
1661 Array.each(arguments, this.removeProperty, this);
1665 hasClass: function(className){
1666 return this.className.contains(className, ' ');
1669 addClass: function(className){
1670 if (!this.hasClass(className)) this.className = (this.className + ' ' + className).clean();
1674 removeClass: function(className){
1675 this.className = this.className.replace(new RegExp('(^|\\s)' + className + '(?:\\s|$)'), '$1');
1679 toggleClass: function(className){
1680 return this.hasClass(className) ? this.removeClass(className) : this.addClass(className);
1684 Array.flatten(arguments).each(function(element){
1685 element = $(element, true);
1686 if (element) this.appendChild(element);
1691 appendText: function(text, where){
1692 return this.grab(this.getDocument().newTextNode(text), where);
1695 grab: function(el, where){
1696 inserters[where || 'bottom']($(el, true), this);
1700 inject: function(el, where){
1701 inserters[where || 'bottom'](this, $(el, true));
1705 replaces: function(el){
1707 el.parentNode.replaceChild(this, el);
1711 wraps: function(el, where){
1713 return this.replaces(el).grab(el, where);
1716 getPrevious: function(match, nocash){
1717 return walk(this, 'previousSibling', null, match, false, nocash);
1720 getAllPrevious: function(match, nocash){
1721 return walk(this, 'previousSibling', null, match, true, nocash);
1724 getNext: function(match, nocash){
1725 return walk(this, 'nextSibling', null, match, false, nocash);
1728 getAllNext: function(match, nocash){
1729 return walk(this, 'nextSibling', null, match, true, nocash);
1732 getFirst: function(match, nocash){
1733 return walk(this, 'nextSibling', 'firstChild', match, false, nocash);
1736 getLast: function(match, nocash){
1737 return walk(this, 'previousSibling', 'lastChild', match, false, nocash);
1740 getParent: function(match, nocash){
1741 return walk(this, 'parentNode', null, match, false, nocash);
1744 getParents: function(match, nocash){
1745 return walk(this, 'parentNode', null, match, true, nocash);
1748 getSiblings: function(match, nocash) {
1749 return this.getParent().getChildren(match, nocash).erase(this);
1752 getChildren: function(match, nocash){
1753 return walk(this, 'nextSibling', 'firstChild', match, true, nocash);
1756 getWindow: function(){
1757 return this.ownerDocument.window;
1760 getDocument: function(){
1761 return this.ownerDocument;
1764 getElementById: function(id, nocash){
1765 var el = this.ownerDocument.getElementById(id);
1766 if (!el) return null;
1767 for (var parent = el.parentNode; parent != this; parent = parent.parentNode){
1768 if (!parent) return null;
1770 return $.element(el, nocash);
1773 getSelected: function(){
1774 return new Elements($A(this.options).filter(function(option){
1775 return option.selected;
1779 getComputedStyle: function(property){
1780 if (this.currentStyle) return this.currentStyle[property.camelCase()];
1781 var computed = this.getDocument().defaultView.getComputedStyle(this, null);
1782 return (computed) ? computed.getPropertyValue([property.hyphenate()]) : null;
1785 toQueryString: function(){
1786 var queryString = [];
1787 this.getElements('input, select, textarea', true).each(function(el){
1788 if (!el.name || el.disabled) return;
1789 var value = (el.tagName.toLowerCase() == 'select') ? Element.getSelected(el).map(function(opt){
1791 }) : ((el.type == 'radio' || el.type == 'checkbox') && !el.checked) ? null : el.value;
1792 $splat(value).each(function(val){
1793 if (typeof val != 'undefined') queryString.push(el.name + '=' + encodeURIComponent(val));
1796 return queryString.join('&');
1799 clone: function(contents, keepid){
1800 contents = contents !== false;
1801 var clone = this.cloneNode(contents);
1802 var clean = function(node, element){
1803 if (!keepid) node.removeAttribute('id');
1804 if (Browser.Engine.trident){
1805 node.clearAttributes();
1806 node.mergeAttributes(element);
1807 node.removeAttribute('uid');
1809 var no = node.options, eo = element.options;
1810 for (var j = no.length; j--;) no[j].selected = eo[j].selected;
1813 var prop = props[element.tagName.toLowerCase()];
1814 if (prop && element[prop]) node[prop] = element[prop];
1818 var ce = clone.getElementsByTagName('*'), te = this.getElementsByTagName('*');
1819 for (var i = ce.length; i--;) clean(ce[i], te[i]);
1826 destroy: function(){
1827 Element.empty(this);
1828 Element.dispose(this);
1834 $A(this.childNodes).each(function(node){
1835 Element.destroy(node);
1840 dispose: function(){
1841 return (this.parentNode) ? this.parentNode.removeChild(this) : this;
1844 hasChild: function(el){
1846 if (!el) return false;
1847 if (Browser.Engine.webkit && Browser.Engine.version < 420) return $A(this.getElementsByTagName(el.tagName)).contains(el);
1848 return (this.contains) ? (this != el && this.contains(el)) : !!(this.compareDocumentPosition(el) & 16);
1851 match: function(tag){
1852 return (!tag || (tag == this) || (Element.get(this, 'tag') == tag));
1857 Native.implement([Element, Window, Document], {
1859 addListener: function(type, fn){
1860 if (type == 'unload'){
1861 var old = fn, self = this;
1863 self.removeListener('unload', fn);
1867 collected[this.uid] = this;
1869 if (this.addEventListener) this.addEventListener(type, fn, false);
1870 else this.attachEvent('on' + type, fn);
1874 removeListener: function(type, fn){
1875 if (this.removeEventListener) this.removeEventListener(type, fn, false);
1876 else this.detachEvent('on' + type, fn);
1880 retrieve: function(property, dflt){
1881 var storage = get(this.uid), prop = storage[property];
1882 if (dflt != undefined && prop == undefined) prop = storage[property] = dflt;
1886 store: function(property, value){
1887 var storage = get(this.uid);
1888 storage[property] = value;
1892 eliminate: function(property){
1893 var storage = get(this.uid);
1894 delete storage[property];
1900 window.addListener('unload', purge);
1904 Element.Properties = new Hash;
1906 Element.Properties.style = {
1908 set: function(style){
1909 this.style.cssText = style;
1913 return this.style.cssText;
1917 this.style.cssText = '';
1922 Element.Properties.tag = {
1925 return this.tagName.toLowerCase();
1930 Element.Properties.html = (function(){
1931 var wrapper = document.createElement('div');
1933 var translations = {
1934 table: [1, '<table>', '</table>'],
1935 select: [1, '<select>', '</select>'],
1936 tbody: [2, '<table><tbody>', '</tbody></table>'],
1937 tr: [3, '<table><tbody><tr>', '</tr></tbody></table>']
1939 translations.thead = translations.tfoot = translations.tbody;
1943 var html = Array.flatten(arguments).join('');
1944 var wrap = Browser.Engine.trident && translations[this.get('tag')];
1946 var first = wrapper;
1947 first.innerHTML = wrap[1] + html + wrap[2];
1948 for (var i = wrap[0]; i--;) first = first.firstChild;
1949 this.empty().adopt(first.childNodes);
1951 this.innerHTML = html;
1956 html.erase = html.set;
1961 if (Browser.Engine.webkit && Browser.Engine.version < 420) Element.Properties.text = {
1963 if (this.innerText) return this.innerText;
1964 var temp = this.ownerDocument.newElement('div', {html: this.innerHTML}).inject(this.ownerDocument.body);
1965 var text = temp.innerText;
1973 Script: Element.Event.js
1974 Contains Element methods for dealing with events, and custom Events.
1980 Element.Properties.events = {set: function(events){
1981 this.addEvents(events);
1984 Native.implement([Element, Window, Document], {
1986 addEvent: function(type, fn){
1987 var events = this.retrieve('events', {});
1988 events[type] = events[type] || {'keys': [], 'values': []};
1989 if (events[type].keys.contains(fn)) return this;
1990 events[type].keys.push(fn);
1991 var realType = type, custom = Element.Events.get(type), condition = fn, self = this;
1993 if (custom.onAdd) custom.onAdd.call(this, fn);
1994 if (custom.condition){
1995 condition = function(event){
1996 if (custom.condition.call(this, event)) return fn.call(this, event);
2000 realType = custom.base || realType;
2002 var defn = function(){
2003 return fn.call(self);
2005 var nativeEvent = Element.NativeEvents[realType];
2007 if (nativeEvent == 2){
2008 defn = function(event){
2009 event = new Event(event, self.getWindow());
2010 if (condition.call(self, event) === false) event.stop();
2013 this.addListener(realType, defn);
2015 events[type].values.push(defn);
2019 removeEvent: function(type, fn){
2020 var events = this.retrieve('events');
2021 if (!events || !events[type]) return this;
2022 var pos = events[type].keys.indexOf(fn);
2023 if (pos == -1) return this;
2024 events[type].keys.splice(pos, 1);
2025 var value = events[type].values.splice(pos, 1)[0];
2026 var custom = Element.Events.get(type);
2028 if (custom.onRemove) custom.onRemove.call(this, fn);
2029 type = custom.base || type;
2031 return (Element.NativeEvents[type]) ? this.removeListener(type, value) : this;
2034 addEvents: function(events){
2035 for (var event in events) this.addEvent(event, events[event]);
2039 removeEvents: function(events){
2041 if ($type(events) == 'object'){
2042 for (type in events) this.removeEvent(type, events[type]);
2045 var attached = this.retrieve('events');
2046 if (!attached) return this;
2048 for (type in attached) this.removeEvents(type);
2049 this.eliminate('events');
2050 } else if (attached[events]){
2051 while (attached[events].keys[0]) this.removeEvent(events, attached[events].keys[0]);
2052 attached[events] = null;
2057 fireEvent: function(type, args, delay){
2058 var events = this.retrieve('events');
2059 if (!events || !events[type]) return this;
2060 events[type].keys.each(function(fn){
2061 fn.create({'bind': this, 'delay': delay, 'arguments': args})();
2066 cloneEvents: function(from, type){
2068 var fevents = from.retrieve('events');
2069 if (!fevents) return this;
2071 for (var evType in fevents) this.cloneEvents(from, evType);
2072 } else if (fevents[type]){
2073 fevents[type].keys.each(function(fn){
2074 this.addEvent(type, fn);
2082 Element.NativeEvents = {
2083 click: 2, dblclick: 2, mouseup: 2, mousedown: 2, contextmenu: 2, //mouse buttons
2084 mousewheel: 2, DOMMouseScroll: 2, //mouse wheel
2085 mouseover: 2, mouseout: 2, mousemove: 2, selectstart: 2, selectend: 2, //mouse movement
2086 keydown: 2, keypress: 2, keyup: 2, //keyboard
2087 focus: 2, blur: 2, change: 2, reset: 2, select: 2, submit: 2, //form elements
2088 load: 1, unload: 1, beforeunload: 2, resize: 1, move: 1, DOMContentLoaded: 1, readystatechange: 1, //window
2089 error: 1, abort: 1, scroll: 1 //misc
2094 var $check = function(event){
2095 var related = event.relatedTarget;
2096 if (related == undefined) return true;
2097 if (related === false) return false;
2098 return ($type(this) != 'document' && related != this && related.prefix != 'xul' && !this.hasChild(related));
2101 Element.Events = new Hash({
2114 base: (Browser.Engine.gecko) ? 'DOMMouseScroll' : 'mousewheel'
2123 Script: Element.Style.js
2124 Contains methods for interacting with the styles of Elements in a fashionable way.
2130 Element.Properties.styles = {set: function(styles){
2131 this.setStyles(styles);
2134 Element.Properties.opacity = {
2136 set: function(opacity, novisibility){
2139 if (this.style.visibility != 'hidden') this.style.visibility = 'hidden';
2141 if (this.style.visibility != 'visible') this.style.visibility = 'visible';
2144 if (!this.currentStyle || !this.currentStyle.hasLayout) this.style.zoom = 1;
2145 if (Browser.Engine.trident) this.style.filter = (opacity == 1) ? '' : 'alpha(opacity=' + opacity * 100 + ')';
2146 this.style.opacity = opacity;
2147 this.store('opacity', opacity);
2151 return this.retrieve('opacity', 1);
2158 setOpacity: function(value){
2159 return this.set('opacity', value, true);
2162 getOpacity: function(){
2163 return this.get('opacity');
2166 setStyle: function(property, value){
2168 case 'opacity': return this.set('opacity', parseFloat(value));
2169 case 'float': property = (Browser.Engine.trident) ? 'styleFloat' : 'cssFloat';
2171 property = property.camelCase();
2172 if ($type(value) != 'string'){
2173 var map = (Element.Styles.get(property) || '@').split(' ');
2174 value = $splat(value).map(function(val, i){
2175 if (!map[i]) return '';
2176 return ($type(val) == 'number') ? map[i].replace('@', Math.round(val)) : val;
2178 } else if (value == String(Number(value))){
2179 value = Math.round(value);
2181 this.style[property] = value;
2185 getStyle: function(property){
2187 case 'opacity': return this.get('opacity');
2188 case 'float': property = (Browser.Engine.trident) ? 'styleFloat' : 'cssFloat';
2190 property = property.camelCase();
2191 var result = this.style[property];
2194 for (var style in Element.ShortStyles){
2195 if (property != style) continue;
2196 for (var s in Element.ShortStyles[style]) result.push(this.getStyle(s));
2197 return result.join(' ');
2199 result = this.getComputedStyle(property);
2202 result = String(result);
2203 var color = result.match(/rgba?\([\d\s,]+\)/);
2204 if (color) result = result.replace(color[0], color[0].rgbToHex());
2206 if (Browser.Engine.presto || (Browser.Engine.trident && !$chk(parseInt(result, 10)))){
2207 if (property.test(/^(height|width)$/)){
2208 var values = (property == 'width') ? ['left', 'right'] : ['top', 'bottom'], size = 0;
2209 values.each(function(value){
2210 size += this.getStyle('border-' + value + '-width').toInt() + this.getStyle('padding-' + value).toInt();
2212 return this['offset' + property.capitalize()] - size + 'px';
2214 if ((Browser.Engine.presto) && String(result).test('px')) return result;
2215 if (property.test(/(border(.+)Width|margin|padding)/)) return '0px';
2220 setStyles: function(styles){
2221 for (var style in styles) this.setStyle(style, styles[style]);
2225 getStyles: function(){
2227 Array.each(arguments, function(key){
2228 result[key] = this.getStyle(key);
2235 Element.Styles = new Hash({
2236 left: '@px', top: '@px', bottom: '@px', right: '@px',
2237 width: '@px', height: '@px', maxWidth: '@px', maxHeight: '@px', minWidth: '@px', minHeight: '@px',
2238 backgroundColor: 'rgb(@, @, @)', backgroundPosition: '@px @px', color: 'rgb(@, @, @)',
2239 fontSize: '@px', letterSpacing: '@px', lineHeight: '@px', clip: 'rect(@px @px @px @px)',
2240 margin: '@px @px @px @px', padding: '@px @px @px @px', border: '@px @ rgb(@, @, @) @px @ rgb(@, @, @) @px @ rgb(@, @, @)',
2241 borderWidth: '@px @px @px @px', borderStyle: '@ @ @ @', borderColor: 'rgb(@, @, @) rgb(@, @, @) rgb(@, @, @) rgb(@, @, @)',
2242 zIndex: '@', 'zoom': '@', fontWeight: '@', textIndent: '@px', opacity: '@'
2245 Element.ShortStyles = {margin: {}, padding: {}, border: {}, borderWidth: {}, borderStyle: {}, borderColor: {}};
2247 ['Top', 'Right', 'Bottom', 'Left'].each(function(direction){
2248 var Short = Element.ShortStyles;
2249 var All = Element.Styles;
2250 ['margin', 'padding'].each(function(style){
2251 var sd = style + direction;
2252 Short[style][sd] = All[sd] = '@px';
2254 var bd = 'border' + direction;
2255 Short.border[bd] = All[bd] = '@px @ rgb(@, @, @)';
2256 var bdw = bd + 'Width', bds = bd + 'Style', bdc = bd + 'Color';
2258 Short.borderWidth[bdw] = Short[bd][bdw] = All[bdw] = '@px';
2259 Short.borderStyle[bds] = Short[bd][bds] = All[bds] = '@';
2260 Short.borderColor[bdc] = Short[bd][bdc] = All[bdc] = 'rgb(@, @, @)';
2265 Script: Element.Dimensions.js
2266 Contains methods to work with size, scroll, or positioning of Elements and the window object.
2272 - Element positioning based on the [qooxdoo](http://qooxdoo.org/) code and smart browser fixes, [LGPL License](http://www.gnu.org/licenses/lgpl.html).
2273 - Viewport dimensions based on [YUI](http://developer.yahoo.com/yui/) code, [BSD License](http://developer.yahoo.com/yui/license.html).
2280 scrollTo: function(x, y){
2282 this.getWindow().scrollTo(x, y);
2284 this.scrollLeft = x;
2290 getSize: function(){
2291 if (isBody(this)) return this.getWindow().getSize();
2292 return {x: this.offsetWidth, y: this.offsetHeight};
2295 getScrollSize: function(){
2296 if (isBody(this)) return this.getWindow().getScrollSize();
2297 return {x: this.scrollWidth, y: this.scrollHeight};
2300 getScroll: function(){
2301 if (isBody(this)) return this.getWindow().getScroll();
2302 return {x: this.scrollLeft, y: this.scrollTop};
2305 getScrolls: function(){
2306 var element = this, position = {x: 0, y: 0};
2307 while (element && !isBody(element)){
2308 position.x += element.scrollLeft;
2309 position.y += element.scrollTop;
2310 element = element.parentNode;
2315 getOffsetParent: function(){
2317 if (isBody(element)) return null;
2318 if (!Browser.Engine.trident) return element.offsetParent;
2319 while ((element = element.parentNode) && !isBody(element)){
2320 if (styleString(element, 'position') != 'static') return element;
2325 getOffsets: function(){
2326 if (Browser.Engine.trident){
2327 var bound = this.getBoundingClientRect(), html = this.getDocument().documentElement;
2328 var isFixed = styleString(this, 'position') == 'fixed';
2330 x: bound.left + ((isFixed) ? 0 : html.scrollLeft) - html.clientLeft,
2331 y: bound.top + ((isFixed) ? 0 : html.scrollTop) - html.clientTop
2335 var element = this, position = {x: 0, y: 0};
2336 if (isBody(this)) return position;
2338 while (element && !isBody(element)){
2339 position.x += element.offsetLeft;
2340 position.y += element.offsetTop;
2342 if (Browser.Engine.gecko){
2343 if (!borderBox(element)){
2344 position.x += leftBorder(element);
2345 position.y += topBorder(element);
2347 var parent = element.parentNode;
2348 if (parent && styleString(parent, 'overflow') != 'visible'){
2349 position.x += leftBorder(parent);
2350 position.y += topBorder(parent);
2352 } else if (element != this && Browser.Engine.webkit){
2353 position.x += leftBorder(element);
2354 position.y += topBorder(element);
2357 element = element.offsetParent;
2359 if (Browser.Engine.gecko && !borderBox(this)){
2360 position.x -= leftBorder(this);
2361 position.y -= topBorder(this);
2366 getPosition: function(relative){
2367 if (isBody(this)) return {x: 0, y: 0};
2368 var offset = this.getOffsets(), scroll = this.getScrolls();
2369 var position = {x: offset.x - scroll.x, y: offset.y - scroll.y};
2370 var relativePosition = (relative && (relative = $(relative))) ? relative.getPosition() : {x: 0, y: 0};
2371 return {x: position.x - relativePosition.x, y: position.y - relativePosition.y};
2374 getCoordinates: function(element){
2375 if (isBody(this)) return this.getWindow().getCoordinates();
2376 var position = this.getPosition(element), size = this.getSize();
2377 var obj = {left: position.x, top: position.y, width: size.x, height: size.y};
2378 obj.right = obj.left + obj.width;
2379 obj.bottom = obj.top + obj.height;
2383 computePosition: function(obj){
2384 return {left: obj.x - styleNumber(this, 'margin-left'), top: obj.y - styleNumber(this, 'margin-top')};
2387 position: function(obj){
2388 return this.setStyles(this.computePosition(obj));
2393 Native.implement([Document, Window], {
2395 getSize: function(){
2396 if (Browser.Engine.presto || Browser.Engine.webkit) {
2397 var win = this.getWindow();
2398 return {x: win.innerWidth, y: win.innerHeight};
2400 var doc = getCompatElement(this);
2401 return {x: doc.clientWidth, y: doc.clientHeight};
2404 getScroll: function(){
2405 var win = this.getWindow(), doc = getCompatElement(this);
2406 return {x: win.pageXOffset || doc.scrollLeft, y: win.pageYOffset || doc.scrollTop};
2409 getScrollSize: function(){
2410 var doc = getCompatElement(this), min = this.getSize();
2411 return {x: Math.max(doc.scrollWidth, min.x), y: Math.max(doc.scrollHeight, min.y)};
2414 getPosition: function(){
2415 return {x: 0, y: 0};
2418 getCoordinates: function(){
2419 var size = this.getSize();
2420 return {top: 0, left: 0, bottom: size.y, right: size.x, height: size.y, width: size.x};
2427 var styleString = Element.getComputedStyle;
2429 function styleNumber(element, style){
2430 return styleString(element, style).toInt() || 0;
2433 function borderBox(element){
2434 return styleString(element, '-moz-box-sizing') == 'border-box';
2437 function topBorder(element){
2438 return styleNumber(element, 'border-top-width');
2441 function leftBorder(element){
2442 return styleNumber(element, 'border-left-width');
2445 function isBody(element){
2446 return (/^(?:body|html)$/i).test(element.tagName);
2449 function getCompatElement(element){
2450 var doc = element.getDocument();
2451 return (!doc.compatMode || doc.compatMode == 'CSS1Compat') ? doc.html : doc.body;
2458 Native.implement([Window, Document, Element], {
2460 getHeight: function(){
2461 return this.getSize().y;
2464 getWidth: function(){
2465 return this.getSize().x;
2468 getScrollTop: function(){
2469 return this.getScroll().y;
2472 getScrollLeft: function(){
2473 return this.getScroll().x;
2476 getScrollHeight: function(){
2477 return this.getScrollSize().y;
2480 getScrollWidth: function(){
2481 return this.getScrollSize().x;
2485 return this.getPosition().y;
2488 getLeft: function(){
2489 return this.getPosition().x;
2496 Script: Selectors.js
2497 Adds advanced CSS Querying capabilities for targeting elements. Also includes pseudoselectors support.
2503 Native.implement([Document, Element], {
2505 getElements: function(expression, nocash){
2506 expression = expression.split(',');
2507 var items, local = {};
2508 for (var i = 0, l = expression.length; i < l; i++){
2509 var selector = expression[i], elements = Selectors.Utils.search(this, selector, local);
2510 if (i != 0 && elements.item) elements = $A(elements);
2511 items = (i == 0) ? elements : (items.item) ? $A(items).concat(elements) : items.concat(elements);
2513 return new Elements(items, {ddup: (expression.length > 1), cash: !nocash});
2520 match: function(selector){
2521 if (!selector || (selector == this)) return true;
2522 var tagid = Selectors.Utils.parseTagAndID(selector);
2523 var tag = tagid[0], id = tagid[1];
2524 if (!Selectors.Filters.byID(this, id) || !Selectors.Filters.byTag(this, tag)) return false;
2525 var parsed = Selectors.Utils.parseSelector(selector);
2526 return (parsed) ? Selectors.Utils.filter(this, parsed, {}) : true;
2531 var Selectors = {Cache: {nth: {}, parsed: {}}};
2533 Selectors.RegExps = {
2536 quick: (/^(\w+|\*)$/),
2537 splitter: (/\s*([+>~\s])\s*([a-zA-Z#.*:\[])/g),
2538 combined: (/\.([\w-]+)|\[(\w+)(?:([!*^$~|]?=)(["']?)([^\4]*?)\4)?\]|:([\w-]+)(?:\(["']?(.*?)?["']?\)|$)/g)
2543 chk: function(item, uniques){
2544 if (!uniques) return true;
2545 var uid = $uid(item);
2546 if (!uniques[uid]) return uniques[uid] = true;
2550 parseNthArgument: function(argument){
2551 if (Selectors.Cache.nth[argument]) return Selectors.Cache.nth[argument];
2552 var parsed = argument.match(/^([+-]?\d*)?([a-z]+)?([+-]?\d*)?$/);
2553 if (!parsed) return false;
2554 var inta = parseInt(parsed[1], 10);
2555 var a = (inta || inta === 0) ? inta : 1;
2556 var special = parsed[2] || false;
2557 var b = parseInt(parsed[3], 10) || 0;
2560 while (b < 1) b += a;
2561 while (b >= a) b -= a;
2567 case 'n': parsed = {a: a, b: b, special: 'n'}; break;
2568 case 'odd': parsed = {a: 2, b: 0, special: 'n'}; break;
2569 case 'even': parsed = {a: 2, b: 1, special: 'n'}; break;
2570 case 'first': parsed = {a: 0, special: 'index'}; break;
2571 case 'last': parsed = {special: 'last-child'}; break;
2572 case 'only': parsed = {special: 'only-child'}; break;
2573 default: parsed = {a: (a - 1), special: 'index'};
2576 return Selectors.Cache.nth[argument] = parsed;
2579 parseSelector: function(selector){
2580 if (Selectors.Cache.parsed[selector]) return Selectors.Cache.parsed[selector];
2581 var m, parsed = {classes: [], pseudos: [], attributes: []};
2582 while ((m = Selectors.RegExps.combined.exec(selector))){
2583 var cn = m[1], an = m[2], ao = m[3], av = m[5], pn = m[6], pa = m[7];
2585 parsed.classes.push(cn);
2587 var parser = Selectors.Pseudo.get(pn);
2588 if (parser) parsed.pseudos.push({parser: parser, argument: pa});
2589 else parsed.attributes.push({name: pn, operator: '=', value: pa});
2591 parsed.attributes.push({name: an, operator: ao, value: av});
2594 if (!parsed.classes.length) delete parsed.classes;
2595 if (!parsed.attributes.length) delete parsed.attributes;
2596 if (!parsed.pseudos.length) delete parsed.pseudos;
2597 if (!parsed.classes && !parsed.attributes && !parsed.pseudos) parsed = null;
2598 return Selectors.Cache.parsed[selector] = parsed;
2601 parseTagAndID: function(selector){
2602 var tag = selector.match(Selectors.RegExps.tag);
2603 var id = selector.match(Selectors.RegExps.id);
2604 return [(tag) ? tag[1] : '*', (id) ? id[1] : false];
2607 filter: function(item, parsed, local){
2609 if (parsed.classes){
2610 for (i = parsed.classes.length; i--; i){
2611 var cn = parsed.classes[i];
2612 if (!Selectors.Filters.byClass(item, cn)) return false;
2615 if (parsed.attributes){
2616 for (i = parsed.attributes.length; i--; i){
2617 var att = parsed.attributes[i];
2618 if (!Selectors.Filters.byAttribute(item, att.name, att.operator, att.value)) return false;
2621 if (parsed.pseudos){
2622 for (i = parsed.pseudos.length; i--; i){
2623 var psd = parsed.pseudos[i];
2624 if (!Selectors.Filters.byPseudo(item, psd.parser, psd.argument, local)) return false;
2630 getByTagAndID: function(ctx, tag, id){
2632 var item = (ctx.getElementById) ? ctx.getElementById(id, true) : Element.getElementById(ctx, id, true);
2633 return (item && Selectors.Filters.byTag(item, tag)) ? [item] : [];
2635 return ctx.getElementsByTagName(tag);
2639 search: function(self, expression, local){
2642 var selectors = expression.trim().replace(Selectors.RegExps.splitter, function(m0, m1, m2){
2647 var items, filtered, item;
2649 for (var i = 0, l = selectors.length; i < l; i++){
2651 var selector = selectors[i];
2653 if (i == 0 && Selectors.RegExps.quick.test(selector)){
2654 items = self.getElementsByTagName(selector);
2658 var splitter = splitters[i - 1];
2660 var tagid = Selectors.Utils.parseTagAndID(selector);
2661 var tag = tagid[0], id = tagid[1];
2664 items = Selectors.Utils.getByTagAndID(self, tag, id);
2666 var uniques = {}, found = [];
2667 for (var j = 0, k = items.length; j < k; j++) found = Selectors.Getters[splitter](found, items[j], tag, id, uniques);
2671 var parsed = Selectors.Utils.parseSelector(selector);
2675 for (var m = 0, n = items.length; m < n; m++){
2677 if (Selectors.Utils.filter(item, parsed, local)) filtered.push(item);
2690 Selectors.Getters = {
2692 ' ': function(found, self, tag, id, uniques){
2693 var items = Selectors.Utils.getByTagAndID(self, tag, id);
2694 for (var i = 0, l = items.length; i < l; i++){
2695 var item = items[i];
2696 if (Selectors.Utils.chk(item, uniques)) found.push(item);
2701 '>': function(found, self, tag, id, uniques){
2702 var children = Selectors.Utils.getByTagAndID(self, tag, id);
2703 for (var i = 0, l = children.length; i < l; i++){
2704 var child = children[i];
2705 if (child.parentNode == self && Selectors.Utils.chk(child, uniques)) found.push(child);
2710 '+': function(found, self, tag, id, uniques){
2711 while ((self = self.nextSibling)){
2712 if (self.nodeType == 1){
2713 if (Selectors.Utils.chk(self, uniques) && Selectors.Filters.byTag(self, tag) && Selectors.Filters.byID(self, id)) found.push(self);
2720 '~': function(found, self, tag, id, uniques){
2721 while ((self = self.nextSibling)){
2722 if (self.nodeType == 1){
2723 if (!Selectors.Utils.chk(self, uniques)) break;
2724 if (Selectors.Filters.byTag(self, tag) && Selectors.Filters.byID(self, id)) found.push(self);
2732 Selectors.Filters = {
2734 byTag: function(self, tag){
2735 return (tag == '*' || (self.tagName && self.tagName.toLowerCase() == tag));
2738 byID: function(self, id){
2739 return (!id || (self.id && self.id == id));
2742 byClass: function(self, klass){
2743 return (self.className && self.className.contains(klass, ' '));
2746 byPseudo: function(self, parser, argument, local){
2747 return parser.call(self, argument, local);
2750 byAttribute: function(self, name, operator, value){
2751 var result = Element.prototype.getProperty.call(self, name);
2752 if (!result) return (operator == '!=');
2753 if (!operator || value == undefined) return true;
2755 case '=': return (result == value);
2756 case '*=': return (result.contains(value));
2757 case '^=': return (result.substr(0, value.length) == value);
2758 case '$=': return (result.substr(result.length - value.length) == value);
2759 case '!=': return (result != value);
2760 case '~=': return result.contains(value, ' ');
2761 case '|=': return result.contains(value, '-');
2768 Selectors.Pseudo = new Hash({
2770 // w3c pseudo selectors
2772 checked: function(){
2773 return this.checked;
2777 return !(this.innerText || this.textContent || '').length;
2780 not: function(selector){
2781 return !Element.match(this, selector);
2784 contains: function(text){
2785 return (this.innerText || this.textContent || '').contains(text);
2788 'first-child': function(){
2789 return Selectors.Pseudo.index.call(this, 0);
2792 'last-child': function(){
2794 while ((element = element.nextSibling)){
2795 if (element.nodeType == 1) return false;
2800 'only-child': function(){
2802 while ((prev = prev.previousSibling)){
2803 if (prev.nodeType == 1) return false;
2806 while ((next = next.nextSibling)){
2807 if (next.nodeType == 1) return false;
2812 'nth-child': function(argument, local){
2813 argument = (argument == undefined) ? 'n' : argument;
2814 var parsed = Selectors.Utils.parseNthArgument(argument);
2815 if (parsed.special != 'n') return Selectors.Pseudo[parsed.special].call(this, parsed.a, local);
2817 local.positions = local.positions || {};
2818 var uid = $uid(this);
2819 if (!local.positions[uid]){
2821 while ((self = self.previousSibling)){
2822 if (self.nodeType != 1) continue;
2824 var position = local.positions[$uid(self)];
2825 if (position != undefined){
2826 count = position + count;
2830 local.positions[uid] = count;
2832 return (local.positions[uid] % parsed.a == parsed.b);
2835 // custom pseudo selectors
2837 index: function(index){
2838 var element = this, count = 0;
2839 while ((element = element.previousSibling)){
2840 if (element.nodeType == 1 && ++count > index) return false;
2842 return (count == index);
2845 even: function(argument, local){
2846 return Selectors.Pseudo['nth-child'].call(this, '2n+1', local);
2849 odd: function(argument, local){
2850 return Selectors.Pseudo['nth-child'].call(this, '2n', local);
2853 selected: function() {
2854 return this.selected;
2862 Contains the domready custom event.
2868 Element.Events.domready = {
2870 onAdd: function(fn){
2871 if (Browser.loaded) fn.call(this);
2878 var domready = function(){
2879 if (Browser.loaded) return;
2880 Browser.loaded = true;
2881 window.fireEvent('domready');
2882 document.fireEvent('domready');
2885 if (Browser.Engine.trident){
2886 var temp = document.createElement('div');
2889 temp.doScroll('left');
2890 return $(temp).inject(document.body).set('html', 'temp').dispose();
2891 })) ? domready() : arguments.callee.delay(50);
2893 } else if (Browser.Engine.webkit && Browser.Engine.version < 525){