﻿var subscribed = false;
var pushURL = '';
var token = '';
var symbolFIDs = '';
var symbolList = new Array();
var subscribers = new Array();
var pairInfo = new Array();
var listener = null;
var hndsk = null;

function initComet() {
    try {
        dojox.cometd.init(pushURL, { "ext":
                                   { "teletrader":
                                        {
                                            "SymbolFIDs": symbolFIDs,
                                            "AuthToken": token
                                        }
                                   }
        });

        // subscribe to cometd meta events (handshake, connect, disconnect)
        var handle = dojo.subscribe("/cometd/meta", this, function(event) {
            console.debug("action=" + event.action + "; successful=" + event.successful + ";");
            if (event.action == "connect" && event.successful) {
                if (!subscribed) {
                    subscribed = true;
                    subscribeSymbols();
                }
            }
        });
    }
    catch (e) {
        console.debug("Cometd initialization procedure failed. Error name: " + e.name + "; Message: " + e.message + ".");
    }
    
    $j('.ttratescaglink').click(function(event) {
        event.preventDefault;
        window.location.replace(event.currentTarget.attributes['data-urlcag'].value);
    });
}


function subscribeSymbols() {
    var i = 0;
    for (i = 0; i < symbolList.split(',').length; i++) {
        var channel = '/teletrader/symbols/' + symbolList.split(',')[i];
        if (subscribers[symbolList.split(',')[i]]) {
            subscribers[symbolList.split(',')[i]] = null;
        }

        dojox.cometd.subscribe(channel, function handleMessage(msg) {
            if (typeof msg.data.last != 'undefined') {
                msg.data.last = NumbRound(msg.data.last, pairInfo[msg.data.symbolId][1]);
                $j('#last_' + msg.data.symbolId).text(msg.data.last);
            }
            if (typeof msg.data.open != 'undefined') {
                msg.data.open = NumbRound(msg.data.open, pairInfo[msg.data.symbolId][1]);
                $j('#open_' + msg.data.symbolId).text(msg.data.open);
            }
            if (typeof msg.data.bid != 'undefined') {
                msg.data.bid = NumbRound(msg.data.bid, pairInfo[msg.data.symbolId][1]);
                $j('#bid_' + msg.data.symbolId).text(msg.data.bid);
            }
            if (typeof msg.data.ask != 'undefined') {
                msg.data.ask = NumbRound(msg.data.ask, pairInfo[msg.data.symbolId][1]);
                $j('#ask_' + msg.data.symbolId).text(msg.data.ask);
            }
            if (typeof msg.data.tradeVolume != 'undefined') {
                $j('#tradeVolume_' + msg.data.symbolId).text(msg.data.tradeVolume);
            }
            if (typeof msg.data.high != 'undefined') {
                msg.data.high = NumbRound(msg.data.high, pairInfo[msg.data.symbolId][1]);
                $j('#high_' + msg.data.symbolId).text(msg.data.high);
            }
            if (typeof msg.data.low != 'undefined') {
                msg.data.low = NumbRound(msg.data.low, pairInfo[msg.data.symbolId][1]);
                $j('#low_' + msg.data.symbolId).text(msg.data.low);
            }
            if (typeof msg.data.change != 'undefined') {
                msg.data.change = NumbRound(msg.data.change, 4);
                if (msg.data.change > new Number($j('#change_' + msg.data.symbolId).text())) {
                    $j('#change_' + msg.data.symbolId).addClass('arrow-green');
                    $j('#change_' + msg.data.symbolId).removeClass('arrow-red');
                }
                else if (msg.data.change < new Number($j('#change_' + msg.data.symbolId).text())) {
                    $j('#change_' + msg.data.symbolId).addClass('arrow-red');
                    $j('#change_' + msg.data.symbolId).removeClass('arrow-green');
                }
                else {
                    $j('#change_' + msg.data.symbolId).removeClass('arrow-green');
                    $j('#change_' + msg.data.symbolId).removeClass('arrow-red');
                }
                
                if (msg.data.change > 0) {
                    $j('#change_' + msg.data.symbolId).text('+' + msg.data.change);
                    $j('#change_' + msg.data.symbolId).addClass('td-green');
                    $j('#change_' + msg.data.symbolId).removeClass('td-red');
                }
                else {
                    if (msg.data.change < 0) {
                        $j('#change_' + msg.data.symbolId).addClass('td-red');
                        $j('#change_' + msg.data.symbolId).removeClass('td-gree')
                    }
                    else {
                        $j('#change_' + msg.data.symbolId).removeClass('td-green');
                        $j('#change_' + msg.data.symbolId).removeClass('td-red')
                    }
                    $j('#change_' + msg.data.symbolId).text(msg.data.change);
                }

            }
            if (typeof msg.data.changePercent != 'undefined') {
                msg.data.changePercent = NumbRound(msg.data.changePercent, 2);
                if (msg.data.changePercent > new Number($j('#changePercent_' + msg.data.symbolId).text().replace('%', ''))) {
                    $j('#changePercent_' + msg.data.symbolId).addClass('arrow-green');
                    $j('#changePercent_' + msg.data.symbolId).removeClass('arrow-red');
                }
                else if (msg.data.changePercent < new Number($j('#changePercent_' + msg.data.symbolId).text().replace('%', ''))) {
                    $j('#changePercent_' + msg.data.symbolId).addClass('arrow-red');
                    $j('#changePercent_' + msg.data.symbolId).removeClass('arrow-green');
                }
                else {
                    $j('#changePercent_' + msg.data.symbolId).removeClass('arrow-green');
                    $j('#changePercent_' + msg.data.symbolId).removeClass('arrow-red');
                }
                
                if (msg.data.changePercent > 0) {
                    $j('#changePercent_' + msg.data.symbolId).text('+' + msg.data.changePercent + '%');
                    $j('#changePercent_' + msg.data.symbolId).addClass('td-green');
                    $j('#changePercent_' + msg.data.symbolId).removeClass('td-red');
                }
                else {
                    if (msg.data.changePercent < 0) {
                        $j('#changePercent_' + msg.data.symbolId).addClass('td-red');
                        $j('#changePercent_' + msg.data.symbolId).removeClass('td-gree');
                    }
                    else {
                        $j('#changePercent_' + msg.data.symbolId).removeClass('td-green');
                        $j('#changePercent_' + msg.data.symbolId).removeClass('td-red');
                    }
                    $j('#changePercent_' + msg.data.symbolId).text(msg.data.changePercent + '%');
                }
            }
            if (typeof msg.data.dateTime != 'undefined') {
                $j('#dateTime_' + msg.data.symbolId).text(msg.data.dateTime.split(' ')[1]);
            }
        });
    }
}


function NumbRound(num, dec) {
    var rounded = (parseInt(num * Math.pow(10, dec)) / Math.pow(10, dec));
    rounded = rounded.toString();
    if (rounded.split('.').length == 1) {
        rounded += '.';
    }
    var numDec = rounded.split('.')[1].length;
    if (numDec < dec) {
        var i = 0;
        for (i = 0; i < (dec - numDec); i++) {
            rounded += '0';
        }
    }
    else if (numDec > dec) {
        rounded.split('.')[1] = rounded.split('.')[1].substring(0, dec);
    }

    return rounded;
}