
if (typeof(window.$)=='undefined'){
	function $(element){
		if (typeof(element)=='string')
			element = document.getElementById(element);
		else if (window._prototyper) // IE (.htc)
			element = window._prototyper(element);
		return element;
	}
}

function $A(iterable){
	if(typeof(iterable)=='undefined'){
		iterable = [];
	}
	else if(iterable.item){
		var l = iterable.length, array = new Array(l);
		while (l--) array[l] = iterable[l];
		return array;
	}
	return Array.prototype.slice.call(iterable);
};

function $clear(timer){
	clearTimeout(timer);
	clearInterval(timer);
	return null;
};


function isIE(){
	return typeof(ActiveXObject)!='undefined';
};



var Element = window.Element || function() {};

Element.prototype.__prototyped = true;



Element.prototype.getElementsByClassName = function(className){
	var className = new RegExp('\\b'+className+'\\b');
	var out = [];
	var elements = this.getElementsByTagName('*');
	for(var i=0, element; element=elements[i]; i++){
		if(className.test(element.className)){
			out.push(element);
		}
	}
	return $A(out);
};
document.getElementsByClassName = Element.prototype.getElementsByClassName;
window.getElementsByClassName = function(className){
	return document.getElementsByClassName(className);
};



/*
 * Events
 */
var Event = function(event){
	for(pName in event)
		this[pName] = event[pName];
	this.stop = function(){
		return this.stopPropagation().preventDefault();
	};
	this.preventDefault = function(){
		if (this.event.preventDefault) this.event.preventDefault();
		else this.event.returnValue = false;
		return this;
	};
	this.stopPropagation = function(){
		if (this.event.stopPropagation) this.event.stopPropagation();
		else this.event.cancelBubble = true;
		return this;
	};
	this.target = event.target || event.srcElement;
	this.event = event;
	return this;
};
Event.removeOn = function(string){
	return string.replace(/^on([A-Z])/, function(full, first){
		return first.toLowerCase();
	});
};



Element.prototype._runEventFcts = function(type, event){
	type = Event.removeOn(type);
	if (!this.$events[type]) return;
	if (event) event = new Event(event);
	for (var i in this.$events[type])
		this.$events[type][i].apply(this, [event]);
};
window._runEventFcts = document._runEventFcts = Element.prototype._runEventFcts;

Element.prototype.addEvent = function(type, fn){
	type = Event.removeOn(type);

	this.$events = this.$events || {};
	var mustAttach = (typeof(this.$events[type])=='undefined');
	this.$events[type] = this.$events[type] || [];
	this.$events[type].push(fn);

	if (mustAttach && type!='domready'){
		var self = this;
		var fn = function(event){self._runEventFcts(type, event);};
		if (this.addEventListener)
			this.addEventListener(type, fn, false);
		else if (this.attachEvent)
			this.attachEvent('on'+type, fn);
	}
	return this;
};
window.addEvent = document.addEvent = Element.prototype.addEvent;

Element.prototype.addEvents = function(events){
	for (var key in events)
		this.addEvent(key, events[key]);
	return this;
};
window.addEvents = document.addEvents = Element.prototype.addEvents;

Element.prototype.fireEvent = function(type, args, delay){
	type = Event.removeOn(type);
	if (!this.$events || !this.$events[type]) return this;
	for(var i=0, fn; fn=this.$events[type][i]; i++){
		if (delay){
			var self = this;
			var returns = function() {
				return fn.apply(self, (args || []));
			};
			return setTimeout(returns, delay);
		} else {
			fn.apply(this, (args || []));
		}
	}
	return this;
};
window.fireEvent = document.fireEvent = Element.prototype.fireEvent;

Element.prototype.removeEvent = function(type, fn){
	for (var key in this.$events[type]){
		if (this.$events[type][key]==fn)
			delete this.$events[type][key];
	}
	return this;
};
window.removeEvent = document.removeEvent = Element.prototype.removeEvent;

Element.prototype.removeEvents = function(type){
	type = Event.removeOn(type);
	this.$events[type] = [];
	return this;
};
window.removeEvents = document.removeEvents = Element.prototype.removeEvents;



/*
 * DomReady
 */
(function(){
	Event.domReady = false;

	var domready = function(){
		if (Event.domReady) return;
		Event.domReady = true;
		window.fireEvent('domready');
		document.fireEvent('domready');
	};

	// TODO: Implémenter d'autres méthodes de test
	// http://devblog.techhead.biz/2009/04/domcontentloaded-ondocumentready.html

	// IE le "signale" depuis le fichier ProtoJS.IE.js
	window.addEvent('load', domready);
	document.addEvent('DOMContentLoaded', domready);
})();



/*
 * Misc
 */
if (window.HTMLCollection){
	window.addEvent('domready', function(){
		for (var key in Array.prototype){
			try {
				HTMLCollection.prototype[key] = Array.prototype[key];
			} catch(e) {}
		}
	});
}


