﻿$(document).ready(function() {
    initDiggs();
});
function initDiggs() {

    var username = $.cookie("username");

    var digged = [];
    var cookieval = $.cookie("digged");
    if (cookieval) {
        digged = cookieval.split(",");
    }
    $(".digg").each(function() {
        var $this = $(this);
        var id = $this.attr("id");
        var articleId = id.replace("digg_", "");
        var diggNum = $this.attr("digg");
        var buryNum = $this.attr("bury");
        if (digged.contain(articleId)) {
            //顶过了
            $this.html('<span class="up">顶</span><span>(' + diggNum + ')</span><span class="trample">踩</span><span>(' + buryNum + ')</span>')
        } else {
            $this.html('<a href="#up" class="up">顶<span>(' + diggNum + ')</span></a><a href="#trample" class="trample">踩<span>(' + buryNum + ')</span></a>')
        }
    });
    $(".digg a").click(function() {

        if (!username) {
            if (confirm("您的操作必须登录，是否登录？")) {
                location.href = "/account/login.aspx?ReturnUrl=" + encodeURIComponent(location.href);
                return false;
            }
            return false;
        }
        var parent = $(this).parent();
        var articleId = parent.attr("id").replace("digg_", "");
        var action = $(this).attr("class");
        var blogId = parent.attr("blog");
        $.get("/account/digg.aspx?id=" + articleId + "&blog=" + blogId + "&user=" + username + "&action=" + action, function(txt) {
            //alert(txt);
            var data = eval("(" + txt + ")");
            if (data.result == 1) {
                var diggNum = parseInt(parent.attr("digg"));
                var buryNum = parseInt(parent.attr("bury"));
                if (action == "up") {
                    diggNum++
                } else {
                    buryNum++;
                }
                parent.html('<span class="up">顶</span><span>(' + diggNum + ')</span><span class="trample">踩</span><span>(' + buryNum + ')</span>');
                if (digged.length > 50) { digged.shift(); }
                digged.push(articleId);
                $.cookie("digged", digged.join());
            } else {
                if (data.content != "") {
                    alert(data.content);
                }
            }
        });
        return false;
    });
}
function userstate() {
    var p__un = document.cookie.match(new RegExp("(^| )username=([^;]*)(;|$)"));
    if (p__un) {
        document.write('欢迎您，<span class="bold">' +decodeURI(p__un[2]) + '</span>！　我的[<a href="/Account/">用户中心</a>]　[<a href="/Account/LogOut.aspx">退出</a>]');
    } else {
        document.write('[<a href="/Account/Login.aspx">登录</a>] [<a href="/Account/Register.aspx">注册</a>]');
    }
}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
Array.prototype.contain = function(val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] === val) {
            return true;
        }
    }
    return false;
}
