/* ATPL Portal - DIV Scroller
 * Author: Heiko Rettinger
 * Versioncontrol: $Id: hrwsScroller.js 2 2009-03-03 21:01:19Z heiko $
 */

function hrwsScroller() {
	hrwsScroller.ref = this;
	this.refString = "hrwsScroller.ref";
}
 
hrwsScroller.prototype.enable = function(FrameId, ContentId) {
    var frame = document.getElementById(FrameId);
    this.id = FrameId;
    this.load(ContentId);
    if (frame.addEventListener) {
        frame.addEventListener('DOMMouseScroll', hrwsScroller.doOnMouseWheel, false);
    } 
    frame.onmousewheel = hrwsScroller.doOnMouseWheel;
}

hrwsScroller.defSpeed = hrwsScroller.prototype.speed = 100;
hrwsScroller.defSlideDur = hrwsScroller.prototype.slideDur = 300;

hrwsScroller.prototype.setButtonRelease = function(ButtonId) {
	this.btnId = ButtonId;
}

hrwsScroller.prototype.on_scroll_end = function() {
	var btn;
	if (btn=document.getElementById(this.btnId)) {
		btn.disabled = false;
	}
}

hrwsScroller.prototype.load = function(ContentId) {
    var frame, content;
    this.content = content = document.getElementById(ContentId);
    this.content.style.position = 'absolute'; 
    this.ContentId = ContentId;
    frame = document.getElementById(this.id);
    this.y = 0; this.shiftTo(0);
    this.getDims(frame, content); 
    content.style.visibility = "visible";
}

hrwsScroller.prototype.shiftTo = function(y) {
    if (this.content) {
        this.content.style.top = (this.y = y) + "px";
		if (y <= -this.maxY)
            this.on_scroll_end(this.endY);
    }
}

hrwsScroller.prototype.getY = function() { return this.y; }

hrwsScroller.prototype.getDims = function(frame, content) { 
    this.maxX = (content.offsetWidth - frame.offsetWidth > 0)? content.offsetWidth - frame.offsetWidth: 0;
    this.maxY = (content.offsetHeight - frame.offsetHeight > 0)? content.offsetHeight - frame.offsetHeight: 0;
}

hrwsScroller.prototype.initScrollByVal = function(dy, dur) {
    if ( this.sliding ) return;
    this.startY = this.y;
    this.destY = this.distY = 0;
    if (dy < 0) {
        this.distY = (this.startY + dy >= -this.maxY)? dy: -(this.startY  + this.maxY);
    } else if (dy > 0) {
        this.distY = (this.startY + dy <= 0)? dy: -this.startY;
    }
    this.destY = this.startY + this.distY;
    this.glideScrollPrep(this.destY, dur);
}

hrwsScroller.prototype.initScrollToVal = function(destY, dur) {
    if ( this.sliding ) return;
    this.startY = this.y;
    this.destY = -Math.max( Math.min(destY, this.maxY), 0);
    this.distY = this.destY - this.startY;
    this.glideScrollPrep(this.destY, dur);
}

hrwsScroller.prototype.glideScrollPrep = function(destY, dur) {
    this.slideDur = (typeof dur == 'number')? dur: hrwsScroller.defSlideDur;
    this.per = Math.PI/(2 * this.slideDur); this.sliding = true;
    this.content = document.getElementById(this.ContentId); 
    this.startTime = new Date().getTime();
    this.timerId = setInterval(this.refString + ".doGlideScroll()",10);
}

hrwsScroller.prototype.doGlideScroll = function() {
    var elapsed = new Date().getTime() - this.startTime;
    if (elapsed < this.slideDur) {
        var y = this.startY + Math.round( this.distY * Math.sin(this.per*elapsed) );
        this.shiftTo(y); 
    } else {	// if time's up
        clearInterval(this.timerId); this.timerId = 0; this.sliding = false;
        this.shiftTo(this.destY);
    }
}

hrwsScroller.handleMouseWheel = function(delta) {
    var refObj = hrwsScroller.ref;
    var y = refObj.y;
    var ny;
    ny = 20  * delta + y
    ny = (ny < 0 && ny >= -refObj.maxY)? ny: (ny < -refObj.maxY)? -refObj.maxY: 0;
    refObj.shiftTo(ny);
}

hrwsScroller.doOnMouseWheel = function(e) {
    var delta = 0;
    if (!e) e = window.event;
    if (e.wheelDelta) { /* IE/Opera. */
        delta = e.wheelDelta/120;
        if (window.opera) delta = -delta;
    } else if (e.detail) { // Mozilla 
        delta = -e.detail/3;
    }
    if (delta) { // > 0 up, < 0 down
        hrwsScroller.handleMouseWheel(delta);
    }
    if (e.preventDefault) e.preventDefault();
    e.returnValue = false;
}

var scroller = new hrwsScroller();
