/************************************
**  Skimbee Studios Page Animations
**	@author: Rocco Augusto
**	@url: http://www.skimbee.com
************************************/

var w = $(window).width();

function init()
{	
	//determine page dimensions
	var pwidth = $(window).width(),
		cwidth = pwidth*3,
		selected = $('#navigation a.on').parent('li').attr('id');
	
	$('#content').css('width', cwidth);
	$('#services, #work, #contact').css('width', pwidth);
	
	//if viewing any 'page' besides the first
	if(selected != 0)
	{
		var a = pwidth*selected,
			b = w*selected;
			
		//alert('a: ' + a + ' | b: '+ b);
			
		(a > b)? $('#content').animate({'left': '-=' + (a-b)}, 'slow') : $('#content').animate({'left': '-=' + (a-b)}, 'slow');
	}
	
	w = pwidth;
}

$(function()
{
	//highlight navigation on page load
	var url = window.location.href.split('/');
	
	switch(url[3])
	{
		case '#work':
			$('#navigation li:eq(1) a').addClass('on');
			break;
		case '#contact':
			$('#navigation li:eq(2) a').addClass('on');
			break;
		default:
			$('#navigation li:eq(0) a').addClass('on');
	}
	
	$('#navigation li').each(function(i){ $(this).attr('id', i); });
	
	//begin page transition animation
	$('#navigation li a').click(function()
	{
		var selected = $('#navigation a.on').parent('li').attr('id'),
			destination = $(this).parent('li').attr('id'),
			pwidth = $(window).width()
			d = destination*parseInt(pwidth),
			s = selected*parseInt(pwidth);
			
			if (destination == 3) return true;
			
			if(destination > selected)
			{
				var left = d-s;
					
				$('#content').animate({'left': '-=' + left}, 'slow');
			}
			
			if(destination < selected)
			{
				var left = s-d;
					
				$('#content').animate({'left': '+=' + left }, 'slow');
			}
		
		$('#navigation #' + selected + ' a').removeClass('on');
		$(this).addClass('on');	
		
		return false;
	});	
	
	/////////////////////////////
	// Navigation
	/////////////////////////////
	$(document).keydown(function(e)
	{	
		var selected = $('#navigation a.on').parent('li').attr('id'),
			pwidth = $(window).width(),
			destination = selected;
			
		if(e.which == 37 && $('select').attr('class') != 'focus'){ destination = (selected <= 0)? 2 : selected-1; }
		if(e.which == 39 && $('select').attr('class') != 'focus'){ destination = (selected >= 2)? 0 : parseInt(selected)+1; }
			
		var d = destination*parseInt(pwidth),
			s = selected*parseInt(pwidth);
			
			if(destination > selected)
			{
				var left = d-s;
					
				$('#content').animate({'left': '-=' + left}, 'slow');
			}
			
			if(destination < selected)
			{
				var left = s-d;
					
				$('#content').animate({'left': '+=' + left }, 'slow');
			}
		
		$('#navigation #' + selected + ' a').removeClass('on');
		$('#' + destination + ' a').addClass('on');			
	});
});