﻿$(function () {
    var position_selected_num = 0;
    $(".positions_c li input").attr("checked", false);

    /*小类菜单*/
    $(".positions_b td:has(span)").click(function () {
        var positionlist_offset = $(".pop_up").offset();
        var positionlist_height = $(".pop_up").height();
        var positionlist_bottom = positionlist_offset.top + positionlist_height;
        var thisleft = $(this).offset().left - positionlist_offset.left - 2;
        var thistop = $(this).offset().top - positionlist_offset.top + 14;
        var this_show_top = thistop;
        var this_show_left = thisleft;
        var this_show_id = $(this).find("span").attr("id").replace("p", "l");
        var this_show_bottom = $("#" + this_show_id).height() + thistop;
        if (this_show_bottom - positionlist_bottom > 0) { this_show_top = this_show_top - $("#" + this_show_id).height() - 20 };
        $("#" + this_show_id).css({ "left": this_show_left + "px", "top": this_show_top + "px" });
        $("#" + this_show_id).show();
    }).hover(
	function () {
	    $(this).css("background-color", "#FFF0C1");
	    $(".positions_c").hide();
	},
	function () {
	    $(this).css("background", "none");
	});

    /*选择小类*/
    $(".positions_c li input").click(function () {
        if ($(this).attr("checked") == true) {
            if (position_selected_num >= 5) {
                alert("您选择的类别数量已超过5个，无法再次选取。\n请删除之前选择或清空再选择。");
                $(this).attr("checked", false);
            }
            else {
                var thisid = $(this).attr("id");
                if (thisid.substr(thisid.length - 1, 1) == "n") {
                    $(this).parent().nextAll().find("input:checked").each(function (index, element) {
                        $(this).attr("checked", false);
                        $("#" + $(this).attr("id").replace("p", "s")).remove();
                        position_selected_num--;
                    });
                }
                else {
                    if ($(this).parents("ul").find("li:first > input").attr("checked") == true) {
                        $(this).parents("ul").find("li:first > input").attr("checked", false);
                        $("#" + $(this).parents("ul").find("li:first > input").attr("id").replace("p", "s")).remove();
                        position_selected_num--;
                    }
                }
                position_selected_num++;
                $("#positions_selected").append("<div class=\"p_selected\" id=\"" + $(this).attr("id").replace("p", "s") + "\"\
				>" + $(this).next("label").text() + "</div>");
                $("#" + $(this).attr("id").replace("p", "s")).bind("click", function () {
                    $(this).remove();
                    $("#" + $(this).attr("id").replace("s", "p")).attr("checked", false);
                    position_selected_num--;
                    $(this).unbind("click");
                });
            }
        }
        else {
            $("#" + $(this).attr("id").replace("p", "s")).remove();
            position_selected_num--;
        }
    });

    /*清空所选*/
    $(".pop_reselect").click(function (e) {
        $(".p_selected").remove();
        $(".positions_c li input").attr("checked", false);
        position_selected_num = 0;
    });

    $("#position_list").click(function (e) {
        var windows_width = $(window).width();
        var windows_height = $(window).height();
        var popup_x = (windows_width - $(".pop_up").width()) / 2;
        var popup_y = (windows_height - $(".pop_up").height()) / 2 + $(window).scrollTop();

        $(".pop_up").css({ "left": popup_x, "top": popup_y }).show();
        if (typeof ($("#jd_shadow")[0]) == "undefined") {
            $("body").prepend("<div id=\"jd_shadow\">&nbsp;</div>");
            var _jd_shadow = $("#jd_shadow");
            _jd_shadow.css("width", windows_width + "px");
            _jd_shadow.css("height", $("body").height() + "px");
        }
        $("body").css("overflow", "hidden");
    });

    window.onresize = SetPopPosition;

    $(".pop_close").click(function (e) {
        $(".pop_up").hide();
        $("#jd_shadow").remove();
        $("body").css("overflow", "auto");
        if (position_selected_num != 0) {
            $("#position_list").text("已选择 " + position_selected_num + " 项职位");
        }
        else {
            $("#position_list").text("请选择职位类别");
        }
    });

    $("#jobkeyword").focus(function () {
        $(this).attr("value", "");
    }).blur(function () {
        if ($(this).attr("value") == "") {
            $(this).attr("value", "请输入职位名称关键字");
        }
    });
    $("#unitname").focus(function () {
        $(this).attr("value", "");
    }).blur(function () {
        if ($(this).attr("value") == "") {
            $(this).attr("value", "请输入单位名称关键字");
        }
    });
});

//设置pop_up位置函数
function SetPopPosition() {
    var _jd_shadow = $("#jd_shadow");
    var windows_width = $(window).width();
    var windows_height = $(window).height();
    var popup_x = (windows_width - $(".pop_up").width()) / 2;
    var popup_y = (windows_height - $(".pop_up").height()) / 2 + $(window).scrollTop();
    $(".pop_up").css({ "left": popup_x, "top": popup_y });
    _jd_shadow.css("width", windows_width + "px");
    _jd_shadow.css("height", $("body").height() + "px");
}
