$(document).keyup(function(A) {
    var B = "";
    if (window.event) {
        B = A.keyCode
    } else {
        if (A.which) {
            B = A.which
        }
    }
    if (B == 27) {
        $.fn.popupClose()
    }
});
$(document).bind("click", popupClick);
$(document).data("popup", {lastParent:0, modal:false, id:0});
jQuery.fn.popupOpen = function(M) {
    var F = $(document).data("popup");
    var C = $(this).attr("id") || "p007";
    var I = M.type || "success";
    var K = (I == "success" || I == "inform" || I == "warning" || I == "action_modal");
    if (!F.lastParent || (F.lastParent && !F.modal && "popup" + $(this).attr("id") !== F.id)) {
        $('div[class~="b-popup"]').remove();
        var H = "";
        $("body").append('<div id="popup' + C + '" class="b-popup"></div>');
        var A = $("#popup" + C);
        window._pp = A;
        $('<i class="b-popup-tl"></i><i class="b-popup-tr"></i><i class="b-popup-t"></i><i class="b-popup-l"></i><div class="b-popup-content"><div class="b-popup-close" title="Close (Esc)"><i></i></div><div class="b-popup-body"></div></div><i class="b-popup-r"></i><i class="b-popup-b"></i><i class="b-popup-bl"></i><i class="b-popup-br"></i>').appendTo(A);
        if ("title" in M) {
            A.find("div.b-popup-content").prepend('<div class="b-popup-title">' + M.title + "</div>")
        } else {
            A.find("div.b-popup-body").css({"padding-top":"10px"})
        }
        if (M.url) {
            $("div.b-popup-body").load(M.url, function() {
            })
        } else {
            if (M.node) {
                H = "<div>" + $(M.node).html() + "</div>"
            } else {
                if (M.html) {
                    H = M.html
                }
            }
        }
        A.find("div.b-popup-body").append(H);
        if (K) {
            A.addClass("b-popup-modal");
            $("body").addClass("doNotFlicker")
        }
        if (I != "note") {
            A.addClass("b-popup-" + I)
        } else {
            A.addClass("b-popup-note");
            A.find("div.b-popup-content").prepend('<div class="b-popup-tail"></div>');
            A.css({position:"absolute"})
        }
        var E = parseInt(A.outerWidth());
        var L = (M.height) ? ((parseInt(M.height) % 2 == 1) ? parseInt(M.height) + 1 : parseInt(M.height)) : false;
        if (L) {
            A.find("div.b-popup-body").css({height:L + "px"})
        }
        var B = (M.width) ? ((parseInt(M.width) % 2 == 1) ? parseInt(M.width) + 1 : parseInt(M.width)) : false;
        if (B) {
            if (K) {
                A.find("div.b-popup-body").css({width:B + "px"})
            }
            A.css({width:(B + 30) + "px"})
        }
        if (K) {
            (M.top) ? A.css({top:parseInt(M.top)}) : A.css({"margin-top":Math.floor(A.outerHeight() / 2 * (-1))});
            (M.left) ? A.css({left:parseInt(M.left)}) : A.css({"margin-left":Math.floor(A.outerWidth() / 2 * (-1))})
        } else {
            if (I == "note") {
                A.css({top:getAbsolutePosz($(this).get(0)).y + $(this).innerHeight(),left:getAbsolutePosz($(this).get(0)).x})
            } else {
                var J = Math.floor(($(window).height() / 2) - (A.outerHeight() / 2) + $(document).scrollTop());
                (J < $(document).scrollTop()) ? J = $(document).scrollTop() : false;
                var D = Math.floor(($(window).width() / 2) - (A.outerWidth() / 2) + $(document).scrollLeft());
                (D < $(document).scrollLeft()) ? D = $(document).scrollLeft() : false;
                A.css({top:J,left:D})
            }
        }
        if ($(window).width() - $(this).offset().left < A.outerWidth() && $(this).offset().left > A.outerWidth() && !K) {
            A.addClass("b-popup-note-right");
            var G = (-1) * (A.outerWidth() - (E + parseInt(A.css("margin-left"))));
            A.css({"margin-left":G})
        } else {
            if (!K && I == "note") {
                A.addClass("b-popup-note-left")
            }
        }
        A.find(".b-popup-frame").load(function() {
            A.find(".b-popup-frame").css({height:A.find("div.b-popup-body").innerHeight() - 18,width:A.find("div.b-popup-body").innerWidth() - 47})
        });
        A.find("div.b-popup-close").bind("click", function() {
            $(this).popupClose(A)
        });
        $(document).data("popup").lastParent = this;
        $(document).data("popup").modal = K;
        $(document).data("popup").id = "popup" + C;
        $(document).data("popup").onclose = "onclose" in M ? M.onclose : null;
        A.addClass("someFakeClassName");

        return false
    }
};
function getAbsolutePosz(B) {
    var C = {x:B.offsetLeft,y:B.offsetTop};
    if (B.offsetParent) {
        var A = getAbsolutePosz(B.offsetParent);
        C.x += A.x;
        C.y += A.y
    }
    return C
}
jQuery.fn.popupClose = function(A) {
    if (jQuery.support.opacity) {
        $('div[class~="b-popup"]').fadeOut("fast", popupRemove)
    } else {
        popupRemove()
    }
    return true
};
function popupRemove() {
    $('div[class~="b-popup"]').remove();
    $("body").removeClass("doNotFlicker");
    $(document).data("popup").lastParent = 0;
    $(document).data("popup").modal = false;
    $(document).data("popup").id = 0;
    if (typeof $(document).data("popup").onclose == "function") {
        $(document).data("popup").onclose()
    }
}
function popupClick(B) {
    var A = $(document).data("popup").modal;
    if (B.target == $(document).data("popup").lastParent || $(B.target).parents().is("div.b-popup") || $(B.target).is(".b-popup") || A) {
        return true
    } else {
        if (!$(document).data("popup").modal && $(document).data("popup").lastParent) {
            $.fn.popupClose();
            return false
        } else {
            return true
        }
    }
}

$().ready(function() {
    $("a.faq").bind("click", function(C) {
        $(this).popupOpen({type: "note", node: '#' + this.id + '_desc'});
        return false;
    })
});

