window.addEvent( 'domready', function() 
{	
	/*$$('a[rel=extern]').each(function(el)
	{
		//console.log( el );
		el.addEvent( 'click', function(event)
	    {
	    	event.stop()
	    	
	    });
	});*/
	
	/*$$('#tabs ul li').each(function(el)
	{
		el.set('effects', new Fx.Morph( el, {duration: 200, transition: Fx.Transitions.Sine.easeInOut}));
	    el.set( 'class', 'tabstat_normal' );
	    
	    el.addEvent( 'mouseover', function(e)
	    {
			//el.fade( 1 );
			//el.setStyle( 'opacity', 1 );
			new Fx.Morph( el, {duration: 200, transition: Fx.Transitions.Sine.easeInOut}).start( {'opacity':1} );
			//el.tween('opacity', 0.8);
	    });
	    el.addEvent( 'mouseout', function(e)
	    {
			//el.fade( [0, 0.5] );
	    });
	});*/


	// Navigator
	
	$$('.tag span').each(function(el)
	{
	    el.addEvent( 'click', function(event)
	    {
	    	event.stop();
	       	suche( [ this.getParent().getProperty('href').replace(/#/g,'') ] );
	       	navigatorOpen();
	    });
	});
	
	var url = this.location.href;
	var show = ( !url.contains('info') && !url.contains('kontakt') && !url.contains('login') && !url.contains('admin') ) ? true : false;
	
	var navigateSlide = new Fx.Morph( 'navigator', { duration: 1300, fps:150, transition: Fx.Transitions.Pow.easeOut, wait: false } );
	var navigateAlpha = new Fx.Morph( 'navigator', { duration: 500, transition: Fx.Transitions.Circ.easeInOut, wait: false } );
	var opened = show;
	var height = $( 'navigator' ).getStyle( 'height' );
	var autoSlide;
	
	/*$('head').addEvent('mouseout',function()
	{
		autoSlide = setTimeout(function()
		{
			if( opened )
			{
				opened = true;
				navigatorToggle( );	
			}	
		},
		1000);
	});
	$('head').addEvent('mouseover',function()
	{
		clearTimeout(autoSlide);
	});*/
	
	if( !show )
	{
		navigateSlide.set( 
			{
				'height': '5px',
				'opacity': 0
			} 
		);
		$$( '#slider div' ).toggleClass( 'closed' );
	}	
	
	function navigatorOpen( )
	{
		var toggle = opened;
		opened = false;
		
		clearTimeout(autoSlide);
		navigatorToggle( toggle );
	}
	function navigatorToggle( notoggle )
	{
		opened = !opened;
		
		clearTimeout(autoSlide);
	
		if( !opened )
		{
			suche( false );
			navigateSlide.start( 
				{
					'height': '5px'
				} 
			);
			navigateAlpha.start( { 'opacity': 0 } );
		}
		else
		{
			navigateSlide.start( 
				{
					'height': height
				} 
			);
			navigateAlpha.start( { 'opacity': 1 } );
		}
		
		
		if( !notoggle ) $$( '#slider div' ).toggleClass( 'closed' );
	}
	$$( '#slider a' ).addEvent( 'click', function( e )
	{
		e.stop();
		navigatorToggle();
		e.stopPropagation();
		e.preventDefault();
	});
	
	if( !Browser.Engine.trident )
	{
		var buttonAlpha = new Fx.Tween( $('slider'), { duration: 200, wait: false} );
		//buttonAlpha.set( 'opacity', .8 );
		
		$$( '#slider a' ).addEvent( 'mouseover',function( e )
		{
			if( opened ) buttonAlpha.start( 'opacity', 1 );
		})
		
		$$( '#slider a' ).addEvent( 'mouseout',function( e )
		{
			if( opened ) buttonAlpha.start( 'opacity', .8 );
		})
	}
});

function suche( suche )
{
	var scroll = $(window).getScroll();
	var timeOffset = 0;
	if( scroll.y > 80 ) 
	{
		timeOffset = 300;
		var myFx = new Fx.Scroll(window,{duration:250}).start(0,80);
	}
	if( $defined( this.lastSearch ) )
	{
		if( this.lastSearch[0] == suche[0] )
		{
			suche = undefined;
		}
	}
	
	this.lastSearch = suche;
	
	_showResults.delay( timeOffset, this, [suche] );
}

function _showResults( suche )
{
	
	var links = $$('#list');
	links.getElements('a').each(function(el)
	{
	    el.getParent().set('morph', {duration: 300, transition: 'sine:out'});
	});
	links.getElements('a').each(function(el,i)
	{
	   el.getParent().removeClass('find');
	   el.getParent().morph({'opacity':'.1'});
	}); 
	if( suche )
	{
		// show just the needed links
		suche.each(function(item,i)
		{
		    var num = (i+1);
		    links.getElements('a[rel~='+item+']').each(function(el,i)
		    {   
		        el.getParent().addClass('find');
		    });  
		})
		links.getElements('li.find').each(function(el,i)
		{
		    el.morph({'opacity':'1'});
		});
	}
	else
	{
		// show all links
		links.getElements('a').each(function(el,i)
		{
		   el.getParent().morph({'opacity':'1'});
		}); 
	}
}

