Ext.namespace('Virt');
Virt.app = function() {
    var menuList, menu, menuTimer, menuShow = false;
    var isAlreadySmall = false;
    var isSoundPlay = false;
    var menuIn = function() {
        if (menuTimer) window.clearTimeout(menuTimer);
        if (menuShow) return;
        menuShow = true;
        menuList.stopFx();
        if (Ext.isIE) menuList.show();
        else menuList.fadeIn({duration: .25});
    }
    var menuOut = function() {
        if (menuTimer) window.clearTimeout(menuTimer);
        if (!menuShow) return;
        menuShow = false;
        menuList.stopFx();
        if (Ext.isIE) menuList.hide();
        else menuList.fadeOut({duration: .25});
    }
    var playSound = function() {
        if (isSoundPlay) return;
        isSoundPlay = true;
        Ext.get('sound-button').removeClass('off');
        set_cookie('stopSound',0,null,'/');
        Ext.get('sound-button').removeClass('off');
        Ext.get('sound-button').insertHtml('beforeEnd', '<div style="width:1px;height:1px;visibility:hidden;position:absolute;left:0;top:0;" id="soundWrapper"></div>');
        var flashvars = {};
        var params = {
            menu: "false",
            scale: "noScale",
            allowFullscreen: "true",
            allowScriptAccess: "always"
        };
        var attributes = {};
        swfobject.embedSWF("/swf/background_sound.swf", "soundWrapper", "1", "1", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
    }
    var stopSound = function(dnsc) {
        isSoundPlay = false;
        Ext.get('sound-button').addClass('off');
        if (!dnsc) set_cookie('stopSound',1,null,'/');
        if (Ext.get('soundWrapper')) Ext.get('soundWrapper').remove();
        if (Ext.get("flashIntro")) Ext.get("flashIntro").remove();
    }
    var initMenu = function() {
        menu = Ext.get('menu');
        menu.removeClass('cssHover');
        menuList = Ext.get('menu-list');
        Ext.get('menu').on('mouseover', function() {
            menuIn();
        });
        menu.on('mouseout', function(ev,el) {
            if(!ev.within(menu,true))
            {
                menuTimer = window.setTimeout(menuOut, 1000);
            }
        });
        if (get_cookie('stopSound') == 1) stopSound();
        else if (Ext.get('body-128')) stopSound(true);
        else {
            if (!Ext.get('body-61')) playSound();
            else isSoundPlay = true;
        }
        Ext.get('sound-button').on('click', function() {
            if (isSoundPlay) {
                stopSound();
            } else {
                playSound();
            }
        });
    }
    
    var initCheckBoxGroups = function() {
        var fields = Ext.select('input.checkbox-group');
        for (var i = 0; i < fields.elements.length; i++) {
            if (!fields.elements[i].checked) {
                var r = fields.elements[i].className.match(/group-(\d)/);
                if (r) {
                    Ext.select('div.group-box-'+r[1]).setDisplayed(false);
                }
            }
        }
        fields.on('click', function(ev,el) {
            var r = el.className.match(/group-(\d)/);
            if (r) {
                Ext.select('div.group-box-'+r[1]).setDisplayed(el.checked);
                if (!el.checked) {
                    var fields = Ext.select('div.group-box-'+r[1]+' input.checkbox');
                    for (var i = 0; i < fields.elements.length; i++) {
                        fields.elements[i].checked = false;
                    }
                }
            }
        });
    }
    
    var showStartpageButtons = function() {
        Ext.get("start-page-btn_desktop").fadeIn({duration: 0.5});
        Ext.get("start-page-btn_weitere").fadeIn({
            duration: 0.5,
            callback: function() {
                Ext.get("start-page-btn_admin").fadeIn({duration: 0.5});
                Ext.get("start-page-btn_anwender").fadeIn({duration: 0.5});
                Ext.get("start-page-btn_mobil").fadeIn({
                    duration: 0.5,
                    callback: function() {
                        Ext.get("start-page-btn_test").fadeIn({duration: 0.5});
                        Ext.get("start-page-btn_news").fadeIn({duration: 0.5});
                    }
                });
            }
        });
    }
    var initFlashIntro = function() {
        
        Ext.get("rahmen").insertHtml('beforeEnd', '<div style="position:absolute;width:951px;height:711px;left:13px;top:13px;" id="flashIntro"><div id="flashIntroContent"></div></div>');
        var flashvars = {};
        var params = {
            menu: "false",
            scale: "noScale",
            allowFullscreen: "true",
            allowScriptAccess: "always",
            wmode: "transparent"
        };
        var attributes = {};
        swfobject.embedSWF("/swf/intro.swf", "flashIntroContent", "951px", "711px", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
    }
    return {
        init: function() {
            initMenu();
            if (Ext.get('body-61') && swfobject.getFlashPlayerVersion().major >= 9 && get_cookie('stopSound') != 1) {
                initFlashIntro();
            } else {
                Ext.get('background-image').show();
                if (Ext.get('content-slide-1')) new Ext.ux.ScrollPane('content-slide-1', {scrollbarWidth:17,dragMinHeight:12,dragMaxHeight:9999});
                if (Ext.get('content-slide-2')) new Ext.ux.ScrollPane('content-slide-2', {scrollbarWidth:17,dragMinHeight:12,dragMaxHeight:9999});
                if (Ext.get('content-slide-3')) new Ext.ux.ScrollPane('content-slide-3', {scrollbarWidth:17,dragMinHeight:12,dragMaxHeight:9999});
                if (Ext.get('start-page-buttons')) {
                    showStartpageButtons();
                }
                if (Ext.get('body-11')) {
                    Shadowbox.open({
                        content: '/swf/special.swf',
                        player: 'swf',
                        width: 960,
                        height: 677
                    });
                }
            }
            initCheckBoxGroups();
        },
        makeFlashSmall: function() {
            if (isAlreadySmall) return;
            isAlreadySmall = true;
            var fl1 = Ext.get("flashIntro");
            var fl2 = Ext.get("flashIntroContent");
            fl1.setStyle('width', '1px');
            fl1.setStyle('height', '1px');
            fl2.setStyle('width', '1px');
            fl2.setStyle('height', '1px');
            fl1.setVisible(false);
            fl2.setVisible(false);
            Ext.get("background-image").show();
            showStartpageButtons();
        }
    }
}();

Ext.onReady(function(){
    Virt.app.init();
});

function get_cookie ( cookie_name )
{
    var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
    if ( results )
        return ( unescape ( results[2] ) );
    else
        return null;
}

function set_cookie ( name, value, expires, path, domain, secure )
{
    var cookie_string = name + "=" + escape ( value );
    if ( expires )
    {
        cookie_string += "; expires=" + expires.toGMTString();
    }
    if ( path )
        cookie_string += "; path=" + escape ( path );
    if ( domain )
        cookie_string += "; domain=" + escape ( domain );
    if ( secure )
        cookie_string += "; secure";
    document.cookie = cookie_string;
}
function makeFlashSmall(auto)
{
    Virt.app.makeFlashSmall();
}

function validateContactForm(el) {
    var form = Ext.get(el);
    var error = false;
    if (Ext.get('form_firma').getValue() == "") error = true;
    if (Ext.get('form_position').getValue() == "") error = true;
    if (Ext.get('form_lastname').getValue() == "") error = true;
    if (Ext.get('form_strasse').getValue() == "") error = true;
    if (Ext.get('form_ort').getValue() == "") error = true;
    if (Ext.get('form_email').getValue() == "") error = true;
    if (Ext.get('form_message').getValue() == "") error = true;
    if (error) {
        alert('Bitte füllen Sie alle Pflichtfelder aus!');
        return false;
    } else {
        return true;
    }
}
