dojo.addOnLoad(function() {   //executable code from different .js files gathered here
    dojo.event.connect(document, "onmousemove", panraven.mouse, "moveListener");
    var modalSubstLayer = dojo.byId("modalSubstLayer");
    if (modalSubstLayer)
        dojo.event.connect(modalSubstLayer, "onmousedown", function() {
            if (!popupDialog.getActive() || !popupDialog.getActive().isModal) {
                popupDialog.hide();
            }
        });
    dojo.event.connect(document, "onmousedown", advSelectDialog, "hide");

    //dragNdrop.js
    dojo.event.connect(document, "onmousemove", window, "globalMouseMove");

    //dialog.js
    popupDialog.addListener("onBeforeShow", function(active) {
        if (dialogRefreshContent(active)) {
            showDialog(active.dialog.id, active.initialTabIndex ? active.initialTabIndex : parseInt(active.dialog.getAttribute('defaultTabIndex')));
        }
    });

    popupDialog.addListener("onTabSwitch", dialogRefreshContent);

    /*clear innerHTML on hide*/
    popupDialog.addListener("onHide", function(active) {
        var dialog = active.dialog;
        if (dojo.html.hasClass(dialog, 'Dialog')) {
            if (dialog.getAttribute("isLightWeight") == "true") return;
            var _remove_ids = eval('document.' + dialog.id + '_formIds');
            if (!_remove_ids) return;

            var remove_ids = _remove_ids.split(',');
            if (remove_ids[remove_ids.length-1] == '') {
                remove_ids.pop();
            }
            var formName = dialog.getAttribute('formName');
            var frm = formName && formName != 'undefined' ? find_form(formName) : undefined;
            if (!frm) return;
            var _formids = frm.elements['formids'];
            var formids = _formids.value.split(',');
            for(var q = 0; q < formids.length; q++) {
                if (formids[q] == remove_ids[0]) {
                    formids.splice(q, remove_ids.length);
                    break;
                }
            }
            _formids.value = formids.join(',');
            dojo.byId(dialog.id + 'tabContentRegion').innerHTML = "";
        }
    });

    panraven.event.subscribe("/ajaxCallProcessed", function() {
        displayRequestLayer(false);
        window.clearTimeout(document.removeLayerTimeout);
    });

    window.dialogTailsByClassId = {"DialogBorderGrey" : {tailTop: dojo.byId("dialogBorderTopGreyTail"), tailBottom: dojo.byId("dialogBorderBotGreyTail")},
                "DialogBorderWhite" : {tailTop: dojo.byId("dialogBorderTopWhiteTail"), tailBottom: dojo.byId("dialogBorderBotWhiteTail")},
                "DialogBorderAudio" : {tailTop: dojo.byId("dialogBorderAudioTopTail"), tailBottom: dojo.byId("dialogBorderAudioBotTail")}
    };


    /*recalculating vertical position for tailed dialogs*/
    popupDialog.addListener ("onBeforeShow", function (active) {
        var dialog = active.dialog;
        var dialogTop;
        if (dojo.html.hasClass(dialog, 'DialogBorder') && dojo.html.hasClass(dialog, 'TailedDialog') && window.dialogTailsByClassId) {
            var dialogClassId = dialog.getAttribute("classId");
            var dialogTail = window.dialogTailsByClassId[dialogClassId].tailTop;
            var reflection = active.reflectionVert;
            switch (reflection) {
                case popupDialog.reflectionType.BOTTOM:
                    dialogTop = (parseInt(dialog.style.top) + dialogTail.height - 2) + 'px';
                    break;
                case popupDialog.reflectionType.TOP:
                    dialogTop = (parseInt(dialog.style.top) - dialogTail.height + 2) + 'px';
                    break;
                default:
                    throw Error("Reflection type not supported!");
            }
            if (!active.dialogPositionGetter) {
                dialog.style.top = dialogTop;
            }
        }
    }, true);

    /*choosing and positioning bubble tail*/
    popupDialog.addListener("onBeforeShow", function(active) {
        var dialog = active.dialog;
        var tail, tailTop;
        if (dojo.html.hasClass(dialog, 'DialogBorder') && dojo.html.hasClass(dialog, 'TailedDialog') && window.dialogTailsByClassId) {
            var reflection = active.reflectionVert;
            var dialogClassId = dialog.getAttribute("classId");
            switch (reflection) {
                case popupDialog.reflectionType.BOTTOM:
                    tail = window.dialogTailsByClassId[dialogClassId].tailTop;
                    tailTop = (parseInt(dialog.style.top) - tail.height + 2 + (panraven.isIE6minus ? 2 : 0));
                    break;
                case popupDialog.reflectionType.TOP:
                    tail = window.dialogTailsByClassId[dialogClassId].tailBottom;
                    tailTop = (parseInt(dialog.style.top) + dialog.offsetHeight - 2 - (panraven.isIE6minus ? 2 : 0));
                    break;
                default:
                    throw Error("Reflection type not supported!");
            }
            var borderWidth = 17;
            var contentLeft = parseInt(dialog.style.left) + borderWidth + tail.width/2;
            var contentRight = parseInt(dialog.style.left) + dialog.offsetWidth - borderWidth - tail.width/2;
            active.tail = tail;
            if (active.dialogPositionGetter)
                return;
            var tailLeft = (panraven.mouse.posX - tail.width/2);
            if (tailLeft <= contentLeft) {
                tailLeft = contentLeft;
            } else if (tailLeft >= contentRight) {
                tailLeft = contentRight;
            }
            tail.style.left = tailLeft + 'px';
            tail.style.top = tailTop + 'px';
            tail.style.display = "block";
        }
    });

    /*recalculate position if getter is specified*/
    popupDialog.addListener("onBeforeShow", function(active) {
        if (active.dialogPositionGetter) {
            popupDialog.recalculatePosition();
        }
    });

    dojo.event.connect(document, "onkeypress", function(event) {
        if (!event) event = window.event;
        switch(event.keyCode) {
            case 27:
                if (popupDialog.getActive() && !popupDialog.getActive().modal) {
                    if (popupDialog.getActive() && popupDialog.getActive().closeAction) {
                        eval(popupDialog.getActive().closeAction);
                    } else {
                        popupDialog.hide();
                    }
                }
                break;
            case 13:
                if (popupDialog.getActive() != null && !document.dialogTextAreaFocused) {
                    var dialog = popupDialog.getActive().dialog;
                    var defaultButton = dojo.html.getElementsByClassName("default-button", dialog, "*", dojo.html.classMatchType.ContainsAny, true)[0];
                    if (defaultButton) {
                        _fireEvent(defaultButton, "onclick");
                        var link = defaultButton.parentNode;
                        if (!link) return;
                        if (dojo.html.hasClass(link, "rb-marker-div"))
                            link = link.parentNode;
                        if (!link) return;
                        if (link.tagName.toLowerCase() == 'a') {
                            pressLink(link);
                        }
                    }
                    return;
                }
                //search
                if (document.activeSearchButton && (!dojo.exists(event, "target.id") || event.target.id.indexOf('_searchByTagInputField') == -1)) {
                    if (!document.activeSearchButton.onclick || document.activeSearchButton.onclick() != false) {
                        var buttonHref = document.activeSearchButton.href;
                        if (buttonHref.indexOf('javascript:') != -1) {
                            buttonHref = decodeURIComponent(buttonHref);
                            eval(buttonHref.substring(buttonHref.indexOf('javascript:')+11));
                        }
                    }
                }
        }
    });

    panraven.event.subscribe("/shellResized", window, "dialogOnResize");

    popupDialog.addListener("onAfterHide", function(active) {
        active.dialog.style.left = "-5000px";
        active.dialog.style.display = "block";
    });

    //videoPanel.js

    if (window.VideoPanel) {
        panraven.event.subscribe("videoVolume", function(volume) {
            g_videoPanel_soundVolume = parseInt(volume);
        });

        setInterval(function() {
            if (g_videoPanel_soundVolume != g_videoPanel_soundVolume_temp) {
                g_videoPanel_soundVolume_temp = g_videoPanel_soundVolume;
                panraven.event.publish("globalVolumeChanged");
            }
        }, 100);
    }

});
