﻿$(function() {
    $.waitUntil = function(fun, waitLoop, interval) {

        var timer = setInterval(function() {
            if (waitLoop()) {
                fun();
                clearInterval(timer);
            };
        }, interval);
    };
    bDialog_init = function() {
        $("<div style='display:none' id='boolan_dialog_container'></div>")
            .load("/Content/Others/BoolanDialog/BoolanDialog.htm", function() {
            }).appendTo("body");
    };

    bDialog_init();
    bConfirm = function(text, funOK, funCancel, img, captionOK, captionCan) {
        $.waitUntil(function() {
            //find the template div and clone it
            var $template = $("#boolan_dialog_container").find(".boolan_dialog").clone();

            //bind caption
            $template.find(".boolan_dialog_content").html(text);
            //bind img
            $template.find(".boolan_dialog_img").attr("src", img || configData.confirm);
            //add specific button
            var $btn_div = $template.find(".boolan_dialog_btn_div");

            $("<input type='button' class='boolan_dialog_btn'  value='" + (captionOK || "确定") + "' />").click(function() {
                $template.attr("actionFlag", "true");
                if (funOK) funOK.apply($template[0], [this]);
                $template.dialog("close");
            }).appendTo($btn_div);
            $("<input type='button' class='boolan_dialog_btn' value='" + (captionCan || "取消") + "' />").click(function() {
                $template.attr("actionFlag", "true");
                if (funCancel) funCancel.apply($template[0], [this]);
                $template.dialog("close");
            }).appendTo($btn_div);

            //convert the template div into dialog
            $template.dialog(
            {
                autoOpen: false,
                width: 310,
                height: 140,
                modal: true,
                resizable: false,
                close: function() {
                    if (funCancel && !$template.is("[actionFlag]")) funCancel.apply($template[0], [this]);
                    $(this).dialog("destroy");
                    $(this).remove();
                }
            }).dialog("open");

        }, function() { return $("#boolan_dialog_container").length != 0 }, 100);

    }

    bAlert = function(text, funOK, img, caption) {
        $.waitUntil(function() {
            //find the template div and clone it
            var $template = $("#boolan_dialog_container").find(".boolan_dialog").clone();
            //add specific button
            var $btn_div = $template.find(".boolan_dialog_btn_div");
            //bind caption
            $template.find(".boolan_dialog_content").html(text);
            //bind img
            $template.find(".boolan_dialog_img").attr("src", img || configData.yes);
            //bind btn
            $("<input type='button' class='boolan_dialog_btn' value='" + (caption || "确定") + "' />").click(function() {
                $template.attr("actionFlag", "true");

                $template.dialog("close");
                if (funOK) funOK.apply($template[0], [this]);
            }).appendTo($btn_div);
            //convert the template div into dialog
            $template.dialog(
            {
                autoOpen: false,
                width: 310,
                height: 140,
                modal: true,
                resizable: false,
                close: function() {
                    if (funOK && !$template.is("[actionFlag]")) funOK.apply($template[0], [this]);
                    $(this).dialog("destroy");
                    $(this).remove();
                }
            }).dialog("open");
        }, function() { return $("#boolan_dialog_container").length != 0 }, 100);

    }
    $.fn.extend({
        offsetTo: function(target) {
            if (!this[0]) return null;
            var so = $(this[0]).offset();
            var to = $(target).offset();
            var result = {
                left: so.left - to.left,
                top: so.top - to.top
            }
            return result;
        }
    });


});
$(function() {
    function SetWatered($target, value) {
        value ? $target.addClass("watered") : $target.removeClass("watered");
    }
    function GetWatered($target) {
        return $target.is(".watered");
    }
    $.fn.extend({
        watertxt: function() {
            this.unbind("blur.watertxt").bind("blur.watertxt", function() {
                $self = $(this);
                //如果为空，就显示watertxt
                if ($self.val().length == 0) {
                    SetWatered($self, true);
                    $self.val($self.attr("watertxt"));
                }
            }).unbind("focus.watertxt").bind("focus.watertxt", function() {
                $self = $(this);
                if (GetWatered($self)) {
                    SetWatered($self, false);
                    $self.val("");
                }
            }).trigger("blur.watertxt");
        }
    });
    $("[watertxt]").watertxt();
});

String.prototype.getStringWidth = function() {
    var re = /[\u4e00-\u9fa5]/;
    var width = 0;
    for (var i = 0, ch; ch = this.substring(i, i + 1); i++) {
        //全角
        if (re.test(ch)) {
            width += 2;
        }
        else
            width += 1;
    }

    return width;
}
