// Doghole Post
(function() {
    if (!window.Doghole) {
        window.Doghole = {};
    }
    window.Doghole.Cookies = {
        set: function(name, value, expiredDays, path='/'){
            var exp = new Date(); 
            if (expiredDays == undefined) {
                expiredDays = 30;
            }
            exp.setTime(exp.getTime() + expiredDays * 24 * 60 * 60 * 1000); 
            document.cookie = name + "="+ escape(value) + ";expires=" + exp.toGMTString() + ";path="+path; 
        },
        get: function(name) {
            var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
            if(arr = document.cookie.match(reg))
                return unescape(arr[2]); 
            else 
                return null; 
        },
        del: function(name) {
            this.set(name, "", -1);
        }
    }
    window.Doghole.PostPass = (function(){
        function d(url) {
            if (!url) url = window.location.pathname;
            return t = /^\/([\w\.]+?)\/(.*?)\/?(password)?$/.exec(url), 
            t && [`post${t[1]=='archives'?'Id':'PageName'}`, t[2]] || undefined;
        }
        var c = window.Doghole.Cookies, k = 'postPassword';
        function gp(n) {
            var pc = c.get(k), df = {postId:{}, postPageName:{}};
            if (!!pc) {
                try {pc = JSON.parse(pc)} catch(_){pc = 0}
            }
            return pc ? pc : n ? df : undefined;
        }
        function cp(pc) {
            c.set(k, JSON.stringify(pc), 30, '/');
        }
        return {
            set: function (url, password) {
                var pc = gp(1), x = d(url);
                pc[x[0]][x[1]] = password;
                cp(pc);
            },
            del: function (url) {
                var pc = gp(), x = d(url);
                if (!pc) return;
                delete pc[x[0]][x[1]];
                cp(pc);
            },
            exists: function (url) {
                return pc = gp(), x = d(url), pc && x && pc[x[0]] && pc[x[0]][x[1]]
            }
        }
    })();
    var e = x => `.post-lock-input [type=${x}]`,
        b = e('button'), i = e('password');
    $('body').on('click', b, function(){
        $(this).addClass('btn-loading');
        var passwordInput = $(i), that = $(this),
        password = $.md5(passwordInput.val()),
        url = passwordInput.attr('url');
        $.ajax({
            url : url,
            data: {password: password},
            type: 'post',
            success: function(res){
                window.Doghole.PostPass.set(url, password);
                location.reload();
            },
            error: function () {
                that.removeClass('btn-loading');
                $('.post-pass-error').text(arguments[0].responseJSON.data);
            }
        });
    }).on('keypress', i, function(e){
        if (e.which == 13) {
            $(b).click();
        }
    });
    $(function(){
        var pp = window.Doghole.PostPass;
        if (pp.exists()) {
            $('.post.post-page:not(.post-lock) .post-title').append(
                $('<a href="#" title="该文章是加密文章，点击上锁"><i class="post-icon icon-unlocked"></i></a>')
                .on('click', function (e){
                    e.preventDefault();
                    pp.del();
                    location.reload();
                })
            )
        }
    })
}).call(this);