var A_Scrollbar = []; function TextScroll(scrollname, div_name, scroll_t_name, up_name, down_name, curs_name, vertical) { // JS variable name this.name = scrollname; // Div to scroll this.div_name = div_name; this.div_obj = document.getElementById(this.div_name); // Elem for scrollbare this.up_name = up_name; // Arrow up this.up_obj = document.getElementById(this.up_name); this.dn_name = down_name; // Arrow down this.dn_obj = document.getElementById(this.dn_name); this.curs_name = curs_name; // Barre cursor this.curs_obj = document.getElementById(this.curs_name); this.scroll_t_name = scroll_t_name; // Table (container) for scroll elements this.scroll_t_obj = document.getElementById(this.scroll_t_name); // If scroll is vertical this.vertical = vertical; if (vertical === undefined) this.vertical = true; this.scrollCursor = 0; this.speed = 5; this.speed_wheel = 20; this.timeoutID = null; this.div_size = 0; this.div_maxsize = 0; this.curs_size = 0; this.curs_maxsize = 0; this.ratio = 0; this.barepos = 0; this.mousestate = false; this.mousepos = null; if (this.vertical) { this.div_maxsize = this.div_obj.offsetHeight; this.div_size = this.div_obj.scrollHeight; this.curs_maxsize = this.curs_obj.offsetHeight; } else { this.div_maxsize = this.div_obj.offsetWidth; this.div_size = this.div_obj.scrollWidth; this.curs_maxsize = this.curs_obj.offsetWidth; } //alert(this.div_maxsize + " / " + this.div_size); A_Scrollbar.push(new Array(this.name, this)); if (this.div_maxsize < this.div_size) { // Initialyse div to scroll this.div_obj.style.overflow = 'hidden'; if (this.div_obj.addEventListener) { this.div_obj.addEventListener("DOMMouseScroll", function(event) { eval("GetScrollElement('" + scrollname + "').WheelEvent(event)"); }, false); } this.div_obj.onmousewheel = function() { eval("GetScrollElement('" + scrollname + "').WheelEvent()"); }; // Initialyse arrows if (this.up_obj && this.dn_obj) { this.up_obj.onmouseover = function() { eval("GetScrollElement('" + scrollname + "').scrollUp()"); }; this.up_obj.onmouseout = function() { eval("GetScrollElement('" + scrollname + "').stopScroll()"); }; this.up_obj.onmousedown = function() { eval("GetScrollElement('" + scrollname + "').FastscrollUp()"); }; this.up_obj.onmouseup = function() { eval("GetScrollElement('" + scrollname + "').stopScroll()"); }; this.dn_obj.onmouseover = function() { eval("GetScrollElement('" + scrollname + "').scrollDown()"); }; this.dn_obj.onmouseout = function() { eval("GetScrollElement('" + scrollname + "').stopScroll()"); }; this.dn_obj.onmousedown = function() { eval("GetScrollElement('" + scrollname + "').FastscrollDown()"); }; this.dn_obj.onmouseup = function() { eval("GetScrollElement('" + scrollname + "').stopScroll()"); }; } // Initialyse scrollbar if (this.curs_obj) { this.curs_obj.onmousedown = function(ev) { eval("GetScrollElement('" + scrollname + "').StartDrag(ev)"); }; if (this.vertical) { this.curs_obj.style.height = Math.round(this.curs_maxsize * this.div_maxsize / this.div_size) + "px"; } else { this.curs_obj.style.width = Math.round(this.curs_maxsize * this.div_maxsize / this.div_size) + "px"; } this.ratio = (this.curs_maxsize / this.div_size); //alert(this.div_maxsize + " / " + this.div_size + " / " + this.curs_maxsize + " / " + this.curs_obj.style.height); } } else { this.scroll_t_obj.style.display = "none"; //this.div_obj.style.height = this.div_maxsize + "px"; } } TextScroll.prototype.mouseCoords = function (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 }; } TextScroll.prototype.getMouseOffset = function (target, ev) { ev = ev || window.event; var docPos = this.getPosition(target); var mousePos = this.mouseCoords(ev); return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y}; } TextScroll.prototype.getPosition = function (e) { var left = 0; var top = 0; while (e.offsetParent){ left += e.offsetLeft; top += e.offsetTop; e = e.offsetParent; } left += e.offsetLeft; top += e.offsetTop; return {x:left, y:top}; } TextScroll.prototype.Reload = function() { var name = this.name; this.scroll_t_obj.style.display = "inline"; if (this.vertical) { this.div_maxsize = this.div_obj.offsetHeight; this.div_size = this.div_obj.scrollHeight; if (this.div_maxsize == 0) return; if (this.div_size == 0) return; this.curs_obj.style.height = "100%"; this.curs_maxsize = this.curs_obj.offsetHeight; this.curs_obj.style.height = Math.round(this.curs_maxsize * this.div_maxsize / this.div_size) + "px"; this.ratio = (this.curs_maxsize / this.div_size); this.scrollCursor = this.div_obj.scrollTop; this.barepos = this.scrollCursor * this.ratio; } else { this.div_maxsize = this.div_obj.offsetWidth; this.div_size = this.div_obj.scrollWidth; if (this.div_maxsize == 0) return; if (this.div_size == 0) return; this.curs_obj.style.width = "100%"; this.curs_maxsize = this.curs_obj.offsetWidth; this.curs_obj.style.width = Math.round(this.curs_maxsize * this.div_maxsize / this.div_size) + "px"; this.ratio = (this.curs_maxsize / this.div_size); this.scrollCursor = this.div_obj.scrollLeft; this.barepos = this.scrollCursor * this.ratio; } if (this.div_maxsize < this.div_size) { // Initialyse div to scroll this.div_obj.style.overflow = 'hidden'; if (window.addEventListener) { this.div_obj.addEventListener("DOMMouseScroll", function(event) { eval("GetScrollElement('" + name + "').WheelEvent(event)"); }, false); } else { this.div_obj.onmousewheel = function() { eval("GetScrollElement('" + name + "').WheelEvent()"); }; } // Initialyse arrows if (this.up_obj && this.dn_obj) { this.up_obj.onmouseover = function() { eval("GetScrollElement('" + name + "').scrollUp()"); }; this.up_obj.onmouseout = function() { eval("GetScrollElement('" + name + "').stopScroll()"); }; this.up_obj.onmousedown = function() { eval("GetScrollElement('" + name + "').FastscrollUp()"); }; this.up_obj.onmouseup = function() { eval("GetScrollElement('" + name + "').stopScroll()"); }; this.dn_obj.onmouseover = function() { eval("GetScrollElement('" + name + "').scrollDown()"); }; this.dn_obj.onmouseout = function() { eval("GetScrollElement('" + name + "').stopScroll()"); }; this.dn_obj.onmousedown = function() { eval("GetScrollElement('" + name + "').FastscrollDown()"); }; this.dn_obj.onmouseup = function() { eval("GetScrollElement('" + name + "').stopScroll()"); }; } // Initialyse scrollbar if (this.curs_obj) { this.curs_obj.onmousedown = function(ev) { eval("GetScrollElement('" + name + "').StartDrag(ev)"); }; } // Initialyse bare if (this.curs_obj) { //alert(Math.round(this.curs_maxsize * this.div_maxsize / this.div_size) + "px"); if (this.vertical) { this.curs_obj.style.height = Math.round(this.curs_maxsize * this.div_maxsize / this.div_size) + "px"; } else { this.curs_obj.style.width = Math.round(this.curs_maxsize * this.div_maxsize / this.div_size) + "px"; } this.ratio = (this.curs_maxsize / this.div_size); } } else { var scroll_t = document.getElementById(this.scroll_t_name); scroll_t.style.display = "none"; } if (this.vertical) { this.div_obj.scrollTop = this.scrollCursor; this.curs_obj.style.top = this.barepos + "px"; } else { this.div_obj.scrollLeft = this.scrollCursor; this.curs_obj.style.left = this.barepos + "px"; } } TextScroll.prototype.StartDrag = function(event) { var name = this.name; if (!event) event = window.event; this.mousestate = true; this.mousepos = this.getMouseOffset(this.curs_obj, event); document.onmouseup = function(ev) { eval("GetScrollElement('" + name + "').StopDrag(ev)"); }; document.onmousemove = function(ev) { eval("GetScrollElement('" + name + "').MoveDrag(ev)"); }; //this.curs_obj.onmouseup = function(ev) { //eval(scrollname + ".StopDrag(ev);") //}; //this.curs_obj.onmousemove = function(ev) { //eval(scrollname + ".MoveDrag(ev);") //}; //alert("Start Drag: " + this.mousepos.x + "/" + this.mousepos.y); } TextScroll.prototype.StopDrag = function(event) { if (!event) event = window.event; this.mousestate = false; this.mousepos = this.getMouseOffset(this.curs_obj, event); document.onmouseup = null; document.onmousemove = null; //alert("Stop Drag: " + this.mousepos.x + "/" + this.mousepos.y); } TextScroll.prototype.MoveDrag = function(event) { if (this.mousestate) { if (!event) event = window.event; tmpmousepos = this.getMouseOffset(this.curs_obj, event); if (this.vertical) { this.scrollCursor += (tmpmousepos.y - this.mousepos.y) / this.ratio; this.div_obj.scrollTop = this.scrollCursor; if (this.div_obj.scrollTop != this.scrollCursor) { this.scrollCursor = this.div_obj.scrollTop; } this.barepos = this.scrollCursor * this.ratio; this.curs_obj.style.top = this.barepos + "px"; } else { this.scrollCursor += (tmpmousepos.x - this.mousepos.x) / this.ratio; this.div_obj.scrollLeft = this.scrollCursor; if (this.div_obj.scrollLeft != this.scrollCursor) { this.scrollCursor = this.div_obj.scrollLeft; } this.barepos = this.scrollCursor * this.ratio; this.curs_obj.style.left = this.barepos + "px"; } } } TextScroll.prototype.WheelEvent = function(event) { var delta = 0; if (!event) event = window.event; if (event.wheelDelta) { delta = event.wheelDelta/120; // if (window.opera) delta = -delta; if (is_ie){ delta=delta; } else { delta=-delta; } // if (safari) delta= delta; // if (ua.indexOf("opera") != -1) delta = -delta; } else if (event.detail) { delta = event.detail/3; } //alert(delta); if (is_ie){ this.scrollCursor -= delta * this.speed_wheel; } else { this.scrollCursor += delta * this.speed_wheel; } if ((this.scrollCursor - this.speed) <= 0) { this.scrollCursor = 0; this.barepos = 0; } if (this.vertical) { this.div_obj.scrollTop = this.scrollCursor; if (this.div_obj.scrollTop != this.scrollCursor) { this.scrollCursor = this.div_obj.scrollTop; } this.barepos = this.scrollCursor * this.ratio; this.curs_obj.style.top = this.barepos + "px"; } else { this.div_obj.scrollLeft = this.scrollCursor; if (this.div_obj.scrollLeft != this.scrollCursor) { this.scrollCursor = this.div_obj.scrollLeft; } this.barepos = this.scrollCursor * this.ratio; this.curs_obj.style.left = this.barepos + "px"; } } TextScroll.prototype.stopScroll = function() { clearTimeout(this.timeoutID); this.timeoutID = null; } TextScroll.prototype.scrollUp = function() { if (this.div_obj) { if ((this.scrollCursor - this.speed) <= 0) { this.stopScroll(); this.scrollCursor = 0; this.barepos = 0; } else { this.timeoutID = setTimeout("GetScrollElement('" + this.name + "').scrollUp()", 60); this.scrollCursor = this.scrollCursor - this.speed; this.barepos = this.scrollCursor * this.ratio; } if (this.vertical) { this.div_obj.scrollTop = this.scrollCursor; this.curs_obj.style.top = this.barepos + "px"; } else { this.div_obj.scrollLeft = this.scrollCursor; this.curs_obj.style.left = this.barepos + "px"; } } } TextScroll.prototype.FastscrollUp = function() { if (this.div_obj) { if ((this.scrollCursor - this.speed) < 0) { this.stopScroll(); this.scrollCursor = 0; this.barepos = 0; } else { this.timeoutID = setTimeout("GetScrollElement('" + this.name + "').FastscrollUp()", 60); this.scrollCursor = this.scrollCursor - (2 * this.speed); this.barepos = this.scrollCursor * this.ratio; } if (this.vertical) { this.div_obj.scrollTop = this.scrollCursor; this.curs_obj.style.top = this.barepos + "px"; } else { this.div_obj.scrollLeft = this.scrollCursor; this.curs_obj.style.left = this.barepos + "px"; } } } TextScroll.prototype.scrollDown = function() { if (this.div_obj) { this.scrollCursor += this.speed; if (this.vertical) { this.div_obj.scrollTop = this.scrollCursor; if (this.div_obj.scrollTop == this.scrollCursor) { this.barepos = this.scrollCursor * this.ratio; this.curs_obj.style.top = this.barepos + "px"; this.timeoutID = setTimeout("GetScrollElement('" + this.name + "').scrollDown()", 60); } else { this.scrollCursor = this.div_obj.scrollTop; this.barepos = this.scrollCursor * this.ratio; this.curs_obj.style.top = this.barepos + "px"; } } else { this.div_obj.scrollLeft = this.scrollCursor; if (this.div_obj.scrollLeft == this.scrollCursor) { this.barepos = this.scrollCursor * this.ratio; this.curs_obj.style.left = this.barepos + "px"; this.timeoutID = setTimeout("GetScrollElement('" + this.name + "').scrollDown()", 60); } else { this.scrollCursor = this.div_obj.scrollLeft; this.barepos = this.scrollCursor * this.ratio; this.curs_obj.style.left = this.barepos + "px"; } } } } TextScroll.prototype.FastscrollDown = function() { if (this.div_obj) { this.scrollCursor += 2 * this.speed; if (this.vertical) { this.div_obj.scrollTop = this.scrollCursor; if (this.div_obj.scrollTop == this.scrollCursor) { this.barepos = this.scrollCursor * this.ratio; this.curs_obj.style.top = this.barepos + "px"; this.timeoutID = setTimeout("GetScrollElement('" + this.name + "').FastscrollDown()", 60); } else { this.scrollCursor = this.div_obj.scrollTop; this.barepos = this.scrollCursor * this.ratio; this.curs_obj.style.top = this.barepos + "px"; } } else { this.div_obj.scrollLeft = this.scrollCursor; if (this.div_obj.scrollLeft == this.scrollCursor) { this.barepos = this.scrollCursor * this.ratio; this.curs_obj.style.left = this.barepos + "px"; this.timeoutID = setTimeout("GetScrollElement('" + this.name + "').FastscrollDown()", 60); } else { this.scrollCursor = this.div_obj.scrollLeft; this.barepos = this.scrollCursor * this.ratio; this.curs_obj.style.left = this.barepos + "px"; } } } } TextScroll.prototype.resetScroll = function() { if (this.div_obj) { if (this.vertical) { this.div_obj.scrollTop = 0; } else { this.div_obj.scrollLeft = 0; } this.scrollCursor = 0; } } TextScroll.prototype.Remove = function () { for (var i = 0; i < A_Scrollbar.length; i++) { if (A_Scrollbar[i][0] == this.name) { A_Scrollbar.splice(i, 1); break; } } } TextScroll.prototype.SetPos = function (elem) { tmpdivpos = this.getPosition(this.div_obj); tmpelempos = this.getPosition(document.getElementById(elem)); this.div_obj.scrollTop = (tmpelempos.y - tmpdivpos.y - (this.div_maxsize / 2)); this.Reload(); } function ReloadAllScrollbar() { for (var i = 0; i < A_Scrollbar.length; i++) { if (A_Scrollbar[i][1]) { A_Scrollbar[i][1].Reload(); } } } function GetScrollElement(name) { for (var i = 0; i < A_Scrollbar.length; i++) { if (A_Scrollbar[i][0] == name) { return A_Scrollbar[i][1]; } } }