/**
 * Gestion des evenements
 */
var Events = (function() {
	var guid = 1;
	var observers = [];
	function handle(event) {
		var returnValue = true;
		event = event || fix(window.event);
		var handlers = this.events[event.type];
		for(var i in handlers) {
			this.$$handle = handlers[i];
			if(this.$$handle(event) === false) {returnValue = false;}
		}
		return returnValue;
	};
	function fix(event) {
		event.preventDefault = function() {this.returnValue = false;};
		event.stopPropagation = function() {this.cancelBubble = true;};
		return event;
	};
	var onload = [];
	function loaded() {
		if(arguments.callee.done) {return;}
		arguments.callee.done = true;
		for(var i = 0; i < onload.length; i++) {onload[i]();}
	};
	return {
		add: function(element, type, handler) {
			if(element.addEventListener) {
				element.addEventListener(type, handler, false);
			} else {
				if(!handler.$$guid) {handler.$$guid = guid++;}
				if(!element.events) {element.events = {};}
				var handlers = element.events[type];
				if(!handlers) {
					handlers = element.events[type] = {};
					if(element["on" + type]) {handlers[0] = element["on" + type];}
				}
				handlers[handler.$$guid] = handler;
				element["on" + type] = handle;
			}
			observers[observers.length] = {element: element, type: type, handler: handler};
		},
		remove: function(element, type, handler) {
			if (element.removeEventListener) {
				element.removeEventListener(type, handler, false);
			} else {
				if (element.events && element.events[type]) {
					delete element.events[type][handler.$$guid];
				}
			}
		},
		clean: function() {
			for(var i = 0, oi; oi = observers[i]; i++) {
				Events.remove(oi.element, oi.type, oi.handler);
			}
			observers = null;
			onload = null;
		},
		load: function(fireThis) {
			onload[onload.length] = fireThis;
			if(document.addEventListener) {document.addEventListener('DOMContentLoaded', loaded, null);}
			if(/KHTML|WebKit/i.test(navigator.userAgent)) { 
				var _timer = setInterval(function() {
					if(/loaded|complete/.test(document.readyState)) {
						clearInterval(_timer);
						delete _timer;
						loaded();
					}
				}, 10);
			}
			/*@cc_on @*/
			/*@if (@_win32)
			var proto = "src='javascript:void(0)'";
			if(location.protocol == "https:") proto = "src=//0";
			document.write("<scr"+"ipt id=__ie_onload defer " + proto + "><\/scr"+"ipt>");
			var script = document.getElementById("__ie_onload");
			script.onreadystatechange = function() {if(this.readyState == "complete") {loaded();}};
			/*@end @*/
			Events.add(window, 'load', loaded);
		}
	};
})();

// Suppression des evenements en unload (fuites memoire)
Events.add(window, 'unload', Events.clean);

/**
 * DomDrag : par Aaron Boodman
 */
var DomDrag = {
	obj: null,
	
	init: function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper) {
		o.onmousedown = DomDrag.start;
		o.hmode = bSwapHorzRef ? false : true;
		o.vmode = bSwapVertRef ? false : true;
		o.root = oRoot && oRoot != null ? oRoot : o;
		if(o.hmode && isNaN(parseInt(o.root.style.left))) {o.root.style.left = '0px';}
		if(o.vmode && isNaN(parseInt(o.root.style.top))) {o.root.style.top = '0px';}
		if(!o.hmode && isNaN(parseInt(o.root.style.right))) {o.root.style.right = '0px';}
		if(!o.vmode && isNaN(parseInt(o.root.style.bottom))) {o.root.style.bottom = '0px';}
		o.minX = typeof minX != 'undefined' ? minX : null;
		o.minY = typeof minY != 'undefined' ? minY : null;
		o.maxX = typeof maxX != 'undefined' ? maxX : null;
		o.maxY = typeof maxY != 'undefined' ? maxY : null;
		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;
		o.root.onDragStart = new Function();
		o.root.onDragEnd = new Function();
		o.root.onDrag = new Function();
	},
	
	start: function(e) {
		var o = DomDrag.obj = this;
		e = DomDrag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right);
		o.root.onDragStart(x, y);
		o.lastMouseX = e.clientX;
		o.lastMouseY = e.clientY;
		if(o.hmode) {
			if(o.minX != null) {o.minMouseX = e.clientX - x + o.minX;}
			if(o.maxX != null) {o.maxMouseX = o.minMouseX + o.maxX - o.minX;}
		} else {
			if(o.minX != null) {o.maxMouseX = -o.minX + e.clientX + x;}
			if(o.maxX != null) {o.minMouseX = -o.maxX + e.clientX + x;}
		}
		if(o.vmode) {
			if(o.minY != null) {o.minMouseY = e.clientY - y + o.minY;}
			if(o.maxY != null) {o.maxMouseY = o.minMouseY + o.maxY - o.minY;}
		} else {
			if(o.minY != null) {o.maxMouseY = -o.minY + e.clientY + y;}
			if(o.maxY != null) {o.minMouseY = -o.maxY + e.clientY + y;}
		}
		document.onmousemove = DomDrag.drag;
		document.onmouseup = DomDrag.end;
		return false;
	},
	
	drag: function(e) {
		e = DomDrag.fixE(e);
		var o = DomDrag.obj;
		var ey = e.clientY;
		var ex = e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right);
		var nx, ny;
		if(o.minX != null) {ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);}
		if(o.maxX != null) {ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);}
		if(o.minY != null) {ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);}
		if(o.maxY != null) {ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);}
		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
		if(o.xMapper) {nx = o.xMapper(y)}
		else if(o.yMapper) {ny = o.yMapper(x)}
		DomDrag.obj.root.style[o.hmode ? 'left' : 'right'] = nx + 'px';
		DomDrag.obj.root.style[o.vmode ? 'top' : 'bottom'] = ny + 'px';
		DomDrag.obj.lastMouseX = ex;
		DomDrag.obj.lastMouseY = ey;
		DomDrag.obj.root.onDrag(nx, ny);
		return false;
	},
	
	end: function() {
		document.onmousemove = null;
		document.onmouseup = null;
		DomDrag.obj.root.onDragEnd(parseInt(DomDrag.obj.root.style[DomDrag.obj.hmode ? 'left' : 'right']), parseInt(DomDrag.obj.root.style[DomDrag.obj.vmode ? 'top' : 'bottom']));
		DomDrag.obj = null;
	},
	
	fixE: function(e) {
		if(typeof e == 'undefined') {e = window.event;}
		if(typeof e.layerX == 'undefined') {e.layerX = e.offsetX;}
		if(typeof e.layerY == 'undefined') {e.layerY = e.offsetY;}
		return e;
	}
};

/**
  * création et gestion de la scrollbar
  */
var scroll = {
	init: function() {
		scroll.mask = document.getElementById('listinfo');
		scroll.full = document.getElementById('listinfo-inner');
		if(!scroll.mask || !scroll.full) {return;}
		if(scroll.full.offsetHeight > scroll.mask.offsetHeight) {scroll.create();}
	},
	
	create: function() {
		var div = document.createElement('div');
		div.setAttribute('id', 'scroll');
		div.innerHTML = '<a href="#" id="scroll-up"><span>Go up</span></a>'
						+'<a href="#" id="scroll-bar"><span>Go up or down</span></a>'
						+'<a href="#" id="scroll-down"><span>Go down</span></a>';
		this.mask.insertBefore(div, this.mask.firstChild);
		this.up = document.getElementById('scroll-up');
		this.down = document.getElementById('scroll-down');
		Events.add(this.up, 'click', this.click);
		Events.add(this.down, 'click', this.click);
		this.bar = document.getElementById('scroll-bar');
		this.bar.onclick = function() {return false;}
		this.barH = this.bar.offsetHeight;
		this.bar.style.height = Math.round(this.mask.offsetHeight / this.full.offsetHeight * this.bar.offsetHeight) +'px';
		this.amountB = this.bar.offsetHeight - 10;
		var yMax = this.barH - this.bar.offsetHeight;
		var barMove = new DomDrag.init(this.bar, null, 0, 0, 0, yMax);
		this.bar.onDrag = function(x, y) {scroll.move(x, y);}
	},
	
	move: function(x, y) {
		this.full.style.top = -Math.round(y / this.barH * this.full.offsetHeight) +'px';
	},
	
	click: function(e) {
		e.preventDefault();
		if(this.id == 'scroll-down') {
			scroll.goDown();
		} else if(this.id == 'scroll-up') {
			scroll.goUp();
		}
	},
	
	goUp: function() {
		var top = parseInt(this.bar.style.top);
		var h = this.bar.offsetHeight;
		var sB, sF;
		if((top - this.amountB) < 0) {
			sB = 0;
		} else {
			sB = top - this.amountB;
		}
		this.bar.style.top = sB +'px';
		sF = sB / this.barH * this.full.offsetHeight;
		this.full.style.top = - sF +'px';
	},
	
	goDown: function() {
		var top = parseInt(this.bar.style.top);
		var h = this.bar.offsetHeight;
		var sB, sF;
		if((top + h + this.amountB) > this.barH) {
			sB = this.barH - h;
		} else {
			sB = top + this.amountB;
		}
		this.bar.style.top = sB +'px';
		sF = sB / this.barH * this.full.offsetHeight;
		this.full.style.top = - sF +'px';
	}
};

Events.load(scroll.init);