(function ($) {

    if (!window.MVD) {
        window.MVD = { };
    }

    MVD.ComCal = function ( ) {
        this.cals = {};
        this.pend = {};
    };

    $.extend(MVD.ComCal.prototype, {

         _getAppId: function (str) {
            var coded = (str || "").split("/");
            return (coded.length == 2) ? { app: coded[0], id: coded[1] } : null;
         },

         get: function (html) {
            var apps = {};
            var that = this;

            var h = $('<div></div>').html(html);

            $(".comment-rating", h).each(function (i, e) {
                var coded = that._getAppId(e.innerHTML);
                if (coded) {
                    var applist = apps[coded.app] || [];
                    applist.push(coded.id);
                    apps[coded.app] = applist;
                }
            });
            h.remove();

            that._getCals(apps);
         },

         getAndReplaceCals: function (bignode) {
            var apps = {};
            var that = this;

            $(".comment-rating", bignode).each(function (i, e) {
                var c = e.innerHTML;                
                var coded = that._getAppId(c);
                if (coded) {
                    var applist = apps[coded.app] || [];
                    applist.push(coded.id);
                    apps[coded.app] = applist;
                    that.pend[c] = e.parentNode;
                }
            });
            that._getCals(apps);
         },

         _saveCals: function (app, data) {
            var that = this;
            var c = that.cals[app] || { };
            $.each(data, function (i, e) {
                c[e.id.join(",")] = e.cal;
            });
            that.cals[app] = c;
         },

         _processPending: function () {
            var that = this;
            $.each(that.pend, function (i, e) {
                that.configNode(e);
            });
         },

         _getCals: function (apps) {
            var that = this;

            $.each(apps, function (app, a) {
                $.ajax({
                    type: "POST",
                    url: "aappajaxproxy.aspx",
                    data: { AplCod:app, program:"ancomgetcal.aspx", comlist: a.join(";") },
                    dataType: "json",
                    success: function (data) { that._saveCals(app, data); that._processPending() },
                    error: function () {  }
                });
            });
         },

         _getCal: function (fullid) {
            var that = this;
            var c = that._getAppId(fullid);

            // console.log("_getCal", fullid, c);
            if (!c) {
                return null;
            }
            var calsApp = that.cals[c.app];
            if (calsApp && (typeof calsApp[c.id] == 'number')) {
                return calsApp[c.id];
            } else {
                return null;
            }
         },

         configNode: function (node) {
            var that = this;
            var e = $(".comment-rating:first", node);
            if (e.length) {
                var code = e.html()
                var cal = that._getCal(code);
                // console.log("calificacion de:" , e.html(), "=", cal);
                if (typeof cal == 'number') {
                    delete that.pend[code];
                    e.html(cal).removeClass("comment-rating").addClass((cal > 0) ? 'comment_pos' : (cal <= -4) ? 'comment_neg' : 'comment_med');
                } else {
                    that.pend[code] = node;
                }
            }
         }
    });


}) (jQuery);