
activeMenuAnimation = false;
activeSubmenuAnimation = false;
currentMenuName = 'none';
currentSubmenuName = null;
contentBoxAnimation = null;
contentTextAnimation = null;
footerLineActive = false;
footerLineAnimation = null;
footerContentActive = false;
footerLineTimer = null;
footerActiveItem = null;
footerBoxAnimation = null;
footerTextAnimation = null;
excontentActive = false;
excontentBoxAnimation = null;
excontentTextAnimation = null;
contentBackgroundOpacity = 0.85;
excontentBackgroundOpacity = 0.85;
footerContentBackgroundOpacity = 0.7;
contentTextScroller = null;
contentTextScrollerStarter = null;
contentPreloaded = false;


function afterAjaxContent() {
    if( footerActiveItem ) {
        $('footer-content-loading').hide();
        if( footerTextAnimation )
            footerTextAnimation.cancel();
        
        footerTextAnimation = new Effect.Opacity( 'footer-content-text', { from: 0, to: 1, duration: 0.3 } );
        $('footer-content-text').scrollBar.resetToContent();
    }
    else if( excontentActive ) {
        $('excontent-loading').hide();
        
        if( excontentTextAnimation )
            excontentTextAnimation.cancel();
        
        excontentTextAnimation = new Effect.Opacity( 'excontent-text', { from: 0, to: 1, duration: 0.3 } );
        $('excontent-text').scrollBar.resetToContent();
    }
    else {
        $('content-loading').hide();
        if( contentTextAnimation )
            contentTextAnimation.cancel();
        
        contentTextAnimation = new Effect.Opacity( 'content-text', { from: 0, to: 1, duration: 0.3 } );
        
        $('tekst').style.marginTop = '0px';
        
        $('content-text').scrollBar.resetToContent();

        var links = $A( $('tekst').getElementsByTagName( 'a' ) );
        for( var n = 0; n < links.length; n++ ) {
            if( links[n].hasAttribute('onclick') ) {
                links[n].setAttribute( 'onclick', 'openExContent(this); return false;' );
            }
        }
    }
}

function initNavigation() {
    activeMenuLine = $('menu-line-none');
    
    var solutions = $('intro-solutions');
    activeBackground = solutions;
    
    var contentBack = $('content-background');
    contentBack.setOpacity( contentBackgroundOpacity );
    
    var excontentBack = $('excontent-background');
    excontentBack.setOpacity( excontentBackgroundOpacity );
    
    new Scrollbar( $('excontent-text'), $('excontent') );
    new Scrollbar( $('content-text'), $('content-text-wrapper') );
    new Scrollbar( $('footer-content-text'), $('footer-content-text-wrapper') );
    
    var tekst = $('tekst');
    tekst.onmouseover = function() { tekst.dontScroll = true; };
    tekst.onmouseout = function() { tekst.dontScroll = false; };
    tekst.dontScroll = false;
    
    solutions.setOpacity( 0 );
    solutions.style.marginLeft = '700px';

    $('footer-content-background').setOpacity( contentBackgroundOpacity );
    
    $('menu').morph( { 'margin-left': '0px' }, { 'delay': 0.2, 'duration': 0.4 } );
    $('menu-line-none').morph( { 'margin-left': '0px' }, { 'delay': 0, 'duration': 0.5 } );

    new Effect.Parallel( [
        new Effect.Move( $('footer-background'), { sync: true, y: -22, mode: 'relative' } ),
        new Effect.Move( $('footer'), { sync: true, y: -22, mode: 'relative' } )
    ], { 'delay': 1.5, 'duration': 1 } );
    
    var path = window.location.pathname.substr(1).split('/');
    if( path.length == 2 && (path[0] == 'graphic' || path[0] == 'online' || path[0] == 'broadcast') ) {
        contentPreloaded = true;
        openMenu( path[0], path[1] );
        return;
    }
    else if( path.length == 3 ) {
        contentPreloaded = true;
        WPRequest( path[0]+'/'+path[1], 'tekst' );
        openMenu( path[0], path[1] );
        setTimeout( 'openExContent(); afterAjaxContent();', 1000 );
        return;
    }
    else if( path.length = 1 && path[0].length != 0 && parseInt(path[0])+'' != path[0] ) {
        footerItemOver( $('footer-'+path[0]) );
        openFooterItem( $('footer-'+path[0]) );
        return;
    }

    new Effect.Parallel( [
        new Effect.Move( solutions, { sync: true, x: -700, mode: 'relative' } ),
        new Effect.Opacity( solutions, { sync: true, from: 0, to: 1 } )
    ], { 'delay': 0.5, 'duration': 1.8 } );
}


function openMenu( name, subname ) {
    if( activeMenuAnimation || currentMenuName == name )
        return;

    if( footerActiveItem ) {
        if( footerBoxAnimation ) {
            footerBoxAnimation.cancel();
            footerBoxAnimation = null;
        }
        
        footerActiveItem.removeClassName( 'current' );
        $('footer-content-shade').hide();
        
        footerBoxAnimation = new Effect.Parallel( [
            new Effect.Fade( 'footer-content-background', { sync: true } ),
            new Effect.Fade( 'footer-content-text', { sync: true } )
        ], { afterFinish: function() { footerBoxAnimation = null; } } );
        
        footerLineActive = false;
        var line = $('footer-line');
        footerLineAnimation = new Effect.Move( line, { y: 18, x: parseInt(line.style.left), mode: 'absolute', duration: 0.3, afterFinish: function() { footerLineAnimation = null; } } );
        footerActiveItem = null;
    }
    
    if( typeof(activeMenuLine) != 'undefined' ) {
        if( currentMenuName != name ) {
            activeMenuLine.fade();
            
            if( currentMenuName != 'none' ) {
                $('menu-'+currentMenuName).down().removeClassName( 'current' );
                $('submenu-'+currentMenuName).hide();
                if( currentSubmenuName ) {
                    $('sub-'+currentMenuName.substr(0,1)+'-'+currentSubmenuName).down().removeClassName('current');
                    $('sub-'+currentMenuName.substr(0,1)+'-line-'+currentSubmenuName).hide();
                    currentSubmenuName = null;
                }
            }
        }
    }
    
    currentMenuName = name;
    
    activeMenuAnimation = true;
    
    if( name != 'none' )
        $('menu-'+name).down().addClassName( 'current' );
    
    activeMenuLine = $('menu-line-'+name);
    new Effect.Appear( activeMenuLine, { afterFinish: function() { activeMenuAnimation = false; } } );
    
    activeBackground.up().style.zIndex = 99;
    activeBackground.fade();
    
    var backgrounds = $('backgrounds-'+name).childElements();
    activeBackground = backgrounds[Math.floor(Math.random()*backgrounds.length)];
    activeBackground.up().style.zIndex = 100;
    activeBackground.appear();
    
    if( name != 'none' ) {
        if( contentBoxAnimation ) {
            contentBoxAnimation.cancel();
            contentBoxAnimation = null;
        }
        $('content-background').show().setOpacity( contentBackgroundOpacity );
        $('content-container').show().setOpacity( 1 );
        var submenu = $('submenu-'+name);
        submenu.show();
        if( subname )
            openSubmenu( subname, $('sub-'+name.substr(0,1)+'-'+subname) );
        else {
            var firstLink = $('sub-'+name.substr(0,1)+'-'+submenu.down().id.substr(11)).down();
            openSubmenu( submenu.down().id.substr(11), firstLink );
        }
    }
}

function openSubmenu( name, a ) {
    if( activeSubmenuAnimation )
        return;
    
    if( excontentActive )
        closeExContent();
    
    if( currentSubmenuName != name ) {
        if( currentSubmenuName ) {
            var oldline = $('sub-'+currentMenuName.substr(0,1)+'-line-'+currentSubmenuName);
            oldline.fade();
            $('sub-'+currentMenuName.substr(0,1)+'-'+currentSubmenuName).down().removeClassName( 'current' );
        }

        activeSubmenuAnimation = true;
    
        var newline = $('sub-'+currentMenuName.substr(0,1)+'-line-'+name);
        new Effect.Appear( newline, { afterFinish: function() { activeSubmenuAnimation = false; } } );

        $('sub-'+currentMenuName.substr(0,1)+'-'+name).down().addClassName( 'current' );
    
        currentSubmenuName = name;
    }
    
    if( contentTextAnimation )
        contentTextAnimation.cancel();
    
    if( contentPreloaded ) {
        $('content-loading').hide();
        $('content-text').setOpacity( 1 );
        contentPreloaded = false;
    }
    else {
        $('content-loading').show();
        contentTextAnimation = new Effect.Opacity( 'content-text', { from: 1, to: 0, duration: 0.3, afterFinish: function() { contentTextAnimation = null; WPRequest( a.href ); } } );
    }
}

function openFooterItem( a ) {
    if( excontentActive )
        closeExContent();
    
    if( footerActiveItem ) {
        footerActiveItem.removeClassName( 'current' );

        if( footerTextAnimation )
            footerTextAnimation.cancel();

        $('footer-content-loading').show();
        footerTextAnimation = new Effect.Opacity( 'footer-content-text', { from: 1, to: 0, duration: 0.3, afterFinish: function() { footerTextAnimation = null; WPRequest( a.href ); } } );
    }
    else {
        if( footerBoxAnimation ) {
            footerBoxAnimation.cancel();
            footerBoxAnimation = null;
        }
        
        openMenu( 'none' );
        var fcb = $('footer-content-background');
        var fct = $('footer-content-text');
        fcb.show().setOpacity(0);
        fct.show().setOpacity(0);
        
        footerBoxAnimation = new Effect.Opacity( fcb, { from: 0, to: footerContentBackgroundOpacity, afterFinish: function() { footerBoxAnimation = null; } } );
        WPRequest( a.href );
    }
    
    footerActiveItem = $(a);
    footerActiveItem.addClassName( 'current' );
}

function openExContent( a ) {
    if( excontentActive ) {
        if( excontentTextAnimation )
            excontentTextAnimation.cancel();
        
        $('excontent-loading').show();
        excontentTextAnimation = new Effect.Opacity( 'excontent-text', { from: 1, to: 0, duration: 0.3, afterFinish: function() { excontentTextAnimation = null; if(a) WPRequest( a.href, 'excontent' ); } } );
    }
    else {
        if( excontentBoxAnimation ) {
            excontentBoxAnimation.cancel();
            excontentBoxAnimation = null;
        }
        
        var exb = $('excontent-background');
        var ext = $('excontent-text');
        exb.show().setOpacity(0);
        ext.show().setOpacity(0);
        
        excontentBoxAnimation = new Effect.Opacity( exb, { from: 0, to: excontentBackgroundOpacity, afterFinish: function() { excontentBoxAnimation = null; } } );
        if(a) WPRequest( a.href, 'excontent' );
    }

    excontentActive = true;
}

function closeExContent() {
    if( excontentBoxAnimation ) {
        excontentBoxAnimation.cancel();
        excontentBoxAnimation = null;
    }
    if( excontentTextAnimation ) {
        excontentTextAnimation.cancel();
        excontentTextAnimation = null;
    }
    
    excontentBoxAnimation = new Effect.Parallel( [
        new Effect.Fade( 'excontent-background', { sync: true } ),
        new Effect.Fade( 'excontent-text', { sync: true } )
    ], { afterFinish: function() { excontentBoxAnimation = null; } } );
    
    excontentActive = false;
}

function footerItemOver( a ) {
    if( footerLineTimer ) {
        window.clearTimeout( footerLineTimer );
        footerLineTimer = null;
    }
    if( footerLineAnimation ) {
        footerLineAnimation.cancel();
        footerLineAnimation = null;
    }
    
    var elOffset = $(a).positionedOffset()[0] + 21 + Math.round(a.getWidth()/2);
    var line = $('footer-line');
    
    if( footerLineActive ) {
        line.style.top = '0px';
        footerLineAnimation = new Effect.Move( line, { y: 0, x: elOffset, mode: 'absolute', duration: 0.3, afterFinish: function() { footerLineAnimation = null; } } );
    }
    else {
        $('footer-content-shade').show();
        if( currentMenuName != 'none' ) {
            if( contentBoxAnimation )
                contentBoxAnimation.cancel();
            
            contentBoxAnimation = new Effect.Parallel( [
                new Effect.Fade( 'content-container', { sync: true } ),
                new Effect.Fade( 'content-background', { sync: true } )
            ], { afterFinish: function() { contentBoxAnimation = null; } } );

            if( excontentActive ) {
                if( excontentBoxAnimation )
                    excontentBoxAnimation.cancel();
                
                excontentBoxAnimation = new Effect.Parallel( [
                    new Effect.Opacity( 'excontent-background', { sync: true, from: excontentBackgroundOpacity, to: 0 } ),
                    new Effect.Opacity( 'excontent-text', { sync: true, from: 1, to: 0 } )
                ], { afterFinish: function() { excontentBoxAnimation = null; } } );
            }
        }
        line.style.left = elOffset+'px';
        footerLineAnimation = new Effect.Move( line, { y: 0, x: elOffset, mode: 'absolute', duration: 0.3, afterFinish: function() { footerLineAnimation = null; } } );
        footerLineActive = true;
    }
}

function footerItemOut( a ) {
    footerLineTimer = window.setTimeout( function() {
        if( footerLineAnimation ) {
            footerLineAnimation.cancel();
            footerLineAnimation = null;
        }
        
        if( ! footerActiveItem ) {
            $('footer-content-shade').hide();
        
            if( currentMenuName != 'none' ) {
                if( contentBoxAnimation )
                    contentBoxAnimation.cancel();
                
                var bg = $('content-background');
                bg.show().setOpacity(0);
                contentBoxAnimation = new Effect.Parallel( [
                    new Effect.Appear( 'content-container', { sync: true } ),
                    new Effect.Opacity( bg, { from: 0, to: contentBackgroundOpacity, sync: true } )
                ], { afterFinish: function() { contentBoxAnimation = null; } } );
            }
            
            if( excontentActive ) {
                if( excontentBoxAnimation )
                    excontentBoxAnimation.cancel();
                
                excontentBoxAnimation = new Effect.Parallel( [
                    new Effect.Opacity( 'excontent-background', { sync: true, from: 0, to: excontentBackgroundOpacity } ),
                    new Effect.Fade( 'excontent-text', { sync: true, from: 0, to: 1 } )
                ], { afterFinish: function() { excontentBoxAnimation = null; } } );
            }
            
            footerLineActive = false;
            var line = $('footer-line');
            footerLineAnimation = new Effect.Move( line, { y: 18, x: parseInt(line.style.left), mode: 'absolute', duration: 0.3, afterFinish: function() { footerLineAnimation = null; } } );
        }
        else {
            var elOffset = footerActiveItem.positionedOffset()[0] + 21 + Math.round(footerActiveItem.getWidth()/2);
            var line = $('footer-line');
            line.style.top = '0px';
            footerLineAnimation = new Effect.Move( line, { y: 0, x: elOffset, mode: 'absolute', duration: 0.3, afterFinish: function() { footerLineAnimation = null; } } );
        }
    }, 1000 );
}




