﻿function ModalBox(block,mensagem) {
    var modal = this;
    this.block;
    this.mensagem;
    this.callbackFechar;

    this.init = function() {
        modal.block = document.getElementById(block);
        modal.mensagem = document.getElementById(mensagem);

        new Effect.Opacity(modal.block, { duration: 0, to: 0.0 });
        var tam = modal.getPageSize();

        modal.mensagem.style.left = ((tam[0] / 2) - 193) + "px";
        modal.mensagem.style.top = ((tam[1] / 2) - 66) + "px";

        modal.block.style.backgroundColor = "#000000";
        modal.block.style.width = tam[0] + "px";
        modal.block.style.height = tam[1] + "px";
        modal.block.style.top = 0;
        modal.block.style.left = 0;
        modal.block.style.position = "absolute";
        modal.block.id = "block";
    }

    this.mostrarBox = function(msg, callback) {
        modal.block.style.display = "block";
        modal.mensagem.getElementsByTagName("p")[0].innerHTML = msg;
        new Effect.Opacity(this.block, { duration: 1, from: 0.0, to: 0.6, afterFinish: function() { modal.mensagem.style.display = "block"; if (callback != undefined) { callback() } } });
    }

    this.ocultarBox = function() {
        modal.mensagem.style.display = "none";
        new Effect.Opacity(modal.block, { duration: 1, from: 0.6, to: 0.0, afterFinish: function() { modal.block.style.display = "none"; if (modal.callbackFechar != undefined) { modal.callbackFechar() } } });
    }

    this.getPageSize = function() {
        var xScroll, yScroll;

        if (window.innerHeight && window.scrollMaxY) {
            xScroll = window.innerWidth + window.scrollMaxX;
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }

        var windowWidth, windowHeight;

        if (self.innerHeight) {	// all except Explorer
            if (document.documentElement.clientWidth) {
                windowWidth = document.documentElement.clientWidth;
            } else {
                windowWidth = self.innerWidth;
            }
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }

        // for small pages with total height less then height of the viewport
        if (yScroll < windowHeight) {
            pageHeight = windowHeight;
        } else {
            pageHeight = yScroll;
        }

        // for small pages with total width less then width of the viewport
        if (xScroll < windowWidth) {
            pageWidth = xScroll;
        } else {
            pageWidth = windowWidth;
        }

        return [pageWidth, pageHeight];
    }
    
    if (window.addEventListener)
        window.addEventListener("load", this.init, false);
    else
        window.attachEvent("onload", this.init);

}