function getInnerWindowWidth()
{
    var window_width = 0;
    if ( typeof( window.innerWidth ) == 'number' )
       window_width = window.innerWidth;
    else
    if (
         document.documentElement
      && document.documentElement.clientWidth
       )
       window_width = document.documentElement.clientWidth;
    else
    if (
         document.body
      && document.body.clientWidth
       )
       window_width = document.body.clientWidth;

    return window_width;
}
function getInnerWindowHeight() {
    var window_Height = 0;
    if (typeof window.innerHeight == "number") {
        window_Height = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        window_Height = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight) {
        window_Height = document.body.clientHeight;
    }
    return window_Height;
}

function getSubmenuItems() {
    if (!getSubmenuItems.submenuItems) {
        getSubmenuItems.submenuItems = $('navigation_layer').childElements().findAll( function(elem) {
            return getAllMenuItemNames().include( elem.id.substr('navigation_layer_content_'.length ) );
        });
    };

    return getSubmenuItems.submenuItems;
}

function getSubmenuNames() {
    if (!getSubmenuNames.submenuNames) {
        getSubmenuNames.submenuNames = getSubmenuItems().collect(function( menuItem ) {
            return menuItem.id.substr('navigation_layer_content_'.length);
        });
    };
    return getSubmenuNames.submenuNames;
}

function getAllMenuItems() {
    if (!getAllMenuItems.menuItems) {
        getAllMenuItems.menuItems = $('navigation_content2').childElements().findAll(function(elem){
            return elem.tagName == 'A'; 
        });
    };
    
    return getAllMenuItems.menuItems; 
}

function getAllMenuItemNames() {
    if (!getAllMenuItemNames.submenuNames) {
        getAllMenuItemNames.submenuNames = getAllMenuItems().collect(function( menuItem ) {
            return menuItem.id.substr('navigation_'.length);
        });
    };
    return getAllMenuItemNames.submenuNames;
}

function getAllMenuItemsWithoutSubmenuNames (){
    if (!getAllMenuItemsWithoutSubmenuNames.submenuNames) {
        getAllMenuItemsWithoutSubmenuNames.submenuNames = getAllMenuItemNames().reject(function( menuItem ) {
            return getSubmenuNames().include(menuItem);
        });
    };
    return getAllMenuItemsWithoutSubmenuNames.submenuNames;
}







function activate_active(is_aktiv) {
    getSubmenuNames().each( function(naviItemName) {
        if( is_aktiv == naviItemName ) {
            if( $('navigation_' + naviItemName).hasClassName('active_permanent') ) {
                $('navigation_' + naviItemName).removeClassName('active_permanent');
                $('navigation_' + naviItemName).addClassName('active');
                window[naviItemName + '_open'] = true;
            }
        } else {
            if( $('navigation_' + naviItemName).hasClassName('active') ) {
                $('navigation_' + naviItemName).removeClassName('active');
                $('navigation_' + naviItemName).addClassName('active_permanent');
            }
        }
    });
}

function init_all() {
    // set global status vars

    getSubmenuNames().each( function(submenuName) {
        this[submenuName + '_open'] = false;
    }, window);
    
    // Check Navigationstatus
    activate_active();
    
    // CONTENT START
    if( $('body_willkommen') ) {
        var innerWindowWidth = getInnerWindowWidth() + "px";
        var contentHeight = $('content_start_news').getHeight() + 26 + "px";
        $('content_start_container').setStyle({
            width: innerWindowWidth,
            height: contentHeight
        });
        Event.observe(window, 'resize', function() {
            var innerWindowWidth = getInnerWindowWidth() + "px";
            $('content_start_container').setStyle({
                width: innerWindowWidth
            });
        });
    }
    
    // MAIN-NAVIGATION
    getSubmenuNames().each( function(submenuName) {
        Event.observe('navigation_' + submenuName, 'mouseover', function() {
            open_navigation_layer(submenuName,true);
            activate_active(submenuName);
        });
        Event.observe('navigation_' + submenuName, 'mouseout', function(e) {
            if (typeof event != 'undefined') {
                if (!event.toElement) {
                    return;
                }
            }
            if ( $(e.relatedTarget || event.toElement).descendantOf('navigation_layer')) {
                return;
            }
            hide_filter_popup();
            change_navigation_allLinksBack();
            activate_active();
        });
    });
    
    getAllMenuItemsWithoutSubmenuNames().each( function(WithoutSubmenuNames) {
        Event.observe('navigation_' + WithoutSubmenuNames, 'mouseover', function() {
            change_navigation_link('navigation_' + WithoutSubmenuNames,'short');
        });
        
        Event.observe('navigation_' + WithoutSubmenuNames, 'mouseout', function(e) {
            if (typeof event != 'undefined') {
                if (!event.toElement) {
                    return;
                }
            }
            if ( $(e.relatedTarget || event.toElement).descendantOf('navigation_layer')) {
                return;
            }
            change_navigation_allLinksBack();
            activate_active();
        });
    });

    Event.observe('navigation_layer', 'mouseout', function(e) {
        if (typeof event != 'undefined') {
            if (!event.toElement) {
                return;
            }
        }
        if ( $(e.relatedTarget || event.toElement).descendantOf('navigation_layer')) {
            return;
        }
        hide_filter_popup();
        change_navigation_allLinksBack();
        activate_active();
    });
}
// open Navigation Layer
function open_navigation_layer(which,show_carpet) {
    var popup = $('navigation_layer');
    change_navigation_allLinksBack();
    $('navigation_content2').setStyle({
        height: '300px'
    });
    
    Element.show( popup );
    
    // Pop-Up Inhalt setzen
    function clear_navigation() {
        getSubmenuItems().each( function(elem){
            elem.hide();
        });
    }
    clear_navigation();
    if( show_carpet ) {
        open_navigation_carpet();
    }
    change_navigation_link('navigation_' + which,'large');
    Element.show( 'navigation_layer_content_' + which );
}

function open_navigation_carpet() {
    var innerWindowWidth = getInnerWindowWidth() + "px";
    $('navigation_carpet').setStyle({
        display: 'block',
        width: innerWindowWidth
    });
}
function hide_navigation_carpet() {
    $('navigation_carpet').setStyle({
        display: 'none'
    });
}


function change_navigation_link(which,length) {
    switch (length) {
        case 'short':
            $(which).addClassName('active_short');
            break;
        case 'large':
            $(which).addClassName('active');
            break;
    }
}

function change_navigation_allLinksBack() {
    getSubmenuNames().each( function(submenuName) {
        if( !window[submenuName + '_open'] ) {
            $('navigation_' + submenuName).removeClassName('active');
        }
    });
    getAllMenuItemsWithoutSubmenuNames().each( function(WithoutSubmenuNames) {
        $('navigation_' + WithoutSubmenuNames).removeClassName('active_short');
    });
    
    hide_navigation_carpet();
}

function hide_filter_popup() {
    var popup = $('navigation_layer');
    $('navigation_content2').setStyle({
        height: '52px'
    });

    Element.hide( popup );
}
