/***********************************************
* Pausing up-down scroller- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

function pausescroller(content, divId, divClass, delay) {
    this.content = content //message array content
    this.tickerid = divId //ID of ticker div to display information
    this.delay = delay //Delay between msg change, in miliseconds.
    this.mouseoverBol = 0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)
    this.nextDivpointer = 0 //index of message array for hidden div
    this.mostRecentTimeOut = null //id of the most recent timeout.
    this.isAnimating = false //indicates if the marquis is currently animating.
    var scrollerinstance = this
    document.write('<div id="' + divId + '" class="' + divClass + '" style="position: relative; overflow: hidden"><div class="innerDiv" style="position: absolute; width: 100%; visibility:hidden;" id="' + divId + '0">' + content[content.length - 1] + '</div><div class="innerDiv" style="position: absolute; width: 100%" id="' + divId + '1">' + content[0] + '</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="' + divId + '2">' + content[1] + '</div><div class="marquisControls" id="' + divId + 'Controls"><div class="previous" id="' + divId + 'ControlsPrevious"><</div><div class="next" id="' + divId + 'ControlsNext">></div></div></div>')   
    if (window.addEventListener) //run onload in DOM2 browsers
        window.addEventListener("load", function() { scrollerinstance.initialize() }, false)
    else if (window.attachEvent) //run onload in IE5.5+
        window.attachEvent("onload", function() { scrollerinstance.initialize() })
    else if (document.getElementById) //if legacy DOM browsers, just start scroller after 0.5 sec
        setTimeout(function() { scrollerinstance.initialize() }, 500)
}

// -------------------------------------------------------------------
// initialize()- Initialize scroller method.
// -Get div objects, set initial positions, start up down animation
// -------------------------------------------------------------------

pausescroller.prototype.initialize = function() {
this.tickerdiv = document.getElementById(this.tickerid)
    this.previousDiv = document.getElementById(this.tickerid + "0")
    this.currentDiv = document.getElementById(this.tickerid + "1")
    this.nextDiv = document.getElementById(this.tickerid + "2")
    this.controlDiv = document.getElementById(this.tickerid + "Controls")
    this.previousControlDiv = document.getElementById(this.tickerid + "ControlsPrevious")
    this.nextControlDiv = document.getElementById(this.tickerid + "ControlsNext")
    this.currentDivtop = parseInt(pausescroller.getCSSpadding(this.tickerdiv))
    //set width of inner DIVs to outer DIV's width minus padding (padding assumed to be top padding x 2)
    this.previousDiv.style.width = this.currentDiv.style.width = this.nextDiv.style.width = this.tickerdiv.offsetWidth - (this.currentDivtop * 2) + "px"
    this.getinline(this.previousDiv, this.currentDiv, this.nextDiv)
    this.previousDiv.style.visibility = "visible"
    this.nextDiv.style.visibility = "visible"
    var scrollerinstance = this
    document.getElementById(this.tickerid).onmouseover = function() { scrollerinstance.mouseoverBol = 1; scrollerinstance.showControls(scrollerinstance); }
    document.getElementById(this.tickerid).onmouseout = function() { scrollerinstance.mouseoverBol = 0; scrollerinstance.hideControls(scrollerinstance); }
    this.nextControlDiv.onclick = function() { scrollerinstance.setmessage(true, 'next'); }
    this.previousControlDiv.onclick = function() { scrollerinstance.setmessage(true, 'previous'); }
    if (window.attachEvent) //Clean up loose references in IE
        window.attachEvent("onunload", function() { scrollerinstance.tickerdiv.onmouseover = scrollerinstance.tickerdiv.onmouseout = null })
    this.mostRecentTimeOut = setTimeout(function() { scrollerinstance.setmessage(false, 'next') }, this.delay)
}


// -------------------------------------------------------------------
// animateup()- Move the inner divs of the scroller up and in sync
// -------------------------------------------------------------------

pausescroller.prototype.animateup = function() {
    var scrollerinstance = this
    if (parseInt(this.nextDiv.style.top) > (this.currentDivtop + 5)) {
        this.isAnimating = true;
        this.previousDiv.style.top = parseInt(this.previousDiv.style.top) - 5 + "px" 
        this.currentDiv.style.top = parseInt(this.currentDiv.style.top) - 5 + "px"
        this.nextDiv.style.top = parseInt(this.nextDiv.style.top) - 5 + "px"
        this.mostRecentTimeOut = setTimeout(function() { scrollerinstance.animateup() }, 50)
    }
    else {
        this.isAnimating = false;
        this.getinline(this.currentDiv, this.nextDiv, this.previousDiv)
        this.swapdivs('next')
        this.mostRecentTimeOut = setTimeout(function() { scrollerinstance.setmessage(false, 'next') }, this.delay)
    }
}

// -------------------------------------------------------------------
// animatedown()- Move the inner divs of the scroller down and in sync
// -------------------------------------------------------------------

pausescroller.prototype.animatedown = function() {
    var scrollerinstance = this
    if (parseInt(this.previousDiv.style.top) < (this.currentDivtop - 5)) {
        this.isAnimating = true;
        this.previousDiv.style.top = parseInt(this.previousDiv.style.top) + 5 + "px" 
        this.currentDiv.style.top = parseInt(this.currentDiv.style.top) + 5 + "px"
        this.nextDiv.style.top = parseInt(this.nextDiv.style.top) + 5 + "px"
        this.mostRecentTimeOut = setTimeout(function() { scrollerinstance.animatedown() }, 50)
    }
    else {
        this.isAnimating = false;
        this.getinline(this.nextDiv, this.previousDiv, this.currentDiv)
        this.swapdivs('previous')
        this.mostRecentTimeOut = setTimeout(function() { scrollerinstance.setmessage(false, 'next') }, this.delay)
    }
}

// -------------------------------------------------------------------
// swapdivs()- Swap between which is the visible and which is the hidden div
// -------------------------------------------------------------------

pausescroller.prototype.swapdivs = function(direction) {
    var tempcontainer = this.currentDiv;
    if (direction == 'next') {
        this.currentDiv = this.nextDiv;
        this.nextDiv = this.previousDiv;
        this.previousDiv = tempcontainer;
    } else {
        this.currentDiv = this.previousDiv;
        this.previousDiv = this.nextDiv;
        this.nextDiv = tempcontainer;
    }
}

pausescroller.prototype.getinline = function(div0, div1, div2) {
    var difference = Math.max(div1.parentNode.offsetHeight, div1.offsetHeight)
    div1.style.top = this.currentDivtop + "px"
    div2.style.top = difference + "px"
    div0.style.top = 0 - difference + "px"
}

// -------------------------------------------------------------------
// setmessage()- Populate the hidden div with the next message before it's visible
// -------------------------------------------------------------------

pausescroller.prototype.setmessage = function(userControlled, direction) {
    //if the marquis is animating, we reject the function call
    if (this.isAnimating) {
        return;
    }
    //if the user clicked the control, remove the most recent timeout, otherwise we start stacking timeouts
    if (userControlled == true && this.mostRecentTimeOut != null) {
        clearTimeout(this.mostRecentTimeOut);
    }
    var scrollerinstance = this
    if (this.mouseoverBol == 1 && userControlled == false) //if mouse is currently over scoller and the user didn't click
        this.mostRecentTimeOut = setTimeout(function() { scrollerinstance.setmessage(false, 'next') }, 100)
    else {
        if (direction == 'next') {
            var i = this.nextDivpointer
            var ceiling = this.content.length
            this.nextDivpointer = (i + 1 > ceiling - 1) ? 0 : i + 1
            this.nextDiv.innerHTML = this.content[this.nextDivpointer]
            this.animateup()
        } else {
            var previousDivPointer = (this.nextDivpointer - 1 >= 0) ? this.nextDivpointer - 1 : this.content.length - 1;
            this.nextDivpointer = previousDivPointer
            this.previousDiv.innerHTML = this.content[previousDivPointer]
            this.animatedown()
        }
    }
}

pausescroller.getCSSpadding = function(tickerobj) { //get CSS padding value, if any
    if (tickerobj.currentStyle)
        return tickerobj.currentStyle["paddingTop"]
    else if (window.getComputedStyle) //if DOM2
        return window.getComputedStyle(tickerobj, "").getPropertyValue("padding-top")
    else
        return 0
}

pausescroller.prototype.showControls = function(scrollerinstance) {
scrollerinstance.controlDiv.style.display = "inline-block";
}

pausescroller.prototype.hideControls = function(scrollerinstance) {
scrollerinstance.controlDiv.style.display = "none";
}
