var mouseOffset = null;
var isMouseDown = false;
var isSliding = false;
var scroll_id = null;
var timer = null;
var interval = null;
var factor = 1;
var max_slide = 0;

Number.prototype.NaN0=function(){return isNaN(this)?0:this;}

function getPosition(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
		top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
		e     = e.offsetParent;
	}

	left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
	top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);

	return {x:left, y:top};
}

function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function getMouseOffset(target, ev){
	ev = ev || window.event;

	var docPos    = getPosition(target);
	var mousePos  = mouseCoords(ev);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}


function getSpacer(id){
  var obj = getTrackbar(id);
  for(var i=0; i<obj.childNodes.length; i++){
	if(obj.childNodes[i].nodeName=='#text') continue;
    if(obj.childNodes[i].getAttribute('element')=='spacer' ){
      return obj.childNodes[i];
    }
  }
}
function getSlider(id){
  var obj = getTrackbar(id);
  for(var i=0; i<obj.childNodes.length; i++){
	if(obj.childNodes[i].nodeName=='#text') continue;
    if(obj.childNodes[i].getAttribute('element')=='slider' ){
      return obj.childNodes[i];
    }
  }
}

function getTrackbar(id){
  var obj = document.getElementById(id);

  for(var i=0; i<obj.childNodes.length; i++){
	if(obj.childNodes[i].nodeName=='#text') continue;
    if(obj.childNodes[i].getAttribute('element')=='trackbar' ){
      return obj.childNodes[i];
    }
  }
}

function mouseDown(ev){
  ev         = ev || window.event;
  var target = ev.target || ev.srcElement;

  if( target.getAttribute('scrollObj') ){
    scroll_id = target.getAttribute('scrollObj');

    if( target.parentNode.getAttribute('element')=='slider' ){
      isSliding = true;
      mouseOffset = getMouseOffset(target.parentNode, ev);

      max_slide = getTrackbar(scroll_id).offsetHeight-target.parentNode.offsetHeight;
      if( max_slide > 0 ){
        factor = ((document.getElementById(scroll_id).getAttribute('max')-document.getElementById(scroll_id).getAttribute('min'))/max_slide);
      }else{
        factor = 0;
      }
    }

    if( target.getAttribute('element')=='button' ){
      var direction = parseInt(target.getAttribute('direction'));
      _scroll(scroll_id,direction);
      timer = setTimeout("_slide('"+scroll_id+"',"+direction+")", 500);
    }

    isMouseDown = true;
    return false;
  }
}
function mouseMove(ev){
  ev         = ev || window.event;
  var target = ev.target || ev.srcElement;

  if( isMouseDown && isSliding ){
    var offset = getMouseOffset(getTrackbar(scroll_id), ev);
    var top = offset.y-mouseOffset.y;
    if( top<0 ) top=0;
    if( top>max_slide) top= max_slide;

    getSlider(scroll_id).style.top = top+'px';
    var position = Math.round(factor*top);
    document.getElementById(scroll_id).setAttribute('position', position);

    var attr = document.getElementById(scroll_id).getAttribute('onScroll');
    typeof attr == 'function' ? attr(document.getElementById(scroll_id).getAttribute('position')) : eval(attr);
  }
  resetSpeed(ev); // nur wegen projekt scroll
  return false;
}
function mouseUp(ev){
  ev         = ev || window.event;
  var target = ev.target || ev.srcElement;

  clearInterval(interval);
  clearTimeout(timer);
  isMouseDown = false;
  isSliding = false;
  scroll_id = null;
  return false;
}

function setPosition(id, pos){
  if( pos>=document.getElementById(id).getAttribute('min') && pos<=document.getElementById(id).getAttribute('max') ){
    document.getElementById(id).setAttribute('position', pos);

    max_slide = getTrackbar(id).offsetHeight-getSlider(id).offsetHeight;
    factor = ((document.getElementById(id).getAttribute('max')-document.getElementById(id).getAttribute('min'))/max_slide);
    getSlider(id).style.top = parseInt(pos/factor)+'px';
  }
}

function _slide(id, direction){
  if( isMouseDown ){
    interval = setInterval( "_scroll('"+scroll_id+"',"+direction+")",10)
  }
}

function _scroll(id, direction){
  var position = parseInt(document.getElementById(id).getAttribute('position'))+parseInt(direction);

  if( position>parseInt(document.getElementById(id).getAttribute('max')) ) position = parseInt(document.getElementById(id).getAttribute('max'));
  if( position<parseInt(document.getElementById(id).getAttribute('min')) ) position = parseInt(document.getElementById(id).getAttribute('min'));

  document.getElementById(id).setAttribute('position', position);
  setPosition(id, position)
  var attr = document.getElementById(id).getAttribute('onScroll');
  typeof attr == 'function' ? attr(document.getElementById(id).getAttribute('position')) : eval(attr);
}

function iniScrollbar(id){
  var min = parseInt(document.getElementById(id).getAttribute('min'));
  var max = parseInt(document.getElementById(id).getAttribute('max'));
  var page= parseInt(document.getElementById(id).getAttribute('page'));
  var obj = getSlider(id);
  var size = parseInt(page*getTrackbar(id).offsetHeight/(max-min+page));
  if( size<1 ) size=1;
  for(var i=0; i<obj.childNodes.length; i++){
    if(obj.childNodes[i].nodeName=='#text') continue;
    if(obj.childNodes[i].getAttribute('element')=='box' ){
      obj.childNodes[i].style.height = size+'px';
    }
  }
  setPosition(id, document.getElementById(id).getAttribute('position'));
}

document.onmousemove = mouseMove;
document.onmousedown = mouseDown;
document.onmouseup   = mouseUp;

window.onload = function(){
  iniScrollbar('scroll_1')
}

function handle(delta) {
  _scroll('scroll_1',-1*delta);
}


function wheel(event){
        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/Math.abs(delta)*16);
        /** 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;
}

//if (window.addEventListener)
//  window.addEventListener('DOMMouseScroll', wheel, false);
//window.onmousewheel = document.onmousewheel = wheel;
