﻿var first = true;
var ctr = 0;
var timeOut = 4000;
var hiliteTimeout = 2000;

 $("body").ajaxError(function(event, request, settings){
   // error getting quotes, keep trying
   setTimeout("updateQuotes()",timeOut); 
 });


function updateQuotes() {
    var symbols = [];
    var idx = 0;
    currReqs = 0;
    
    
    $('.ticker').each(function() { var currIdx = parseInt(idx / 100); if (symbols[currIdx] == null) symbols[currIdx]=''; symbols[currIdx] += this.id.substring(0,this.id.indexOf("_")) + ',';idx++; } );
    
    if (symbols.length > 0 && symbols[0].length > 0 ) {
        for (var i = 0; i < symbols.length; i++) { // 
            
            if ( i == (symbols.length - 1) ) { 
                jQuery.post("/GetQuotes.ashx","symbols=" + symbols[i],writeQuotesFinal,"json"); 
            }
            else {
                jQuery.post("/GetQuotes.ashx","symbols=" + symbols[i],writeQuotes,"json");
            }
        }
    }
}

function writeQuotesFinal(quotes) {
    ctr++;
    writeQuotes(quotes);
    if ( ctr < 200 ) { setTimeout("updateQuotes()",timeOut); }
}


function writeQuotes(quotes) {
    $('.ticker').each(function() {
        var symb = this.id;
        var currq = parseFloat(this.innerHTML.replace("$", ""));
        //var newq = quotes[this.id.substring(0, this.id.indexOf("_"))];
        var newQuote = quotes[this.id.substring(0, this.id.indexOf("_"))];

        var arrayReturned;
        arrayReturned = newQuote.indexOf("|");
        if (arrayReturned != -1)
        {
            var newQuoteArray = newQuote.split("|");
            var newq = parseFloat(newQuoteArray[0]);
            var newChg = parseFloat(newQuoteArray[1]);
            var newRtn = parseFloat(newQuoteArray[2]);
            var prClose = parseFloat(newQuoteArray[3]);
        }
        else
        {    
            var newq = 0;
            var newChg = 0;
            var newRtn = 0;
            var prClose = 0;
        }    

        //document.write("the value from the handler!!!! " + newq);

        var hColor = "#ffff99";

        if (newq < 0) return;
        if (!isNaN(currq)) {
            if (currq > newq) hColor = "#db4228";
            else if (currq < newq) hColor = "#129f49";
            else if (currq = newq) hColor = "transparent";
            else return;
        }

        $("#\\" + symb).text(newq.numberFormat("$0.00"));
        if (ctr > 0) $("#\\" + symb).effect('highlight', { color: hColor }, hiliteTimeout);
        writeReturn(symb, 0, newq, hColor);
        writeReturn(symb, 1, newq, hColor);
        writeReturn(symb, 2, newq, hColor);
        writeChange(symb, 0, newq, hColor, newChg, newRtn, prClose);
//        writeChange(symb, 1, newq, hColor);
//        writeChange(symb, 2, newq, hColor);
    });
}

function writeChange(symb, idx, curr, highlightColor, newChg, newRtn, prClose) {
    var el = $("#\\" + symb + "tickerChg" + idx)[0];
    if (el == null) return;

    el = $(el);
    if (prClose != 0)
    {
        var chg = newChg
        var chgEx = chg.numberFormat("0.00");
        chgEx = chgEx + ' (' + (newRtn/100).numberFormat("0.0%") + ')';
    }
    else
    {
        var chg = getChange(curr, parseFloat($("#\\" + symb + "tickerPr" + idx).text().replace("$", "")));
        var chgEx = chg.numberFormat("0.00");
        chgEx = chgEx + ' (' + getReturn(curr, parseFloat($("#\\" + symb + "tickerPr" + idx).text().replace("$", ""))) + ')';
    }
    
    if (chg > 0) {
        el.text("+" + chgEx);
        $(el).addClass("upText").removeClass("downText");
    }
    else if (chg < 0) {
        el.text(chgEx);
        $(el).addClass("downText").removeClass("upText");
    }
    else {
        el.text(chgEx);
        $(el).removeClass("upText").removeClass("downText");
    }

    if (ctr > 0) $("#\\" + symb + "tickerChg" + idx).effect('highlight', { color: highlightColor }, hiliteTimeout);
    
}

function getChange(curr,prior) {
    if ( isNaN(curr) ) return 0;
    if ( isNaN(prior) ) return 0;
    return curr - prior;
}

function writeReturn(symb, idx, curr, highlightColor) {
    $("#\\" + symb + "tickerRet" + idx).text(getReturn(curr, parseFloat($("#\\" + symb + "tickerPr" + idx).text().replace("$", ""))));
    if (ctr > 0) $("#\\" + symb + "tickerRet" + idx).effect('highlight', { color: highlightColor }, hiliteTimeout);

}

function getReturn(curr, prior) {
    if ( prior == 0 ) {
        return "N/A";
    }
    if ( window.pctFormatOverride ) 
    {
        return ( ((curr - prior) / prior).numberFormat(window.pctFormatOverride) );
    }
    if (curr < prior) 
    {
        return ( (((curr-prior) / prior)*-1).numberFormat("0.0%") );
    }
       
    return ( ((curr - prior) / prior).numberFormat("0.0%") );
}