﻿var m_TopBannerTimerInterval = 5000;
var m_TopBannerIndexTable;
var m_TopBannerIndexTableBody;
var m_TopBannerInterestTable;
var m_TopBannerInterestTableBody;
var m_TopBannerCommodityTable;
var m_TopBannerCommodityTableBody;
var m_TopBannerCurrencyTable;
var m_TopBannerCurrencyTableBody;
var m_IndexInstrumentType = "Index";
var m_InterestInstrumentType = "Interest";
var m_CommodityInstrumentType = "Commodity";
var m_CurrencyInstrumentType = "Currency";
var m_IndexList;
var m_InterestList;
var m_CurrencyList;
var m_CommodityList;

function InitializePublicIndicatorTimer() {
    setTimeout("GetPublicIndicatorsFromQuoteService()", m_TopBannerTimerInterval);
}

function GetPublicIndicatorsFromQuoteService() {
    try {        
        var quoteService = new Marketmind.Web.Stocklink.Services.QuoteService();
        //Get public indicators data from service               
        quoteService.GetPublicIndicators(OnTopBannerSucceededCallback, OnTopBannerFailedCallback, null);
        //Call function again in m_TopBannerTimerInterval ms.
        setTimeout("GetPublicIndicatorsFromQuoteService()", m_TopBannerTimerInterval);
    }
    catch (e) {
        //TODO
    }
}

function OnTopBannerFailedCallback() {
    //TODO
}

function OnTopBannerSucceededCallback(e) {
    if (e.toString().length > 0) {

        //deserialize json string from service
        result = Sys.Serialization.JavaScriptSerializer.deserialize(e, true);
        m_IndexList = result[m_IndexInstrumentType];
        m_InterestList = result[m_InterestInstrumentType];
        m_CurrencyList = result[m_CurrencyInstrumentType];
        m_CommodityList = result[m_CommodityInstrumentType];

        if (m_IndexList != null) {
            UpdateTopBanner(m_IndexList);
        }

        if (m_InterestList != null) {
            UpdateTopBanner(m_InterestList);
        }

        if (m_CurrencyList != null) {
            UpdateTopBanner(m_CurrencyList);
        }

        if (m_CommodityList != null) {
            UpdateTopBanner(m_CommodityList);
        }
    }
}

function UpdateTopBanner(result) {
    var number;
    var cssFlash;
    var str;

    for (var i = 0; i < result.length; i++) {
        var quoteServiceEntry = result[i];

        var lastValueCell = document.getElementById("Last_" + quoteServiceEntry.QuoteOId);
        if (lastValueCell != null) {
            str = lastValueCell.innerHTML;
        }
        lastValueCell.innerHTML = result[i].Last;

        if (lastValueCell != null) {
            number = CompareTwoNumbers(str, result[i].Last);
            cssFlash = GetCssFlash(number);
            if (cssFlash != "") {
                lastValueCell.setAttribute("class", cssFlash);
                lastValueCell.setAttribute("className", cssFlash);
            }
            else {
                lastValueCell.removeAttribute("class", cssFlash);
                lastValueCell.removeAttribute("className", cssFlash);
            }
        }

        var changeValueCell = document.getElementById("Change_" + quoteServiceEntry.QuoteOId);
        if (changeValueCell != null) {
            str = changeValueCell.innerHTML;            
        }
        changeValueCell.innerHTML = result[i].Change;

        if (changeValueCell != null) {
            number = CompareTwoNumbers(str, result[i].Change);
            cssFlash = GetCssFlash(number);
            if (cssFlash != "") {
                changeValueCell.setAttribute("class", cssFlash);
                changeValueCell.setAttribute("className", cssFlash);
            }
            else {
                changeValueCell.removeAttribute("class", cssFlash);
                changeValueCell.removeAttribute("className", cssFlash);
            }
        }

        var percentageChangeValueCell = document.getElementById("PercentageChange_" + quoteServiceEntry.QuoteOId);
        if (percentageChangeValueCell != null) {
            str = percentageChangeValueCell.innerHTML;             
        }
        percentageChangeValueCell.innerHTML = result[i].PercentageChange + "%";

        if (percentageChangeValueCell != null) {
            number = CompareTwoNumbers(str, result[i].PercentageChange);
            cssFlash = GetCssFlash(number);
            if (cssFlash != "") {
                percentageChangeValueCell.setAttribute("class", cssFlash);
                percentageChangeValueCell.setAttribute("className", cssFlash);
            }
            else {
                percentageChangeValueCell.removeAttribute("class", cssFlash);
                percentageChangeValueCell.removeAttribute("className", cssFlash);
            }
        }
    }
    setTimeout("ClearFlashElements()", 500);
}

function ClearFlashElements() {
    ClearTopBannerFlashElements(m_IndexList);
    ClearTopBannerFlashElements(m_InterestList);
    ClearTopBannerFlashElements(m_CommodityList);
    ClearTopBannerFlashElements(m_CurrencyList);
}

function ClearTopBannerFlashElements(result) {
    for (var i = 0; i < result.length; i++) {
        var quoteServiceEntry = result[i];

        var lastValueCell = document.getElementById("Last_" + quoteServiceEntry.QuoteOId);
        lastValueCell.removeAttribute("class");
        lastValueCell.removeAttribute("className");

        var changeValueCell = document.getElementById("Change_" + quoteServiceEntry.QuoteOId);
        changeValueCell.removeAttribute("class");
        changeValueCell.removeAttribute("className");

        var percentageChangeValueCell = document.getElementById("PercentageChange_" + quoteServiceEntry.QuoteOId);
        percentageChangeValueCell.removeAttribute("class");
        percentageChangeValueCell.removeAttribute("className");
    }
}

//setStyle function
function setStyle(element, styleText) {
    if (element.style.setAttribute)
        element.style.setAttribute("cssText", styleText);
    else
        element.setAttribute("style", styleText);
}
 
function ConvertToNumber(str) {
    var value;
    //Remove middle white space in the string
    str = str.replace(String.fromCharCode(160), "");
    //Remove "&nbsp;" in the middle of the string
    str = str.replace(/(&nbsp;)*/g, '');       
    str = str.replace(/%/, "");
    if (str.replace(/-/, "").length > 0) {
        value = new Number(str.replace(",", ".")); ;
        return value;
    }
    return null;
}

function CompareTwoNumbers(str1, str2) {
    var number1 = ConvertToNumber(str1);
    var number2 = ConvertToNumber(str2);

    if (number1 != null || number2 != null) {
        if (number1 < number2)
            return 1;
        if(number1 > number2)
            return -1;
    }
    return 0;   
}

function GetCssFlash(str){
    if(str == 1)
        return "bluecell";
    if(str == -1)
        return "redcell";
    return "";
}





