Scroller = {};
Scroller.amount = 1;
Scroller.interval = 30;

Scroller.scrollposition = 0;
Scroller.scrollindex = 0;

//edit these to increase gaps between stories
Scroller.padding = 1;
Scroller.margin = 1;

Scroller.id = "Scroller";
Scroller.childId = "Scroller_"
Scroller.parentId = "Scroller_parent";

//these need to be set up in init
Scroller.scrollindex = 0;
//the point of the last elm
Scroller.lastChild = 1;
//last elm
Scroller.elmArray = [];
//first array (will alwyas be 1..n-1
Scroller.tmpArray = [];
//will always be length [0]
Scroller.totalHeight = 0;
//height of the scrolling elm sum(1..n.offsetheight)


Scroller.start = function(mouse) {
    if (mouse == true && !Scroller.mousehover) {
        return;
    }
    if(Scroller.elmArray.length  < 2){
        return;
    }
    if (!Scroller.running) {
        if (Scroller.expanded) {
            document.getElementById(Scroller.parentId).style.height = Scroller.parentHeight + "px";
            Scroller.expanded = false;
        }
        Scroller.intervalId = setInterval(Scroller.scroll, Scroller.interval);
        Scroller.running = true;
        Scroller.mouseEvents(true);
    }
}

Scroller.stop = function(mouse) {
    if (Scroller.running) {
        clearInterval(Scroller.intervalId);
        Scroller.running = false;
        Scroller.mouseEvents(false);
    }
}

Scroller.mousestop = function() {
    clearInterval(Scroller.intervalId);
    Scroller.running = false;
}

Scroller.mousestart = function() {
    Scroller.intervalId = setInterval(Scroller.scroll, Scroller.interval);
    Scroller.running = true;

}

Scroller.toggleOnOff = function() {
    if (Scroller.running) {
        Scroller.stop();
    } else {
        Scroller.start();
    }
}

Scroller.toggleExpand = function() {
    if (Scroller.expanded) {
        Scroller.start();
    } else {
        Scroller.expand();
    }
}

Scroller.expand = function() {
    Scroller.stop();
    Scroller.expanded = true;
    var scroll = document.getElementById(Scroller.id);
    for (var i = 1; i <= Scroller.lastChild; i++) {
        scroll.appendChild(document.getElementById(Scroller.childId + i));
    }
    scroll.style.top = 0;

    var parent = document.getElementById(Scroller.parentId);
    //save the old height
    Scroller.parentHeight = parent.offsetHeight;
    //set the expanded height
    parent.style.height = Scroller.totalHeight + "px";
}

Scroller.init = function() {

    //max 50 nodes
    var elm;
    var lastElm;
    for (var i = 1; i < 50; i++) {
        elm = document.getElementById(Scroller.childId + i);
        if (elm != null) {
            //weird rendering thing were you need padding and margin set to actually get the offsetheight
            elm.style.padding = Scroller.padding + "px";
            elm.style.margin = Scroller.margin + "px";
            Scroller.scrollindex += elm.offsetHeight;
            Scroller.totalHeight += elm.offsetHeight;
            Scroller.lastChild = i;
            lastElm = elm;
        } else {
            break;
        }
    }
    if (lastElm != null) {
        Scroller.scrollindex -= lastElm.offsetHeight;

        Scroller.elmArray = [];
        Scroller.tmpArray = [];
        for (var i = 1; i < Scroller.lastChild; i++) {
            Scroller.elmArray[i - 1] = i;
            Scroller.tmpArray[i - 1] = 0;
        }
    }

}

Scroller.mouseEvents = function(on) {
    var scroller = document.getElementById(Scroller.id);
    if (on) {
        scroller.onmouseover = Scroller.mousestop;
        scroller.onmouseout = Scroller.mousestart;
    } else {
        scroller.onmouseover = null;
        scroller.onmouseout = null;
    }

}

Scroller.scroll = function() {
    var scroll = document.getElementById(Scroller.id);
    if (Scroller.scrollposition >= Scroller.scrollindex) {
        var pos = 0;
        for (var i = 0; i < Scroller.elmArray.length; i++) {
            var _child = document.getElementById(Scroller.childId + Scroller.elmArray[i]);
            pos += _child.offsetHeight;
            scroll.appendChild(_child);
        }
        scroll.style.top = "0px";
        Scroller.scrollposition = 1;

        Scroller.tmpArray[0] = Scroller.lastChild;
        for (var i = 1; i < Scroller.tmpArray.length; i++) {
            Scroller.tmpArray[i] = Scroller.elmArray[i - 1];
        }
        Scroller.lastChild = Scroller.elmArray[Scroller.elmArray.length - 1];
        if (document.getElementById(Scroller.childId + Scroller.lastChild) == null) {
            //there is an error getout
            Scroller.stop();
        } else {
            Scroller.scrollindex = Scroller.totalHeight - document.getElementById(Scroller.childId + Scroller.lastChild).offsetHeight;
            for (var i = 0; i < Scroller.tmpArray.length; i++) {
                Scroller.elmArray[i] = Scroller.tmpArray[i];
            }
        }
    } else {
        Scroller.scrollposition += Scroller.amount;
        scroll.style.top = (-1 * Scroller.scrollposition) + "px";
    }
}