﻿
function Position(x, y)
{
  this.X = x;
  this.Y = y;

  this.Add = function(val)
  {
    var newPos = new Position(this.X, this.Y);
    if(val != null)
    {
      if(!isNaN(val.X))
        newPos.X += val.X;
      if(!isNaN(val.Y))
        newPos.Y += val.Y
    }
    return newPos;
  }

  this.Subtract = function(val)
  {
    var newPos = new Position(this.X, this.Y);
    if(val != null)
    {
      if(!isNaN(val.X))
        newPos.X -= val.X;
      if(!isNaN(val.Y))
        newPos.Y -= val.Y
    }
    return newPos;
  }

  this.Min = function(val)
  {
    var newPos = new Position(this.X, this.Y)
    if(val == null)
      return newPos;

    if(!isNaN(val.X) && this.X > val.X)
      newPos.X = val.X;
    if(!isNaN(val.Y) && this.Y > val.Y)
      newPos.Y = val.Y;

    return newPos;
  }

  this.Max = function(val)
  {
    var newPos = new Position(this.X, this.Y)
    if(val == null)
      return newPos;

    if(!isNaN(val.X) && this.X < val.X)
      newPos.X = val.X;
    if(!isNaN(val.Y) && this.Y < val.Y)
      newPos.Y = val.Y;

    return newPos;
  }

  this.Bound = function(lower, upper)
  {
    var newPos = this.Max(lower);
    return newPos.Min(upper);
  }

  this.Check = function()
  {
    var newPos = new Position(this.X, this.Y);
    if(isNaN(newPos.X))
      newPos.X = 0;
    if(isNaN(newPos.Y))
      newPos.Y = 0;
    return newPos;
  }

  this.Apply = function(element)
  {
    if(typeof(element) == "string")
      element = document.getElementById(element);
    if(element == null)
      return;
    if(!isNaN(this.X))
      element.style.left = this.X + 'px';
    if(!isNaN(this.Y))
      element.style.top = this.Y + 'px';
  }
}
// end position


function cancelEvent(e)
{
  e = e ? e : window.event;
  if(e.stopPropagation)
    e.stopPropagation();
  if(e.preventDefault)
    e.preventDefault();
  e.cancelBubble = true;
  e.cancel = true;
  e.returnValue = false;
  return false;
}

function getEventTarget(e)
{
  e = e ? e : window.event;
  return e.target ? e.target : e.srcElement;
}

function absoluteCursorPostion(eventObj)
{
  eventObj = eventObj ? eventObj : window.event;

  if(isNaN(window.scrollX))
    return new Position(eventObj.clientX + document.documentElement.scrollLeft + document.body.scrollLeft,
      eventObj.clientY + document.documentElement.scrollTop + document.body.scrollTop);
  else
    return new Position(eventObj.clientX + window.scrollX, eventObj.clientY + window.scrollY);
}

function dragObject(contenu, element, attachElement, lowerBound, upperBound, startCallback, moveCallback, endCallback,wheelCallback,attachLater)
{
  if(typeof(element) == "string")
    element = document.getElementById(element);
  if(element == null)
      return;

  var cursorStartPos = null;
  var elementStartPos = null;
  var dragging = false;
  var listening = false;
  var disposed = false;

  function dragStart(eventObj)
  {
    if(dragging || !listening || disposed) return;
    dragging = true;

    if(startCallback != null) startCallback(eventObj, element);
    cursorStartPos = absoluteCursorPostion(eventObj);
    elementStartPos = new Position(parseInt(element.style.left), parseInt(element.style.top));
    elementStartPos = elementStartPos.Check();
    hookEvent(document, "mousemove", dragGo);
    hookEvent(document, "mouseup", dragStopHook);
    return cancelEvent(eventObj);
  }

  function dragGo(eventObj)
  {
    if(!dragging || disposed) return;

    var newPos = absoluteCursorPostion(eventObj);
    newPos = newPos.Add(elementStartPos).Subtract(cursorStartPos);
    newPos = newPos.Bound(lowerBound, upperBound)
    newPos.Apply(element);
    if(moveCallback != null)
      moveCallback(newPos, element);

    return cancelEvent(eventObj);
  }

  function dragStopHook(eventObj)
  {
    dragStop();
    return cancelEvent(eventObj);
  }

  function dragStop()
  {
    if(!dragging || disposed) return;
    unhookEvent(document, "mousemove", dragGo);
    unhookEvent(document, "mouseup", dragStopHook);
    cursorStartPos = null;
    elementStartPos = null;
    if(endCallback != null)
      endCallback(element);
    dragging = false;
  }

  this.Dispose = function()
  {
    if(disposed) return;
    this.StopListening(true);
    element = null;
    attachElement = null
    lowerBound = null;
    upperBound = null;
    startCallback = null;
    moveCallback = null
    endCallback = null;
    disposed = true;
  }

  this.StartListening = function()
  {
    if(listening || disposed) return;
    listening = true;
    hookEvent(attachElement, "mousedown", dragStart);
	hookEvent(contenu,"mousewheel",wheel)
  }

  this.StopListening = function(stopCurrentDragging)
  {
    if(!listening || disposed) return;
    unhookEvent(attachElement, "mousedown", dragStart);
    listening = false;

    if(stopCurrentDragging && dragging)
      dragStop();
  }


  function wheel(event)
  {
	if (!event) event = window.event;
	if (event.wheelDelta) 
	{
		delta = event.wheelDelta/120; 
		if (window.opera) delta = -delta;
	} else if (event.detail) {
		delta = -event.detail/3;
	}
	if (delta) {wheelCallback(event,delta);}
    if (event.preventDefault) event.preventDefault();
    event.returnValue = false;
  }
	
  this.IsDragging = function(){ return dragging; }
  this.IsListening = function() { return listening; }
  this.IsDisposed = function() { return disposed; }

  if(typeof(attachElement) == "string")
    attachElement = document.getElementById(attachElement);
  if(attachElement == null)
    attachElement = element;

  if(!attachLater)
    this.StartListening();
}
// end DragObject

/**************************************************************************************************************************/
/**************************************************************************************************************************/

function Trackbar(DIVcontenant,DivContenu, callback) 
{ 
	var LargeurScroll=20;
	var MargeScroll=0;
 
	var _container = DivContenu

	var _TrackBar= document.createElement("div");
	_TrackBar.id = "scroller" + DIVcontenant.id;
  	_TrackBar.style.zIndex = 1;
  	_TrackBar.style.position = "absolute";
	_TrackBar.style.left=(DIVcontenant.offsetWidth-LargeurScroll-1*MargeScroll)+"px";
	_TrackBar.style.top=MargeScroll+"px";
  	_TrackBar.style.width=LargeurScroll+"px";
  	_TrackBar.style.height=(DIVcontenant.offsetHeight-2*MargeScroll)+"px";
  	//_TrackBar.style.backgroundColor="#123456"
	DIVcontenant.appendChild(_TrackBar);

	//TOP
	var D = document.createElement("div");
  	D.id= "top";
  	D.style.zIndex = 1;
  	D.style.position = "absolute";
	D.style.left=0+"px";
	D.style.top=0+"px";
  	D.style.width=LargeurScroll+"px";
  	D.style.height = LargeurScroll + "px";
    //D.style.backgroundColor="red"
  	D.onclick = function () { OnWheel(null, 1) }
	_TrackBar.appendChild(D);
	
	//BOTTOM
	D = document.createElement("div");
  	D.id= "bottom";
  	D.style.zIndex = 1;
  	D.style.position = "absolute";
	D.style.left=0+"px";
	D.style.top=(parseInt(DIVcontenant.offsetHeight)-(1*LargeurScroll)-2*MargeScroll)+"px";
  	D.style.width=LargeurScroll+"px";
  	D.style.height = LargeurScroll + "px";
  	//D.style.backgroundColor = "red"
  	D.onclick = function () { OnWheel(null, -1) }
	_TrackBar.appendChild(D);
	
	//ASCENSEUR
	D = document.createElement("div");
  	D.id= "fond";
  	D.style.zIndex = 1;
  	D.style.position = "absolute";
	D.style.left=0+"px";
	D.style.top=LargeurScroll+"px";
  	D.style.width=LargeurScroll+"px";
  	D.style.height = (parseInt(DIVcontenant.offsetHeight) - (2 * LargeurScroll) - (2 * MargeScroll)) + "px";
  	
	_TrackBar.appendChild(D);

	
	//CURSEUR
	var _pointer = document.createElement("div");
  	_pointer.id= "curseur";
  	_pointer.style.zIndex = 1;
  	_pointer.style.position = "absolute";
	_pointer.style.left=1+"px";
	_pointer.style.top=LargeurScroll+"px";
  	_pointer.style.width=LargeurScroll-2+"px";
  	_pointer.style.height = parseInt(parseInt(D.style.height) * DIVcontenant.offsetHeight / DivContenu.offsetHeight) + "px";
  	//_pointer.style.backgroundColor = "yellow"
	_TrackBar.appendChild(_pointer);

	var _FinalPos = 0;
	var _currentValue =  0; 
	var _minimumVal = 1;
	var _maximumVal = _container.offsetHeight-DIVcontenant.offsetHeight;
	var _height= DIVcontenant.offsetHeight;
	
    var ascenseur=D	
	var _pointerLB = new Position(1,LargeurScroll);
	var _pointerUB = new Position(1, _height-LargeurScroll-parseInt(_pointer.style.height)-2*MargeScroll);
	//var _pointerDrag = new dragObject(DIVcontenant, _pointer, null, _pointerLB, _pointerUB, OnDragBegin, OnDrag, null, OnWheel, true);
	var _pointerDrag = new dragObject(DIVcontenant, _pointer, _TrackBar, _pointerLB, _pointerUB, OnDragBegin, OnDrag, null, OnWheel, true);

	debugscroll("Contenant =" + DIVcontenant.offsetHeight + " Contenu=" + DivContenu.offsetHeight)
	debugscroll("Xontenant =" + (_pointer.style.height) + " Xontenu=" + parseInt(ascenseur.style.height))

	_callback = callback;		
	_contenant=DIVcontenant;
	_contenu=_container;
  
  	function getDivContenu(div)
  	{
  		var D=div
		while (D.className!="mscontainer") 
			D=D.parentElement
 		return D
	}

    function OnWheel(eventObj, delta) {
        var Div = DivContenu;
        var Ydest = "";
        var pos = getMousePos(eventObj);
        P = Div.style.top
        var DIVS = Div.getElementsByTagName("div")

        if(delta<0)
            for (i = 0; i < DIVS.length; i++) 
            {
                var N = DIVS[i]
                if (N.id=="X") 
                     if ((parseInt(N.offsetTop) + parseInt(P)) > 0) { Ydest = parseInt(N.offsetTop); _FinalPos = -Ydest; startslide(this); break }
            }
        else if(delta > 0)
            for (i = DIVS.length-1; i >=0; i--) {
                var N = DIVS[i]
                if (N.id == "X") 
                    if ((parseInt(N.offsetTop) + parseInt(P)) < 0 ) { Ydest = parseInt(N.offsetTop); _FinalPos = -Ydest; startslide(this); break }
            }


        if(Ydest=="")
        {
            if (P == "") { Div.style.top = "0"; P = 0 }
            P = parseInt(P) + delta * 20;
            Div.style.top = (P > 0 ? 0 : P) + "px";
            if (((Div.offsetHeight + P - DIVcontenant.offsetHeight) < -10) && (DIVcontenant.offsetHeight < _container.offsetHeight)) {
                P = DIVcontenant.offsetHeight - _container.offsetHeight - 10
                _container.style.top = P + "px";
            }
            C = parseInt(_container.style.top)
            ZH = _height - (0 * LargeurScroll) - parseInt(_pointer.style.height) - 2 * MargeScroll;
            pos.Y = -(C - _minimumVal) / (_maximumVal - _minimumVal) * ZH;
            pos = pos.Bound(_pointerLB, _pointerUB);
            pos.Apply(_pointer);
        }

        debugscroll("mouse wheel "+_FinalPos)
    }


	function startslide(E) {
        slide()
	}

	function slide() {
        
	    var Div = DivContenu;
	    Div.style.top = parseInt((parseInt(Div.style.top) + _FinalPos) / 2) + "px";
        debugscroll("slide "+Div.style.top)
	    if (Math.abs(parseInt((parseInt(Div.style.top) - _FinalPos))) < 3) {
	        Div.style.top = _FinalPos + "px";


	        ZH = _height - (2 * LargeurScroll) - parseInt(_pointer.style.height);
	        YCC = -_FinalPos;
	        Delta = _FinalPos / (parseInt(ascenseur.style.height) - DivContenu.offsetHeight)
	        _currentValue = Delta * (_maximumVal - _minimumVal) + LargeurScroll - parseInt(_pointer.style.height)

	        _currentValue = parseInt(-_FinalPos * (parseInt(ascenseur.style.height) ) / DivContenu.offsetHeight) //+ LargeurScroll

	        //_currentValue=8

	        debugscroll("cur=" + _currentValue + "final=" + _FinalPos)
	        UpdatePointerPos();
	       


        }
        else
            setTimeout(slide, 40);
    }

    
	function OnDragBegin(eventObj, element) {
    
  
	    var pos = getMousePos(eventObj);
	    var target = getEventTarget(eventObj);
	    if (target == _pointer)
	        pos.Y += parseInt(_pointer.style.top) - parseInt(_pointer.style.height) / 2;
	    else if (target == ascenseur) {
            debugscroll(pos.Y)
        }
        else
        return 
	    //else if(target == _bottomCap)
	     // pos.Y += _height-1;
	    pos.Y -= 4;
	    pos = pos.Bound(_pointerLB, _pointerUB);
	    pos.Apply(_pointer);
	    OnDrag(pos);
	}
  
	function OnDrag(newPos, element) {
  
	  	var ZH=0;
	    newPos.Y += 4;
	    // zone de deplacement = Hauteur-2xbouton-curseur;
	    ZH = _height - (2 * LargeurScroll) - parseInt(_pointer.style.height);

	    YCC=newPos.Y-LargeurScroll-4;
	    _currentValue = Math.round(1000 * ((((YCC) / (ZH)) * (_maximumVal - _minimumVal)) + _minimumVal)) / 1000;
	    
	    if(_callback != null)
			callback(_currentValue,_container);
	}
  
	function UpdatePointerPos() {
    
   debugscroll("cur="+_currentValue+" max="+_maximumVal)
	    if(_currentValue < _minimumVal)
	        _currentValue = _minimumVal;
	    if ((_currentValue + parseInt(_pointer.style.height)) > parseInt(ascenseur.style.height)) {
            debugscroll(">MAX")
            _currentValue = parseInt(ascenseur.style.height) - parseInt(_pointer.style.height);
	    }

	  if (_maximumVal != _minimumVal) {
	      _pointer.style.top = (((_currentValue - _minimumVal) / (_maximumVal - _minimumVal)) * _height + LargeurScroll) + 'px';
	      _pointer.style.top = (_currentValue + LargeurScroll) + "px";
	  }
	  else
	      _pointer.style.top = LargeurScroll+'px';
	}
  
	this.GetMaxValue = function()
	{ return _maximumVal; }
  
	this.GetMinValue = function()
	{ return _minimumVal; }
  
	this.GetCurrentValue = function()
	{ return _currentValue; }
  
	this.GetWidth = function()
	{ return _width; }
   
	this.SetMaxValue = function(value)
	{
		value = parseFloat(value);
		if(isNaN(value))
			value = 1;
		_maximumVal = value;
		UpdatePointerPos(); 
	}
  
	this.SetMinValue = function(value)
	{
		value = parseFloat(value);
		if(isNaN(value))
			value = 0;
		_minimumVal = value;
		UpdatePointerPos(); 
	}
  
	this.SetCurrentValue = function(value) {
	    
		value = parseFloat(value);
		if(isNaN(value))
		value = 0;
		_currentValue = value;
		UpdatePointerPos();
	}
  
	this.GetContainer = function()
	{ return _container; }
  
	this.SetCallback = function(newCallback)
	{ _callback = newCallback; }
  
	this.StartListening = function()
	{ _pointerDrag.StartListening(); }
  
	this.StopListening = function()
	{ _pointerDrag.StopListening(); }
}

function trackBarChange(val,elt)
{
	elt.style.top=-val+"px"
}

function getMousePos(eventObj)
{
  eventObj = eventObj ? eventObj : window.event;
  var pos;
  if(isNaN(eventObj.layerX))
    return new Position(eventObj.offsetX, eventObj.offsetY);
  else
    return new Position(eventObj.layerX, eventObj.layerY);
}

/**************************************************************************************************************************/
/**************************************************************************************************************************/

var lbord=10;
var hbottom=7;



function docadre(DivContenant,mode) {

  	var left=parseInt(DivContenant.style.left);
	var top=parseInt(DivContenant.style.top);
	var width=parseInt(DivContenant.style.width);
	var height=parseInt(DivContenant.style.height);
	var html=DivContenant.innerHTML
		
	//contenu
	var DivContenu = DivContenant.getElementsByTagName("div")[0]
	DivContenu.id="scrollcontent"
	DivContenu.style.position = "relative";
	DivContenu.style.left=0+"px";
	DivContenu.style.top=0+"px";
	DivContenu.style.width = DivContenant.offsetWidth + "px";

    //supprimer le trackbar avant de le creer si il existe
	e =DivContenant //document.getElementById("scroller" + DivContenant.id)
	if (e) {
	    //while (e.hasChildNodes()) {
	        //e.removeChild(e.firstChild);  

        //}
	    //e.parentNode.removeChild(e);
	}

	if ((DivContenu.offsetHeight>DivContenant.offsetHeight)) {

	    debugscroll("Besoin de scroll")
	    DivContenu.style.width=DivContenant.offsetWidth-24+"px";
	    trackbar = new Trackbar(DivContenant,DivContenu,trackBarChange);
	    trackbar.StartListening();
	}
    else
        debugscroll("Pas besoin de scroll")
}


function ScrollerInit()
{
    unhookEvent(window, "resize", ScrollerInit);
	var divs=document.getElementsByTagName("div")
	for(i=0;i<divs.length;i++)
		if((divs[i].className=="boxmain") || (divs[i].className=="boxTmain") || (divs[i].className=="boxvide")) {
        
		    if (divs[i].lang!="noscroll") docadre(divs[i])
			divs[i].title="";
		}


hookEvent(window, "resize", ScrollerInit);
}

// Add initscroll event that has wide browser support

if (1 == 2) {

    if (typeof window.addEventListener != "undefined")
        window.addEventListener("load", ScrollerInit, false);
    else if (typeof window.attachEvent != "undefined")
        window.attachEvent("onload", ScrollerInit);
    else {
        if (window.onload != null) {
            var oldOnload = window.onload;
            window.onload = function (e) { oldOnload(e); ScrollerInit(); }
        }
        else
            window.onload = msinit;
    }

}

function debugscroll(TXT) {
    return
    if (document.getElementById("debug"))
        document.getElementById("debug").innerHTML = TXT + "<br>" + document.getElementById("debug").innerHTML
}
