Object.extend = function(destination, source) {
	for (var property in source)
		destination[property] = source[property];
	return destination;
};
Object.extend(Object, {
	inspect: function(object){
		for(var i in object)
			console.log(i+": "+object[i]);
	}
});
Object.extend(String.prototype, {
	trim: function(charlist){
		var whitespace, str = this + '', l = str.length, i = 0;
		whitespace = (!charlist) ? " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000" 
									: (charlist+'').replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1'); 
		for (; i < l; i++) {
			if (whitespace.indexOf(str.charAt(i)) === -1) {
				str = str.substring(i);
				break;
			}
		}
		l = str.length;
		for (i = l - 1; i >= 0; i--) {
			if (whitespace.indexOf(str.charAt(i)) === -1) {
				str = str.substring(0, i + 1);
				break;
			}
		}
		return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
	},
	capitalize: function() {
		return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
	}
});
Object.extend(Array.prototype, {
	filter: function filter(fn, bind){
		var results = [];
		for (var i = 0, l = this.length; i < l; i++)
			if (fn.call(bind, this[i], i, this))
				results.push(this[i]);
		return results;
	},
	clean: function clean(){
		return this.filter(function (obj){
			return (obj != undefined);
		});
	},
	each: function each(fn){
		for(var i = 0, l = this.length; i < l; i++)
			this[i] = fn(this[i], i);
		return this;
	},
	indexOf: function indexOf(item){
		for(var i = 0, l = this.length; i < l; i++)
			if(this[i] === item)
				return i;
		return -1;
	},
	contains: function contains(item){
		return this.indexOf(item) != -1;
	},
   remove: function remove(item){
      var results = [];
      for(var i = 0, l = this.length; i < l; i++){
         if(this[i] !== item){
            results.push(this[i]);
         }
      }
      return results;
   }
});
function delegate(o, m){
	return function(){
		return m.apply(o, arguments);
	}
}
Element = {
	extend: function(destination, source) {
		for (var property in source){
			
			if(typeof source[property] == 'object'){
				destination[property] = {};
				for (var p in source[property]){
					destination[property][p] = delegate(destination, source[property][p]);
				}
			} else 
				destination[property] = source[property];
		}
		return destination;
	}
}

window.addEvent = function(e, fn){
	if(e.substr(0,2) == 'on') e = e.substr(2);
	if(window.addEventListener){
		window.addEventListener(e, fn, false);
	} else if(window.attachEvent){
		window.attachEvent('on'+e, function(){
			return fn.call(window, event);
		});
	}
}
function $(id){
	//return document.getElementById(id);
	var el;
	if(typeof id == "string")
		el	= document.getElementById(id);
	else if (typeof id == "object" && id.nodeType)
		el = id;
	else
		return null;
	var tag = el.tagName.toLowerCase();
	if(tag == 'input' || tag == 'select' || tag == 'button')
		Element.extend(el, Forms.Fields);
		//console.log('form item');//;
	Element.extend(el, {
		addEvent: function(e, fn, args){
			if(e.substr(0,2) == 'on') e = e.substr(2);
			if(el.addEventListener){
				el.addEventListener(e, function(ev){
					if((fn.call(el, ev, args)) === false) ev.preventDefault();
				}, true);
			} else if(el.attachEvent){
				el.attachEvent('on'+e, function(){
					return fn.call(el, event, args);
				});
			}
		}
	});
	return el;
}