﻿function dig(id, digurl, digorbury) {
    $.ajax({
        type: "get",
        dataType: "json",
        url: digurl,
        cache: false,
        data: "a_id=" + id + "&digorbury=" + digorbury,
        complete: function() { },
        success: function(msg) {
            switch (msg[2]) {
                case 1:
                    if (msg[1] == 1) {
                        $("#item_dig_" + id + "").html(msg[0] + "<span>votes</span>");
                        $("#item_dig_a_" + id + " a:first-child").removeClass("item_a_dig").addClass("item_a_dig_hover").blur();
                        $("#item_dig_a_" + id + " a:last-child").removeClass("item_a_bury_hover").addClass("item_a_bury").blur();
                    } else {
                        $("#item_dig_" + id + "").html(msg[0] + "<span>votes</span>");
                        $("#item_dig_a_" + id + " a:first-child").removeClass("item_a_dig_hover").addClass("item_a_dig").blur();
                        $("#item_dig_a_" + id + " a:last-child").removeClass("item_a_bury").addClass("item_a_bury_hover").blur();
                    }
                    break;
                case -1:
                    alert("未知错误");
                    break;
                case -2:
                    var p = function() {
                        dig(id, digurl, digorbury);
                    }
                    Login(p);
                    break;
                default:
                    alert("未知错误");
                    break;
            }
        },
        error: function() {
        }
    });
}
function Login(callback) {
    AjaxBackGround(true);
    $("#AjaxInfoHead").text("用户登陆");
    $("#AjaxInfoContent").html("").append('<div><p><label for="username"> 用户名:</label><input id="username" name="username" type="text" value="" /><br />还没有帐户？点这<a href="/Account/Register">注册</a>.</p><p><label for="password"> 密码:</label><input id="password" name="password" type="password" /><br /><a href="/Account/GetPassword">忘记密码？</a></p><p><input name="rememberMe" type="checkbox" id="rememberMe" value="true" checked /><label class="inline" for="rememberMe">记住我?</label></p></div>');
    $("#AjaxInfoFootButton").unbind("click").bind("click", function() {
        $.ajax({
            type: "post",
            dataType: "json",
            url: "/Account/LogOnAjax",
            cache: false,
            data: $("#AjaxForm").serialize(),
            complete: function() {
            },
            success: function(msg) {
                if (msg[0] == "true") {
                    $("#login").html(msg[1]);
                    setTimeout(callback, 0);
                    AjaxBackGround(false);
                } else {
                    $("#AjaxInfoContent :password").each(function() {
                        $(this).addClass("input-validation-error").after("*");
                    });
                    $("#AjaxInfoContent :text").each(function() {
                        $(this).addClass("input-validation-error").after("*");
                    });
                }
            },
            error: function() {
                alert('未知错误');
                AjaxBackGround(false);
            }
        });
    });
}
function SubmitComment(url, obj, submitObj) {
    submitObj.attr('disabled', 'disabled').val('正在添加...');
    $.ajax({
        type: "post",
        dataType: "json",
        url: url,
        cache: false,
        data: obj.serialize(),
        complete: function() { },
        success: function(msg) {
            submitObj.attr('disabled', '').val('添加');
            if (msg[0] == "1") {

                $("#CommentSuccess").html("添加成功").show().animate({ opacity: 1 }, 1).animate({ opacity: 0.0 }, 1800, function() { $(this).hide(); });
                $("#comment_content").val("");
                $("#CommentList").append(msg[1]);
                $(".CommentList:last").show("drop", {}, 500);
            }
            if (msg[0] == "2") {
                alert(msg[1]);
            }
            if (msg[0] == "3") {
                var hello = function() {
                    SubmitComment(url, obj, submitObj);
                };
                Login(hello);
            }
        },
        error: function() {
        }
    });
    return false;
}
function DelComment(url, obj) {
    $.ajax({
        type: "post",
        dataType: "json",
        url: url,
        cache: false,
        data: "",
        complete: function() { },
        success: function(msg) {
            if (msg[0] == "1") {
                //obj.animate({ opacity: 0.0 }, 50, function() { $(this).hide(); });
                obj.hide("drop", {}, 500);
            }
            if (msg[0] == "2") {
                alert(msg[1]);
            }
            if (msg[0] == "3") {
                var hello = function() {
                    DelComment(url, obj);
                };
                Login(hello);
            }
        },
        error: function() {
        }
    });
}
function AjaxBackGround(bool) {
    if (bool) {
        $("#AjaxBackGround").height(document.documentElement.clientHeight).css("top", document.documentElement.scrollTop).css("opacity", 0).show().animate({ opacity: 0.5 }, 200);
        $("#AjaxInfo").css("top", document.documentElement.scrollTop + document.documentElement.clientHeight * 0.3).css("opacity", 0).show().animate({ opacity: 0.9 }, 500);
        $(window).scroll(function() {
            $("#AjaxInfo").stop().animate({ top: document.documentElement.scrollTop + document.documentElement.clientHeight * 0.3 }, 500);
            $("#AjaxBackGround").css("top", document.documentElement.scrollTop);
        });
        $(window).resize(function() {
            $("#AjaxBackGround").height(document.documentElement.clientHeight);
        });
    } else {
        $("#AjaxBackGround").hide();
        $("#AjaxInfo").hide();
        $(window).unbind("scroll").unbind("resize");
    }
}
//-------------------------ctrl enter
function isKeyTrigger(e, keyCode) {
    var argv = isKeyTrigger.arguments;
    var argc = isKeyTrigger.arguments.length;
    var bCtrl = false;
    if (argc > 2) {
        bCtrl = argv[2];
    }
    var bAlt = false;
    if (argc > 3) {
        bAlt = argv[3];
    }

    var nav4 = window.Event ? true : false;

    if (typeof e == 'undefined') {
        e = event;
    }

    if (bCtrl && !((typeof e.ctrlKey != 'undefined') ? e.ctrlKey : e.modifiers & Event.CONTROL_MASK > 0)) {
        return false;
    }
    if (bAlt && !((typeof e.altKey != 'undefined') ? e.altKey : e.modifiers & Event.ALT_MASK > 0)) {
        return false;
    }
    var whichCode = 0;
    if (nav4) whichCode = e.which;
    else if (e.type == "keypress" || e.type == "keydown")
        whichCode = e.keyCode;
    else whichCode = e.button;

    return (whichCode == keyCode);
}

function ctrlEnter(e, callback) {
    var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false;
    if (ie) {
        if (event.ctrlKey && window.event.keyCode == 13) { setTimeout(callback, 0); }
    } else {
        if (isKeyTrigger(e, 13, true)) { setTimeout(callback, 0); }
    }
}
