/***********************************************

* 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.direction = 1

    this.step = 4

    this.stepTimeout = 200;

    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.delayCount = -1 //Couter for better mouse response

    this.delayCountNumber = 20 //Top value of delay counter

    this.delayShort = 0.1 // Shorter delay multiplier

    this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)

    this.scrollUp = 0

    this.scrollDown = 0

    this.hiddendivpointer=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">'+unescapeHTML(content[0])+'</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="'+divId+'2">'+unescapeHTML(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)
  
    
}



function unescapeHTML(code)

{

    code = code.replace(/&lt;/gi, '<');

    code = code.replace(/&gt;/gi, '>');

    code = code.replace(/&amp;/gi, '&');

    code = code.replace(/&quot;/gi, '"');



    return code;

}



// -------------------------------------------------------------------

// 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.hiddendiv=document.getElementById(this.tickerid+"2")

    this.visibledivtop=parseInt(pausescroller.getCSSpaddingTop(this.tickerdiv))

    this.visibledivbottom=parseInt(pausescroller.getCSSpaddingBottom(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.hiddendiv.style.width=this.tickerdiv.offsetWidth-(this.visibledivtop*2)-30+"px"

    this.getinline(this.visiblediv, this.hiddendiv)

    this.hiddendiv.style.visibility="visible"

    var scrollerinstance=this

    document.getElementById(this.tickerid).onmouseover=function(){scrollerinstance.mouseoverBol=1}

    document.getElementById(this.tickerid).onmouseout=function(){scrollerinstance.mouseoverBol=0}



    document.getElementById("scrollUp").onmouseover=function(){scrollerinstance.scrollUp=1}

    document.getElementById("scrollUp").onmouseout=function(){scrollerinstance.scrollUp=0}



    document.getElementById("scrollDown").onmouseover=function(){scrollerinstance.scrollDown=1}

    document.getElementById("scrollDown").onmouseout=function(){scrollerinstance.scrollDown=0}



    if (window.attachEvent) //Clean up loose references in IE

    window.attachEvent("onunload", function(){scrollerinstance.tickerdiv.onmouseover=scrollerinstance.tickerdiv.onmouseout=null})

    setTimeout(function(){scrollerinstance.animate()}, this.delay)

}





// -------------------------------------------------------------------

// animate()- Move the two inner divs of the scroller and in sync

// -------------------------------------------------------------------



pausescroller.prototype.animate=function()

{

    var scrollerinstance=this

    var step = this.step

    var tempDirection = scrollerinstance.direction

    if (scrollerinstance.scrollUp || scrollerinstance.scrollDown) step *= 2;



    if (scrollerinstance.scrollUp)   scrollerinstance.direction = -1;

    if (scrollerinstance.scrollDown) scrollerinstance.direction = 1;



    if (tempDirection != scrollerinstance.direction) this.swapdivs();



    if ((parseInt(this.hiddendiv.style.top)*this.direction)>((this.visibledivtop+(step*this.direction))*this.direction)){

        this.visiblediv.style.top=parseInt(this.visiblediv.style.top)-(step*this.direction)+"px"

        this.hiddendiv.style.top=parseInt(this.hiddendiv.style.top)-(step*this.direction)+"px"



        if (parseInt(this.hiddendiv.style.top) > parseInt(this.visiblediv.style.top)) {

            if ((parseInt(this.visiblediv.style.top) + this.visiblediv.offsetHeight) > parseInt(this.hiddendiv.style.top)) {

                //this.visiblediv.style.top = (parseInt(this.hiddendiv.style.top) - this.visiblediv.offsetHeight)+"px"

                this.hiddendiv.style.top = (parseInt(this.visiblediv.style.top) + this.visiblediv.offsetHeight)+"px"

            }

        } else {

            //if ((parseInt(this.hiddendiv.style.top) + this.hiddendiv.offsetHeight) > parseInt(this.visiblediv.style.top)) {

                this.hiddendiv.style.top = (parseInt(this.visiblediv.style.top) - this.hiddendiv.offsetHeight)+"px"

            //}

        }



        var timeout = this.stepTimeout

        if (scrollerinstance.scrollUp || scrollerinstance.scrollDown) timeout /= 2;

        if (scrollerinstance.mouseoverBol) timeout *= 2;



        setTimeout(function(){scrollerinstance.animate()}, timeout)

    }

    else{

        this.getinline(this.hiddendiv, this.visiblediv)

        this.swapdivs()



        if ((scrollerinstance.scrollUp || scrollerinstance.scrollDown))

        setTimeout(function(){scrollerinstance.setmessage()}, this.delay*this.delayShort )

        else

        scrollerinstance.setmessage()

    }

}



// -------------------------------------------------------------------

// swapdivs()- Swap between which is the visible and which is the hidden div

// -------------------------------------------------------------------



pausescroller.prototype.swapdivs = function()

{

    var tempcontainer=this.visiblediv

    this.visiblediv=this.hiddendiv

    this.hiddendiv=tempcontainer

}



pausescroller.prototype.getinline = function(div1, div2)

{

    div1.style.top=this.visibledivtop+"px"

    if (this.direction > 0)

    div2.style.top=  Math.max(div1.parentNode.offsetHeight, div1.offsetHeight)+"px"

    else

    div2.style.top= -Math.max(div2.parentNode.offsetHeight, div2.offsetHeight)+"px"

}



// -------------------------------------------------------------------

// setmessage()- Populate the hidden div with the next message before it's visible

// -------------------------------------------------------------------



pausescroller.prototype.setmessage = function()

{

    var scrollerinstance=this

    if (!(scrollerinstance.scrollUp || scrollerinstance.scrollDown)) {

        if (scrollerinstance.delayCount == -1) scrollerinstance.delayCount = scrollerinstance.delayCountNumber



        if (scrollerinstance.delayCount > 0) {

            scrollerinstance.delayCount--;

            setTimeout(function(){scrollerinstance.setmessage()}, Math.round(this.delay/scrollerinstance.delayCountNumber))

            return

        }

    }

    scrollerinstance.delayCount = -1



    /*

    if (this.mouseoverBol==1) { //if mouse is currently over scoller, do nothing (pause it)

        setTimeout(function(){scrollerinstance.setmessage()}, 10)

    }

    */

    

    //else {

        var i=this.hiddendivpointer

        var ceiling=this.content.length - 1



        this.hiddendivpointer += this.direction



        if (this.hiddendivpointer > ceiling) this.hiddendivpointer = 0;

        if (this.hiddendivpointer < 0)       this.hiddendivpointer = ceiling



        this.hiddendiv.innerHTML = unescapeHTML(this.content[this.hiddendivpointer]);



        this.animate()

    //}



}



pausescroller.getCSSpaddingTop = 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.getCSSpaddingBottom = function(tickerobj)

{ //get CSS padding value, if any

    if (tickerobj.currentStyle)

    return tickerobj.currentStyle["paddingBottom"]

    else if (window.getComputedStyle) //if DOM2

    return window.getComputedStyle(tickerobj, "").getPropertyValue("padding-bottom")

    else

    return 0

}
