﻿/***********************************************
* 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(scrollhorizontal, content, divId, divClass, delay, onprev, onnext) {
    this.scrollhorizontal = scrollhorizontal
    this.onprev = onprev
    this.onnext = onnext
    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.animatedelay = 50 //Delay between animation frames
    this.animatesteps = 1 //How far to move in each animation frame
    this.mouseoverBol = 0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)
    this.visiblepointer = 0
    this.animatingprev = false
    this.animatingnext = false
    this.hiddenprevdivpointer = this.content.length - 1 //index of message array for hidden div
    this.hiddennextdivpointer = 1 //index of message array for hidden div
    document.write('<div id="' + divId + '" class="' + divClass + '" style="position: relative; overflow: hidden"><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 + 'prev">' + content[1] + '</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="' + divId + 'next">' + content[1] + '</div></div>')
    var scrollerinstance = this
    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.visiblediv = document.getElementById(this.tickerid + "1")
    this.hiddenprevdiv = document.getElementById(this.tickerid + "prev")
    this.hiddennextdiv = document.getElementById(this.tickerid + "next")
    this.visibledivpadding = 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.visiblediv.style.width = this.hiddenprevdiv.style.width = this.hiddennextdiv.style.width = this.tickerdiv.offsetWidth - (this.visibledivpadding * 2) + "px"
    this.getinline()
    this.hiddenprevdiv.style.visibility = "visible"
    this.hiddennextdiv.style.visibility = "visible"
    var scrollerinstance = this
    document.getElementById(this.tickerid).onmouseover = function() { scrollerinstance.mouseoverBol = 1 }
    document.getElementById(this.tickerid).onmouseout = function() { scrollerinstance.mouseoverBol = 0 }
    if (window.attachEvent) //Clean up loose references in IE
        window.attachEvent("onunload", function() { scrollerinstance.tickerdiv.onmouseover = scrollerinstance.tickerdiv.onmouseout = null })
    if (this.delay > 0) this.timeout = setTimeout(function() { scrollerinstance.next() }, this.delay)
}


// -------------------------------------------------------------------
// animateup()- Move the two inner divs of the scroller up and in sync
// -------------------------------------------------------------------

pausescroller.prototype.animateleft = function() {
    clearTimeout(this.timeout)
    var scrollerinstance = this
    if (this.mouseoverBol == 1) //if mouse is currently over scoller, do nothing (pause it)
        this.timeout = setTimeout(function() { scrollerinstance.animateleft() }, 10)
    else if (parseInt(this.visiblediv.style.left) < (this.visibledivpadding - 5)) {
        this.visiblediv.style.left = parseInt(this.visiblediv.style.left) + this.animatesteps + "px"
        this.hiddenprevdiv.style.left = parseInt(this.hiddenprevdiv.style.left) + this.animatesteps + "px"
        this.hiddennextdiv.style.left = parseInt(this.hiddennextdiv.style.left) + this.animatesteps + "px"
        this.timeout = setTimeout(function() { scrollerinstance.animateleft() }, this.animatedelay)
    }
    else {
        this.getinline()
        if (this.delay > 0) this.timeout = setTimeout(function() { scrollerinstance.next() }, this.delay)
    }
}

pausescroller.prototype.animateright = function() {
    clearTimeout(this.timeout)
    var scrollerinstance = this
    if (this.mouseoverBol == 1) //if mouse is currently over scoller, do nothing (pause it)
        this.timeout = setTimeout(function() { scrollerinstance.animateright() }, 10)
    else if (parseInt(this.visiblediv.style.left) > (this.visibledivpadding + 5)) {
        this.visiblediv.style.left = parseInt(this.visiblediv.style.left) - this.animatesteps + "px"
        this.hiddenprevdiv.style.left = parseInt(this.hiddenprevdiv.style.left) - this.animatesteps + "px"
        this.hiddennextdiv.style.left = parseInt(this.hiddennextdiv.style.left) - this.animatesteps + "px"
        this.timeout = setTimeout(function() { scrollerinstance.animateright() }, this.animatedelay)
    }
    else {
        this.getinline()
        if (this.delay > 0) this.timeout = setTimeout(function() { scrollerinstance.next() }, this.delay)
    }
}

// -------------------------------------------------------------------
// animateup()- Move the two inner divs of the scroller up and in sync
// -------------------------------------------------------------------

pausescroller.prototype.animateup = function() {
    clearTimeout(this.timeout)
    var scrollerinstance = this
    if (this.mouseoverBol == 1) //if mouse is currently over scoller, do nothing (pause it)
        this.timeout = setTimeout(function() { scrollerinstance.animateup() }, 10)
    else if (parseInt(this.visiblediv.style.top) < this.visibledivpadding - 5) {
        this.visiblediv.style.top = parseInt(this.visiblediv.style.top) + this.animatesteps + "px"
        this.hiddenprevdiv.style.top = parseInt(this.hiddenprevdiv.style.top) + this.animatesteps + "px"
        this.hiddennextdiv.style.top = parseInt(this.hiddennextdiv.style.top) + this.animatesteps + "px"
        this.timeout = setTimeout(function() { scrollerinstance.animateup() }, this.animatedelay)
    }
    else {
        this.getinline()
        if (this.delay > 0) this.timeout = setTimeout(function() { scrollerinstance.next() }, this.delay)
    }
}

pausescroller.prototype.animatedown = function() {
    clearTimeout(this.timeout)
    var scrollerinstance = this
    if (this.mouseoverBol == 1) //if mouse is currently over scoller, do nothing (pause it)
        this.timeout = setTimeout(function() { scrollerinstance.animatedown() }, 10)
    else if (parseInt(this.visiblediv.style.top) > (this.visibledivpadding + 5)) {
        this.visiblediv.style.top = parseInt(this.visiblediv.style.top) - this.animatesteps + "px"
        this.hiddenprevdiv.style.top = parseInt(this.hiddenprevdiv.style.top) - this.animatesteps + "px"
        this.hiddennextdiv.style.top = parseInt(this.hiddennextdiv.style.top) - this.animatesteps + "px"
        this.timeout = setTimeout(function() { scrollerinstance.animatedown() }, this.animatedelay)
    }
    else {
        this.getinline()
        if (this.delay > 0) this.timeout = setTimeout(function() { scrollerinstance.next() }, this.delay)
    }
}

// -------------------------------------------------------------------
// swapdivs()- Swap between which is the visible and which is the hidden div
// -------------------------------------------------------------------

pausescroller.prototype.swapdivsprev = function() {
    var rightdiv = this.hiddennextdiv

    this.hiddennextdiv = this.visiblediv
    this.visiblediv = this.hiddenprevdiv
    this.hiddenprevdiv = rightdiv

    var i = this.visiblepointer
    var ceiling = this.content.length
    this.visiblepointer = (i == 0) ? ceiling - 1 : i - 1
}

pausescroller.prototype.swapdivsnext = function() {
    var leftdiv = this.hiddenprevdiv

    this.hiddenprevdiv = this.visiblediv
    this.visiblediv = this.hiddennextdiv
    this.hiddennextdiv = leftdiv

    var i = this.visiblepointer
    var ceiling = this.content.length
    this.visiblepointer = (i + 1 > ceiling - 1) ? 0 : i + 1
}

pausescroller.prototype.getinline = function() {
    if (this.scrollhorizontal) this.getinlinehorizontal()
    else this.getinlinevertical()
}

pausescroller.prototype.getinlinehorizontal = function() {
    this.animatingprev = false
    this.animatingnext = false
    this.visiblediv.style.left = this.visibledivpadding + "px"
    this.hiddenprevdiv.style.left = -Math.max(this.visiblediv.parentNode.offsetWidth, this.visiblediv.offsetWidth) + "px"
    this.hiddennextdiv.style.left = Math.max(this.visiblediv.parentNode.offsetWidth, this.visiblediv.offsetWidth) + "px"
}

pausescroller.prototype.getinlinevertical = function() {
    this.animatingprev = false
    this.animatingnext = false
    this.visiblediv.style.top = this.visibledivpadding + "px"
    this.hiddenprevdiv.style.top = -Math.max(this.visiblediv.parentNode.offsetHeight, this.visiblediv.offsetHeight) + "px"
    this.hiddennextdiv.style.top = Math.max(this.visiblediv.parentNode.offsetHeight, this.visiblediv.offsetHeight) + "px"
}

// -------------------------------------------------------------------
// setmessage()- Populate the hidden div with the next message before it's visible
// -------------------------------------------------------------------

pausescroller.prototype.setcontent = function() {
    var i = this.visiblepointer
    var ceiling = this.content.length
    this.visiblediv.innerHTML = this.content[i]
    this.hiddenprevdivpointer = (i == 0) ? ceiling - 1 : i - 1
    this.hiddenprevdiv.innerHTML = this.content[this.hiddenprevdivpointer]
    this.hiddennextdivpointer = (i + 1 > ceiling - 1) ? 0 : i + 1
    this.hiddennextdiv.innerHTML = this.content[this.hiddennextdivpointer]
}

pausescroller.prototype.next = function() {
    if (this.scrollhorizontal) this.nexthorizontal()
    else this.nextvertical()
}

pausescroller.prototype.nexthorizontal = function() {
    var scrollerinstance = this

    if (this.animatingnext) return;

    if (this.mouseoverBol == 1) //if mouse is currently over scoller, do nothing (pause it)
        this.timeout = setTimeout(function() { scrollerinstance.next() }, 100)
    else {
        this.animatingprev = false
        this.animatingnext = true
        if (this.onnext != null) this.onnext()
        this.setcontent()
        this.swapdivsnext()
        this.animateright()
    }
}

pausescroller.prototype.nextvertical = function() {
    var scrollerinstance = this

    if (this.animatingnext) return;

    if (this.mouseoverBol == 1) //if mouse is currently over scoller, do nothing (pause it)
        this.timeout = setTimeout(function() { scrollerinstance.next() }, 100)
    else {
        this.animatingprev = false
        this.animatingnext = true
        if (this.onnext != null) this.onnext()
        this.setcontent()
        this.swapdivsnext()
        this.animatedown()
    }
}

pausescroller.prototype.prev = function() {
    if (this.scrollhorizontal) this.prevhorizontal()
    else this.prevvertical()
}

pausescroller.prototype.prevhorizontal = function() {
    var scrollerinstance = this

    if (this.animatingprev) return;

    if (this.mouseoverBol == 1) //if mouse is currently over scoller, do nothing (pause it)
        setTimeout(function() { scrollerinstance.prev() }, 100)
    else {
        this.animatingprev = true
        this.animatingnext = false
        if (this.onprev != null) this.onprev()
        this.setcontent()
        this.swapdivsprev()
        this.animateleft()
    }
}

pausescroller.prototype.prevvertical = function() {
    var scrollerinstance = this

    if (this.animatingprev) return;

    if (this.mouseoverBol == 1) //if mouse is currently over scoller, do nothing (pause it)
        setTimeout(function() { scrollerinstance.prev() }, 100)
    else {
        this.animatingprev = true
        this.animatingnext = false
        if (this.onprev != null) this.onprev()
        this.setcontent()
        this.swapdivsprev()
        this.animateup()
    }
}

pausescroller.getCSSpadding = function(tickerobj) { //get CSS padding value, if any
    if (this.scrollhorizontal) return this.getCSSpaddinghorizontal(tickerobj)
    else return this.getCSSpaddingvertical(tickerobj)
}

pausescroller.getCSSpaddinghorizontal = function(tickerobj) { //get CSS padding value, if any
    if (tickerobj.currentStyle)
        return tickerobj.currentStyle["paddingLeft"]
    else if (window.getComputedStyle) //if DOM2
        return window.getComputedStyle(tickerobj, "").getPropertyValue("padding-left")
    else
        return 0
}

pausescroller.getCSSpaddingvertical = 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
}