/* To change the div_ticker interval, the start div, and the number of
   divs/end div:
   Change the div_ticker.init call to the number of the div to start
   on, how many divs there are/what div to end on, and the number of
   seconds you want between the next div display.
   For example:

       <body onload="div_ticker.init(1,5,30)">

   This code is in the above_content.tpl in this site - and thus requires a
   iwregen of the page after the change -but  it might be implemented in
   different files on other sites. Search for 'div_ticker.init'.

   This code assumes the following exists in the HTML of the page that
   div_ticker.init is called from:
   - an element with the id of ticker_control_pause
   - an element with the id of ticker_control_play
   - div(s) with the id in the format ticker_content_0x
   - link(s) with the id in the format link_0x

   This code assumes the following exists in the stylesheet of the page that
   div_ticker.init is called from:
   - a css style named hide_ticker_element
   - a css style named ticker_button
   - a ccs style named current
   - a css style named not_current

   Any calls to div_ticker.ticker_actions(n, prev, stopInterval, buttonClicked)
   assume that:
   - n and prev are integers within the bounds of the number of divs or 0
   - stopInterval is true or false
   - buttonClicked equals one of:
        * play
        * pause
        * next
        * prev
        * link
        * blank e.g. ''
		
	Stop ticker's automatic start:
	By default the ticker will run when the page loads. However in some circumstances 
	you may want to have the ticker paused on page load. To do this UNCOMMENT the line
	in the init method that pauses the ticker with the following comment:
	// COMMENT OUT THE FOLLOWING LINE TO AUTOMATICALLY START THE TICKER ON PAGE LOAD
	
	To have the ticker automatically run on page load again you simply need to comment 
	out the above line - DO NOT delete the code!!

*/

// create namespace div_ticker
var div_ticker = {};

    div_ticker.timeDelay;
    div_ticker.start;
    div_ticker.end;

    div_ticker.i=1;
    div_ticker.myInterval;
    div_ticker.myTimeout;

    div_ticker.init = function(start, end, timeDelay) {
        div_ticker.timeDelay = (timeDelay*1000); // convert from seconds
        div_ticker.start = start;
        div_ticker.end = end;
        div_ticker.myInterval = setInterval('div_ticker.seq_no()',div_ticker.timeDelay)   
		
		// COMMENT OUT THE FOLLOWING LINE TO AUTOMATICALLY START THE TICKER ON PAGE LOAD
		// div_ticker.pause_ticker('pause'); // pauses the ticker
    }

    div_ticker.ticker_actions = function(n, prev, stopInterval, buttonClicked) {
        if (buttonClicked =='next')
        {
          div_ticker.seq_no(true); // move to next div immediately
        }
            
        if (buttonClicked =='prev') 
        {
            div_ticker.seq_no(false); // move to previous div
        }
        
        if (buttonClicked =='play')
        {
            // clear intervals to prevent conflicting sequences
            clearTimeout(div_ticker.myTimeout);
            clearInterval(div_ticker.myInterval);
            div_ticker.seq_no(true); // move to next div immediately
            div_ticker.myTimeout = setTimeout("div_ticker.myInterval = setInterval('div_ticker.seq_no()', div_ticker.timeDelay)", div_ticker.timeDelay);    // restart ticker
        }   
        
        if (buttonClicked =='link')
        {
            div_ticker.display_div(n, prev); // display the specified div
            div_ticker.pause_ticker(buttonClicked); // pause ticker on div
        }   
        // auto ticker progress
        if (stopInterval == false) // don't pause
        {         
            if (buttonClicked=='')
            {   
                div_ticker.display_div(n, prev); // display the next div
            }
        }
        else {
            div_ticker.pause_ticker(buttonClicked); // pause ticker on current div
        }         
    }
 
    // generate seq number
    div_ticker.seq_no = function(increment) {
        var pause_button = document.getElementById("ticker_control_pause");
        var play_button = document.getElementById("ticker_control_play");
        
        play_button.className='hide_ticker_element';
        pause_button.className='ticker_button';
    
        prev = div_ticker.i;    
        if (increment==false) // is ticker moving forward or backwards - false is backwards
        {
            div_ticker.i--;
        }
        else {
            div_ticker.i++;
        }
       
        if (div_ticker.i < div_ticker.start)
        {
            div_ticker.i = div_ticker.end;
            div_ticker.prev = div_ticker.start;
        }
         
        if (div_ticker.i > div_ticker.end)
        {
            div_ticker.i = div_ticker.start;
        }
        div_ticker.ticker_actions(div_ticker.i,prev, false, '');
    }
 
    div_ticker.pause_ticker = function(buttonClicked) {
        // turn off the intervals and wait
        clearTimeout(div_ticker.myTimeout);
        clearInterval(div_ticker.myInterval);
        
        var pause_button = document.getElementById("ticker_control_pause");
        var play_button = document.getElementById("ticker_control_play");
        pause_button.className='hide_ticker_element';
        play_button.className='ticker_button';    
        
        if (buttonClicked != "pause") // for link clicks and prev next buttons
        {
            // restart the sequence after an appropriate amount of time if its not a pause
            div_ticker.myTimeout = setTimeout("div_ticker.myInterval = setInterval('div_ticker.seq_no()', div_ticker.timeDelay)", 10000);  
        }
    }
 
    div_ticker.display_div = function(n, prev) {
        div_ticker.i=n;
    
        var currentdiv = document.getElementById("ticker_content_0"+n);
        var previousdiv = document.getElementById("ticker_content_0"+prev);
        previousdiv.style.display = "none";
        currentdiv.style.display = "block";
        
        document.getElementById("link_0"+n).className="current";
        document.getElementById("link_0"+prev).className="not_current";
    }

