﻿var issues = new Array();
var fields;
var streamer = new vwdStreamer();
var aexSubscription;
var chartSubscription;
var tableSubscription;

$(document).ready(function() {
    var excludeUrls = ["/koersen/aex", "/koersen/amx", "/koersen/ascx", "/indices/amx", "/indices/aex"];
    for (url in excludeUrls) {
        // conflicting streamers on these urls
        if (document.URL.toLowerCase().indexOf(url) > -1) return;
    }

    streamer.initialize("beurs.nl", "streaming.beurs.nl");
    streamer.connect();

    // Register the subscriptions
    aexSubscription = streamer.getSubscription(updateAex);
    chartSubscription = streamer.getSubscription(updateChart);
    tableSubscription = streamer.getSubscription(fieldUpdate);

    fields = new Array("LastPrice", "LastTime", "CumulativeVolume", "HighPrice", "LowPrice", "RelativeDifference", "AbsoluteDifference", "PreviousClosePrice");
    // Register the aex subscription
    fieldsToSubscribe = new Array();
    for (fieldIndex in fields) {
        fieldsToSubscribe[fieldsToSubscribe.length] = "12272." + fields[fieldIndex];
    }
    aexSubscription.subscribe(fieldsToSubscribe);

    // Register the chart subscriptions
    for (fieldIndex in fields) {
        // AEX subscription is already available in our subscription set. So only add the AMX specific fields
        fieldsToSubscribe[fieldsToSubscribe.length] = "12222." + fields[fieldIndex];
    }
    chartSubscription.subscribe(fieldsToSubscribe);

    resetStream();
});

resetStream = function() {
    // Release the current fields that are registered for this subscription
    tableSubscription.unsubscribeAllFields();

    // Reset the fields array to which we want to subscribe
    fields = new Array("LastPrice", "LastTime", "CumulativeVolume", "HighPrice", "LowPrice", "RelativeDifference", "AbsoluteDifference", "PreviousClosePrice");

    // Subscribe to the issues/fields that are currently located in the issues/fields arrays
    fieldsToSubscribe = new Array();
    for (issueIndex in issues) {
        for (fieldIndex in fields) {
            fieldsToSubscribe[fieldsToSubscribe.length] = issues[issueIndex] + "." + fields[fieldIndex];
        }
    }
    tableSubscription.subscribe(fieldsToSubscribe);
}

updateAex = function(updatePacket) {
    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) + "%";
        $("#tickerAexPercentage").html(output.replace('.', ','));
    } else if (field.toUpperCase() == "ABSOLUTEDIFFERENCE" && updatePacket.value != null) {
        output = updatePacket.value.toFixed(precision);
        $("#tickerAexAbsoluteDifference").html(output.replace('.', ','));
        if (updatePacket.value < 0) {
            $("#tickerAexPercentage").parent().parent().attr("class", "down");
        }
        if (updatePacket.value > 0) {
            $("#tickerAexPercentage").parent().parent().attr("class", "up");
        }
    } else if (field.toUpperCase() == "LASTPRICE" && updatePacket.value != null) {
        output = updatePacket.value.toFixed(precision);
        $("#tickerAexValue").html(output.replace('.', ','));
        $("#tickerAexPercentage").parent().animate({ opacity: 0.7 }, 500);
        $("#tickerAexPercentage").parent().animate({ opacity: 1.0 }, 100);
    }
}
