﻿
var winheight = 360;
var arrowHeight = 15;
var sliderHeight = 12;
var ie = document.all;
var nn6 = document.getElementById && !document.all;
var isdrag = false;
var x,y;
var dobj;
var margin = 0;
var scrollTop = 0;

function initScrollableText(id) {
    if (id == null || id == "") {
        divid = "text";
        barid = "scrollbar";
    } else {
        divid = id + "_text";
        barid = id + "_scrollbar";
    }
	var div = document.getElementById(divid);
	var img = document.getElementById(barid);
    try {
	    if (window.addEventListener) div.addEventListener("DOMMouseScroll", wheel, false);
	    div.onmousewheel = div.onmousewheel = wheel;

	    img.onmousedown = selectmouse;
	    document.onmouseup = new Function("isdrag=false");
    } catch(e) {
        alert("scroll: " + e.description);
    }
    //winheight = div.clientHeight;
    resizeDiv();
    window.onresize = resizeDiv;
 }

function resizeDiv() {
	var div = document.getElementById("content_text");
	var nav = document.getElementById("content_textnav");
	var extraHeight = div.ownerDocument.body.parentNode.clientHeight - 590 - 30;

	if ((winheight + extraHeight) > div.scrollHeight) extraHeight = div.scrollHeight - winheight;
	if (extraHeight > 0) {
	    div.style.height = winheight + extraHeight + "px";
	    div.parentNode.style.height = winheight + extraHeight + "px";
	    nav.style.height = winheight + extraHeight + "px";
	    nav.parentNode.style.height = winheight + extraHeight + "px";
	    //winheight = div.clientHeight;
	}
}

function scroll(direction, id) {
    if (id == null || id == "") {
        divid = "text";
        barid = "scrollbar";
    } else if (id.indexOf("_") == -1) {
        divid = id + "_text";
        barid = id + "_scrollbar";
    } else {
        if (id.indexOf("_text") == -1) {
            divid = id.replace("scrollbar","text");
            barid = id;
        } else {
            divid = id;
            barid = id.replace("text","scrollbar");
        }
    }
    var div = document.getElementById(divid);
    if (!div) div = document.getElementById("content_text");
    var offset;
    var increment = 56;
    var max = div.clientHeight - (2 * arrowHeight) - sliderHeight - (2 * margin);

    if (div) {
        if (div.scrollHeight - div.clientHeight == 0) {
            offset = arrowHeight + margin;
        } else {
            offset = (max * (scrollTop / (div.scrollHeight - div.clientHeight)) + arrowHeight); // below top arrow
        }

        if (arguments.length == 3) {
            if (arguments[2] == 0) {
                //wheel
                if (direction == "up") increment *= -1;
                scrollTop += increment;
                if (scrollTop > div.scrollHeight - div.clientHeight) scrollTop = div.scrollHeight - div.clientHeight;
                if (scrollTop < arrowHeight) scrollTop = arrowHeight;
                div.scrollTop = scrollTop;
                document.getElementById(barid).style.top = offset + "px";
            } else {
                //drag
                offset = (arguments[2] - arrowHeight) / max;
                div.scrollTop = (div.scrollHeight - div.clientHeight) * offset;
            }
        } else {
            //wheel
            if (direction == "up") increment *= -1;
            scrollTop += increment;
            if (scrollTop > div.scrollHeight - div.clientHeight) scrollTop = div.scrollHeight - div.clientHeight;
            if (scrollTop < 0 ) scrollTop = 0;
            div.scrollTop = scrollTop;
            try {
                document.getElementById(barid).style.top = offset + "px";
            } catch (ex) {
            }
        }
        //document.getElementById("debug").value = "max: " + max + " offset: " + Math.ceil(offset) + " height: " + div.scrollHeight + " top: " + div.scrollTop + " : " + arguments[2]; //ty + e.clientY - y;
    }
}

function isScrollable(id) {
    if (id == null || id == "") {
        divid = "text";
        navid = "textnav";
    } else {
        divid = id + "_text";
        navid = id + "_textnav";
    }
    var div = document.getElementById(divid);
    document.getElementById(navid).style.display = ((div.scrollHeight - arrowHeight) > div.clientHeight) ? "" : "none";
}

//http://adomas.org/javascript-mouse-wheel/
function handle(delta, target) {
    var direction = (delta < 0) ? "down" : "up";
	scroll(direction, target, delta);
}

function wheel(e){
    var delta = 0;
    var targ;

	if (!e) e = window.event;
	if (e.target) {
	    targ = e.target;
    } else if (e.srcElement) {
        targ = e.srcElement;
    }
	if (targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug
    while (!targ.id) {
        targ = targ.parentNode;
    }
    if (targ.id.indexOf("_") == -1) targ = targ.parentNode;
    if (e.wheelDelta) {
		delta = e.wheelDelta/120; 
		if (window.opera) delta = -delta;
	} else if (e.detail) {
		delta = -e.detail/3;
	}

	if (delta) {
		scroll((delta < 0) ? "down" : "up", targ.id);
        if (e.preventDefault) e.preventDefault();
        e.returnValue = false;
    }
}

//http://elouai.com/javascript-drag-and-drop.php
function movemouse(e)
{
  if (!e) e = event;
  var pos = nn6 ? ty + e.clientY - y : ty + e.clientY - y
  var div = document.getElementById("content_text");
  if (isdrag) {
    if (pos >= 6) {
        if (pos <= div.clientHeight - (2 * arrowHeight)) { 
            dobj.style.top  = pos + "px";
        } else {
            dobj.style.top  = (div.clientHeight - (2 * arrowHeight)) + "px";
        }
    } else {
        dobj.style.top  = arrowHeight + "px";
    }

	if (e.target) {
	    targ = e.target;
    } else if (e.srcElement) {
        targ = e.srcElement;
    }
	if (targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug

    try {
        while (!targ.id) {
            targ = targ.parentNode;
            if (targ) {
                if (targ.id) {
                    if (targ.id.indexOf("scrollbar") == -1) {
                        var imgs = targ.getElementsByTagName("img");
                        for (i = 0; i < imgs.length; i++) {
                            if (imgs[i].id.indexOf("scrollbar") > -1) {
                                targ = imgs[i];
                                break;
                            }
                        }
                    }
                }
            }
        }
    } catch(err) {
    }
    handle(pos, targ.id);
    //scroll("down", pos);
    return false;
  }
}

function selectmouse(e) 
{
  var fobj = nn6 ? e.target : event.srcElement;
  {
    isdrag = true;
    dobj = fobj;
    tx = parseInt(dobj.style.left+0);
    ty = parseInt(dobj.style.top+0);
    x = nn6 ? e.clientX : event.clientX;
    y = nn6 ? e.clientY : event.clientY;
    document.onmousemove = movemouse;
    return false;
  }
}
