/** HANDLE NAV **/

function handleNav(){
	navlock = true;
	mytext = $('#s2 .cont').text();
	
	ww = $(window).width();
	wh = $(window).height();
	
	cp = $('#content').position();
	
	$('#nav, #locker').css({ opacity: 0.1 });
	
	// mouse move
	$(document).mousemove(function(e){	
		if (!navlock) {
			
			np = $('#nav').position();
			
			if (np.left > e.clientX) {	// from right
				nX = e.clientX + 80;
				nY = e.clientY;
			}
			else {						// from left
				nX = e.clientX - 80;
				nY = e.clientY;
			}
			
			// other checks
			if (e.clientX > (ww - 300)) {
				nX = e.clientX - 180;
				nY = e.clientY;				
			}

			if (e.clientY > (wh - 200)) nY = e.clientY - 100;
			
			$('#nav').stop().animate({ left: nX, top: nY }, 5000, 'easeOutExpo'); 						
		}
	});
	
	// over
	$('#nav').hover(function(){
		navlock = true;
		$(this).stop().animate({ opacity: 1, width: 190 }, 500);		
	}, function(){
		$(this).animate({ opacity: 0.1, width: 40 }, 100, function(){
			if ($('#locker').hasClass('unlocked')) navlock = false;			
		});		
	});
	
	$('#content').hover(function(){
		navlock = true;
		//$('#nav').stop().animate({ left: (cp.left - 150), top: (cp.top + 40) }, 5000, 'easeOutExpo'); 		
	}, function(){
		if ($('#locker').hasClass('unlocked')) navlock = false;	
	});
	
	// click
	$('#nav a').click(function(){
		$('.section .cont, .section h1').hide();
		
		$('#s3 .list1').css({ marginLeft: '250px' });
		$('#s3 .list2').css({ marginLeft: '-250px' });
		$('#s3 .list3').css({ marginLeft: '250px' });
		$('#s3 .list4').css({ marginLeft: '-250px' });
		
		myindex = $('#nav a').index(this);		
		mt = - (myindex * 260);
		
		myid = $(this).attr('href');
		
		if (myid == '#s1') {					
			$('body').prepend('<div id="fx"></div>');
			$('body').css({ background: 'url(images/bg.png) #6cfdff' }).animate({ backgroundColor: '#c8c8c8' }, 1000);			
			$('#fx').fadeOut(200, function(){ 
				$(this).fadeIn(200, function(){
					$(this).remove(); 
				});
			});			
			
			$('#viewport').css({ marginTop: mt });
			$(myid).find('h1').show();
		}
		else if (myid == '#s2') { 
			$('#viewport').css({ marginTop: mt });
			clearInterval(timer);
			$(myid).find('.cont').text(mytext);
			$(myid).find('.cont').fadeIn().typewriter(); 
		}
		else if (myid == '#s3') { 
			$('#viewport').css({ marginTop: mt });
			$(myid).find('.cont').fadeIn(500);	
			$('#s3 .list1').animate({ marginLeft: '45px' }, 1000, 'easeOutExpo');			
			$('#s3 .list2').animate({ marginLeft: '-40px' }, 1400, 'easeOutExpo');			
			$('#s3 .list3').animate({ marginLeft: '45px' }, 1800, 'easeOutExpo');			
			$('#s3 .list4').animate({ marginLeft: '-35px' }, 2000, 'easeOutExpo');					
		}
		else {
			$('#viewport').animate({ marginTop: mt }, 500);
			$(myid).find('.cont').fadeIn(800);
		}
		
		return false;
	});
	
	// locker
	$('#locker').toggle(function(){
		$(this).addClass('unlocked');
		$(this).attr('title','Oh please, stop this hell now!');
		navlock = false;		
	}, function(){
		$('#nav').stop().animate({ left: 20, top: 20 }, 2000, 'easeOutExpo'); 
		$(this).removeClass('unlocked');
		$(this).attr('title','Make it free!');
		navlock = true;
	});
	
	$('#locker').hover(function(){
		$(this).stop().animate({ opacity: 1 }, 500);
	}, function(){
		$(this).stop().animate({ opacity: 0.1 }, 100);
	});
}

/** ON DOCUMENT LOAD... **/

$(function(){
	// navigation
	handleNav();
	
	// fx
	$('body').prepend('<div id="fx"></div>');
	$('#fx').hide();
	
	//$('.payoff').css({ paddingTop: 10, opacity: 0 }).animate({ paddingTop: 270, opacity: 1 }, 1000, 'easeOutElastic');	
	
});
