﻿function IsNull(txt) { return typeof (txt) == 'undefined' || txt == null || txt.length == 0; }
function IsZero(txt) { txt = parseInt(txt); return isNaN(txt) || txt == 0; }
function ToInte(txt) { txt = parseInt(txt); if (isNaN(txt)) { txt = 0; } return txt; }

function ShowIdeWarn(elm,nat,txt) { if (IsNull(txt)) {$("#" + elm).hide(); } else {$("#" + elm).removeClass("Good Warn").addClass(nat ? "Good" : "Warn").html(txt).show(); } }

function LoadIdeCode(elm) { $("#" + elm).attr("src", $("#" + elm).attr("src") + "&" + Math.round(Math.random() * 100)); }

function CombActPath(act) {return SitePath + "Service.ashx?act=" + act; }

function CateCurent() {
    var cCateID = parseInt($("#inCate").val());
    if (!IsZero(cCateID)) {
        $("#inCateTxt").load(CombActPath("CateCurent&idx=" + cCateID));
    }
}

function CateSelect(btn) {
    if ($("#CateListDiv").length > 0) { CateSelectHide(); } else {
        var sOffset = $("#inCateTxt").offset();
        $("#inCateTxt").after("<div id=\"CateListDiv\" class=\"selist\"><h6><span></span><a href=\"javascript:CateSelectHide();\">关闭</a></h6><ul></ul></div>");
        $("#CateListDiv").css({ left: sOffset.left + "px", top: sOffset.top + $("#inCateTxt").outerHeight() - 1 + "px" });
        CateSelectShow(0,btn);
    }
}

function CateSelectHide() { $("#CateListDiv").remove(); }

function CateSelectShow(idx,btn) {
    $.get(CombActPath("CateSelect&rID=" + idx), function(txt) {
        if (!IsNull(txt)) {
            $("#CateListDiv ul").html(txt).children("li").click(function() {
            if (idx > 0 && $(this).is(":first-child")) {
                    CateSelectDone($(this).attr("value"),true,btn);
                } else { CateSelectShow($(this).attr("value"),btn); }
            });
            if (idx > 0) {
                var curHtml = $("#CateListDiv h6 span").html();
                curHtml = IsNull(curHtml) ? curHtml : curHtml + " － ";
                $("#CateListDiv h6 span").html(curHtml + $("#CateListDiv ul li").eq(0).html());
            }
        }
        else {
            CateSelectDone(idx,false,btn);
        }
    });
}
function CateSelectDone(idx, bte, btn) {
    if (bte) {
        $("#inCateTxt").html($("#CateListDiv h6 span").html());
    } else {
    $("#inCateTxt").html($("#CateListDiv h6 span").html() + " - " + $("#celm_" + idx).html());
    }
    $("#inCate").val(idx); CateSelectHide();
    if (btn) {window.location = SitePath + "cate/" + idx + "/index-" + $("#inKind").val() + "-" + $("#inZone").val() + "-1" + SiteExts; }
}

function ZoneCurent() {
    var cZoneID = parseInt($("#inZone").val());
    if (!IsZero(cZoneID)) {
        $("#inZoneTxt").load(CombActPath("ZoneCurent&idx=" + cZoneID));
    }
}

function ZoneSelect(btn) {
    if ($("#ZoneListDiv").length >0) { ZoneSelectHide(); } else {
        var sOffset = $("#inZoneTxt").offset();
        $("#inZoneTxt").after("<div id=\"ZoneListDiv\" class=\"selist\"><h6><span></span><a href=\"javascript:ZoneSelectHide();\">关闭</a></h6><ul></ul></div>");
        $("#ZoneListDiv").css({ left: sOffset.left + "px", top: sOffset.top + $("#inZoneTxt").outerHeight() - 1 + "px" });
        ZoneSelectShow(1,btn);
    }
}

function ZoneSelectHide() { $("#ZoneListDiv").remove(); }

function ZoneSelectShow(idx,btn) {
    $.get(SitePath + "html/zone/ZoneList-" + idx + ".txt", function(txt) {
        if (!IsNull(txt) && txt.indexOf("\r\n") > 0) {
            var strList = ""; var stcList = txt.split("\r\n");
            $.each(stcList, function(i, n) { if (!IsNull(n)) { var stcItem = n.split("|"); strList += "<li id=\"zelm_" + stcItem[0] + "\" value=\"" + stcItem[0] + "\" title=\"" + stcItem[1] + "\">" + stcItem[1] + "</li>" } });
            $("#ZoneListDiv ul").html(strList).children("li").click(function() {
                if ($(this).is(":first-child")) {
                    ZoneSelectDone($(this).attr("value"), true, btn);
                } else { ZoneSelectShow($(this).attr("value"), btn); }
            });
            var curHtml = $("#ZoneListDiv h6 span").html();
            curHtml = IsNull(curHtml) ? curHtml : curHtml + " - ";
            $("#ZoneListDiv h6 span").html(curHtml + $("#ZoneListDiv ul li").eq(0).html());
        }
        else {
            ZoneSelectDone(idx, false, btn);
        }
    });
}
function ZoneSelectDone(idx, bte,btn) {
    if (bte) {
        $("#inZoneTxt").html($("#ZoneListDiv h6 span").html());
    } else { $("#inZoneTxt").html($("#ZoneListDiv h6 span").html() + " - " + $("#zelm_" + idx).html()); }
    $("#inZone").val(idx); ZoneSelectHide();
    if (btn) {window.location = SitePath + "cate/" + $("#inCate").val() + "/index-" + $("#inKind").val() + "-" + idx + "-1" + SiteExts; }
}


function QuizMark(idx) {
    var inText = edit_inText.getSource(); ;
    if (IsNull(inText) || inText == "<p></p>" || inText == "<br />") {
        ShowIdeWarn("msErr", 0, "请输入回答内容");
    } else {
    $.post(CombActPath("KnowMarkAdd"), { nID: idx, nText: inText }, function(txt) {
        if (IsZero(txt.substr(0, 1))) {
            ShowIdeWarn("msErr", 0, txt.substr(1, txt.length));
        } else {
            window.location = window.location.href.replace(window.location.hash, "") + "#a" + txt.substr(1, txt.length);
            window.location.reload();
        }
    });
    }
}

function QuizViewFav(idx) {
    $.get(CombActPath("UserPack&kid=2&idx=" + idx), function(txt) {
        switch (parseInt(txt)) { case 1: $("#DoFav").html("取消收藏"); break; case 2: $("#DoFav").html("收藏问题"); break; default: $("#DoFav").html("操作失败"); break; }
    });
}

function MarkBestSet(idx) {
    $.get(CombActPath("KnowMarkFun&kid=1&idx=" + idx), function(txt) {
        if (IsZero(txt.substr(0, 1))) {
            $("#MarkBestInfo_" + idx).html("操作失败");
        } else {
            window.location = window.location.href.replace(window.location.hash, "") + "#a" + txt.substr(1, txt.length);
            window.location.reload();
        }
    });
}

function AutoCateInit() {
    var inName = $("#inName").val();
    if (IsNull(inName)) {
        $("#inCateTip").addClass("no").html("请输入标题");
    } else {
        $.get(CombActPath("AutoCateInit&txt=" + encodeURIComponent(inName)), function(txt) {
            var inCate = ToInte(txt);
            if (inCate == 0) {
                $("#inCateTip").addClass("ok").html("无法匹配分类，请手动选择");
            }
            else {
                $("#inCateTip").removeClass("ok no").html("");
                $("#inCate").val(inCate);
                CateCurent();
            }
        });
    }
}


$(document).ready(function() {
    $("#sevKind").click(function() {
        $(this).addClass("onOver");
        var sOffset = $(this).offset();
        $("#seuKind").css({ left: sOffset.left + 8 + "px", top: sOffset.top + $(this).outerHeight() - 6 + "px" }).show();
        $("#seuKind li").each(function(i) {
            $(this).click(function() {
                $("#SearchKid").val(i + 1);
                $("#sevKind").removeClass("onOver").html($(this).html());
                $("#seuKind").hide();
            });
        });
    });
});


function VideoDing(id) {
    $.ajax({
        type: "GET",
        url: "/Service.ashx?act=VideoGood&add=1&id=" + id + "&rand=" + new Date(),
        cache: false,
        success: function(html) {
            $("#Ding").html(html);
        }
    });
}

function swPost() { $("#VideoView .ivPost").toggle(); }

function copyToClipboard(jQSel) {
    var clipBoardContent = $(jQSel).val();
    if (window.clipboardData) {
        window.clipboardData.setData("Text", clipBoardContent);
        alert("复制成功，您现在可以CTRL+V粘贴到BBS或博客中。");
    } else {
        alert("您的浏览不支持此复制功能，请使用CTRL+C或鼠标右键。");
    }
    $(jQSel).select();
}

function addFavorite() {
    if (document.all) {
        window.external.addFavorite('http://www.nongye.cn', '中国农业信息分类发布平台');
    } else if (window.sidebar) {
        window.sidebar.addPanel('中国农业信息分类发布平台', 'http://www.nongye.cn', '');
    }
}