﻿if (!window.console) {
    console = { log: function () { } };
}


if (!window.MVD) {
    MVD = {};
}


(function ($) {
    MVD.img_loading = new Image();
    MVD.img_loading.src = 'images/loading.gif';

    MVD.Perfil = { };
    MVD.MAX_ACT_HEIGHT = 50;
    
    MVD.getUrlAnchor = function () {
        var url = window.location.href;
        var p = url.split("#");        
        return p[1];               
    }    

    MVD.Actividad = function (divid, usrid, app, masbtn) {
        this.container = $("#" + divid);
        this.usrid = usrid;
        this.separator = this.container.html();
        this.container.html('').show();
        this.app = app || 5;
        this.btn = masbtn;
        this.comCal = new MVD.ComCal();
        var that = this;
        masbtn.click(function () {
            masbtn.hide();
            that._more();
            return false;
        });
    };

    $.extend(MVD.Actividad.prototype, {
        get : function (what, onFinish) {
            this.onFinish = onFinish;
            this.container.html('<img src="' + MVD.img_loading.src + '" />');
            this.url = (what === "seguidos") ? "aparticipaciones.aspx" : "aactividadreciente.aspx";
            this.pag = 1;
            this._getData();
        },

        _getData : function () {
            var that = this;
            var pag = this.pag;
            $.ajax({
                type: "POST",
                url: this.url,
                data: { UsrRegId: that.usrid, pag: pag, app: that.app },
                dataType: "json",
                success: function (data) { that._show(data, pag); },
                error: function () { that._show(); }
            });
        },

        _fixHeight : function (cont) {
            if (MVD.Expander) {           
                var that = this;
                var h = that.contractedHeight;
                setTimeout(function () {  
                    $('.autocontracting', cont).each(function (i, el) {
                        // console.log("div=", el);
                        if (!h) {
                            h = that.contractedHeight = MVD.getLinesHeight($('.autocontract', el), 3);                            
                        }                        
                        MVD.Expander($('.autocontract', el), $('.btnExpand', el), that.contractedHeight);                    
                    });                                   
                }, 100);
            }
        /*
            $('.autocontract', cont).each(function (i, el) {
                MVD.fixHeight($(el), MVD.MAX_ACT_HEIGHT);                
            });
            */
        },
        

        _show : function (data, pag) {
            if (data && data.length && data.length > 0) {
                var newHtml = data.join(this.separator);                
                if (pag > 1) {
                    var nc = $("<div></div>").hide().html(newHtml);

                    this.container.append(this.separator).append(nc);
                    this._fixHeight(nc);
                    var that =this;
                    nc.slideDown("slow");
                } else {
                    this.container.html(newHtml);
                    this._fixHeight(this.container);
                }
                this.comCal.getAndReplaceCals(this.container);
                if (data.length == this.app) {
                    this.btn.show();
                }
            } else {
                if (pag == 1) {
                    this.container.text("No hay participaciones.");
                }
                this.btn.hide();
            }
            if (this.onFinish) {
                this.onFinish();
                delete this.onFinish;
            }
            if (this.onLoad) {
                this.onLoad();
            }
        },

        _more : function () {
            this.pag ++;
            this._getData();
        }
    });

    $(document).ready(function () {
        var btn = $('#btnMasDatos');
        var img = $('img', btn);
        var re_off = /_off/g;
        var re_on = /_on/g;

        if (!$.trim($('#masDatos').text())) {
            btn.hide();
        } else {
            btn.click(function () {
                $('#masDatos').slideToggle("slow");
                var src = img.attr('src');
                if (re_off.test(src)) {
                    src = src.replace(re_off, '_on');
                } else {
                    src = src.replace(re_on, '_off');
                }
                img.attr('src', src);
                return false;
            });
        }
    });

}) (jQuery);






