/*

TRAPDOOR JS

*/

$(document).ready (

	function ( )
	{
		
		/* EASING */
		
		jQuery.easing.def = 'easeOutExpo';
		
		
		/* INIT */
		
		$( '.seccion' ).hide ( );
		
		$( '#content' ).removeClass ( 'no-visible' );
		
		$( '#games' ).fadeIn ( );
		
		
		/* LINKS */
		
		var links = new Array ( "about" ,"games" , "warp" , "fez" , "news" , "contact" );
		
		var cant_links = links.length;
		
		var active = "games";
		
		var nav_e = true;
		
		for ( var i = 0 ; i < cant_links ; i++ ) 
		{
			
			$( '#link-' + links[i] ).click (
			
				function ( event )
				{
					event.preventDefault ( );
					
					if ( nav_e )
					{
						current = $( this ).attr ( 'id' );
						
						current = current.slice ( 5 );
						
						if ( active != current ) 
						{
							
							nav_e = false;
							
							switch_sections ( active , current );
							
							active = current;
							
							$( '.selected' ).removeClass ( 'selected' );
							
							if ( current == "warp" || current == "fez" )
							{
								$( '#link-games' ).addClass ( 'selected' );
							}
							else
							{
								$( this ).addClass ( 'selected' );
							}
							
							window.location.hash = current;
							
							gallery ( );
						
						}
					}
				}
			);
			
		}
		
		function switch_sections ( active , current )
		{			
			$( '#' + active ).stop ( true , true ).fadeOut ( function ( ) { $( '#' + current ).stop ( true , true ).fadeIn ( ); $( '#' + active ).hide ( ); nav_e = true; } );
		}
		
		
		/* BACK */
		
		$( '.go-back' ).click (
			
			function ( event )
			{
				
				event.preventDefault ( );
				
				window.location.hash = 'games';
				
			}
			
		);
			
		
		/* HASH CHANGE */
		
		nav_hash ( true );
		
		$( window ).hashchange ( 
		
			function ( )
			{
				nav_hash ( );
			}
		
		);
		
		function nav_hash ( first_load )
		{
			
			var hash = window.location.hash;
			
			hash = hash.slice ( 1 );
			
			if ( first_load && hash != "" && active != hash )
			{
				$( '#' + active ).hide ( );
			}
			
			if ( links.indexOf ( hash ) != -1 )
			{
				
				$( '#link-' + hash ).click ( );
					
			}
				
		}
		
		
		/* GALLERY */
		
		gallery ( );
		
		var current_img  = 0;
		var current_path = '';
		var total_img    = 1;
		
		var imgs = Array ( );
		
		$( '#prev' ).click (
		
			function ( )
			{
				if ( current_img > 0 )
				{
					current_img --;
					
					load_img ( );
					
					$( '#contador' ).html ( '<span>' + ( current_img + 1 ) + '</span> / ' + total_img );
					
					$( '#next' ).removeClass ( 'next-disabled' );
					
					if ( current_img == 0 )
					{
						$( '#prev' ).addClass ( 'prev-disabled' );	
					}
				}
			}
		
		);
		
		$( '#next' ).click (
		
			function ( )
			{
				imgs = eval ( "img_" + active );
				total_img   = imgs.length;
				
				if ( current_img < ( total_img - 1 ) )
				{						
					current_img ++;
					
					$( '#contador' ).html ( '<span>' + ( current_img + 1 ) + '</span> / ' + total_img );
					
					load_img ( );
					
					$( '#prev' ).removeClass ( 'prev-disabled' );
					
					if ( current_img == ( total_img - 1 ) )
					{
						$( '#next' ).addClass ( 'next-disabled' );	
					}
				}
			}
		
		);
		
		function gallery ( )
		{
			imgs = eval ( "img_" + active );
			
			current_img = 0;
			total_img   = imgs.length;
			
			$( '#prev' ).addClass ( 'prev-disabled' );
			
			if ( total_img > 1 )
			{
				$( '#prev' ).show ( );
				$( '#next' ).show ( );
				$( '#contador' ).show ( );
				
				$( '#next' ).removeClass ( 'next-disabled' );
			}
			else
			{	
				$( '#prev' ).hide ( );
				$( '#next' ).hide ( );
				$( '#contador' ).hide ( );
			}
			
			$( '#contador' ).html ( '<span>' + ( current_img + 1 ) + '</span> / ' + total_img );
			
			load_img ( );
		}
		
		function load_img ( )
		{
			$( "#imgs img" ).removeClass ( 'active' );
			$( "#imgs img" ).addClass    ( 'bye' );
			
			$( "#imgs" ).prepend ( '<img src="' + img_path + imgs[current_img] + '-1-2.jpg" class="active" style="display:none;" />' );
			
			$( '#loader' ).stop ( true , true ).fadeIn ( );
			
			$( '.active' ).load (
				
				function ( )
				{
					full_bg ( $( this ) );
					
					
					$( '#loader' ).stop ( true , true ).fadeOut ( );
					
					$( '#pattern' ).animate ( {	opacity: 1 } , "slow" , function ( ) { $( '.active' ).show ( ); $( '.bye' ).remove ( ); $( '#pattern' ).animate ( {	opacity: 0.45 } , "slow" ); } );
					
				}
				
			);
		
		}
				
		
		/* BACKGROUND */
		
		$( window ).resize (
			
			function ( )
			{
				full_bg ( '.active' );
				center_loader ( );
			}
		
		);
		
		center_loader ( );
		
		function full_bg ( theItem )
		{
			var winWidth    = $( window ).width ( );
			var winHeight   = $( window ).height ( );
			var imageWidth  = $( theItem ).width ( );
			var imageHeight = $( theItem ).height ( );
			var picHeight   = imageHeight / imageWidth;
			var picWidth    = imageWidth / imageHeight;
			
			if ( $( theItem ).css( "display" ) == "none" )
			{
				$( theItem ).css( "display" , "none" );
			}
			
			if ( ( winHeight / winWidth) < picHeight )
			{
				$( theItem ).css( "width"  , winWidth );
				$( theItem ).css( "height" , picHeight*winWidth );
			}
			else
			{
				$( theItem ).css ( "height" , winHeight );
				$( theItem ).css ( "width"  ,picWidth*winHeight );
			}
			
			$( theItem ).css ( "margin-left" , winWidth  / 2 - $( theItem ).width ( )  / 2 );
			$( theItem ).css ( "margin-top"  , winHeight / 2 - $( theItem ).height ( ) / 2 );
			
		}
		
		function center_loader ( )
		{
			var winWidth    = $( window ).width ( );
			var winHeight   = $( window ).height ( );
			
			$( "#loader" ).css( "top" , ( winHeight - 84 ) / 2 );
			$( "#loader" ).css( "left" , ( winWidth - 42 ) / 2 );	
		}
		
		
		/* NEWS PAGINATION */
		
		var news_cant  = $( '.inner-news > .news' ).length - 3;		
		
		if ( news_cant < 1 )
		{
			$( '#up' ).css ( 'visibility' , 'hidden' );
			$( '#down' ).css ( 'visibility' , 'hidden' );	
		}
		
		var news_height = 152;
		
		var news_actual = 0;
		
		$( '#up' ).click ( 
		
			function ( event )
			{
				event.preventDefault ( );
				
				if ( news_actual > 0 )
				{
					news_actual --;
					mover_news ( );	
				}
				
			}
		
		);
		
		$( '#down' ).click ( 
		
			function ( event )
			{
				event.preventDefault ( );
				
				if ( news_actual < news_cant )
				{
					news_actual ++;
					mover_news ( );	
				}
				
			}
		
		);
		
		function mover_news ( )
		{
			var val_h = - ( news_actual * news_height );
			
			$( '.inner-news' ).animate ( { top: val_h } , 500 );
			
			if ( news_actual == news_cant )
			{
				$( '#down' ).addClass ( 'disabled' );
			}
			else
			{
				$( '#down' ).removeClass ( 'disabled' );
			}
			
			if ( news_actual == 0 )
			{
				$( '#up' ).addClass ( 'disabled' );
			}
			else
			{
				$( '#up' ).removeClass ( 'disabled' );
			}
			
		}
		
	}

);


