﻿var CustomWidgets = {
    ClearList: function (id) {
        $('#' + id).html('');
    },
    AddSymbol: function (id, symbol, companyName, type) {
        if (companyName.length > 22) {
            companyName = companyName.substring(0, 22) + '...';
        }
        var row = '<span class="' + type + '" id="symbol_' + symbol + '">' + companyName + ' <a  onclick="CustomWidgets.RemoveSymbol(\'' + symbol + '\')">Remove</a><br/></span>';
        $('#' + id).append(row);
    },
    RemoveSymbol: function (symbol) {
        var selector = symbol.indexOf("^") != -1 ? '#symbol_\\' + symbol : '#symbol_' + symbol;
        $(selector).remove();
    },
    SetHiddenSymbols: function () {
        $('#hStocksList').val('');
        $('#hIndicesList').val('');

        // add indices
        var val = '';
        $('.index').each(function (i) {
            var indexID = $(this).attr('id');
            var items = indexID.split("_");
            var symbol = items[1];
            val += symbol + ',';
        });
        val = val.substr(0, val.length - 1);
        $('#hIndicesList').val(val);

        // add stocks
        val = '';
        $('.stock').each(function (i) {
            var stockID = $(this).attr('id');
            var items = stockID.split("_");
            var symbol = items[1];
            val += symbol + ',';
        });
        val = val.substr(0, val.length - 1);
        $('#hStocksList').val(val);
    },
    SetSymbol: function (symbol, shortSymbol, target) {
        var elName = 'symbol' + target;
        $('.' + elName).html(symbol);
        $('.' + elName).attr('href', 'http://www.wallstreetsurvivor.com/Public/Research/Quotes.aspx?symbol=' + shortSymbol);
    },
    SetPrices: function (index, target) {
        var elName = 'price' + target;
        $('.' + elName).html(index.Price.toFixed(2));
        elName = 'change' + target;
        $('.' + elName).html(index.PriceChange.toFixed(2));

        if (index.PriceChange >= 0) {
            $('.' + elName).css('color', '#59d124');
        }
        else {
            $('.' + elName).css('color', '#b3373a');
        }
    },
    DrawHeader: function (color) {
        if (color && color != null) {
            $('.widgetPreview .header').css("background-color", "#" + color);
        }
    },
    DrawRow: function (color) {
        if (color && color != null) {
            $('.widgetPreview .row td').css("background-color", "#" + color);
        }
    },
    DrawAltRow: function (color) {
        if (color && color != null) {
            $('.widgetPreview .altRow td').css("background-color", "#" + color);
        }
    },
    DrawBorder: function (color) {
        if (color && color != null) {
            $('.widgetPreview .border').css("border", "solid #" + color + " 1px");
        }
    }
}
