﻿

$.fn.feedCollect = function (interval) {

    var url = "__Pd.ashx",
        timer = null,
        gramToOunce = 1 / 31.1034768;

    return this.each(function () {
        var el = $(this),
            holders = $(".sptPrice-item", el);

        $(".sptPrice-item:last", el).addClass("last");

        var coms = "";
        for (var i = 0; i < holders.length; i++) {
            var sym = holders.eq(i).find("td").attr("class");
            coms += sym;
            var oz = $("<td class=\"sptPrice-oz\"/>"),
                 gm = $("<td class=\"sptPrice-gram\"/>"),
                 chng = $("<td class=\"sptPrice-chng\"/>");
            holders.eq(i).find("td").addClass(".sptPrice-item-fx").text(sym.substring(3));
            holders.eq(i).append(oz).append(gm).append(chng);
            if (i < holders.length - 1) {
                coms += ",";
            }
        }

        var hDiv = $("<tr class=\"sptPrice-header\"/>");
        hDiv.append($("<th>&nbsp;</th>"));
        hDiv.append($("<th>Oz</th>"));
        hDiv.append($("<th>+/-</th>"));
        hDiv.append($("<th>%</th>"));

        hDiv.insertBefore(".sptPrice-item:first", el);

        getData();

        function getData() {
            $.post("__dta/" + url, { SM: coms }, function (xml) {
                if ($("Result", xml).text() == "true") {

                    $("PriceItem", xml).each(function () {
                        var Symbol = $("Symbol", $(this)).text(),
                            price = parseFloat($("Mid", $(this)).text()),
                            itm = $("." + Symbol, el).parent(),
                            oldprice = parseFloat($(".sptPrice-oz", itm).text());

                        $(".sptPrice-oz", itm).text(roundNumber(price, 0));

                        if (oldprice > 0) {
                            if (oldprice != price) {
                                if (oldprice < price) {
                                    try {
                                        itm.css("background-color", "#00CC00").animate({ backgroundColor: "#EEE" }, 2000);
                                    }
                                    catch (err) {

                                    }
                                }
                                else {
                                    try {
                                        itm.css("background-color", "#CC0000").animate({ backgroundColor: "#EEE" }, 2000);
                                    }
                                    catch (err) {

                                    }
                                }
                            }
                        }

                        //$(".sptPrice-gram", itm).text(roundNumber(price * gramToOunce, 2));
                        $(".sptPrice-gram", itm).text($("Change", $(this)).text());
                        $(".sptPrice-chng", itm).text($("Percentage", $(this)).text());

                    });


                    timer = setTimeout(function () { getData(); }, 5000);
                }
                else {
                    clearTimeout(timer);
                }
            });
        }

        function roundNumber(num, dec) {
            var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
            return result;
        }

    })
}
