/*
===========================================
COPYRIGHT NOTICE 
===========================================
All code on this site is the property of Scott Buckingham.
It has been left in a human-readable format for developers
and potential clients to review.  Please do not copy or
reuse the code in any way.

Thanks!
Scott

mail [at] scottbuckingham.com
===========================================
*/

Cufon.replace('.content a', { fontFamily: 'Engravers', hover: true });
Cufon.replace('h1, h2, h3', { fontFamily: 'Neutra' } );
Cufon.replace('p, li, dt, dd', { fontFamily: 'Engravers' } );
Cufon.replace('#navigation ul li a', { fontFamily: 'Engravers', hover: true });

$(document).ready(function(){
	Robot.Initialize();
});

var Robot = {
	Robot_Speed: 20,
	Robot_Timeout: '',
	Robot_Jumping: false,
	Screen_Locked_Left: true,
	Screen_Locked_Right: false,
	Key_Press_Count: 0,
	Watch_Hash_Timeout: 0,
	Current_Scene: 'about',
	
	Initialize: function() {
		Robot.Activate_Navigation();
		Robot.Activate_Keys();
		Robot.Load_First_Scene();
		Robot.Watch_Hash();
		Robot.Activate_Robot_Hover();
	},
	
	Activate_Navigation: function() {
		$('#navigation ul li a').click(function(){
			var Target = $(this).attr('href').split('#').join('').split('/').join('');
			Robot.Load_Scene( Target );
		});
		
		$('#portfolio dl a').attr('target', '_blank');
		$('#portfolio dl a[rel="local"]').removeAttr('target');
		$('#portfolio dl a[target="_blank"]').click(function(){
			_gaq.push(['_trackEvent', 'Links', 'Outbound', $(this).attr('href')]);
		});
	},
	
	Load_First_Scene: function() {
		var Hash = window.location.hash;

		if( Hash != '' ) {
			Hash = Hash.split('#').join('').split('/').join('');
			Robot.Load_Scene( Hash );
		}
	},
	
	Watch_Hash: function() {
		var Hash = window.location.hash;

		if( Hash != '' ) {
			Hash = Hash.split('#').join('').split('/').join('');
			if( Hash != Robot.Current_Scene ) {
				Robot.Load_Scene( Hash );
			}
		}
		
		Robot.Watch_Hash_Timeout = setTimeout(Robot.Watch_Hash, 250);
	},
	
	Watch_Hash_Off: function() {
		clearTimeout( Robot.Watch_Hash_Timeout );
	},
	
	Load_Scene: function( Target ) {
		Robot.Current_Scene = Target;
		Robot.Robot_Blur(1);
		
		var Tracked_Page = Target.charAt(0).toUpperCase() + Target.substr(1).toLowerCase();
		pageTracker._trackPageview(Tracked_Page);
		
		$('#robot').animate({
			marginRight: '500px'
		}, 100, function() {
			Robot.Robot_Blur(2);
			$('#robot').animate({
				marginRight: '0'
			}, 100, function() {
				Robot.Robot_Reset();
				Robot.Screen_Locked_Left = false;
				Robot.Screen_Locked_Right = false;
				
				var Target_Position = $('#' + Target).position();
					Target_Position = '-' + Math.abs( Target_Position.left ) + 'px';
					
				$('#scenery').animate({
					left: Target_Position
				}, 2000, function() {
					var Robot_Target = Math.round( $(window).width() / 2 ) - 50;
					$('#robot').css('left', Robot_Target);
					
					Robot.Robot_Blur(2);
					$('#robot').animate({
						marginRight: '500px'
					}, 100, function() {
						Robot.Robot_Blur(1);
						$('#robot').animate({
							marginRight: '500px'
						}, 100, function() {
							$('#robot').css('margin-right', '0');
							Robot.Robot_Forward();
						});
					});
				});
				
			});
		});
	},
	
	Activate_Keys: function() {
		
		// only used for Firefox?
		$(document).keypress(function(event) {		  
			var Key_Code = event.keyCode ? event.keyCode : event.which;
			
			if (Key_Code == '37') {
				event.preventDefault();
				Robot.Handle_Keys('left');
			} else if(Key_Code == '39') {
				event.preventDefault();
				Robot.Handle_Keys('right');
			}
		});
		
		$(document).keydown(function(event) {
			
			var Key_Code = event.keyCode ? event.keyCode : event.which;
			
			if(Key_Code == '40') {
				event.preventDefault();
				Robot.Robot_Squat();
				Robot.Key_Press_Count++;
				
			} else if(Key_Code == '38') {
				event.preventDefault();
				Robot.Robot_Jump();
				Robot.Key_Press_Count++;
				
			}
			
			// not for mozilla, it uses keypress for subsequent hits
			if( !$.browser.mozilla ) {
				if(Key_Code == '37') {
					event.preventDefault();
					Robot.Handle_Keys('left');
					Robot.Key_Press_Count++;
					
				} else if(Key_Code == '39') {
					event.preventDefault();
					Robot.Handle_Keys('right');
					Robot.Key_Press_Count++;
				}
			}
		});		
	},
		
	Handle_Keys: function( Direction ) {
		// check where the robot is now (in percent)
		var Current_Position = parseFloat($('#robot').css('left'));
		var Current_Percent = Math.round( Current_Position / $(window).width() * 100 );
		
		if( Direction == 'right' ) {
			// robot in the middle?
			if( Current_Percent >= 40 && Current_Percent <= 60 ) {
				if( Robot.Screen_Locked_Right ) {
					Robot.Move_Robot(Direction);
				} else {
					Robot.Move_Scenery(Direction);
				}
			} else {
				Robot.Move_Robot(Direction);
			}
			
		} else {
			if( Current_Percent >= 40 && Current_Percent <= 60 ) {
				if( Robot.Screen_Locked_Left ) {
					Robot.Move_Robot(Direction);
				} else {
					Robot.Move_Scenery(Direction);
				}
			} else {
				Robot.Move_Robot(Direction);
			}
			
		}
		
	},
	
	Move_Robot: function( Direction ) {
		var Current_Position = parseFloat($('#robot').css('left'));
		var Max_Position = $(window).width() - 500;
		var Min_Position = 100;
		
		if( Direction == 'right' ) {
			var New_Position = Current_Position + Robot.Robot_Speed;
			if( New_Position > Max_Position ) {
				New_Position = Max_Position;
				Robot.Robot_Angry();
			} else {
				Robot.Robot_Walk();
			}
		} else {
			var New_Position = Current_Position - Robot.Robot_Speed;
			if( New_Position < Min_Position ) {
				New_Position = Min_Position;
				Robot.Robot_Angry();
			} else {
				Robot.Robot_Walk();
			}
		}
		$('#robot').css('left', New_Position + 'px');
	},
	
	Move_Scenery: function( Direction ) {
		var Scenery_Position = parseFloat($('#scenery').css('left'));
				
		if( Direction == 'right' ) {
			var New_Position = Scenery_Position - Robot.Robot_Speed;
			if( New_Position <= ($(window).width() * -2) ) {
				New_Position = $(window).width() * -2;
				Robot.Screen_Locked_Right = true;
			} else {
				Robot.Screen_Locked_Left = false;
				Robot.Robot_Walk();
			}
			
			$('#scenery').css('left', New_Position + 'px');
		} else {
			var New_Position = Scenery_Position + Robot.Robot_Speed;
			
			if( New_Position >= 0 ) {
				New_Position = 0;
				Robot.Screen_Locked_Left = true;
			} else {
				Robot.Screen_Locked_Right = false;
				Robot.Robot_Walk();
			}
			
			$('#scenery').css('left', New_Position + 'px');
		}
	},
	
	Robot_Reset: function() {
		$('#robot').removeClass('walking-1');
		$('#robot').removeClass('walking-2');
		$('#robot').removeClass('jumping');
		$('#robot').removeClass('forward');
		$('#robot').removeClass('angry');
		$('#robot').removeClass('squatting');
		$('#robot').removeClass('blurred-1');
		$('#robot').removeClass('blurred-2');
	},
	
	Robot_Blur: function( Target ) {
		Robot.Robot_Reset();
		$('#robot').addClass('blurred-' + Target);
	},
	
	Robot_Jump: function() {
		if( !Robot.Robot_Jumping ) {
			Robot.Robot_Jumping = true;
			
			Robot.Robot_Reset();
			$('#robot').addClass('jumping');
			$('#robot').animate({
				bottom: '+=30'
			}, 250, function() {
				$(this).animate({
					bottom: '-=30'
				}, 250, function() {
					Robot.Robot_Forward();
					Robot.Robot_Jumping = false;
				});
			});
		}
	},
	
	Robot_Squat: function() {
		Robot.Robot_Reset();
		$('#robot').addClass('squatting');
		
		clearTimeout( Robot.Robot_Timeout );
		Robot.Robot_Timeout = setTimeout(Robot.Robot_Forward, 250);
	},
	
	Robot_Forward: function() {
		Robot.Robot_Reset();
		$('#robot').addClass('forward');
	},
	
	Robot_Angry: function() {
		Robot.Robot_Reset();
		$('#robot').addClass('angry');
		
		clearTimeout( Robot.Robot_Timeout );
		Robot.Robot_Timeout = setTimeout(Robot.Robot_Forward, 250);
	},
	
	Robot_Walk: function() {
		var Target_Class = 'walking-1';
		if( $('#robot').hasClass('walking-1') ) {
			Target_Class = 'walking-2';
		}
		
		Robot.Robot_Reset();
		$('#robot').addClass(Target_Class);
		
		clearTimeout( Robot.Robot_Timeout );
		Robot.Robot_Timeout = setTimeout(Robot.Robot_Forward, 500);
	},
	
	Activate_Robot_Hover: function() {
		$('#robot').hover(function() {
			$('#bubble').remove();
			$(this).prepend('<div id="bubble"></div>');
		}, function() {
			$('#bubble').remove();
		});
	}
};
