var animationSpeed = 750;
var ssTimeout;
var ssCurrent = -1;
var ids = ['meet-team', 'visit', 'learn-more', 'see-projects', 'contact'];
var navAnimationFinished = false;

jQuery.easing.def = 'easeInOutQuint';


var inOpts = new Object();
var outOpts = new Object();
var bgPos = new Object();

inOpts['contact'] = {marginTop: '-50px', height: '425px'};
outOpts['contact'] = {marginTop: '0px', height: '325px'};

inOpts['see-projects'] = {marginLeft: '-50px', width: '360px'};
outOpts['see-projects'] = {marginLeft: '0px', width: '260px'};

inOpts['visit'] = {marginRight: '-50px', width: '600px'};
outOpts['visit'] = {marginRight: '0px', width: '500px'};

inOpts['meet-team'] = {marginRight: '-52px', width: '490px'};
outOpts['meet-team'] = {marginRight: '0px', width: '388px'};

inOpts['learn-more'] = {marginTop: '-45px', height: '290px'};
outOpts['learn-more'] = {marginTop: '0px', height: '190px'};



function homeSplashAnimation() {
	jQuery.easing.def = 'easeInOutQuad';
	$('#splash-wordmark').show();
	$('#content').css('background','#fff');
	$('#content-holder').css({height:"5px", background:"#282623"});
	$('#reveal-shape').animate({width: '0px'}, 1000, function() {
		$('#splash-tagline img:first').fadeIn(500, function() {
			$('#splash-logo .square').animate({opacity:1}, 200, function() {
				$('#splash-logo .bottom').animate({top: '0px', left:'0px', opacity:1}, 200, function() {
					$('#splash-logo .right').animate({top: '0px', left:'0px', opacity:1}, 200, function() {
						$('#splash-logo .left').animate({top: '0px', left:'0px', opacity:1}, 200, function() {
							$('#splash-tagline img:last').fadeIn(1000, function(){
								setTimeout('homeCloseSplash()', 500);
							});
						});
					});
				});
			});
		});
	});
	
	$("#skip").click(function(){
		homeCloseSplash();
	});
}

function homeCloseSplash() {
	jQuery.easing.def = 'easeInOutQuint';
	var top = $('#content').position().top;
	$('#splash').animate({opacity:0},750, function() {
		$('#splash-wrapper').animate({height:"5px", top: top+"px"}, 700, function() {
			$('#splash-wrapper').remove();
			$('#content-holder').animate({backgroundColor:'#eee', height:'460px'}, 600, function(){
				homeNavAnimation();
			});
		});
	});
}



function homeNavAnimation() {
	var fadeDelay = 100;
	$('#meet-team').fadeIn(1000);
	setTimeout("$('#visit').fadeIn(1000)", fadeDelay);
	setTimeout("$('#learn-more').fadeIn(1000)", fadeDelay*2);
	setTimeout("$('#see-projects').fadeIn(1000)", fadeDelay*3);
	setTimeout("$('#contact').fadeIn(1000, function() {navAnimationFinished = true;})", fadeDelay*4);
	setTimeout("$('#wordmark').fadeIn(2000)", 2000)
	ssTimeout = setTimeout("homeNavSlideshow()", 4000);
}

function homeNavSlideshow() {
	if (ssCurrent == -1){
		ssCurrent = 0;
	} else {
		hoverOff($('#'+ids[ssCurrent]));
		ssCurrent += 1;
		if (ssCurrent >= ids.length){
			ssCurrent = 0;
		}
	}
	
	hoverOn($('#' + ids[ssCurrent]));
	
	ssTimeout = setTimeout("homeNavSlideshow()", 4000);
}

function killSlideshow() {
	if (ssTimeout != null){
		clearTimeout(ssTimeout);
	}
	if (ssCurrent != -1) {
		hoverOff($('#'+ids[ssCurrent]));
	}
}

function hoverOn(obj){
	if (!navAnimationFinished) return false;
	var id = $(obj).attr('id');
	$(obj).css('z-index', '50');
	$(obj).stop().animate(inOpts[id], animationSpeed);
	$(obj).find('a').stop().animate({opacity: 1}, animationSpeed/2);
	$('#home-nav #'+id+'-text').stop().css('z-index', '51').animate({opacity: 1}, animationSpeed);
	var x= $('#home-nav #'+id+'-text');
}

function hoverOff(obj){
	if (!navAnimationFinished) return false;
	$(obj).css('z-index', '25');
	var id = $(obj).attr('id');
	var text = $('#home-nav #'+id+'-text');
	$(obj).stop().animate(outOpts[id], animationSpeed, function(){$(obj).css('z-index', '20');});
	$(obj).find('a').stop().animate({opacity: 0.7}, animationSpeed);
	$(text).stop().animate({opacity: 0}, animationSpeed, function(){$(text).css('z-index', '-1');});
}


$(window).load(function() {
	// If the splash elements are there, start the splash animation
	// otherwise start the nav animation
	if ($('#splash-wrapper').length > 0){
		homeSplashAnimation();
	} else {
		homeNavAnimation();
	}
});

$(document).ready(function(){
	$('body').removeClass('nojs');
	$('#home-nav').prepend('<span class="js-text" id="contact-text" style="width:163px;"><b></b></span>');
	$('#home-nav').prepend('<span class="js-text" id="see-projects-text" style="background-position: 405px -70px; width:212px"><b></b></span>');
	$('#home-nav').prepend('<span class="js-text" id="visit-text" style="background-position: 405px -140px;width:234px;"><b></b></span>');
	$('#home-nav').prepend('<span class="js-text" id="meet-team-text" style="background-position: 405px -210px;width:206px;"><b></b></span>');
	$('#home-nav').prepend('<span class="js-text" id="learn-more-text" style="background-position: 405px -280px;"><b></b></span>');

	$('#home-nav li').hover(
		function() {
			killSlideshow();
			hoverOn(this);
		}, function() {
			ssTimeout = setTimeout("homeNavSlideshow()", 4000);
			hoverOff(this);
		}
	);
	


});

