
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;
contentBackgroundOpacity = 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 } );
	}
	else {
		$('content-loading').hide();
		if( contentTextAnimation )
			contentTextAnimation.cancel();
		
		contentTextAnimation = new Effect.Opacity( 'content-text', { from: 0, to: 1, duration: 0.3 } );
		
		if( contentTextScroller ) {
			clearInterval( contentTextScroller );
			contentTextScroller = null;
		}
		
		$('tekst').style.marginTop = '0px';
		
		if( contentTextScrollerStarter )
			clearTimeout( contentTextScrollerStarter );

		contentTextScrollerStarter = setTimeout( startContentTextScroller, 3000 );
	}
}

function initNavigation() {
	activeMenuLine = $('menu-line-none');
	
	var solutions = $('intro-solutions');
	activeBackground = solutions;
	
	var contentBack = $('content-background');
	contentBack.setOpacity( contentBackgroundOpacity );
	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 = 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 )
		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;
				}
			}
		}
	}
	
	activeMenuAnimation = true;
	
	if( name != 'none' )
		$('menu-'+name).down().addClassName( 'current' );
	
	activeMenuLine = $('menu-line-'+name);
	new Effect.Appear( activeMenuLine, { afterFinish: function() { activeMenuAnimation = false; } } );
	
	if( currentMenuName != name ) {
		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();
	}
	
	currentMenuName = name;
	
	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( 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( 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 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; } } );
		}
		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; } } );
			}
			
			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 );
}

function startContentTextScroller() {
	var content = $('tekst');
	if( content.getHeight() > 347 ) {
		contentTextScroller = setInterval( function() {
			if( content.dontScroll ) return;
			var scrollHeight = content.getHeight();
			if( parseInt(content.style.marginTop)*-1 < scrollHeight - 347 )
				content.style.marginTop = -1*((-1*parseInt(content.style.marginTop))+1) + 'px';
		}, 20 );
	}
}










