/**
 * Alert window constructor. Creates and shows the DHTML "alert" using window object.
 *
 * @param message [string] - message to be shown.
 * @param config [object] - this object is passed to the window constructor and create functuion.
 */
Zapatec.AlertWindow = function(message,config) {
        config.showMaxButton = false;
        config.showMinButton = false;
        config.showResize = false;
        config.showStatus = false;
        var win = this.win = new Zapatec.Window(config);
        win.create(config.left || "center", config.top || "center", config.width || "auto", config.height || "auto");
        if (config.title) {
                win.setTitle(config.title);
        }
        var content = document.createElement("div");

        this.win.contentArea.className += ' zpWinAlertContent';
        this.messageArea = document.createElement("span");
        this.messageArea.className = "zpWinAlertMessage";
        this.messageArea.innerHTML = message;
        content.appendChild(this.messageArea);

        this.okButton1 = document.createElement("button");
        this.okButton1.innerHTML = "Войти";
        this.okButton1.className = "zpWinAlertMessage1";
        Zapatec.Utils.addEvent(this.okButton1, "click", function (ev) {
                document.auth.submit();
        });


        content.appendChild(this.okButton1);

        this.okButton = document.createElement("button");
        this.okButton.innerHTML = "Отмена";
        this.okButton.className = "zpWinAlertMessage2";
        Zapatec.Utils.addEvent(this.okButton, "click", function (ev) {
                win.close();
        });
        content.appendChild(this.okButton); 

        this.okButton2 = document.createElement("button");
        this.okButton2.innerHTML = "Регистрация";
        this.okButton2.className = "zpWinAlertMessage3";
        Zapatec.Utils.addEvent(this.okButton2, "click", function (ev) {
                document.location.href="index.php?type=register";
        });

        content.appendChild(this.okButton2);


        this.win.setDivContent(content);
        this.win.show();
        if (this.win.contentArea.offsetWidth > this.win.config.width) {
                this.win.setWidth(this.win.contentArea.offsetWidth + this.win.config.contentWidthDiff);
        }

}

/**
 * Confirm window constructor. Creates the DHTML "confirm" using window object. Initialy it is invisible
 *
 * @param message [string] - message to be shown.
 * @param config [object] - this object is passed to the window constructor and create functuion.
 */
Zapatec.ConfirmWindow = function(message, config) {
        var confirmW = this;
        this.result = false;
        this.onConfirm = null;
        config.showMinButton = false;
        config.showMaxButton = false;
        config.showStatus = false;
        config.showResize = false;
        var win = this.win = new Zapatec.Window(config);
        win.create(config.left || "center", config.top || "center", config.width || "auto", config.height || "auto");
        if (config.title) {
                win.setTitle(config.title);
        }
        var content = document.createElement("div");
        this.win.contentArea.className += ' zpWinConfirmContent';
        this.messageArea = document.createElement("div");
        this.messageArea.className = "zpWinConfirmMessage";
        this.messageArea.innerHTML = message;
        content.appendChild(this.messageArea);
        this.okButton = document.createElement("button");
        this.okButton.innerHTML = "Ok";
        this.okButton.className = "zpWinOkButton";
        Zapatec.Utils.addEvent(this.okButton, "click", function () {
                confirmW.result = true;
                win.hide();
                if (confirmW.onConfirm) {
                        confirmW.onConfirm(confirmW.result);
                }
        });
        content.appendChild(this.okButton);
        this.cancelButton = document.createElement("button");
        this.cancelButton.innerHTML = "Cancel";
        this.cancelButton.className = "zpWinCancelButton";
        Zapatec.Utils.addEvent(this.cancelButton, "click", function () {
                confirmW.result = false;
                win.hide();
                if (confirmW.onConfirm) {
                        confirmW.onConfirm(confirmW.result);
                }
        });
        content.appendChild(this.cancelButton);
        this.win.setDivContent(content);
        win.closeButton.customMouseUp = function () {
                confirmW.result = false;
                win.hide();
                if (confirmW.onConfirm) {
                        confirmW.onConfirm(confirmW.result);
                }
        };
        this.win.show();
        if (this.win.contentArea.offsetWidth > this.win.config.width) {
                this.win.setWidth(this.win.contentArea.offsetWidth + this.win.config.contentWidthDiff);
        }
        this.win.hide();
}

/**
 * Shows the confirm window and calls "action" on responce.
 *
 * @param action [function] - action to be taken after the responce. The responce argument
 *                            is passed to this function and it holds the clicked button.
 */
Zapatec.ConfirmWindow.prototype.getResponse = function (action) {
        this.onConfirm = action;
        this.win.show();
}

/**
 * Confirm window constructor. Creates the DHTML "confirm" using window object. Initialy it is invisible
 *
 * @param message [string] - message to be shown.
 * @param config [object] - this object is passed to the window constructor and create functuion.
 *                          The only additional property is buttons, which holds the names and values
 *                          of the buttons to be used in the dialog window.
 */
Zapatec.DialogWindow = function(message, config) {
        dialogW = this;
        this.result = null;
        this.onConfirm = null;
        config.showMinButton = false;
        config.showMaxButton = false;
        if (!config.closeAction) {
                config.showCloseButton = false;
        }
        config.showStatus = false;
        config.showResize = false;
        var win = this.win = new Zapatec.Window(config);
        win.create(config.left || "center", config.top || "center", config.width || "auto", config.height || "auto");
        if (config.title) {
                win.setTitle(config.title);
        }
        var content = document.createElement("div");
        this.win.contentArea.className += ' zpWinDialogContent';
        this.messageArea = document.createElement("div");
        this.messageArea.className = "zpWinDialogMessage";
        this.messageArea.innerHTML = message;
        content.appendChild(this.messageArea);
        this.buttons = [];
        for(var i in config.buttons) {
                button = document.createElement("button");
                button.innerHTML = config.buttons[i];
                button.className = "zpWinDialogButton";
                Zapatec.Utils.addEvent(button, "click", function (ev) {
                        ev = ev || window.event;
                        target = Zapatec.Utils.getTargetElement(ev);
                        dialogW.result = target.innerHTML;
                        win.hide();
                        if (dialogW.onConfirm) {
                                dialogW.onConfirm(dialogW.result);
                        }
                });
                content.appendChild(button);
                this.buttons.push(button);
        }
        this.win.setDivContent(content);
        if (config.closeAction) {
                win.closeButton.customMouseUp = config.closeAction;
        }
        this.win.show();
        if (this.win.contentArea.offsetWidth > this.win.config.width) {
                this.win.setWidth(this.win.contentArea.offsetWidth + this.win.config.contentWidthDiff);
        }
        this.win.hide();
}

/**
 * Shows the dialog window and calls "action" on responce.
 *
 * @param action [function] - action to be taken after the responce. The responce argument
 * is passed to this function and it holds the clicked button.
 */
Zapatec.DialogWindow.prototype.getResponse = function (action) {
        this.onConfirm = action;
        this.win.show();
}