﻿
(function ($) {
    // Popup de avisos, elem para mensaje, nick del usuario del perfil.
    var avisopop, msgcont, nick;    


    MVD.Seguimiento = function (divid, usrid, onLogin) {
        this.container = $("#" + divid);
        this.usrid = usrid;
        this.onLogin = onLogin;
        this.init();
    };

    $.extend(MVD.Seguimiento.prototype, {
        init : function () {
            this.container.html('<img src="' + MVD.img_loading.src + '" />');
            var that = this;

            $.ajax({
                type: "GET",
                url: "aseguimientocheck.aspx?" + this.usrid,
                dataType: "json",
                success: function (data) { that.mostrarEstadoSuscripcion(data); },
                error: function () { that.error(); }
            });
        },

        suscripcionErronea : function () {
            this.container.html('Por favor inténtelo mas tarde').click(null);
        },

        mostrarResultadoSuscripcion : function (accion, resultado) {
            if (resultado >= 0) {                
                msgcont.html(((accion == 'S') ? 'Ahora sigues a ' : 'Dejaste de seguir a ') + nick);
                var that = this;
                avisopop.onClose = function () { that.mostrarEstadoSuscripcion((accion == 'S') ? 1 : 0); };
                avisopop.open();
            } else {
                if (resultado <= -1) {
                    this.mostrarEstadoSuscripcion(resultado);
                } else {
                    this.suscripcionErronea();
                }
            }
        },

        suscribir : function (accion) {
            var that = this;
            $.ajax({
                type: "POST",
                url: "aseguimientosuscribir.aspx",
                data: { UsrRegId: that.usrid, accion: accion },
                dataType: "json",
                success: function (data) { that.mostrarResultadoSuscripcion(accion, data); },
                error: function () { that.suscripcionErronea(); }
            });
        },

        mostrarEstadoSuscripcion : function (data) {
            switch(data) {
                case 0: this.mostrarSuscribir(); break;
                case 1: this.mostrarDesuscribir(); break;
                case -1: this.mostrarLoguearse(); break;
                case -4: 
                case -2: break; // No se puede suscribir
                default: this.error(); break;
            }
        },

        error : function (XMLHttpRequest, textStatus, errorThrown) {
            // console.log("ERROR", XMLHttpRequest, textStatus, errorThrown);
            this.container.parent.hide();
        },

        mostrarLink : function (html, callback) {            
            this.container.html(html).unbind("click").click(callback).parent().show();            
        },
        
        getShortNick: function () {
            var n = MVD.Perfil.UsrNick || "este usuario";
            if (n.length > 28) {
                n = n.substr(0, 25) + '...';
            } 
            return n;
        },

        mostrarSuscribir : function () {
            var that = this;
            this.mostrarLink("Seguir a " + this.getShortNick() , function () {
                that.suscribir('S');
                return false;
            });
            
        },

        mostrarDesuscribir : function () {
            var that = this;
            this.mostrarLink("Dejar de seguir a " + this.getShortNick(), function () {
                that.suscribir('D');
                return false;
            });
        },

        mostrarLoguearse : function () {
            // this.container.html("Debe iniciar sesión para seguir usuarios").attr("href", this.loginlink);
            var that = this;            
            this.mostrarLink("Seguir a " + this.getShortNick(), function () {
                that.onLogin();
                return false;
            });
        }              
    });

   

    $(document).ready(function () {        
        var popupSeguidos = new MVD.Popup($('#pop1cont'));
        var popupSiguenA = new MVD.Popup($('#pop2cont'));
        
        var admseguidos = new MVD.UsrSeguidosPor($("#pop1usrlist"), MVD.Perfil.UsrId)
        var admSiguenA = new MVD.UsrSiguenA($("#pop2usrlist"), MVD.Perfil.UsrId)
        
        $("#usrssigo").click(function () { 
            admseguidos.get();
            popupSeguidos.open();
            return false;
        });
        
        $("#usrsmesiguen").click(function () {
            admSiguenA.get();
            popupSiguenA.open();
            return false;
        });
        
        
        var login = new MVD.Popup($('#poplogin'));
        
        new MVD.Seguimiento("seguimiento-suscripcion", MVD.Perfil.UsrId, function () {             
            login.open();
        });
        
        avisopop = new MVD.Popup($('#seguimnuevo'));
        msgcont = $('#siguenick');
        nick = msgcont.html();
        
        var anchor = MVD.getUrlAnchor();
        if (anchor) {          
            if (anchor == 'sigue1') {            
                msgcont.html('Ahora sigues a ' + nick);
                avisopop.open();
            } else if (anchor == 'sigue0') {            
                msgcont.html('Continuas siguiendo a ' + nick);
                avisopop.open();
            }                       
        }
                
    });

}) (jQuery);
