/* mootools extend */
Element.implement({
	show: function() {
		if (this.hasClass('hidden')) {
			this.removeClass('hidden');
		} else {
			this.setStyle('display', '');
		}
		return this;
	},
	
	hide: function() {
		this.addClass('hidden');
		return this;
	},
	
	enable: function() {
		this.disabled=false;
	},
	
	disable: function() {
		this.disabled=true;
	},
	
  	descendants: function() {
    	return $A(this.getElementsByTagName('*'));
  	},
  	
	getChildById: function(id) {
		var c=this.getElements('*');
		var e=null;
		c.each(function(el) {
			if (el.getProperty('id') == id) {
				e=el;
			}
		});
		return e;
	},
	
	getClasses: function(filter) {
		var a = $A(this.className.split(' ')).map(function(c) {
			return c.trim();
		}).filter(function(c) {
			if (Object.isUndefined(filter) || filter.trim() == '')
				return true;
			return c.trim().startsWith(filter);
		});
		return a;
	},
	
	removeClasses: function(filter) {
		this.getClasses(filter).each(function(c) {
			this.removeClass(c);
		}.bind(this));
	}
});

Array.implement({
	implode: function(s) {
		var t='';
		this.each(function(e) {
			t+=e+s;
		});
		return t.substr(0, t.length-1);
	},
	
	sum: function() {
		var sum = 0;
		this.each(function(n) {
			sum += parseInt(n);
		});
		return sum;
	},
	
	removeEmpty: function() {
		return this.filter(function(i) {
			return (i != '');
		});
	}
});

String.implement({
  	startsWith: function(pattern) {
    	return this.indexOf(pattern) === 0;
  	},

  	endsWith: function(pattern) {
    	var d = this.length - pattern.length;
    	return d >= 0 && this.lastIndexOf(pattern) === d;
  	}
});

if (!$defined(window.console)) {
	window.console = {
		log: function() {}
	};
}
