//Slideball inherits from mover.
//Compatibility IE:

//miscchien moet ik gebruik maken van mouseover ,mouseout, mouseup, mousedown.

//oftwel: als mouse
//onmousewheel. onmouseup. onmousedown.


//onmousedown: event.preventdefault();
//daarna: document.onmousemove = function(); Document houdt altijd mouse position bij.
//document.onmouseup(): zelfde als dragstop als dus ergens de drag gestart is.

function Slideball() {

}

Slideball.inherits(Mover);

Slideball.method('lastClass', function() {
    alert('slideball!!');
});

Slideball.method('init2', function() {
	/*********************************\
		*
		*	Create and initialise scrollobject as SlideballText 
		*	(to create scrollwheel functionality.
		*
		*
		*
	\*********************************/

    var bScroll; var p;
	var initScrollObject = {
		style: {
			n: 0
		},
		property: ['height'],
		value: [ 500],
		n: 1
	};

    this.scrollObject = document.getElementById(this.scrollID);
    
	p = new SlideballText();
	p.init(this.scrollObject,initScrollObject);
	p.scrollBall = this;

	if (p.height == p.o.scrollHeight || p.height > p.o.scrollHeight) {
        bScroll = false;
        this.o.style.display = 'none';
    }
    else {
        bScroll = true;
    }

    if(bScroll) {
        this.scrollObject.correct = this.scrollObject.scrollHeight - 500 - 0;//-height - padding
        if (IEXPLORE) {
            this.scrollObject.correct = this.scrollObject.scrollHeight - 500;
        }
        this.o.ondrag = function(e) {
        	if (!e) {
                var e = window.event;
            }
            e.returnValue = false;
        }
        this.o.onmousedown = function(e) {
        	if (!e) {
                var e = window.event;
            }
            if(e.preventDefault) { e.preventDefault(); }
            mouse.addDrag(this);
            this.p.y = e.clientY;
            this.p.y0 = this.p.y;
            this.p.top0  = this.p.style.top;
            //add to timer
            this.p.startMove();
        }
        this.o.mouseup = function() {
            var v;
            this.p.y = mouse.y;
            
            this.p.endMove();
            
            v = this.p.getCoords(this.p.y);
            this.style.left = v[0];
            this.style.top  = v[1];
            this.p.style.top = v[1];
            this.p.scrollObject.scrollTop = v[2] * this.p.scrollObject.correct;
        }
    }
});

Slideball.method('move2', function() {
        this.y = mouse.y;
        v = this.getCoords(this.y);
        this.o.style.left = v[0];
        this.o.style.top  = v[1];
        this.scrollObject.scrollTop = v[2] * this.scrollObject.correct;
});

Slideball.method('startMove2', function () {
    this.temp = 1;
});

Slideball.method('getCoords', function(y) {
    var x, alpha,R, centreX, centreY;
    var min,max;
    R = 280;
    centreX = 530; 
    centreY = 266; //270
    min = 16; max = 516;
    
    y = this.style.top - this.y0 + y;
    if (y <= min) {y = min }
    else if (y >= max) {y = max}
    
    //of alpha = Math.asin ((y - centreY) / R);
    //x = Math.cos(alpha) * R + centreX;
    x = Math.sqrt(1 - Math.pow((y - centreY) / R, 2)) * R + centreX;
    
    //calculation of scrollTop...
    scrollTop = (y - min) / (max - min);
    return [x,y,scrollTop];
    
});

Slideball.method('getCoords2', function(scrollFract) {
    var x, alpha,R, centreX, centreY;
    var min,max;
    R = this.R;
    centreX = 530; centreY = 266;
    min = 16; max = 516;

	y = min + ((max - min) * scrollFract);
    x = Math.sqrt(1 - Math.pow((y - centreY) / R, 2)) * R + centreX;
	return [x,y];
});


function SlideballText() {

}
SlideballText.inherits(Mover);



SlideballText.method('init2', function() {
    mouse.addScroll(this);
});

SlideballText.method('onScroll', function(e) {
    if ((Math.pow(mouse.x-530,2)+ Math.pow(mouse.y-310,2)) < 62500) {
        var n, bMove;
        n = e.wheelDelta;
        //info.innerHTML = n;
        bMove = this.setTarget(n);
		if (bMove) {
			this.startMove();			
		}
    }
});

SlideballText.method('setTarget', function(n) {
//	alert(this.moving);
	var bMove, max;
	max = this.o.scrollHeight - this.height;
	
	bMove = true;
	
	if (!this.moving) {
		this.target = this.o.scrollTop - n;
	}
	else {
		this.target = this.target - n;
	}
	
	if (this.target > max) {
		this.target = max;
	}
	if (this.target < 0) {
		this.target = 0;
	}

	if (this.o.scrollTop == this.target) {
		bMove = false;
	}
	return bMove;
});

SlideballText.method('move2', function() {
	var delta, step, ballY, ballCoords, newScrollTop;
	step = 8;
	delta = this.target - this.o.scrollTop;

	//if target is closter than maxDelta, move to target and quit.:
	if (Math.abs(delta) < step) {
		newScrollTop = this.target;
		this.endMove();
	}
	else {
		if (delta < 0) {
			step = -step;
		}
		newScrollTop = this.o.scrollTop + step;
	}
	
	//move scrollBall accordingly:
	scrollFract = 1 - (this.o.scrollHeight - this.height - newScrollTop) / (this.o.scrollHeight - this.height);
	ballCoords = this.scrollBall.getCoords2(scrollFract);
	this.o.scrollTop = newScrollTop;
	this.scrollBall.o.style.left = ballCoords[0];
	this.scrollBall.o.style.top = ballCoords[1];
	this.scrollBall.style.top = ballCoords[1];
});
