var projects;
var i;
var prjct_imgs;
var easing;
var project_height = 537;


$(document).ready(function(){
	//browser hax...
	if ($.browser.mozilla == true){
		$('#projects').css({ 'top' : 310})
	}

	prjct_imgs = $('.project .images').css({opacity : 0});
	easing = 'easeOutQuart';
	
	//set footer height to 1/3 of window height so you can scroll to bottom of page w/o screwing up page nav
	if ($(window).height() > 780) {
		$('#footer').height($(window).height()/3);
		$('#footer p').css({ 'padding-top' : ($(window).height()/3)/2 });
	}
	
	//size each project width to the width of the window so you can use arrow keys to nav
	if ($(window).width() > 960 )
		$('.project').width($(window).width());
		
		
	$('.project').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	
	$('#logo').click(function(){
		if ($(this).hasClass('open'))
			$('#hidden_logo_box').animate({ 'left' : '+=240' });
		else
			$('#hidden_logo_box').animate({ 'left' : '-=240' });
		
		$(this).toggleClass('open');
		
		return false;
			
	});
	
	//about click link to open up hidden section
	$('#about_link').click(function(){
		showHidden();
		
		return false;
	});
	
	//about click link to close hidden section
	$('#close').click(function(){
		closeHidden();
		
		return false;
	})
	
	//cycles for each project
	$('.project .images').each(function(){
		var next = '#' + $(this).parent().parent().attr('id') + ' .next';
		var prev = '#' + $(this).parent().parent().attr('id') + ' .prev';
		var pager = '#' + $(this).parent().parent().attr('id') + ' .pager';
		
		$(this).cycle({
			fx: 'scrollHorz',
			timeout: 0,
			next: next,
			prev: prev,
			easing: 'easeOutQuint',
			pager: pager,
			pagerAnchorBuilder: function(idx, slide) {
				return '<a href="#">&bull;</a>';
			}
		});
	});
	
	
	projects = $('#projects .project');
	i = 1;
	
	$('#site_controls a').click(function(){
		if ($(this).attr('id') == 'down') {
			if (projects.length > i ) {
				
				$('body, html').animate({ scrollTop: i*537 });
				i++;
			}
			
			else { //if they are +1 scroll to the top and reset i to 1
					$('body, html').animate({ scrollTop: 0 });
					//reset i
					i = 1;
			}
			
		}

		else {
			$('body, html').animate({ scrollTop: '-=537' });
			if ($(window).scrollTop() != 0)
				i--;
		}
		
		return false;
	});
	
	
	
	
});

$(window).load(function(){
	prjct_imgs.animate({ opacity : 1});
});

$(window).scroll(function(){
	var scrollPos = $(window).scrollTop();
	//closes hidden div if its not closed when scrolling
	if ($('#content, #logo, #divider_right, #site_controls').css('margin-top') == '310px') {
		closeHidden();
	}

});

$(window).resize(function(){
	//set footer height to 1/3 of window height so you can scroll to bottom of page w/o screwing up page nav
	if ($(window).height() > 780) {
		$('#footer').height($(window).height()/3);
		$('#footer p').css({ 'padding-top' : ($(window).height()/3)/2 });
	}
	
	//size each project width to the width of the window so you can use arrow keys to nav
	if ($(window).width() > 960 )
		$('.project').width($(window).width());
	
});
$(window).keydown(function(e){
	if (e.keyCode == 65) { // a
		$('#about_link').trigger('click');
		if (parseInt($('#content, #logo, #divider_right, #site_controls').css('margin-top').substr(0, 3)) > 0 && parseInt($('#content, #logo, #divider_right, #site_controls').css('margin-top').substr(0, 3)) <= 350) {
			$('#close').trigger('click');
		}
	}
	if (e.keyCode == 82) { // r
	//	window.open($('#resume').attr('href'));
	}
	if (e.keyCode == 84) { // t
		window.open($('#twitter').attr('href'));
	}
	if (e.keyCode == 68) { // dspn
		window.open($('#dspn').attr('href'));	
	}
	if (e.keyCode == 85) { // u
		window.open($('#tumblr').attr('href'));
	}
	if (e.keyCode == 76) { // l
		window.open($('#linkedin').attr('href'));
	}
	if (e.keyCode == 89) { // y - click for yu
		$('#logo a').trigger('click');
	}
	if (e.keyCode == 32) { // space
		$('#divider_left p').fadeOut();
		
		if (projects.length > i ) {
			$('body, html').animate({ scrollTop: i*537 }, easing);
			i++;
		}
		
		else { //if they are +1 scroll to the top and reset i to 1
				$('body, html').animate({ scrollTop: 0 }, easing);
				//reset i
				i = 1;
		}
		
		return false;
	}
	
	if (e.keyCode == 40) { //arrow down		
		if (projects.length > i ) {
			$('body, html').animate({ scrollTop: i*537 });
			i++;
		}
		
		else { //if they are +1 scroll to the top and reset i to 1
				$('body, html').animate({ scrollTop: 0 });
				//reset i
				i = 1;
		}
		
		return false;
	}  
	
	if (e.keyCode == 38) { //arrow up		
		$('body, html').animate({ scrollTop: '-=537' });
		if ($(window).scrollTop() != 0)
			i--;
			
		return false;
	}
	//39 right
	//37 left
	
	if (e.keyCode == 39) {
		$('.hover.project .next').trigger('click');
	}
	
	if (e.keyCode == 37) {
		$('.hover.project .prev').trigger('click');
	}
	
	$('#divider_left p').fadeOut();
	
	
})


function increase(i) {
	return i++;
}

function showHidden() {
	easing = 'easeOutQuart';
	
	$('#content, #logo, #divider_right, #divider_left, #hidden_logo_box, #nav').animate({ 'margin-top' : 310 }, easing);
	if ($.browser.mozilla) {
		$('#header').animate({ 'margin-top' : 310 }, easing);
		$('#projects').animate({ 'top' : 310 }, easing);
	}
	$('#hidden_about').css({ 'z-index' : 4 });
	$('#site_controls').animate({ 'margin-top' : 338 });
}

function closeHidden() {
	easing = 'easeOutQuart';
	
	$('#content, #logo, #divider_right, #divider_left, #hidden_logo_box, #nav').animate({ 'margin-top' : 0 }, easing);
	$('#site_controls').animate({ 'margin-top' : 28 }, easing);
	if ($.browser.mozilla) {
		$('#projects').animate({ 'top' : 0}, easing);
		
		$('#header').animate({ 'margin-top' : 0 }, easing);
	}
	if (parseInt($('#content, #logo, #divider_right, #site_controls').css('margin-top').substr(0, 3)) <= 350)
		$('#hidden_about').css({ 'z-index' : 1 });
}


