// stockticker.js v2.0
if (typeof Effect == 'undefined') 
    throw("stockticker.js requires including script.aculo.us' effects.js library!");
var stockticker = Class.create({
    
    thread: null,
    tickers: null,
    tickerArray: [],
    currentPage: null,
    
    initialize: function(container, tickers, options) {
        if (!$(container)) {
            throw(container+" doesn't exist!");
            return false;
        }
        this.options = Object.extend({
            frequency : 5,
            linkUrl: ""
        }, options || {});
        this.currentPage = 0;
        this.tickers = tickers;
        var atickerArray = [];
        this.tickers.scan(/(\w|-|\.)+/, function(match){ atickerArray.push({symbol: match[0]})}); 
        this.tickerArray = atickerArray;
        this.start();
    }, //initalize
    start: function() {
        this.onTimerEvent();
        if(!this.thread)
                this.thread = new PeriodicalExecuter(function(pe) {   
                                this.onTimerEvent()
                            }.bind(this), this.options.frequency);
    }, //start
      onTimerEvent: function() {
        this.update();
    }, //onTimerEvent
    update: function() {
        this.getTickerPage(this.currentPage);
        this.currentPage++;
        if(this.currentPage >= (this.tickerArray.length/5)) {
            this.currentPage = 0;
        }
        
    }, //update
    getTickerPage: function(page) {
        var jsonrequest = $H({'tickersrequest': this.tickerArray.slice((page * 5), ((page * 5) + 5))}).toJSON();
        var _url = '/ticker/Ticker_Cache.aspx?data=' + jsonrequest;
        new Ajax.Request(_url, {
            method:'post',
            postBody:jsonrequest,
            requestHeaders: {Accept: 'application/json'},
            onSuccess: function(transport) {
                syntax = /(^|.|\r|\n)(\<%=\s*(\w+)\s*%\>)/; //matches symbols like '<%= field %>' 
                upTemplate = new Template('<a href="/stock.aspx?pi_symbol=<%=symbol%>"><%=symbol%></a><span class="up"><%=price%></span><span class="up">+<%=change%></span>', syntax); 
                downTemplate = new Template('<a href="/stock.aspx?pi_symbol=<%=symbol%>"><%=symbol%></a><span class="down"><%=price%></span><span class="down"><%=change%></span>', syntax); 
    
                var tickers = transport.responseText.evalJSON();
                var elementTickerValues = $('tickervalues');
                var elementTickerDate = $('tickerdatetime');
                var changeText = "";
                for (i = 0; i < tickers.tickersdata.length; i++) {
                    if(tickers.tickersdata[i].symbol) { 
                        if(tickers.tickersdata[i].netChange < 0) {
                            changeText += downTemplate.evaluate({symbol: tickers.tickersdata[i].symbol, price: tickers.tickersdata[i].last, change: tickers.tickersdata[i].netChange});
                        } else {
                            changeText += upTemplate.evaluate({symbol: tickers.tickersdata[i].symbol, price: tickers.tickersdata[i].last, change: tickers.tickersdata[i].netChange});
                        }
                    }
                    if ((i+1) < tickers.tickersdata.length) 
                        changeText += '<span class="sp">|</span>';
                }
                elementTickerValues.innerHTML = changeText;
                elementTickerDate.innerHTML = tickers.dateTime + "*";
                //new Effect.Appear('tickervalues', {duration:3, from:0.1, to:1.0});        
            }
        });
    }//getTickerPage
}); //stockticker