(function($){
	
	var $active = 'home';
	
	var $pages;
	var $page_dimensions;
	var $section_dimentions;
	
	var $third = 1000;
	var $container_width = 3000;
	var $container_dimentions;
	
	var $is_windows = false;
	
	$.fn.exists = function(){return $(this).length>0;}
	
	$(document).ready(function() 
	{
		$().framerate({framerate: 60, logframes: false})
		
		if (navigator.appVersion.indexOf("Win")!=-1){$is_windows=true;}

		$pages = {
			'home':$('.page.home'), 
			'roastery':$('.page.roastery'), 
			'contact':$('.page.contact'), 
			'wholesale':$('.page.wholesale')}
			
		$page_dimensions = {
			'width': $pages['home'].width(),
			'height': $pages['home'].height()}
			
		$container_dimentions = {
				'width': parseInt($('.container').width()/2),
				'height': parseInt($('.container').height()/2)}
			
		$('.container').stay_centered();
		
		$('a.fly_to').each(function()
		{
			// finding div to fly to
			var $href = $(this).attr('href').replace('#','');
			var $div = $pages[$href];
			
			//on click, fly to that div
			$(this).click(function()
			{
				fly_to($div,750); 
				return false;
			});
		});
		
		// if url has a # in it, start at that div
		if(window.location.hash != '')
		{
			var $href = window.location.hash.replace('#','');
			var $div = $pages[$href];
			fly_to($div, 0);
		}
		
		section_listener();
		key_listerner();
    });
	
	
	//--------------------------------------------------------------------------------------------
	// STAY CENTERED / KEEPS EVERYONE CENTERED AND HAPPY
	//--------------------------------------------------------------------------------------------
	$.fn.stay_centered = function()
	{
		var $container = $(this);
		var $body = $('body');
		
		$(window).resize(function()
		{
			var $full_right = false;
			var $padding = parseInt(($(window).width() - $third)/2);
			var $new_width = parseInt($container_width+$padding);
			$container.css({'margin-left':$padding,'width':$new_width});
			
			fly_to($pages[$active], 0);
			
			
		}).trigger('resize');
		
	}
	
	//--------------------------------------------------------------------------------------------
	// SECTION LISTENER / 
	//--------------------------------------------------------------------------------------------
	function section_listener()
	{
		
		$(window).scroll(function()
		{
			// find center point of window.
			
			var $window_offset_center = {
				'x': parseInt($(window).width()/2)+parseInt($(window).scrollLeft()), 
				'y': parseInt($(window).height()/2)+parseInt($(window).scrollTop())};
				
			
			var $last_active = $active;
			//console.debug('x:'+$window_offset_center['x']+', y:'+$window_offset_center['y']);
			if($window_offset_center['y'] < $container_dimentions['height'])
			{
				// TOP 2 SECTIONS
				if($window_offset_center['x'] < $container_dimentions['width'])
				{
					$active = 'home';
				}
				else
				{
					$active = 'roastery';	
				}
			}
			else
			{
				// BOTTOM 2 SECTIONS
				if($window_offset_center['x'] < $container_dimentions['width'])
				{
					$active = 'wholesale';
				}
				else
				{
					$active = 'contact';
				}	
			}
			
			if($active != $last_active)
			{
				update_nav_active();
			}
			//console.debug('section: '+$active);
			
		});
		$(window).trigger('scroll');
		
	}
	
	//--------------------------------------------------------------------------------------------
	// UPDATE NAV TO THE ACTIVE SECTION
	//--------------------------------------------------------------------------------------------
	function update_nav_active()
	{
		$('.nav li').removeClass('active');
		$('.nav li.'+$active).addClass('active');	
	}
	
	
	//--------------------------------------------------------------------------------------------
	// FLY TO / FLYS YOU TO A DIV
	//--------------------------------------------------------------------------------------------
	function fly_to($div, $speed)
	{
		var $top = $div.offset().top;
		var $margin =  parseInt(($(window).width() - $div.width())/2);
		var $left = $div.offset().left - $margin;
		
		if($speed)
		{
			var speed_x = $speed;
			var speed_y = $speed;
		}
		else if($is_windows)
		{
			var speed_x = Math.abs(($left - $('html').scrollLeft()) * 2.5);
			var speed_y = Math.abs(($top - $('html').scrollTop()) * 3.5);
		}
		else
		{
			var speed_x = 1000;
			var speed_y = 1000;
		}
		
		
		if($active == 'home' || $active == 'contact')
		{
			$('html,body')
			.stop()
			.animate({scrollLeft : $left}, {"queue":$is_windows, "duration":speed_x, "easing": "easeInOutQuad"})
			.animate({scrollTop : $top}, {"queue":$is_windows, "duration":speed_y, "easing": "easeInOutQuad"}); 
		}
		else
		{
			$('html,body')
			.stop()
			.animate({scrollTop : $top}, {"queue":$is_windows, "duration":speed_y, "easing": "easeInOutQuad"})
			.animate({scrollLeft : $left}, {"queue":$is_windows, "duration":speed_x, "easing": "easeInOutQuad"});
		}
	}
	
	$(document).ready(function() {
		//iphone and ipad scroll
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
		{
			
		}
	});
	
	
	//--------------------------------------------------------------------------------------------
	// KEY STROKE LISTENERS
	//--------------------------------------------------------------------------------------------
	function key_listerner()
	{
		$(document).keypress(function(e)
		{
			if(e.keyCode == '38') // UP
			{
				//console.log('UP');
				if($active == 'home'){fly_to($pages['home'], 500);}
				if($active == 'roastery'){fly_to($pages['roastery'], 500);}
				if($active == 'contact'){fly_to($pages['roastery']);}
				if($active == 'wholesale'){fly_to($pages['home']);}
				return false;
			}
			else if(e.keyCode == '39') // RIGHT
			{
				//console.log('RIGHT');
				if($active == 'home'){fly_to($pages['roastery']);}
				if($active == 'roastery'){fly_to($pages['roastery'], 500);}
				if($active == 'contact'){fly_to($pages['contact'], 500);}
				if($active == 'wholesale'){fly_to($pages['contact']);}
				return false;
			}
			else if(e.keyCode == '40') // DOWN
			{
				//console.log('DOWN');
				if($active == 'home'){fly_to($pages['wholesale']);}
				if($active == 'roastery'){fly_to($pages['contact']);}
				if($active == 'contact'){fly_to($pages['contact'], 500);}
				if($active == 'wholesale'){fly_to($pages['wholesale'], 500);}
				return false;
			}
			else if(e.keyCode == '37') // LEFT
			{
				//console.log('LEFT');
				if($active == 'home'){fly_to($pages['home'], 500);}
				if($active == 'roastery'){fly_to($pages['home']);}
				if($active == 'contact'){fly_to($pages['wholesale']);}
				if($active == 'wholesale'){fly_to($pages['wholesale'], 500);}
				return false;
			}
			
			
			
		});
	}
	

	
	
})(jQuery);

