window.addEvent('domready', function()
{
	/*remove the noscript class, so everything
    displays properly*/
    $('ticker').removeClass('noscript');
    
    /*get the list of items generated by php*/
    var ul = $('ticker').getElement('ul');
    
    /*create the effect.  note that when it completes
    it calls the complete() function.  note that the 
    duration is set to the result of getDuration(), see
    below for what that does*/
    var slideUL = new Fx.Tween(ul,
    {
        duration: getDuration(), 
        transition: Fx.Transitions.linear,
        onComplete: function()
        {
            complete();
        }
    });
    
    var stopUL = new Fx.Tween(ul);
    
    /*this is the initial call to start the ticker
    moving when the page has finished loading*/
    slideTicker();
    
    /*all this is doing is ensuring the list is 3400px
    from the left and therefore just out of view.  it checks
    the width of the list and calculates where it should slide
    to.  it then starts the animation*/
    function slideTicker()
    {
        ul.setStyle('left', 3400);
        var ulwidth = ul.getStyle('width');
        
        var slideTo = 3000 - parseInt(ulwidth);
        slideUL.start('left', slideTo);
    }
	
    /*pause the effect*/
    function pauseTicker()
    {
        slideUL.pause();
    }
    
    /*resume the effect*/
    function resumeTicker()
    {
        slideUL.resume();
    }
    
    /*cancel the animation*/
    function stopTicker()
    {
        slideUL.cancel();
    }
    
    /*this recursively calls the slideTicker() function, 
    so when slideTicker has finished it calls complete(),
    which simply calls itself again.  meaning the ticker goes
    on forever*/
    function complete()
    {
        slideTicker();
    }
    
    /*works out how many items there are in the feed, so 
    that however many items there are, the speed is consistent.
    each item in the feed counts for 10 seconds of time. */
    function getDuration()
    {
        var liCount = $('ticker').getElements('li');
        liCount = liCount.length;
        
        var duration = liCount * 10000;
        return duration;
    }
    
    /*event listeners for mouse-over and mouse-leave of the
    ticker; if you hover your mouse over it, it stops the animation
    and allows you to click on the item*/
    $('ticker').getElement('div').addEvent('mouseenter',function(e)
    {
        e = new Event(e).stop();
        pauseTicker();
        
    });
    
    /*if you then stop hovering on the ticker it resumes the animation*/
    $('ticker').getElement('div').addEvent('mouseleave',function(e)
    {
        e = new Event(e).stop();
        resumeTicker();
        
    });
    
    /*this is the control introduced purely for accessibility
    reasons, if you click on it, it pauses the ticker.  click on it
    again and it resumes*/
    $('toggleTicker').addEvent('click',function(e)
    {
        e = new Event(e).stop();
        
        if(this.innerHTML == 'Stop Ticker')
        {
            pauseTicker();
            this.innerHTML = 'Resume Ticker';
        }
        else
        {
            resumeTicker();
            this.innerHTML = 'Stop Ticker';
        }
    });
    
});

