window.addEvent('load',function () {
	new scrollerManager($$(".newcontenu")[0], true);
});
/**
* scroller
**/
var scrollerManager = function(elm, extensible, speed) {
	// setVar
	if(elm.scrollHeight <= elm.offsetHeight) return;
	var that = this;
	elm.innerHTML += '<br /><br />'; // simplifie largement le pb  du "coupage" du bas des lettres p, g et j.
	this.elm = elm;
	this.elm.scrollTop = 0;
	this.initWidth = elm.offsetWidth;
	this.initHeight = elm.offsetHeight;
	this.scHeight = elm.scrollHeight - elm.offsetHeight;
	this.scRatio = elm.offsetHeight / elm.scrollHeight;
	this.percent = 0;
	if(!speed) this.speed = 0.10;
	this.liftClicked = false;
	/* TODO
	pr&eacute;voir le cas ou elm est le body
	*/
	this.container = document.createElement('div');
	this.container.id = "scroller_"+((new Date()).getTime());
	this.container.style.zIndex  = "1000000000";
	
	this.elm.parentNode.appendChild(this.container);
	
	// creation des boutons du scroll 
	this.imgTop = document.createElement('a');
	this.imgTop.className = "scrollerTop";
	this.container.appendChild(this.imgTop);
	
	this.imgRoad = document.createElement('a');
	this.imgRoad.className = "scrollerRoad";
	this.container.appendChild(this.imgRoad);
	
	this.imgBottom = document.createElement('a');
	this.imgBottom.className = "scrollerBottom";
	this.container.appendChild(this.imgBottom);

	this.imgLift = document.createElement('a');
	this.imgLift.className = "scrollerLift";
	this.container.appendChild(this.imgLift);
	// placement dynamique des elms
	this.baseTop = getTop(elm);
	this.RoadHeight = elm.offsetHeight - this.imgTop.offsetHeight - this.imgBottom.offsetHeight;

	this.imgRoad.style.height = this.RoadHeight + "px";
	//
	if(extensible){
		this.imgRoad.innerHTML = "<span class='head'></span><span class='body' id='"+this.container.id+"_road"+"'></span><span class='foot'></span>";
		this.imgLift.innerHTML = "<span class='head'></span><span class='body' id='"+this.container.id+"_lift"+"'></span><span class='foot'></span>";
		this.liftBody = document.getElementById(this.container.id+"_lift");
		this.liftBody.style.height = Math.ceil(this.RoadHeight * this.scRatio) + "px";
		this.roadBody = document.getElementById(this.container.id+"_road");
		this.roadBody.style.height = this.RoadHeight - (getCSSRule(this.imgRoad.firstChild, "height", false) *2)+ "px";
		this.imgRoad.style.height = "";
	}
	//
	this.container.style.overflow = "hidden";
	this.container.style.width = this.imgTop.offsetWidth + "px";
	this.container.style.height = this.initHeight + "px";
	this.container.style.position = "absolute";
	this.container.style.top = "30px";
	//
	
	
	//
	elm.style.overflow = "hidden";
	//alert(getCSSRule(elm, "marginRight", false))
	//elm.style.marginRight = getCSSRule(elm, "marginRight", false) + this.container.offsetWidth + "px";
	//elm.style.width = elm.offsetWidth - this.container.offsetWidth + "px";
	this.container.style.left = elm.offsetWidth - 12 + 'px';
	//if(IS_MS) this.container.style.left = getLeft(elm) + elm.offsetWidth + 25 + 'px';
	
	
	
	// calcul du d&eacute;batement : calcul la vitesse de deplacement en px
	this.debatPx = this.imgRoad.offsetHeight-this.imgLift.offsetHeight;
	var htTot = this.scHeight-this.container.offsetHeight;
	this.deb = (this.debatPx/htTot)*this.speed;
	this.minTop = this.imgTop.offsetHeight;
	this.maxTop = this.debatPx + this.minTop;
	this.liftHeight = this.imgLift.offsetHeight;
	this.imgLift.style.top = this.minTop + "px";
	this.currentPos = this.minTop;
	
	
	/** EVENTS **/
	/*imgTop*/
	this.imgTop.onmousedown = function (obj){
		return function (){
			obj.moveScroll(obj.percent-obj.speed);
			obj.interval = setInterval(function(){obj.moveScroll(obj.percent-obj.speed);},100);
		}
	}(this);
	this.imgTop.onmouseup = this.imgTop.onmouseout = function (obj){
		return function (){
			obj.stopScroll();;
		}
	}(this);
	/*imgBottom */
	this.imgBottom.onmousedown = function (obj){
		return function (){
			obj.moveScroll(obj.percent+obj.speed);
			obj.interval = setInterval(function(){obj.moveScroll(obj.percent+obj.speed);},100);
		}
	}(this);
	this.imgBottom.onmouseup = this.imgBottom.onmouseout = function (obj){
		return function (){
			obj.stopScroll();
		}
	}(this);
	/*imgRoad*/
	this.imgRoad.onmousedown = function (obj){
		return function (e){
			if(!e) e = window.event;
			var Y = e.clientY;
			var sens = (Y > getTop(obj.imgLift)) ? 1 : -1; 
			obj.moveScroll(obj.percent+(obj.speed*sens));
			obj.interval = setInterval(function(e){
				if(sens == 1 && Y < getTop(obj.imgLift)+obj.liftHeight) obj.stopScroll();
				if(sens == -1 && Y > getTop(obj.imgLift)) obj.stopScroll();
				obj.moveScroll(obj.percent+(obj.speed*sens));
			},50);
		}
	}(this);
	this.imgRoad.onmouseup = this.imgRoad.onmouseout = function (obj){
		return function (e){
			obj.stopScroll();
		}
	}(this);
	
	/*imgLift*/	
	Drag.init(this.imgLift, null, 0, 0,this.minTop, this.maxTop, true, false);
	
	this.imgLift.onDrag = function (obj){
		return function (x,y){
			this.style.cursor = "s-resize";
			var delta = (y - obj.minTop)/ obj.debatPx;
			obj.moveScroll(delta, true);
		}
	}(this)
	this.imgLift.onDragEnd = function (obj){
		return function (x,y){
			this.style.cursor = "";
		}
	}(this)
	/*elm*/
	if(document.addEventListener){
	elm.addEventListener('DOMMouseScroll', function (obj){
		return function (e){
			var delta = e.detail;
			obj.moveScroll(obj.percent+(delta/100));
		}
	}(this), null);
	}
	else{
	elm.attachEvent('onmousewheel', function (obj){
		return function (e){
			if(!e) e = window.event;
			var delta = -e.wheelDelta/40;
			//alert(delta);			
			if(window.opera) delta = -delta;
			obj.moveScroll(obj.percent+(delta/100));
		}
	}(this));
	}
	
	
	return this;
}



	
scrollerManager.prototype.moveScroll = function (pourcent, fromDrag){
		if(pourcent>1) pourcent = 1;
		if(pourcent<0) pourcent = 0;
		this.percent = pourcent;
		this.elm.scrollTop = this.scHeight*pourcent;
		var newTop = this.debatPx * pourcent;
		if(!fromDrag) this.imgLift.style.top = newTop + this.minTop + "px";
}

scrollerManager.prototype.stopScroll = function (){
		if(this.interval) clearInterval(this.interval);
}
	
scrollerManager.prototype.destroy = function (instance){
		if(!this.container) return;
		if(this.container.parentNode) this.container.parentNode.removeChild(this.container);
		this.elm.scrolltop = 0;
		this.elm.style.height = "";
		this.elm.style.marginRight = "";
		this.elm.style.width = "";
		if(instance){
			if(instance.interval) clearInterval(instance.interval);
			delete instance;
		}
}

/**
/ renvoie le left et le top d'un elm
**/
function getLeft(MyObject){
    if (MyObject.offsetParent)
	return (MyObject.offsetLeft + getLeft(MyObject.offsetParent));
    else
	return (MyObject.offsetLeft);
    }
function getTop(MyObject){
    if (MyObject.offsetParent)
	return (MyObject.offsetTop + getTop(MyObject.offsetParent));
    else
	return (MyObject.offsetTop);
    }


/**
/ renvoie la valeur CSS rule de l'elm cible --> withoutUnity n'a pas d'effet si rule == "all"
**/
function getCSSRule (cible, rule, withoutUnity){
	var CSSrule, cr;
	if(cible.currentStyle){
		cr = cible.currentStyle;
		CSSrule = cr[rule];
	}
	else {
		cr = window.getComputedStyle(cible, null);
		CSSrule = cr[rule];
	}
	if(rule == "all") {
		CSSrule = [];
		for(var a in cr){
			if (cr[a]!= "" && typeof(cr[a]) == 'string') 
				CSSrule.push([a, cr[a]]);
		}
	}
	if(withoutUnity && rule != "all") CSSrule = parseFloat(CSSrule);
	if(parseInt(CSSrule)/parseInt(CSSrule) != 1) {
		CSSrule = 0;
	}
	return parseFloat(CSSrule);	
}
/**
/  transforme rgb(0,0,0) en #000000
**/
function RGB2Hexa(rgb){
// samori powered
	if(rgb.indexOf('rgb') != -1){
		rgb = rgb.substring(4);
		rgb = rgb.substring(0, rgb.length-1);
		var s = [];
		rgb = rgb.split(',');
		for (x in rgb){
			if(parseInt(x)+1*2){ // si numeric
				var ind = parseInt(rgb[x]).toString(16);
				if(ind<10){ind = "0"+ind}
				s.push(ind);
			}
		}
		rgb = s.join("");
		rgb = "#"+rgb;
	
	}
	return rgb;
}
/***
* DOM-DRAG.js
*
***/

/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/



var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.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 = Drag.obj = this;
		e = Drag.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	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.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)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.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;
	}
};
