﻿(function ($) {

   MVD.UsrList = function (container) {
        if (container) {
            this.cont = container;
            this.template = container.children().clone();        
        }
    }
        
    $.extend(MVD.UsrList.prototype, {
    
        _doReplace: function (html, usr) {
            html = html.replace(/@UsrRegId@/g, '' + usr.id);
            html = html.replace(/@UsrRegNick@/g, usr.nick);
            return html
        },
        
        display : function (data) {            
            var that = this;
            this.cont.css("height", "").empty();            
                        
            if (!data) return;
            
            if (data.length == 0) {
                this.showEmpty();
            } else {            
                $.each(data, function (i, d) {
                    var node = that.template.clone();                
                    var html = node.html();                                    
                    node.html(that._doReplace(node.html(), d));                  
                    node.appendTo(that.cont);
                    that.configNode(node, d);               
                });
            }
            
            if (this.onLoad) {
                this.onLoad();
            }
        },        
        configNode : function (node, usr) {
        
        },
        showMessage : function (msg) {
            this.cont.css("height", "25px").html('<div style="text-align:center; font-weight:bold">' + msg + '</div>');
        },
        showEmpty : function () {
            this.showMessage("No hay usuarios");            
        }
    });
    
    MVD.UsrSigo = function (container, onLoad) {        
        MVD.UsrList.call(this, container);   
        this.onLoad = onLoad;
    };   
    MVD.UsrSigo.prototype = new MVD.UsrList();
       
    $.extend(MVD.UsrSigo.prototype, {    
        // prototype : MVD.UsrList.prototype
        
        /*
        _doReplace: function (html, usr) {        
            return MVD.UsrList.prototype._doReplace(html, usr);
        },
        */   

        _configNode : function (node, id, mail, nick) {
            var that = this;
        
            $('.eliminar', node).unbind("click").click(function () {
                that.desuscribir(id, node); 
                return false;                    
            });      
            
            var img = $('.mail img', node);
            var src = img.attr('src');
                  
            if (mail) {
                $('.mail a', node).unbind("click").click(function () {
                    that.setRecibirMail(id, node, 0, nick);
                    return false;
                }).attr("title", "Clic aquí para dejar de recibir mails de " + nick);
                img.attr('src', src.replace(/_off/g, '_on'));                
            } else {
                $('.mail a', node).unbind("click").click(function () {               
                    that.setRecibirMail(id, node, 1, nick);
                    return false;
                }).attr("title", "Clic aquí para recibir mails de " + nick);
                img.attr('src', src.replace(/_on/g, '_off'));
            }                
        },
        
        configNode : function (node, usr) {
            this._configNode(node, usr.id, usr.mail, usr.nick);
        },
        
        resultadoCambioMail: function (data, node, usuario, activar, nick) {
            if (data) {
                this._configNode(node, usuario, activar, nick);
            }        
        },
        
        setRecibirMail : function(usuario, node, activar, nick) {
            var that = this;
            $.ajax({
                type: "POST",
                url: "aseguimientosetrecepcionmail.aspx",
                data: { UsrRegId: usuario, activar: activar },
                dataType: "json",
                success: function (data) { that.resultadoCambioMail(data, node, usuario, activar, nick); },
                error: function () {  }
            });
        },
                
        resultadoDesuscripcion : function (usrid, res, node) {
            if (res && (res >= 0)) {
                this.changes = true;
                node.slideUp("slow");                
            }                    
        },
        
        changed : function () {
            return this.changes;
        },
        
        desuscribir : function(usuario, node) {
            var that = this;
            $.ajax({
                type: "POST",
                url: "aseguimientosuscribir.aspx",
                data: { UsrRegId: usuario, accion: 'D' },
                dataType: "json",
                success: function (data) { that.resultadoDesuscripcion(usuario, data, node); },
                error: function () { that.resultadoDesuscripcion(usuario, null, node); }
            });
        },
                
        get : function () {           
            var that = this;        
            that.changes = false;
            $.ajax({
                type: "GET",
                url: "aseguidos.aspx",
                dataType: "json",
                success: function (data) { that.display(data); },
                error: function () { MVD.errorPopup.showText("No se pudo obtener la información, vuelva a intentarlo más tarde"); }
            });
        },

        showEmpty : function () {
            this.showMessage("No sigues a nadie");
        }
    });       
  
    
    MVD.UsrMeSiguen = function (container, onLoad) {
        MVD.UsrList.call(this, container);     
        this.onLoad = onLoad;
    };
    MVD.UsrMeSiguen.prototype = new MVD.UsrList();
    
    $.extend(MVD.UsrMeSiguen.prototype, {  
        //prototype : MVD.UsrList.prototype,
        
        configNodeBloquear : function (node, id) {
            var that = this;
            var btn = $('.bloquear', node);
            
            btn.unbind('click').click(function () {
                that.bloquear(id, node, 'B'); 
                return false;                    
            });
            var img = $('img', btn);
            var src = img.attr('src');
            img.attr('src', src.replace(/_on/g, '_off'));                    
        },
        
        configNodeDesbloquear : function (node, id) {
            var that = this;
            var btn = $('.bloquear', node);
            
            btn.unbind('click').click(function () {
                that.bloquear(id, node, 'D'); 
                return false;                    
            });
            var img = $('img', btn);
            var src = img.attr('src');
            img.attr('src', src.replace(/_off/g, '_on'));                    
        },
        
        
        configNode : function (node, d) {                     
            if (d.bloq) {
                this.configNodeDesbloquear(node, d.id);
            } else {                
                this.configNodeBloquear(node, d.id);
            }                     
        },
                
        bloquear : function(usuario, node, accion) {
            var that = this;
            $.ajax({
                type: "POST",
                url: "aseguimientobloquear.aspx",
                data: { UsrRegId: usuario, accion: accion },
                dataType: "json",
                success: function (data) { that.resultadoCambioBloqueo(data, node, usuario, accion ); },
                error: function () { that.resultadoCambioBloqueo(null, node, usuario, accion); }
            });
        },
        
        resultadoCambioBloqueo : function (data, node, usuario, accion) {
            if (data == 1) {
                this.changes = true;
                if (accion == 'D') {
                    this.configNodeBloquear(node, usuario);
                } else {
                    this.configNodeDesbloquear(node, usuario);
                }
            }
        },
        
        changed : function () {
            return this.changes;
        },
        
                
        get : function () {
            var that = this;       
            that.changes = false;
            $.ajax({
                type: "GET",
                url: "amesiguen.aspx",
                dataType: "json",
                success: function (data) { that.display(data); },
                error: function () { MVD.errorPopup.showText("No se pudo obtener la información, vuelva a intentarlo más tarde"); }
            });
        },
        
        showEmpty : function () {
            this.showMessage("Nadie te sigue");
        }
    }); 
    
    MVD.UsrSiguenA = function (container, usrid) {        
        MVD.UsrList.call(this, container);     
        this.usrid = usrid;
    };
    MVD.UsrSiguenA.prototype = new MVD.UsrList();
    
    $.extend(MVD.UsrSiguenA.prototype, {  
                                     
        get : function () {
            var that = this;        
            $.ajax({
                type: "POST",
                url: "asiguena.aspx",
                data: { UsrRegId: that.usrid },
                dataType: "json",
                success: function (data) { that.display(data); },
                error: function () { MVD.errorPopup.showText("No se pudo obtener la información, vuelva a intentarlo más tarde"); }
            });
        },
        
        showEmpty : function () {
            this.showMessage("Nadie lo sigue.");
        }
        
    });
    
    MVD.UsrSeguidosPor = function (container, usrid) {        
        MVD.UsrList.call(this, container);     
        this.usrid = usrid;
    };
    MVD.UsrSeguidosPor.prototype = new MVD.UsrList();
    
    $.extend(MVD.UsrSeguidosPor.prototype, {                
        get : function () {
            var that = this;        
            $.ajax({
                type: "POST",
                url: "aseguidospor.aspx",
                data: { UsrRegId: that.usrid },
                dataType: "json",
                success: function (data) { that.display(data); },
                error: function () { MVD.errorPopup.showText("No se pudo obtener la información, vuelva a intentarlo más tarde"); }
            });
        },
        
        showEmpty : function () {
            this.showMessage("No sigue a nadie.");
        }
        
    });
    
    
    
    MVD.UsrBloqueados = function (container, onLoad) {        
        MVD.UsrList.call(this, container);   
        this.onLoad = onLoad;
    };   
    MVD.UsrBloqueados.prototype = new MVD.UsrList();
       
    $.extend(MVD.UsrBloqueados.prototype, {    
        
        configNode : function (node, usr) {
            var that = this;
        
            $('.desbloquear', node).click(function () {
                that.desbloquear(usr.id, node); 
                return false;                    
            });        
        },
        
        changed : function () {
            return this.changes;
        },
                
        resultadoDesbloqueo : function (usrid, res, node) {
            if (res && (res.result == 1)) {   
                this.changes = true;
                node.slideUp("slow");                
            }                    
        },
        
        desbloquear : function(usuario, node) {
            var that = this;
            $.ajax({
                type: "POST",
                url: "abloqueo.aspx",
                data: { UsrRegId: usuario, accion: 'D' },
                dataType: "json",
                success: function (data) { that.resultadoDesbloqueo(usuario, data, node); },
                error: function () { that.resultadoDesbloqueo(usuario, null, node); }
            });
        },
                
        get : function () {           
            var that = this;        
            that.changes = false;
            $.ajax({
                type: "GET",
                url: "abloqueados.aspx?1",
                dataType: "json",
                success: function (data) { that.display(data); },
                error: function () { MVD.errorPopup.showText("No se pudo obtener la información, vuelva a intentarlo más tarde"); }
            });
        },

        showEmpty : function () {
            this.showMessage("No hay nadie bloqueado");
        }
    });       
    
    
}) (jQuery);