var scrollerSpeed = 3;
var scrollerTimer;
var dragTimer;
var dragLastX = 0;
var dragLastY = 0;
var dragCurrentX = 0;
var dragCurrentY = 0;
var bDraging = false;

document.onselectstart = new Function ("return false");

function scrollText(direction) 
{	
	obj = document.getElementById("scrolltext_content");
    
	if (parseInt(obj.style.top) + (scrollerSpeed * direction) > 0)  
	{
		obj.style.top = "0px";
		clearTimeout(scrollerTimer);
	}
	else if(parseInt(obj.style.top) + (scrollerSpeed * direction)<-(obj.offsetHeight-document.getElementById("scrolltext_container").offsetHeight)) 
	{
		obj.style.top = -(obj.offsetHeight - document.getElementById("scrolltext_container").offsetHeight) + "px";
		clearTimeout(scrollerTimer);
	}
    else 
	{
        obj.style.top = (parseInt(obj.style.top) + (scrollerSpeed * direction)) + "px";
	}
	scrollerTimer = setTimeout("scrollText(" + direction + ");", 30);
	
	refreshSlider();
}

function refreshSlider()
{
	var icontentHeight = document.getElementById("scrolltext_container").offsetHeight;
	var icontentTextHeight = document.getElementById("scrolltext_content").offsetHeight;
	var icontentTextTop = parseInt(document.getElementById("scrolltext_content").style.top);
	var iscrollerHeight = document.getElementById("scrollbar_slider").offsetHeight;
	var iscrollerContainerHeight = document.getElementById("scrollbar_sildercontainer").offsetHeight;
	
	var itop = Math.round((icontentTextTop / (icontentHeight- icontentTextHeight)) * (iscrollerContainerHeight - iscrollerHeight));
	
	document.getElementById("scrollbar_slider").style.marginTop = itop + "px";
}

function scrollUp() 
{
	clearTimeout(scrollerTimer);
	scrollText(1);
}

function scrollDown() 
{
	clearTimeout(scrollerTimer);
	scrollText(-1);
}

function scrollStop()
{
	clearTimeout(scrollerTimer);
}

/* body onLoad (initialisation de l'evenement) */
if (window.addEventListener)
	window.addEventListener('DOMMouseScroll', wheel, false);

window.onmousewheel = document.onmousewheel = wheel;

function handle(delta)
{
	scrollText(delta * scrollerSpeed);
	clearTimeout(scrollerTimer);
}

function wheel(event)
{
	clearTimeout(scrollerTimer);
	var delta = 0;
	if (!event) /* For IE. */
			event = window.event;
	if (event.wheelDelta) { /* IE/Opera. */
			delta = event.wheelDelta/120;
			/** In Opera 9, delta differs in sign as compared to IE.
			 */
			if (window.opera)
					delta = -delta;
	} else if (event.detail) { /** Mozilla case. */
			/** In Mozilla, sign of delta is different than in IE.
			 * Also, delta is multiple of 3.
			 */
			delta = -event.detail/3;
	}
	/** If delta is nonzero, handle it.
	 * Basically, delta is now positive if wheel was scrolled up,
	 * and negative, if wheel was scrolled down.
	 */
	if (delta)
			handle(delta);
	/** Prevent default actions caused by mouse wheel.
	 * That might be ugly, but we handle scrolls somehow
	 * anyway, so don't bother here..
	 */
	if (event.preventDefault)
			event.preventDefault();
	event.returnValue = false;
}

function dragOnSlider()
{
	bDraging = true;
	draging();
}

function dragOffSlider()
{
	bDraging = false;
}

function draging()
{
	if (bDraging == false)
	{
		clearTimeout(dragTimer)
	}
	else
	{
		refreshDraging();
		dragTimer = setTimeout("draging();", 30);
	}
}

function position(e) 
{
	x = (navigator.appName.substring(0,3) == "Net") ? e.pageX : event.x+document.body.scrollLeft;
	y = (navigator.appName.substring(0,3) == "Net") ? e.pageY : event.y+document.body.scrollTop;
	dragCurrentX = x;
	dragCurrentY = y;
}

if (navigator.appName.substring(0,3) == "Net") document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = position;

/*
function mousingdown(e)
{
	dragOnSlider()
}

if (navigator.appName.substring(0,3) == "Net") document.captureEvents(Event.MOUSEDOWN);
document.onmousedown = mousingdown;
*/

function mousingup(e)
{
	dragOffSlider()
}

if (navigator.appName.substring(0,3) == "Net") document.captureEvents(Event.MOUSEUP);
document.onmouseup = mousingup;


function refreshDraging()
{
	if (dragLastY != 0)
	{
		var fMovingInterval = -(dragCurrentY - dragLastY);
		dragLastY = dragCurrentY;
		
		obj = document.getElementById("scrolltext_content");
    
		if (parseInt(obj.style.top) + (fMovingInterval) > 0)  
		{
			obj.style.top = "0px";
		}
		else if(parseInt(obj.style.top) + (fMovingInterval)<-(obj.offsetHeight-document.getElementById("scrolltext_container").offsetHeight)) 
		{
			obj.style.top = -(obj.offsetHeight - document.getElementById("scrolltext_container").offsetHeight) + "px";
		}
		else 
		{
			obj.style.top = (parseInt(obj.style.top) + (fMovingInterval)) + "px";
		}
	}
	else
		dragLastY = dragCurrentY;
		
	refreshSlider();
}
