﻿var _prevTimestamp = new Date().getTime();

updateChart = function(updatePacket) {
    if ($('#chartValue').length == 0)
        return;

    var identifier = updatePacket.dataName.substring(0, updatePacket.dataName.indexOf("."));

    if (identifier == 12222 && $("#indexLink").text() != 'Realtime AMX') return;
    if (identifier == 12272 && $("#indexLink").text() != 'Realtime AEX') return;

    var name = "AEX";
    if (identifier == 12222) name = "AMX";
    var precision = 2;
    var identifier = updatePacket.dataName.substring(0, updatePacket.dataName.indexOf("."));
    var field = updatePacket.dataName.substring(updatePacket.dataName.indexOf(".") + 1);
    var output = "";

    if (field.toUpperCase() == "RELATIVEDIFFERENCE" && updatePacket.value != null) {
        output = Number(updatePacket.value * 100).toFixed(precision) + "%";
        $("#indexTrend").html((updatePacket.value > 0 ? "STIJGING" : "DALING"));
        if (updatePacket.value == 0) {
            $("#indexTrend").html("GELIJK");
        }
        else
            $("#indexRelDif").html(output.replace('.', ','));

    } else if (field.toUpperCase() == "ABSOLUTEDIFFERENCE" && updatePacket.value != null) {
        output = updatePacket.value.toFixed(precision);
        if (updatePacket.value < 0) {
            $("#chartValue").css("backgroundColor", "#D95757");
            $("#indexTrend").parent().children().css('color', '#da0000');
        }
        if (updatePacket.value > 0) {
            $("#chartValue").css("backgroundColor", "#2EC038");
            $("#indexTrend").parent().children().css('color', '#009f0b');
        }

    } else if (field.toUpperCase() == "LASTPRICE" && updatePacket.value != null) {
        output = updatePacket.value.toFixed(precision);
        $("#chartValue").html("&nbsp;&nbsp;" + name + " " + output.replace('.', ',') + "&nbsp;&nbsp;");
        var timestamp = new Date().getTime();
        if ((timestamp - _prevTimestamp) > 1000 * 60) {
            _prevTimestamp = timestamp;
            var src = $("#indexChart").attr("src");
            if (src.indexOf("&t") > -1) src = src.substr(0, src.indexOf("&t"));
            $("#indexChart").attr("src", "");
            $("#indexChart").attr("src", src);
            $("#indexChart").attr("src", src + "&t=" + timestamp);
        }
    }

    if (updatePacket.type == vwdStreamerDataType.TIME) {
        output = updatePacket.value.toLocaleTimeString();
        var hour = updatePacket.value.getHours();
        var minute = updatePacket.value.getMinutes();
        $("#lasttradetime").html((hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute));
        $("#chartTime").html((hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute));
        var day = updatePacket.value.getDate();
        var month = 1 + updatePacket.value.getMonth();
        var year = updatePacket.value.getFullYear();
        $("#activeChartDate").html((day < 10 ? "0" + day : day) + "." + (month < 10 ? "0" + month : month) + "." + year);
        $("#lasttradedate").html((day < 10 ? "0" + day : day) + "." + (month < 10 ? "0" + month : month) + "." + year);
    }
}

fieldUpdate = function(updatePacket) {
    
    if ($('#chartValue').length == 0)
        return;

    var precision = 2;
    var identifier = updatePacket.dataName.substring(0, updatePacket.dataName.indexOf("."));
    var field = updatePacket.dataName.substring(updatePacket.dataName.indexOf(".") + 1);

    var row = $("#" + identifier);

    var tableCell = $("#" + updatePacket.dataName.toLowerCase().replace(".", "_"));

    var output = "";
    if (field.toUpperCase() == "RELATIVEDIFFERENCE" && updatePacket.value != null) {
        output = Number(updatePacket.value * 100).toFixed(precision) + "%";
    } else if (field.toUpperCase() == "ABSOLUTEDIFFERENCE" && updatePacket.value != null) {
        output = updatePacket.value.toFixed(precision);
    } else if (field.toUpperCase() == "CUMULATIVEVOLUME" && updatePacket.value != null) {
        output = updatePacket.value.toFixed(0);
    } else {
        if (updatePacket.type == vwdStreamerDataType.NUMBER && updatePacket.value != null) {
            output = updatePacket.value.toFixed(precision);
        } else {
            output = (updatePacket.value != null ? updatePacket.value : "--");
        }
    }

    if (updatePacket.value != updatePacket.rawValue)
        output += " (" + updatePacket.rawValue + ")";

    if (updatePacket.type == vwdStreamerDataType.TIME) {
        output = updatePacket.value.toLocaleTimeString();
        var hour = updatePacket.value.getHours();
        var minute = updatePacket.value.getMinutes();
        $("#lasttradetime").html((hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute));
        var day = updatePacket.value.getDate();
        var month = 1 + updatePacket.value.getMonth();
        var year = updatePacket.value.getFullYear();
        $("#lasttradedate").html((day < 10 ? "0" + day : day) + "." + (month < 10 ? "0" + month : month) + "." + year);
    } else if (updatePacket.type == vwdStreamerDataType.DATE) {
        output = updatePacket.value.toLocaleDateString();
    } else if (updatePacket.type == vwdStreamerDataType.DATETIME) {
        output = updatePacket.value.toLocaleString();
    }

    var oldValue = updatePacket.oldValue;
    if (oldValue == null)
        oldValue = updatePacket.value;
    if (updatePacket.type == vwdStreamerDataType.NUMBER && oldValue < updatePacket.value) {
        try {
            tableCell.html(output.replace('.', ','));
            tableCell.css('background-color', '#C1FCC7');
            if (field.toUpperCase() == "ABSOLUTEDIFFERENCE") tableCell.parent().css('background-color', '#C1FCC7');
        }
        catch (e) {
        }
    }
    else if (updatePacket.type == vwdStreamerDataType.NUMBER && oldValue > updatePacket.value) {
        try {
            tableCell.html(output.replace('.', ','));
            tableCell.css('background-color', '#FBD7D7');
            if (field.toUpperCase() == "ABSOLUTEDIFFERENCE") tableCell.parent().css('background-color', '#FBD7D7');
        }
        catch (e) {
        }
    }
    if (field.toUpperCase() == "ABSOLUTEDIFFERENCE" && output >= 0) tableCell.attr('class', 'up');
    if (field.toUpperCase() == "ABSOLUTEDIFFERENCE" && output < 0) tableCell.attr('class', 'down');

    setTimeout(function() {
        tableCell.css('background-color', '#fff'); tableCell.parent().css('background-color', '#fff')
    }, 900);

}  
