
ko.bindingHandlers.FcvCanvasPopoverHandler = {
    update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        $(element).popover({ placement: "top", html: true });
    }
};

var id = 0;
ko.bindingHandlers.FcvCanvasPopoverPTODetalhesHandler = {
    //init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
    //    var value = ko.utils.unwrapObservable(valueAccessor());
    //    $(element).popover({ placement: "bottom", html: true });
    //},
    update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        $(element).popover({ placement: "bottom", html: true });
    }
};
ko.bindingHandlers.popover = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
        var acessor = {};

        if (typeof (valueAccessor()) === "string") {
            acessor.visible = true;
            acessor.tpl = valueAccessor();
        } else {
            acessor = valueAccessor();
        }

        if (acessor.visible) {
            var cssSelectorForPopoverTemplate = ko.utils.unwrapObservable(acessor.tpl);

            if (cssSelectorForPopoverTemplate[0] != "#")
                cssSelectorForPopoverTemplate = "#" + cssSelectorForPopoverTemplate;

            $(element).attr("data-content", $(cssSelectorForPopoverTemplate).html());

            $(element).popover({
                html: true,
                placement: acessor.placement
            });

            $(element).parent().click(function () {
                var isOpen = $(element).data("popoverIsOpen");
                var content = null;
                if (typeof (isOpen) !== "undefined" && isOpen) {
                    $(element).popover('hide');
                    $(element).data("popoverIsOpen", false);
                    return;
                }
                content = $(element).attr("data-content");
                var $elementTemp = $("<div id='" + element.id + "_temp'>" + content + "</div>");
                if ("#terminal-final-vm" == cssSelectorForPopoverTemplate) {
                    viewModel = bindingContext.$parent;
                }
                else if (!acessor.semrfid) {
                    carregarRfidsPorTerminal(viewModel.TerminalInicial.Codigo());
                }
                ko.applyBindings(viewModel, $elementTemp[0]);

                var isInicial = true;
                isInicial = $(this).parent().hasClass("div-terminal-inicial");
                var estilo = getTerminalStyle(isInicial);
                $elementTemp.find("a").attr("click", function () { alert('teste'); });
                $(element).attr("data-content", "<div style='" + estilo + "'>" + $elementTemp.html() + "</div>");
                $(element).data("popoverIsOpen", true);
                $(element).popover('show');
            });
        }
    }
};

function getTerminalStyle(isInicial) {
    var ret = "text-align:left;";

    if (isInicial)
        ret += "width:380px;";
    else
        ret += "width:200px;";

    return ret;
}