﻿var oldFaceBoxBody, faceboxTable;

$(document).ready(function () {
    var idx = 0; html = [];
    html[idx++] = "<table class='facebox'>";
    html[idx++] = "<tr>";
    html[idx++] = "<td class='tl'/>";
    html[idx++] = "<td class='b'/>";
    html[idx++] = "<td class='tr'/>";
    html[idx++] = "</tr>";
    html[idx++] = "<tr>";
    html[idx++] = "<td class='b'/>";
    html[idx++] = "<td class='body'>";
    html[idx++] = "</td>";
    html[idx++] = "<td class='b'/>";
    html[idx++] = "</tr>";
    html[idx++] = "<tr>";
    html[idx++] = "<td class='bl'/>";
    html[idx++] = "<td class='b'/>";
    html[idx++] = "<td class='br'/>";
    html[idx++] = "</tr>";
    html[idx++] = "</table>";
    faceboxTable = html.join("");

    jQuery.fn.center = function () {
        this.css("position", "absolute");
        this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
        this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
        return this;
    }

    jQuery.fn.facebox = function (closeButtons) {
        if (this.parents(".facebox").length < 1) {
            // not in a facebox
            var fb = $(faceboxTable).draggable();
            $("form").append(fb);
            fb.find(".body").append(this.show());

            var header = this.find(".header");
            if (header) {
                if (header.find(".close").size() == 0) {
                    var close = $("<div class='close' title='Close'></div>");
                    close.click(function (event) {
                        fb.hide();
                    });
                    header.append(close);
                }
            }

            if (closeButtons) {
                closeButtons.click(function () {
                    fb.hide();
                });
            }
        }
        this.parents(".facebox").show().center();
        return this;
    }

    var alertBox;
    window.alert = function (message) {
        if (!alertBox) {
            alertBox = $(makeAlertBox("Message", message, false));
        }
        alertBox.find(".content p").text(message);
        var closeButton = alertBox.find("#btnAlertOk");
        alertBox.facebox(closeButton);
    }

    //window.confirm = function (message) {
    //    var e = $(makeAlertBox("Confirm", message, true));
    //    var closeButtons = e.find("#btnConfirmOk").add("#btnConfirmCancel");
    //    e.facebox(closeButtons);

    ////    var d = arguments.callee.caller;
    ////    var t = this;

    ////    e.find("#btnConfirmOk")
    ////    .click(function () {
    ////        alert(t);
    ////    });
    //    return false;
    //}

    // master page message box overrride
    var errBox = $("div[id*='errBox']");
    if (errBox.length > 0) {
        alert(errBox.hide().text());
    }

    function makeAlertBox(title, message, confirm) {
        var idx = 0; html = [];
        html[idx++] = "<div>";
        html[idx++] = "<div class='header'>";
        html[idx++] = "<h4>";
        html[idx++] = title;
        html[idx++] = "</h4>";
        html[idx++] = "</div>";
        html[idx++] = "<div class='content'><p>";
        html[idx++] = message;
        html[idx++] = "</p></div>";
        html[idx++] = "<div class='footer'>";
        if (confirm) {
            html[idx++] = "<input id='btnConfirmOk' type='button' value='Okay' />";
            html[idx++] = "<input id='btnConfirmCancel' type='button' value='Cancel' />";
        } else {
            html[idx++] = "<input id='btnAlertOk' type='button' value='Okay' />";
        }
        html[idx++] = "</div>";
        html[idx++] = "</div>";
        return html.join("");
    }

});

