/**
 * Navigation initialisieren
 */

function link_popup ( src, features )
{
	return raw_popup( src.getAttribute( 'href' ),
	src.getAttribute( 'target' ) || '_blank',
	features );
}

function raw_popup ( url, target, features )
{
	var theWindow = window.open( url, target, features );
	theWindow.focus();
	return theWindow;
}

function init_js ()
{
	var container = $( 'content_container' ) || $( 'content_container_open' ) || $( 'content_container_programme' );
	
	if ( container )
	{
		/**
		 * MouseOver Effekt
		 */
		var original_bg = '';
		var observe = $( 'wrapper' ) ? 'wrapper' : 'wrapper_open';
		var ref_bg = $A( [] );
		observe = !$( 'wrapper_open_programme' ) ? observe : 'wrapper_open_programme';
		
		Event.observe( observe, 'mousemove', function( move_event )
		{
			$( 'accordion' ).immediateDescendants().each( function( e )
			{
				// Hintergrund
				if ( $( e ).hasClassName( 'navi_element' ) )
				{
					if ( Position.within( e, Event.pointerX( move_event ), Event.pointerY( move_event ) ) )
					{
						if ( e.getStyle( 'background-image' ).include( 'navi_transp' ) || e.getStyle( 'background-image' ).include( 'navi_inactive' ) )
						{
							original_bg = e.getStyle( 'background-image' );
							var tmp = original_bg.split( '/' );
							tmp.pop();
							ref_bg.push( $H( { id: e.id + '#' + original_bg } ) );
							e.style.backgroundImage = tmp.join( '/' ) + '/mouseover_navi.png)';
						}
						
						$( e ).descendants().each( function ( s )
						{
							if ( $( s ).hasClassName( 'float_right' ) && s.id.include( e.id ) )
							{
								if ( !s.src.include( '_a.png' ) )
								{
									s.src = s.src.gsub( '.png', '_a.png' );
								};
							};
							if ( $( s ).hasClassName( 'float_left' ) && s.id.include( e.id ) )
							{
								$( s ).setStyle( { 'visibility': 'visible' } );
							};
						} );
					}
					else
					{
						if ( ( e.getStyle( 'background-image' ).include( 'mouseover_navi.png' )
							|| e.getStyle( 'background-image' ).include( 'navi_inactive' ) ) && !$( e ).hasClassName( 'navi_active' ) )
						{
							if ( e.getStyle( 'background-image' ).include( 'mouseover_navi.png' ) )
							{
								ref_bg.each( function( s )
								{
									s.each( function( pair )
									{
										if ( pair.key === 'id' )
										{
											if ( pair.value.include( e.id ) )
											{
												var tmp = pair.value.split( '#' );
												e.style.backgroundImage = tmp[1];
												return;
											};
										};
									} );
								} );
							};
							$( e ).descendants().each( function( s )
							{
								if ( $( s ).hasClassName( 'float_right' ) )
								{
									if ( s.src.include( '_a.png' ) )
									{
										s.src = s.src.gsub( '_a.png', '.png' );
									};
								};
								if ( $( s ).hasClassName( 'float_left' ) && s.id.include( e.id ) )
								{
									$( s ).setStyle( { 'visibility': 'hidden' } );
								};
							} );
						}
					}
				};
			} );
		} );
	};
	
	if ( $( 'home_quotes' ) )
	{
		var quotes = $( 'home_quotes_img' ).descendants();
		
		quotes.each( function ( e )
		{
			Event.observe( e, 'click', function( event )
			{
				Event.element( event ).hide();
			} );
		} );
		$( 'home_quotes' ).descendants().each( function ( e )
		{
			if ( $( e ).hasClassName( 'quote_link' ) && !$( e ).hasClassName( 'no_quote' )  )
			{
				Event.observe( e, 'click', function ( event )
				{
					quotes.each( function( s )
					{
						s.hide();
					} );
					
					var tmp = quotes.find( function ( s )
					{
						return s.src.include( 'zitat' + e.alt );
					} );
					if ( tmp )
					{
						tmp.toggle();
					};
				} );
			};
		} );
	};
	
	if ( $( 'schedule_table' ) )
	{
		$( 'schedule_table' ).descendants().each( function( e )
		{
			if ( e.match( 'a.internal-link' ) )
			{
				e.onclick = null;
				Event.observe( e, 'click', function( ev )
				{
					raw_popup ( e.href, e.target, 'width=577,height=577,scrollbars=yes,resizable=yes' )
				} );
			}
		} );
		
		var mon = [];
		var tue = [];
		var wed = [];
		var wed = [];
		var thu = [];
		var fri = [];
		var sat = [];
		
		$( 'schedule_table' ).descendants().each( function( e )
		{
			if ( e.id.include( 'mon' ) )
			{
				mon.push( e );
			}
			else if ( e.id.include( 'tue' ) )
			{
				tue.push( e );
			}
			else if ( e.id.include( 'wed' ) )
			{
				wed.push( e );
			}
			else if ( e.id.include( 'thu' ) )
			{
				thu.push( e );
			}
			else if ( e.id.include( 'fri' ) )
			{
				fri.push( e );
			}
			else if ( e.id.include( 'sat' ) )
			{
				sat.push( e );
			}
		} );
		
		var last_id;
		
		$( 'schedule_table' ).getElementsBySelector( 'td' ).each( function( e )
		{
			Event.observe( e, 'mouseover', function( event )
			{
				var row = find_row( e.id );
				if ( row )
				{
					row.each( function( s )
					{
						var tmp = s.firstDescendant();
						if ( tmp.hasClassName( 'schedule_lecture' ) )
						{
							tmp.style.backgroundColor = '#ECEEEF';
						}
						else if ( tmp.hasClassName( 'schedule_discussion' ) )
						{
							tmp.style.backgroundColor = '#FEEE81';
						}
						else if ( tmp.hasClassName( 'schedule_case_study' ) )
						{
							tmp.style.backgroundColor = '#8CD990';
						}
						else if ( tmp.hasClassName( 'schedule_visits' ) )
						{
							tmp.style.backgroundColor = '#AED7F9';
						}
						else if ( tmp.hasClassName( 'pause_item' ) )
						{
							tmp.style.backgroundColor = '#fdca00';
						}
						else if ( tmp.hasClassName( 'schedule_workshops' ) )
						{
							tmp.style.backgroundColor = '#5983ff';
						}
					} );
					last_id = e.id;
				};
			} );
			Event.observe( e, 'mouseout', function( event )
			{
				if ( last_id )
				{
					var row = find_row( last_id );
					row.each( function( s )
					{
						var tmp = s.firstDescendant();
						if ( tmp.hasClassName( 'schedule_lecture' ) || tmp.hasClassName( 'schedule_discussion' ) || tmp.hasClassName( 'schedule_case_study' ) || tmp.hasClassName( 'schedule_visits' ) || tmp.hasClassName( 'schedule_workshops' ) )
						{
							tmp.style.backgroundColor = '#b3b2b2';
						}
						else if ( tmp.hasClassName( 'pause_item' ) )
						{
							tmp.style.backgroundColor = '#999da0';
						};
					} );
				};
			} );
		} );
	};
	
	function find_row ( id )
	{
		if ( !id )
		{
			return false;
		};
		
		if ( id.include( 'mon' ) )
		{
			return mon;
		};
		if ( id.include( 'tue' ) )
		{
			return tue;
		};
		if ( id.include( 'wed' ) )
		{
			return wed;
		};
		if ( id.include( 'thu' ) )
		{
			return thu;
		};
		if ( id.include( 'fri' ) )
		{
			return fri;
		};
		if ( id.include( 'sat' ) )
		{
			return sat;
		};
	}

	initRollovers();
	
	$( 'accordion' ).descendants().each( function ( e )
	{
		if ( $( e ).hasClassName( 'layout' ) )
		{
			if ( $( 'scroll_container' ) )
			{
				if ( $( e ).getHeight() > $( 'scroll_container' ).getHeight() )
				{
					$( 'scroll_track_container' ).setStyle( { 'display': 'block' } );
					scrollbar.browser.init();
				};
				return;
			};
		};
	} );
	
	var query_params = window.top.location.search.substring( 1 ).toQueryParams();
	if ( query_params.id === "{0}" || query_params.id === 0 || query_params.id === '1' || !query_params.id )
	{
		initSlideshow();
	};
	
	// Sponsoren durchlaufen
	if ( $( 'sponsored_by' ) && typeof techSupport !== 'undefined' )
	{
		var currSponsor = 0;
		new PeriodicalExecuter( function( pe )
		{
			$( 'technical_support_by' ).fade( { afterFinish: function()
			{
				$( 'technical_support_by' ).update( techSupport[currSponsor] );
				$( 'technical_support_by' ).appear();
			} } );
			currSponsor++;
		}, 5 );
	}
	
	// Presse Navigation
	if ( $( 'press_link' ) )
	{
		var containerPos = $( 'top' ).viewportOffset();
		var pos = $( 'press_link' ).viewportOffset();
		$( 'pressnavigation' ).setStyle( { top: pos.top + 9 + 'px', left: pos.left - containerPos.left - 66 + 'px' } );
		
		Event.observe( 'press_link', 'click', function( e )
		{
			Event.stop( e );
			var el = Event.element( e );
			
			if ( el.src.indexOf( 'nav_top_press_a' ) < 0 )
			{
				el.src = el.src.gsub( 'nav_top_press', 'nav_top_press_a' );
			}
			else {
				el.src = el.src.gsub( 'nav_top_press_a', 'nav_top_press' );
			}
			Effect.toggle( 'pressnavigation', 'slide', { duration: 0.5 } );
		} );
	}
	
	// Fix auf Startseite wenn News zu lang sind
	if ( $( 'home_sub_content' ) )
	{
		var offset = $( 'sponsors_sub_content_start' ).positionedOffset();
		if ( offset[1] > 576 )
		{
			$( 'sponsors_sub_content_start' ).absolutize();
			$( 'sponsors_sub_content_start' ).setStyle( { top: '586px' } );
		}
	}
}

function initRollovers ()
{
	var aPreload = [];
	var sTempSrc;
	
	var element = $( 'wrapper' ) || $( 'wrapper_open' )|| $( 'wrapper_open_programme' );
	
	element.descendants().each( function( s )
	{
		var i = 0;
		if ( $( s ).hasClassName( 'imgover' ) )
		{
			var src 	= s.getAttribute( 'src' );
			var ftype 	= src.substring( src.lastIndexOf( '.' ), src.length );
			var ending	= src.substring( 0, src.lastIndexOf( '.' ) );
			ending		= ending.substring( ending.length - 2 );
			if ( ending !== '_a' )
			{
				var hsrc = src.replace( ftype, '_a' + ftype );
				s.setAttribute( 'hsrc', hsrc );

				aPreload[i] = new Image();
				aPreload[i].src = hsrc;

				Event.observe( s, 'mouseover', function( event )
				{
					sTempSrc = Event.element( event ).getAttribute( 'src' );
					Event.element( event ).setAttribute( 'src', Event.element( event ).getAttribute( 'hsrc' ) );
				} );
				Event.observe( s, 'mouseout', function( event )
				{
					if ( !sTempSrc )
					{
						sTempSrc = Event.element( event ).getAttribute( 'src' ).replace( '_a' + ftype, ftype );
					};
					Event.element( event ).setAttribute( 'src', sTempSrc );
				} );
			};
			i++;
		};
	} );
}

function initSlideshow ()
{
	var num_images		= 5;
	var current_image	= 0;
	
	slideshow = new PeriodicalExecuter( function( pe )
	{
		if ( current_image <= num_images )
		{
			Effect.Appear( 'slideshow' + current_image, { duration: 1.2, from: 0.0, to: 1.00, transition: Effect.Transitions.sinoidal } );
			current_image++;
		}
		else
		{
			for ( var i = 0; i < num_images; i++ )
			{
				$( 'slideshow' + i ).setStyle( { 'display': 'none' } );
			};
			Effect.Fade( 'slideshow' + num_images, { duration: 1.2, from: 1.0, to: 0.0, transition: Effect.Transitions.sinoidal } );
			current_image = 0;
		}
	}, 5 );
}

var slideshow;

Event.observe( window, 'load', init_js );
