$( function()
{
	$( '#bird' ).css( 'top', '0' );
	
	var landed	= false;
	var clicked = '';
	
	$(window).bind( 'scroll', function( event )
	{
		var pos  	= $(this).scrollTop();
		var bird 	= $( '#bird-icon' );
		var step 	= Math.ceil( pos / 200 ) % 4;
		
		if( pos >= 5942  )
		{
			if( ! landed )
			{
				$( '#bird-landed' ).show();
				bird.hide();
				
				landed = true;
			}
			
			return false;
		}
		else
		{
			landed = false;
			
			bird.show();
			$( '#bird-landed' ).hide();
		}
				
		if( step == 0 || step == 2 || pos < 100 ) 
		{
			bird.removeClass( 'landed' ).removeClass( 'wings-up' ).removeClass( 'wings-closed' ).addClass( 'wings-level' );
		}
		else if( step == 1 )
		{
			bird.removeClass( 'landed' ).removeClass( 'wings-level' ).removeClass( 'wings-closed' ).addClass( 'wings-up' );
		}
		else
		{
			bird.removeClass( 'landed' ).removeClass( 'wings-up' ).removeClass( 'wings-level' ).addClass( 'wings-closed' );
		}
		
		// Activate the tractor
		var new_right = ( pos - 2000 ) * 2;
		$( '#tractor' ).css({ right: new_right });
		
		// Birds speaking text
		$( '.bird-speech' ).each( function()
		{
			var popup_top = parseInt( $(this).attr( 'data-top' ) );
			
			if( pos > ( popup_top - 600 ) && pos < ( popup_top - 100 ) )
			{
				$(this).fadeIn(100);
			}
			else
			{
				if( $(this).is( ':visible' ) )
				{
					$(this).fadeOut(100);
				}				
			}
		});
	});
	
	$( '#social-house' ).click( function()
	{
		$( '#road' ).fadeOut(1000, function()
		{
			$( 'html, body' ).animate( {scrollTop:0}, 'slow' );
			$( '#blog' ).fadeIn(1000);
		});
		
		$('#modal-window').jqmHide();
		
		return false;
	});
	
	$( '#modal-window' ).jqm(
	{ 
		ajax: '@href',
		modal: true,
//		trigger: '#footer ul li a',
		trigger: '.popup',
		onShow: function(hash)
		{
			hash.w.fadeIn(500);
		    hash.o.fadeIn(500);
		},
		onLoad: function(hash)
		{
			var to_show = clicked.attr( 'data-tab' );
			
			if( to_show )
			{
				$( '.tab-content' ).hide();
				$( '#' + to_show ).show();
			}
		}
	});
	
//	$( '#modal-window' ).jqmAddTrigger( $( '.popup' ) );
	$( '#modal-window' ).jqmAddTrigger( $( '#left-cta a' ) );
	$( '#modal-window' ).jqmAddTrigger( $( '#right-cta a' ) );
	
	$( '.popup' ).click( function()
	{
		clicked = $(this);
	});
	
	$( '.jqmOverlay, .close-modal' ).live( 'click', function()
	{
		$( '#modal-window' ).fadeOut(500, function()
		{
			$(this).jqmHide();
		});
	});
	
	$( ".ss-area" ).init_ss();
	$( "#slingshotbutton" ).fancybox(
	{
		'opacity'		: true,
		'overlayShow'	: false,
		'transitionIn'	: 'fade',
		'transitionOut'	: 'fade'
	});
	
	$( '#menu-primary li a' ).click( function()
	{
		if( $(this).parent().hasClass( 'menu-item-89' ) )
		{
			$( '#road' ).fadeOut(1000, function()
			{
				$( '#blog' ).fadeIn(1000);
			});
			
			return false;
		}
		else if( $(this).parent().hasClass( 'menu-item-717' ) || $(this).parent().hasClass( 'menu-item-735' ) )
		{
			window.open( $(this).attr( 'href' ) );
			
			return false;
		}
		else
		{
			if( $(this).attr( 'title' ) != '' && $( '#' + $(this).attr( 'title' ) ).size() == 1 )
			{
				var scroll_to = parseInt( $( '#' + $(this).attr( 'title' ) ).css( 'top' ) ) - 100;
				
				$( 'html, body' ).animate( {scrollTop:scroll_to + 'px'}, 'slow' );
			}
			
			return false;
		}
	});
	
	$( '#modal-tabs li a' ).live( 'click', function()
	{
		var tab = $(this).attr('title');
				
		$( '.tab-content' ).hide();
		$( '#' + tab ).show();
		
		return false;
	});
	
	$( '#back-to-top' ).click( function()
	{
		if( $( '#road' ).length != 0 )
		{
			$( '#blog' ).fadeOut(1000, function()
			{
				$( '#road' ).fadeIn(1000);
			});
		
			$( 'html, body' ).animate( {scrollTop:0}, 'slow' );
		
			return false;
		}
		else
		{
			return true;
		}
	});
	
	$( '#blog-back' ).click( function()
	{
		if( $( '#road' ).length != 0 ) {
		$( '#blog' ).fadeOut(1000, function()
		{
			$( '#road' ).fadeIn(1000);
		});
		
		return false;
		}
	});
	
	$( '#blog-pagination li a' ).click( function()
	{
		var page_ref = $(this).parent().attr( 'class' );
		
		$( '.blog-page' ).fadeOut( 1000, function()
		{
			$( '.' + page_ref ).fadeIn(1000);
		});
		
		return false;
	});
	
	$( '#contact-tabs a' ).live( 'click', function()
	{
		var form_ref = $(this).attr( 'rel' );
		
		$( '.form-tab' ).fadeOut( 800 ).stop(true,true);
		$( '#' + form_ref ).fadeIn( 800 );
		
		return false;
	});

	$( '.gallery-item a' ).live( 'click', function()
	{
		return false;
	});
});
