/*

    Initialize this class with an object for overlays, like this:
    {
        "overlay1": { 
            "container": "containerId",
            "closeButton": "closeButtonId",
            "closeOnEscapePress": true,
            "animation": (optional) the animation used for hide/unhide, currently available:
                "slide", "fade"
            "inAnimation": (optional) the animation used for unhide, overrides animation
            "inAnimation": (optional) the animation used for hide, overrides animation
            "preStartExec": (optional) function () { will be executed before showing the overlay},
            "postStartExec": (optional) function () { will be executed after showing the overlay }
            "preCloseExec": (optional) function () { will be executed before hiding the overlay }
            "postCloseExec": (optional) function () { will be executed after hiding the overlay}
        }
    }

*/
var overlayControl = function (overlays) {

    var myOverlays = overlays;

    for (var i in myOverlays) {
        myOverlays[i].signals = [];
    }

    var openContainer = function (animation,containerId) {
        if (navigator.appVersion.indexOf("MSIE 6") !== -1) {
            showElement(containerId);
        } else {
            switch (animation) {
                case "slide": slideDown(containerId); break;
                case "fade": appear(containerId); break;
                default: showElement(containerId);
            }
        }
    }

    var closeContainer = function (animation,containerId) {
        if (navigator.appVersion.indexOf("MSIE 6") !== -1) {
            hideElement(containerId);
        } else {
            switch (animation) {
                case "slide": slideUp(containerId); break;
                case "fade": fade(containerId); break;
                default: hideElement(containerId);
            }
        }
    }

    var closeIt = function (id) {
        for (var i=0; i<myOverlays[id].length;i++) {
            disconnect(myOverlays[id].signals[i]);
        }
        myOverlays[id].signals = [];
        if (typeof(myOverlays[id].preCloseExec) == "function") {
            myOverlays[id].preCloseExec();
        }
        var animation;
        if (!isEmpty(myOverlays[id].outAnimation)){
            animation = myOverlays[id].outAnimation;
        } else if (!isEmpty(myOverlays[id].animation)){
            animation = myOverlays[id].animation;
        }
        closeContainer(animation,myOverlays[id].container);
        if (typeof(myOverlays[id].postCloseExec) == "function") {
            myOverlays[id].postCloseExec();
        }
    }

    var closeItEscape = function (id,e) {
        if (e.key().string == "KEY_ESCAPE") {
            closeIt(id);
        }
    }

    this.startOverlay = function (id) {
        if (isEmpty(id) || isEmpty(myOverlays[id].container)) {
            logError("id '"+id+"' does not exist!");
            return false;
        }
        if (typeof(myOverlays[id].preStartExec) == "function") {
            myOverlays[id].preStartExec();
        }
        myOverlays[id].signals[myOverlays[id].signals.length] = connect(myOverlays[id].closeButton,"onclick",partial(closeIt,id));
        if (myOverlays[id].closeOnEscapePress) {
            myOverlays[id].signals[myOverlays[id].signals.length] = connect(document,"onkeyup",partial(closeItEscape,id));
        }
        var animation;
        if (!isEmpty(myOverlays[id].inAnimation)){
            animation = myOverlays[id].inAnimation;
        } else if (!isEmpty(myOverlays[id].animation)){
            animation = myOverlays[id].animation;
        }
        openContainer(animation,myOverlays[id].container);
        if (typeof(myOverlays[id].postStartExec) == "function") {
            myOverlays[id].postStartExec();
        }
    }

    this.stopOverlay = function (id) {
        if (isEmpty(id) || isEmpty(myOverlays[id].container)) {
            logError("id '"+id+"' does not exist!");
            return false;
        }
        closeIt(id);
    }

    this.isOpen = function (id) {
        if (getStyle(getElement(myOverlays[id].container),"display") == "none") {
            return false;
        } else {
            return true;
        }
    }

}

