﻿$(document).ready(function() {
    $('.playbutton').each(function() {
        $(this).prepend('<div class="playbuttonImage"></div>');
        var click = $(this).find('img').attr("onclick");
        
        $(this).find('.playbuttonImage').click(eval(click));
        $(this).find('.playbuttonImage').css({
            marginLeft: ($(this).width() / 2 - 50) + 'px',
            marginTop: ($(this).height() / 2 - 50) + 'px'
        });
    });

    // Select Tab Index Grafiek
    $("#selectIndexChartAEX").click(function() {
        $("#chartValue").text("");
        $("#indexTrend").text("");
        $("#chartTime").text("");
        setIndexChartAEX(this);
    });
    $("#selectIndexChartAMX").click(function() {
        $("#indexTrend").text("");
        $("#chartValue").text("");
        $("#chartTime").text("");
        setIndexChartAMX(this);
    });
    $("#selectIndexChartAScX").click(function() {
        $("#indexChart")[0].src = $("#chartBaseUrl").val() + "/ChartServer/chart.ashx?ctype=1&id=99178&daterange=today&name=AScX";
        $("#indexLink").attr('href', '/Koersen/ASCX/p1');
        $("#indexLink").text('Realtime AScX');
        $("#indicePageLink").attr('href', '/indices/ascx');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
        $("#chartValue").text("");
        $("#chartTime").text("");
        setIndexTrendValue('ASCX');
    });


    $("#selectIndexChartDowJones").click(function() {
        $("#indexChart")[0].src = $("#chartBaseUrl").val() + "/ChartServer/chart.ashx?ctype=1&id=330015505&daterange=today&name=Dow";
        $("#indexLink").attr('href', '/indices/dowjones');
        $("#indexLink").text('Dow Jones');
        $("#indicePageLink").attr('href', '/indices/dowjones');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
        setIndexTrendValue('DOW');
    });

    $("#selectIndexChartSP500").click(function() {
        $("#indexChart")[0].src = "http://dchart.charting.eurobench.nl/tchart/tchartcached.aspx?user=ReedBusiness&layout=homepage&startdate=today&enddate=today&format=image%2fgif&width=191&height=159&issue=190015514&res=intraday&IssueName=SP500&InstrumentType=";
        $("#indexLink").attr('href', '/indices/sp500');
        $("#indexLink").text('S&P 500');
        $("#indicePageLink").attr('href', '/indices/sp500');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
        setIndexTrendValue('SP500');
    });

    $("#selectIndexChartNasdaq").click(function() {
        $("#indexChart")[0].src = $("#chartBaseUrl").val() + "/ChartServer/chart.ashx?ctype=1&id=330016055&daterange=today&name=NASDAQ";
        $("#indexLink").attr('href', '/indices/nasdaq');
        $("#indexLink").text('Nasdaq');
        $("#indicePageLink").attr('href', '/indices/nasdaq');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
        setIndexTrendValue('NASDAQ');
    });

    // Select Tab Index Treemap
    $("#selectTreemapAEX").click(function() {
        $("#indexTreemap")[0].src = $("#chartBaseUrl").val() + "/ChartServer/treemap.ashx?ctype=1&index=AEX";
        $("#treemaplink").attr('href', '/treemap/aex');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });
    $("#selectTreemapAMX").click(function() {
        $("#indexTreemap")[0].src = $("#chartBaseUrl").val() + "/ChartServer/treemap.ashx?ctype=1&index=AMX";
        $("#treemaplink").attr('href', '/treemap/amx');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });
    $("#selectTreemapAScX").click(function() {
        $("#indexTreemap")[0].src = $("#chartBaseUrl").val() + "/ChartServer/treemap.ashx?ctype=1&index=AscX";
        $("#treemaplink").attr('href', '/treemap/ascx');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });

    // Select Tab Index List
    $("#selectIndexListAEX").click(function() {
        $("#indexList").load('home/exchangemarket/aex');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });
    $("#selectIndexListAMX").click(function() {
        $("#indexList").load('home/exchangemarket/amx');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });
    $("#selectIndexListAScX").click(function() {
        $("#indexList").load('home/exchangemarket/ascx');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });

    // Select Tab News List
    $("#selectNewsDomestic").click(function() {
        $("#listNews").load('home/latestnews/binnenland');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });
    $("#selectNewsForeign").click(function() {
        $("#listNews").load('home/latestnews/buitenland');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });
    $("#selectMarketTalk").click(function() {
        $("#listNews").load('home/latestnews/markettalk');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });

    // Select Tab Gainers/Losers List
    $("#selectGainersLosersAEX").click(function() {
        $("#listGainersLosers").load('home/gainersandlosers/aex');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });
    $("#selectGainersLosersAMX").click(function() {
        $("#listGainersLosers").load('home/gainersandlosers/amx');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });
    $("#selectGainersLosersAScX").click(function() {
        $("#listGainersLosers").load('home/gainersandlosers/ascx');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });

    // Select Tab Opinion List
    $("#selectOpinionExpert").click(function() {
        $("#listOpinion").load('home/opinions/latest');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });
    $("#selectOpinionTopFeedback").click(function() {
        $("#listOpinion").load('home/opinions/mostactivecommented');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });

    // Select Tab Business and Finance List
    $("#selectBusiness").click(function() {
        $("#listBusinessFinance").load('home/businessandfinance/BusinessEnFinance');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });
    $("#selectFinance").click(function() {
        $("#listBusinessFinance").load('home/businessandfinance/Financieel');
        $(this).parent('li').siblings().removeClass('active');
        $(this).parent('li').addClass('active');
    });

    // Set Defaults
    setIndexTrendValue('AEX');
    if ($("#indexLink").text() != 'Realtime AEX') setIndexTrendValue('DOW');
    setIndexRealtimeValue('Aex');
    setIndexRealtimeValue('Amx');
    setIndexValueSPF500();
    setIndexValueSP500();
});

function setIndexChart() {
    if ($("#indexLink").text() == 'Realtime AEX') setIndexChartAEX(null);
    if ($("#indexLink").text() == 'Realtime AMX') setIndexChartAMX(null);
}

function setIndexChartAEX(tab) {
    $("#indexChart")[0].src = $("#chartBaseUrl").val() + "/ChartServer/chart.ashx?ctype=1&id=12272&daterange=today&name=AEX";
    $("#indexLink").attr('href', '/Koersen/AEX/p1');
    $("#indexLink").text('Realtime AEX');
    $("#indicePageLink").attr('href', '/indices/aex');
    if (tab) $(tab).parent('li').siblings().removeClass('active');
    if (tab) $(tab).parent('li').addClass('active');
    if ($("#indexTrend").text() == "")
        setIndexTrendValue('AEX');
}

function setIndexChartAMX(tab) {
    $("#indexChart")[0].src = $("#chartBaseUrl").val() + "/ChartServer/chart.ashx?ctype=1&id=12222&daterange=today&name=AMX";
    $("#indexLink").attr('href', '/Koersen/AMX/p1');
    $("#indexLink").text('Realtime AMX');
    $("#indicePageLink").attr('href', '/indices/amx');
    if (tab) $(tab).parent('li').siblings().removeClass('active');
    if (tab) $(tab).parent('li').addClass('active');
    if ($("#indexTrend").text() == "")
        setIndexTrendValue('AMX');
}

function setIndexTrendValue(indexName) {
    if (indexName == "SP500") {
        setIndexTrendValueSP500();
        return;
    }

    $.ajax({
        type: "GET",
        url: "/Services/ContentHandler.ashx?action=GetIndexTrendValue&indexname=" + indexName,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            if (msg.TrendText == "ONVERANDERD") msg.TrendText = "GELIJK";
            $("#indexTrend").text(msg.TrendText);
            if (msg.Value == "0,00") msg.Value = "0";
            $("#indexRelDif").text(msg.Value + '%');
            var value = parseFloat(msg.Value.replace(",", "."));
            if (value < 0) $("#indexTrend").parent().children().css('color', '#da0000');
            if (value > 0) $("#indexTrend").parent().children().css('color', '#009f0b');
        }
    });
}

function setIndexTrendValueSP500() {
    $.getJSON(
        "http://services.eurobench.com/www.beurs.nl/quote?alfa=sp&call=?",
        function(msg) {
            var trendText = "GELIJK";
            if (msg.PercentageDifference) { if (msg.PercentageDifference > 0) trendText = "STIJGING" };
            if (msg.PercentageDifference) { if (msg.PercentageDifference < 0) trendText = "DALING" };
            $("#indexTrend").text(trendText);
            if (msg.PercentageDifference != null)
                $("#indexRelDif").text(formatNumber(msg.PercentageDifference * 100) + '%');
            else
                $("#indexRelDif").text('0,00 %');
            if (msg.PercentageDifference < 0) $("#indexTrend").parent().children().css('color', '#da0000');
            if (msg.PercentageDifference > 0) $("#indexTrend").parent().children().css('color', '#009f0b');
        }
        );
}

function setIndexValueSPF500() {
    $.getJSON(
        "http://services.eurobench.com/www.beurs.nl/quote?alfa=spf&call=?",
        function(msg) {
            if (!msg.NetPriceDifference) {
                msg.NetPriceDifference = msg.LastPrice - msg.PreviousClosePrice;
            }
            if (!msg.PercentageDifference) {
                msg.PercentageDifference = msg.NetPriceDifference / msg.PreviousClosePrice;
            }
            if (msg.PercentageDifference) $("#tickerSp500fPercentage").text(formatNumber(100 * msg.PercentageDifference) + '%');
            if (msg.NetPriceDifference)
                $("#tickerSp500fValue").text(formatNumber(msg.LastPrice) + " (" + formatNumber(msg.NetPriceDifference) + ")");
            else
                $("#tickerSp500fValue").text(formatNumber(msg.LastPrice) + " (0,00)");

            $("#tickerSp500fPercentage").parent().parent().removeClass("up");
            $("#tickerSp500fPercentage").parent().parent().removeClass("down");
            if (msg.PercentageDifference && msg.PercentageDifference < 0) {
                $("#tickerSp500fPercentage").parent().parent().addClass("down");
            }
            if (msg.PercentageDifference && msg.PercentageDifference > 0) {
                $("#tickerSp500fPercentage").parent().parent().addClass("up");
            }
        }
        );
}

function setIndexValueSP500() {
    $.getJSON(
        "http://services.eurobench.com/www.beurs.nl/quote?alfa=sp&call=?",
        function(msg) {

            if (msg.PercentageDifference) $("#tickerSp500Percentage").text(formatNumber(100 * msg.PercentageDifference) + '%');

            if (msg.NetPriceDifference)
                $("#tickerSp500Value").text(formatNumber(msg.LastPrice) + " (" + formatNumber(msg.NetPriceDifference) + ")");
            else
                $("#tickerSp500Value").text(formatNumber(msg.LastPrice) + " (0,00)");

            $("#tickerSp500Percentage").parent().parent().removeClass("up");
            $("#tickerSp500Percentage").parent().parent().removeClass("down");
            if (msg.PercentageDifference < 0) {
                $("#tickerSp500Percentage").parent().parent().addClass("down");
            }
            if (msg.PercentageDifference > 0) {
                $("#tickerSp500Percentage").parent().parent().addClass("up");

            }
        }
        );
}

var count = 0;
function setIndexRealtimeValue(indexName) {
    count++;
    $.ajax({
        type: "GET",
        url: "/Services/ContentHandler.ashx?action=GetRealtimeValue&indexname=" + indexName,
        data: "",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // setTickerValue(indexName, msg);
        }
    });
    setTimeout(function() { setIndexRealtimeValue(indexName); setIndexChart(); }, 30000);
}

function setTickerValue(ticker, quote) {
    var newDiff = formatNumber(quote.PercentagePriceDifference) + "%";
    var oldDiff = $("#ticker" + ticker + "Percentage").text();
    if (newDiff == oldDiff) return;

    $("#ticker" + ticker + "Percentage").text(formatNumber(quote.PercentagePriceDifference) + "%");
    $("#ticker" + ticker + "Value").text(formatNumber(quote.LastPrice) + " (" + formatNumber(quote.NetPriceDifference) + ")");

    $("#ticker" + ticker + "Percentage").parent().animate({ opacity: 0.7 }, 500);
    $("#ticker" + ticker + "Percentage").parent().animate({ opacity: 1.0 }, 100);

    $("#ticker" + ticker + "Percentage").parent().parent().removeClass("up");
    $("#ticker" + ticker + "Percentage").parent().parent().removeClass("down");
    if (quote.NetPriceDifference < 0) {
        $("#ticker" + ticker + "Percentage").parent().parent().addClass("down");
    }
    if (quote.NetPriceDifference > 0) {
        $("#ticker" + ticker + "Percentage").parent().parent().addClass("up");
    }
}

function formatNumber(value) {
    return value.toFixed(2).replace(".", ",");
}
