/*****************************/
//@author yann - wcube


/********* A configurer ******/
var _ID_1ST_DIV = 'div_contenu';
var _ID_2ND_DIV = 'table_contenu';
var _ID_SCROLLBAR = 'scrollBar';
var _ID_CURSOR = 'scrollCursor';
var _ID_BT_UP = 'scrollUp';
var _SPEED = 15; //rapidité de défilement
var _DEBUG = false; //mettre true pour afficher la console de debug

/************* pour caler le curseur par rapport à la souris sur la scrollbar (onDrag) **************
	régler la première ligne de initScrollBar
	_1ST_DIV_TOP = PPK_findPosY(document.getElementById(_ID_1ST_DIV))+15; <---- ce chiffre
	en fait ce chiffre varie en fonction de la différence entre 
	la hauteur de la scrollbar et celle de la div qui la contient
	
	faire varier aussi les petits +2 ou +3 par-ci par là pour caler en fonction de la taille
	des pictos utilisés pour faire la scrollbar (pas super mais provisoire :pp)
**********************************************************************/

/******** SCRIPT SCROLLBAR *******/

document.onmousedown = checkClick;
document.onmouseup = function() { drag=false; };
document.onmousemove = onDragCursorScroll;


var _1stDiv_height, _2ndDiv_height, _2ndDiv, _2ndDiv_hiddenZone, ratioScroll, ruler_height, _cursor_height, _bt_up_down_height, _scrollbar_height;
var _limite_top, _limite_bottom, _1ST_DIV_TOP;
var drag=false;
var scrolling=false;
var clockCursor=0;

function initScrollBar() {
_1ST_DIV_TOP = PPK_findPosY(document.getElementById(_ID_1ST_DIV))-27;  
_1stDiv_height = document.getElementById(_ID_1ST_DIV).clientHeight;
_2ndDiv_height = document.getElementById(_ID_2ND_DIV).clientHeight;
_cursor_height = document.getElementById(_ID_CURSOR).clientHeight;
_bt_up_down_height = document.getElementById(_ID_BT_UP).clientHeight;
_scrollbar_height = document.getElementById(_ID_SCROLLBAR).clientHeight; 

dbg("div1_h:"+_1stDiv_height+" div2_h:"+_2ndDiv_height+" cursor_h:"+_cursor_height+" bt_h:"+_bt_up_down_height+" sbar_h:"+_scrollbar_height);

_2ndDiv = document.getElementById(_ID_2ND_DIV);
_2ndDiv_hiddenZone = _2ndDiv_height-_1stDiv_height;
ruler_height = _scrollbar_height - (_bt_up_down_height*1.5);
ratioScroll = _2ndDiv_hiddenZone/ruler_height;

dbg("hzone:"+_2ndDiv_hiddenZone+" ruler_h:"+ruler_height+" rs:"+ratioScroll+" lim_top:"+_limite_top+" lim_bot:"+_limite_bottom);

	if (_2ndDiv_hiddenZone>0) { //afficher le scroller
	document.getElementById(_ID_SCROLLBAR).style.visibility = 'visible';
	}
}

function startScroll(e) {
clockCursor = setInterval("onMouseOverScroll('"+e+"');", 100);	
scrolling=true;
}

function stopScroll() {
clearInterval(clockCursor);
scrolling=false;
}

function onMouseOverScroll(e) {
var posTop = findPosY(document.getElementById(_ID_CURSOR));
_limite_top = _bt_up_down_height;
_limite_bottom = _scrollbar_height-_bt_up_down_height-_cursor_height;

	if (posTop == "") {
	posTop=_bt_up_down_height;
	dbg("posTop : " + posTop);
	}

	if (scrolling&&!drag) {
		dbg('scrolling.... e:'+e);
		
		if (e=='down') {
			if (parseInt(posTop)+_SPEED<_limite_bottom) {		
				dbg('parseInt(posTop)+_SPEED<_limite_bottom : ' + parseInt(posTop) + '+' + _SPEED + '<' + _limite_bottom );
			var cursorPosTop = (parseInt(posTop) + _SPEED);				
			}					
			else if (parseInt(posTop)+_SPEED>=_limite_bottom) {
				dbg("parseInt(posTop)+_SPEED>=_limite_bottom : " + parseInt(posTop)+' + '+_SPEED + ' >= ' + _limite_bottom);
			var cursorPosTop = _limite_bottom + 3;
			}			
		}
		else {		
			if (parseInt(posTop)-_SPEED>_limite_top) {
				dbg('parseInt(posTop)-_SPEED>_limite_top : ' + parseInt(posTop) + '-' + _SPEED + '>' + _limite_top);
			var cursorPosTop = (parseInt(posTop) - _SPEED);
			}
			else if (parseInt(posTop)-_SPEED<=_limite_top) {
				dbg('parseInt(posTop)-_SPEED <=_limite_top' + parseInt(posTop) + '-' + _SPEED + ' <= ' + _limite_top);
			var cursorPosTop = _limite_top;	
			}
		}
				
	if (!isNaN(cursorPosTop)) scrollCursor(cursorPosTop);	
	else dbg('cursorPosTop isNaN');
	}
	else dbg("not scrolling");	
}

function onDragCursorScroll(e) {
	if (!drag) return;
	else {
	dbg ("---------- DRAG.............");		
	_limite_top = _1ST_DIV_TOP+_bt_up_down_height+_cursor_height*1.5+5; 
	_limite_bottom = _1ST_DIV_TOP+_scrollbar_height+_cursor_height*0.5-3;
	stopScroll();
		
		if (document.selection) { document.selection.empty(); }
	
	var posy = 0;
		
		if (!e) var e = window.event;
		
		if (e.pageY) posy = e.pageY;
		else if (e.clientY) posy = e.clientY + document.body.scrollTop;		

		if ((posy>_limite_top) && (posy<_limite_bottom) ) {		
			dbg("(posy>_limite_top) && (posy<_limite_bottom) : " + posy + ">" + _limite_top + " && " + posy + "<"+_limite_bottom);		
		var cursorPosTop = posy - _1ST_DIV_TOP - _bt_up_down_height - _cursor_height;
		}	
		else if (posy<_limite_top) {
			dbg("posy<_limite_top : " + posy + "<" + _limite_top);
		var cursorPosTop = _bt_up_down_height;
		}
		else if (posy>_limite_bottom) {
			dbg("posy>_limite_bottom : " + posy + ">" + _limite_bottom);
		var cursorPosTop = _scrollbar_height-_bt_up_down_height-_cursor_height+3;
		}					
	if (!isNaN(cursorPosTop)) scrollCursor(cursorPosTop);
	else dbg('cursorPosTop isNaN');
	}
}

function scrollCursor(cursorPosTop) {
var posTop = findPosY(_2ndDiv);
dbg("***" + Math.abs(cursorPosTop)+ " " + _bt_up_down_height + "***");

dbg(" cursorPosTop:" + cursorPosTop);
var position_contenu = - Math.floor((cursorPosTop) * ratioScroll);

	if (cursorPosTop == _bt_up_down_height) position_contenu=0;

dbg(" position_contenu:" + position_contenu);
document.getElementById(_ID_CURSOR).style.top = cursorPosTop + "px";
document.getElementById(_ID_2ND_DIV).style.top = position_contenu + "px";
}

// ---------------------------

function checkClick(e) {
	if (!e) var e = window.event;
	if (e.target) var target = e.target;
	else target = e.srcElement;
	
	if (target.getAttribute("id")) {
	var idTarget = target.getAttribute("id");	
	//window.status=idTarget;
	
		if (idTarget==_ID_CURSOR) {  drag=true; }
	}
}

function findPosY(obj) {
var posTop = obj.style.top.replace(/px/,'');	
return posTop;
}

function PPK_findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function dbg(txt) {
	if (!_DEBUG) return;
var o=document.getElementById('debug');	

	if (!o) {
	var d=document;
		if (document.body) d=document.body;
		else if (document.firstChild) d=document.firstChild;		
		
	o=document.createElement("div");
	o.setAttribute("id", "debug");
	
	/*var o_css=	"position:absolute; left:50%; margin-left:-150px; bottom:0; direction:ltr; border:2px solid #42FF00; font:10px Arial;" 
		+ 	"background-color:black; overflow:auto; width:300px; height:200px; color:#42FF00; padding:4px;";*/		

		try {
			with (o.style) {
			position='absolute';
			left='50%';
			bottom='0';
			marginLeft='-150px'
			border='2px solid #42FF00';
			fontFamily='Arial';
			fontSize='10px';
			backgroundColor='black';
			overflow='auto';
			width='300px';
			height='200px';
			color='#42FF00';
			padding='4px';							
			}
		d.appendChild(o);
		}
		catch (e) {
		alert(e);	
		}		
	}
	
	if (o.innerHTML.length>756) o.innerHTML='';
o.innerHTML = txt + "<br>" + o.innerHTML;		
}