$( document ).ready(
	function()
	{
		var map_links = $( 'div.class_dakar_map map area' );
		var contentContainer = $( 'div.class_dakar_map div.middle_layer' );
		var menu_links = $( 'div.class_dakar_map div.day a' );
		var menuLinksContainer = $( 'div.rally_map_head div.days' );
		current = -1;
		if( map_links.length > 0 )
		{
			function showContent( rel )
			{
				var currentContent = $( contentContainer ).find( 'div.' + rel );
				if( currentContent.length > 0 )
				{
					$( contentContainer ).children().not( '.red' ).hide();
					$( currentContent ).show();
					$( currentContent ).addClass( 'hovered' );
					if( $( currentContent ).hasClass( 'red' ) )
					{
						$( currentContent ).find( 'div.day_popup' ).show();
					}
				}
			}
			function hideAll()
			{
				$( contentContainer ).children().not( '.red' ).hide();
				$( contentContainer ).children().removeClass( 'hovered' );
				$( contentContainer ).find( 'div.day_item.red' ).find( 'div.day_popup' ).hide();
			}
			function showClass( relValue )
			{
				var currentMenuDay = $( menuLinksContainer ).find( 'a[rel="' + relValue + '"]' );
				if( currentMenuDay.length > 0 )
				{
					$( menuLinksContainer ).find( 'a' ).removeClass( 'red' );
					$( currentMenuDay ).addClass( 'red' );
					Cufon.replace( currentMenuDay );
				}
			}
			function hideClass( relValue )
			{
				var currentMenuDay = $( menuLinksContainer ).find( 'a[rel="' + relValue + '"]' );
				$( menu_links ).removeClass( 'red' );
				Cufon.replace( currentMenuDay );
			}
			$.each( map_links,
				function( index, item )
				{
					$( item ).mouseenter(
						function( event )
						{
							current = index;
							showContent( $( this ).attr( 'rel' ) );
							showClass( $( this ).attr( 'rel' ) );
							if( $( this ).attr( 'rel' ) == "day_8" )
							{
								$( 'div.dots_layer img.normal' ).hide();
								$( 'div.dots_layer img.with_white' ).show();
							}
						}
					);
					$( item ).mouseleave(
						function( event )
						{
							current = -1;
							hideAll();
							hideClass( $( this ).attr( 'rel' ) );
							if( $( this ).attr( 'rel' ) == "day_8" )
							{
								$( 'div.dots_layer img.with_white' ).hide();
								$( 'div.dots_layer img.normal' ).show();
							}
						}
					);
				}
			);
			$.each( menu_links,
				function( index, item )
				{
					$( item ).mouseenter(
						function( event )
						{
							current = index;
							showContent( $( this ).attr( 'rel' ) );
							if( $( this ).attr( 'rel' ) == "day_8" )
							{
								$( 'div.dots_layer img.normal' ).hide();
								$( 'div.dots_layer img.with_white' ).show();
							}
						}
					);
					$( item ).mouseleave(
						function( event )
						{
							current = -1;
							hideAll();
							if( $( this ).attr( 'rel' ) == "day_8" )
							{
								$( 'div.dots_layer img.with_white' ).hide();
								$( 'div.dots_layer img.normal' ).show();
							}
						}
					);
				}
			);
		}
	}
);

/*
//dakar sands
$( document ).ready(
	function()
	{
		var sands = $( 'div.dakar_sands' );
		var menuItems = $( '#topmenu.dakar #topmenu-firstlevel li a.main-lheader' );
		if( sands.length > 0 && menuItems.length > 0 )
		{
			//menu functionality
			$.each( menuItems,
				function( index, item )
				{
					$( item ).mouseenter(
						function( event )
						{
							$( this ).siblings( 'div.sands_background' ).show();
						}
					);
					$( item ).mouseleave(
						function( event )
						{
							$( this ).siblings( 'div.sands_background' ).hide();
						}
					);
				}
			);
			//sands functionality
			var sandsItem = $( sands ).find( 'div.sands' );
			var minT = 0.002;
			var maxT = 0.02;
			var max = 0.8;
			var min = 0.4;
			var opacity = new Array();
			var directions = new Array();
			var backHeight = [343, 256, 191];
			var currentIndex = [0, 3, 5];
			var direction = [1, -1, 1];
			$.each( sandsItem,
				function( index, item )
				{
					var direct = -1;
					if( parseInt( 2 * Math.random() ) == 0 )
					{
						direct = -1;
					}
					else
					{
						direct = 1;
					}
					directions.push( { 't': ( minT + ( ( maxT - minT ) * Math.random() ) ), 'd': direct } );
					opacity.push( 0.6 );
				}
			);
			$.each( sandsItem,
				function( index, item )
				{
					var direct = -1;
					if( parseInt( 2 * Math.random() ) == 0 )
					{
						direct = -1;
					}
					else
					{
						direct = 1;
					}
					currentIndex.push( 1+ parseInt( 7 * Math.random() ) );
					direction.push( direct );
				}
			);
			function updateWay( index, nextDirection )
			{
				var direct = -1;
				if( typeof nextDirection === "undefined" )
				{
					if( parseInt( 2 * Math.random() ) == 0 )
					{
						direct = -1;
					}
					else
					{
						direct = 1;
					}
				}
				else
				{
					direct = nextDirection;
				}
				directions[index] = { 't': ( minT + ( ( maxT - minT ) * Math.random() ) ), 'd': direct };
			}
			function sandsUpdate()
			{
				$.each( sandsItem,
					function( index, item )
					{
						if( ( 100 * Math.random() ) < 5 )
						{
							updateWay( index );
						}
						else
						{
							opacity[index] = opacity[index] + ( directions[index].d * directions[index].t );
							if( opacity[index] >= max )
							{
								opacity[index] = max;
								updateWay( index, -1 );
							}
							else if( opacity[index] <= min )
							{
								opacity[index] = min;
								updateWay( index, 1 );
							}
							else
							{
								$( sandsItem[index] ).css( 'opacity', opacity[index] );
							}
						}
					}
				);
			}
			function sandsUpdateIE()
			{
				$.each( sandsItem,
					function( index, item )
					{
						var nextTop = - currentIndex[index] * $( item ).height();
						$( item ).css( 'backgroundPosition', '0px ' + nextTop +'px' );
						currentIndex[index] += direction[index];
						if( currentIndex[index] >= 9 )
						{
							direction[index] = -1;
						}
						if( currentIndex[index] <= 0 )
						{
							direction[index] = 1;
						}
					}
				);
			}
			if( $.browser.msie && !( document.documentMode && document.documentMode == 9 ) )
			{
				window.sandsUpdateHandler = window.setInterval( sandsUpdateIE, 300 );
			}
			else
			{
				window.sandsUpdateHandler = window.setInterval( sandsUpdate, 50 );
			}
		}
	}
);
*/
/*$( document ).ready(
	function()
	{
		var sands = $( 'div.dakar_sands' );
		var sandsItem = $( sands ).find( 'div.sands' );
		var backHeight = 343;
		var currentIndex = 0;
		var direction = 1;
		function sandsUpdate()
		{
			var nextTop = - currentIndex * backHeight;
			$.each( sandsItem,
				function( index, item )
				{
					$( item ).css( 'background-position', '0px ' + nextTop +'px' );
				}
			);
			currentIndex += direction;
			if( currentIndex >= 9 )
			{
				direction = -1;
			}
			if( currentIndex <= 0 )
			{
				direction = 1;
			}
		}
		window.setInterval( sandsUpdate, 100 );
		
	}
);*/
