
// 针对 bootstrap 2.0 的插件 修改
$(function () {

    //modal
    $.extend($.fn, {
        showModalTip: function (content) {
            if ($(".alert", this).length > 0) {
                $(".alert", this).text(content);
                return false;
            }
            var alertHtml = '<div class="alert alert-block alert-model"style="float:left;">' + content + '</div>';
            $(".modal-footer", this).prepend($(alertHtml));
        },
        hideModalTip: function () {
            $(".modal-footer .alert", this).remove();
        },
        modals: function (opt) {
            if (opt.width) {
                this.width(opt.width).css("margin-left", -opt.width / 2);
            }
            if (opt.closeEvent) {
                $('.btncancel,.close', this).click(function() {
                    opt.closeEvent();
                });
            }
            if (opt.brforeShow)
            {
                opt.brforeShow();
            }
            if (opt.primaryKey) {
                //if ($("#followModalprimaryKey").length < 1) {
                //    this.append('<input type="hidden" value="' + opt.primaryKey + '" id="followModalprimaryKey" />');
                //}
                //else {
                //    $("#followModalprimaryKey").val(opt.primaryKey);
                //}
                var id = $(this).attr('id') + "_primaryKey";
                var $key = $("#" + id);
                if ($key.length < 1) {
                    this.append('<input type="hidden" value="' + opt.primaryKey + '" id="' + id + '" />');
                } else {
                    $key.val(opt.primaryKey);
                }

            }
            this.modal(opt);
        },
        getModalsKey: function (val) {
            var id = $(this).attr('id') + "_primaryKey";
            var $key = $("#"+id);
            if (val != null) {
                $key.val(val);
            } else {
                return $key.val();
            }
        }
    });


    //datepicker
    $.extend($.fn, {
        getDateValue: function () {
            return $('input[type=text]', this).val();
        }

    });

});

// 针对 bootstrap 2.0 的插件 扩展

function alertModals(msg, width) {
    alert(msg);
    //if (!msg)
    //{
    //    return false;
    //}
    //if ($("#tzoa_alertbox").length < 1) {
    //    var dates = (new Date()).getTime();
    //    alert("1");
    //    var $html = $('<div id="tzoa_alertbox_' + dates + '" class="modal hide fade">' +
    //                '<div class="modal-header">'+'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>'+
    //                '<h3>消息</h3>' + '</div>' + '<div class="modal-body"><div id="tzoa_alertbox_content_' + dates + '"></div></div>' +
    //                '<div class="modal-footer">' + '<a href="#" data-dismiss="modal" aria-hidden="true" class="btn">关闭</a>' + '</div>' + '</div>');
        
    //    $("#center-right").append($html);
    //}
    //$("#tzoa_alertbox_content_" + dates).text(msg);

    //$("#tzoa_alertbox_" + dates).modals({
    //    toggle: "modal",
    //    width:width,
    //    //backdrop: "static",
    //    target: "#tzoa_alertbox_" + dates
    //});
}

function confirmModals(msg,callback) {
    if (!msg) {
        return false;
    }
    if ($("#tzoa_confirmbox").length < 1) {
        var $html = $('<div id="tzoa_confirmbox" class="modal hide fade">' +
                    '<div class="modal-header">' + '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' +
                    '<h3>确认</h3>' + '</div>' + '<div class="modal-body"><div id="tzoa_confirmbox_content"></div></div>' +
                    '<div class="modal-footer">' + '<a href="#" data-dismiss="modal" id="btn_confirmbox_cancel" aria-hidden="true" class="btn">关闭</a>' +
                    '<button class="btn btnsave btn-primary" data-dismiss="modal" aria-hidden="true" id="btn_confirmbox_sure">确定</button>' +
                    //<a href="#" data-dismiss="modal" id="btn_confirmbox_sure" aria-hidden="true" class="btn btn-primary">确定</a>
                    '</div>' + '</div>');

        $("#center-right").append($html);
    }

    
    $("#btn_confirmbox_sure").unbind("click");
    $("#btn_confirmbox_sure").click(function () {
        callback(true);
    });
    $("#btn_confirmbox_cancel").unbind("click");
    $("#btn_confirmbox_cancel").click(function () {
        callback(false);
    });

    $("#tzoa_confirmbox_content").text(msg);
    $("#tzoa_confirmbox").modals({
        toggle: "modal",
        backdrop: "static",
        target: "#tzoa_confirmbox"
    });
}

function confirmModalsSure(msg, callback) {
    confirmModals(msg, function (result) {
        if (result) {
            callback();
        } else {
            return false;
        }
    });
}


$(function () {
    $.extend($.fn, {
        SelectList: function () {

            $(this).each(function () {
                var $obj = $(this);
                var $selectlist = $obj.parent().find(".selectlist");
                $obj.focus(function () {
                    $selectlist.show();
                });
                $obj.blur(function () {
                    $(document).click(focusout);
                    function focusout(e) {
                        if ($selectlist.find($(e.target)).length < 1 && $(e.target)[0] != $obj[0]) {
                            $selectlist.hide();
                            $(document).unbind("click", focusout);
                        }
                    }
                });
                $("li a", $selectlist).click(function () {
                    $obj.val($(this).text());
                    $obj.attr("realvalue", $(this).attr("value"));
                    $selectlist.hide();
                });
            });
        }
    });
});