﻿var Util = {
    closeOnEsc: false,
    onKeyDown: function(e) {
        e = (e) ? e : event;
        // close window on Esc key
        if(e.keyCode == Event.KEY_ESC && Util.closeOnEsc) {
            window.close(); 
        }
    },
    // open modal dialog (IE) or child window
    openDialog: function(url, width, height, name) {
        if(!width) {
            width = 300;
            height = 300;
        }
        if(!name) {
            name = '';
        }
        //if (window.showModalDialog) {
        if (false) {
            window.showModalDialog(url,window,'resizable:yes;dialogWidth:'+width+'px;dialogHeight:'+height+'px');
        } else {
            var win = window.open(url,name,'width='+width+',height='+height+',toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes');
            if(win==null) {
                alert('Otvorenie okna bolo zablokované!');
            }
            else {
                win.focus();
            }
        }
    },
    // get window opener
    getWindowOpener: function() {
        if(window.opener != null && !window.opener.closed) {
            return window.opener;
        }
        if(window.dialogArguments) {
            return window.dialogArguments;
        }
        return null;
    },
    // queryParams of current url
    queryParams: function() {
        if(!Util.__currentUrlQueryParams) {
            Util.__currentUrlQueryParams = window.document.location.search.toQueryParams();
        }
        return Util.__currentUrlQueryParams;
    },
    // dialog callback
    callBack: function() {
        var opener = Util.getWindowOpener();
        if(opener != null) {
            if(Util.queryParams()._callBackId) {
                var f = opener.eval(Util.queryParams()._callBackId);
                if(f) {
                    f.bind(opener, arguments)();
                }
                if(Util.queryParams()._closeAfterSet) {
                    window.close();
                }                
            }
        }
    }
}

// bind to events
Event.observe(window,'load',function(e) {
    Event.observe(document,'keydown',Util.onKeyDown);
});

