var NavAbout = "#aboutdiv";
var NavHome = "#homepage";
var NavPortfolio = "#portfoliopage";
var NavCaseStudies = "#casestudypage";
var NavPrintProducts = "#printproductspage";
var NavBlog = "#blogpage";
var NavContact = "#contact";
var currentNav = "#homepage";
var processNav = "#homepage";
var NavID = [];
NavID[NavHome] = 0;
NavID[NavPortfolio] = 1;
NavID[NavCaseStudies] = 2;
NavID[NavPrintProducts] = 3;
NavID[NavBlog] = 4;
NavID[NavContact] = 5;
var currentHeight = 0;
var currentScrollTop = 0;
var maxScrollTop = 0;
var animationSpeed = 600;

$(function(){
	var aboutheight = $('#aboutdiv').height();
	var screenheight = $('.screen').height();
	var navheight = $('.nav').height() + 2;
	var designheight = $('#designpage').height();
	var homepageheight = $('#client-banner').height() + $('.featuredprojectscontainer').height();
	var portfolioheight = $('#portfoliopage').height();
	var casestudiesheight = $('#casestudypage').height();
	var printproductsheight = $('#printproductspage').height();
	var contactheight = $('#contact').height();
	$('a.home.anchorLink')
		.click(function(){
			_hideOpenNav(NavHome,'N');
		});

	$('a.about.anchorLink')
		.click(function(){
			_isAboutOpen(NavAbout);
		});
		
	$('a.design.anchorLink')
		.click(function(){
			_isAboutOpen(NavHome);
		});

	$('a.portfolio.anchorLink')
		.click(function(){
			_hideOpenNav(NavPortfolio);
		});

	$('a.casestudy-toggle.anchorLink')
		.click(function(){
			_hideOpenNav(NavCaseStudies);
		});

	$('a.printproducts.anchorLink')
		.click(function(){
			_hideOpenNav(NavPrintProducts);
		});

	$('a.blog.anchorLink')
		.click(function(){
			_hideOpenNav(NavBlog);
		});

	$('a.contact.anchorLink')
		.click(function(){
			_hideOpenNav(NavContact);
		});
		
	$('a.returntop')
		.click(function(){
			_scrollToTop();
		});
	
	function _hideOpenNav(nextNav, isDesign) {
		if(currentNav != nextNav || $(".wrapper").scrollTop() == 0) {
			var screenScrollTop = (aboutheight + screenheight);
			if(nextNav == NavHome || nextNav == NavContact) {
				screenScrollTop = aboutheight;
			}
			if(currentScrollTop != screenScrollTop) {
				currentScrollTop = screenScrollTop;
				var nHeight = maxScrollTop - screenScrollTop + navheight;	
				if(currentNav == NavHome) {
					nHeight = nHeight + homepageheight;
				} else {
					nHeight = nHeight + $(currentNav).height();
				}
				if($('.wrapper').height() > nHeight) {
					$(".wrapper").animate({"scrollTop": screenScrollTop}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
					$('.wrapper').animate({"height": nHeight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
					_processOpenNav(nextNav, isDesign);
				} else {
					$('.wrapper').animate({"height": nHeight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
					$(".wrapper").animate({"scrollTop": screenScrollTop}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
					_processOpenNav(nextNav, isDesign);
				}
			} else {
				_processOpenNav(nextNav, isDesign);
			}
		} else if (isDesign == 'Y' || isDesign == 'N') {
			_processOpenNav(nextNav, isDesign);
		}
	}
	function _processOpenNav(nextNav, isDesign) {
		var dHeight = maxScrollTop - currentScrollTop + navheight;
		if(processNav != nextNav && currentNav != nextNav) {
			processNav = nextNav;
			var cNewTop = 0 - $(currentNav).height();
			var nOldTop = $(currentNav).height() + dHeight;
			var nNewTop = 0;
			if(nextNav == NavHome) {
				currentHeight = dHeight + homepageheight;
			} else {
				currentHeight = dHeight + $(nextNav).height();
			}
			
			if(NavID[currentNav] > NavID[nextNav]) {
				cNewTop = $(nextNav).height();
				nOldTop = 0 - $(nextNav).height();
				nNewTop = 0;
			}
			
			//$('.wrapper').animate({"height": dHeight}, {duration: animationSpeed, easing: 'easeInOutQuad'});
			$(currentNav).animate({"top": cNewTop}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false, complete:function() {
				$(currentNav).css({top: 4000});
			}});
			$(nextNav).css({top: nOldTop});
			if(nextNav == NavHome) {
					if(isDesign == 'Y') {
						$('#designpage').css({height: designheight});
						currentHeight += designheight;
					} else {
						$('#designpage').css({height: 0});
					}
			}
			$('.wrapper').animate({"height": currentHeight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
			$('.wrappersub').animate({"height": currentHeight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
			$(nextNav).animate({"top": nNewTop}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false, complete:function() {
				currentNav = nextNav;
				if(nextNav != NavHome) {
					$('#designpage').css({height: 0});
				}
			}});
		} else if (isDesign == 'Y') {
			if($('#designpage').height() == designheight) {
				$('#designpage').animate({"height": 0}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
				$('.wrapper').animate({"height": dHeight + homepageheight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
				$('.wrappersub').animate({"height": homepageheight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
			} else {
				$('#designpage').animate({"height": designheight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
				$('.wrapper').animate({"height": dHeight + homepageheight + designheight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
				$('.wrappersub').animate({"height": homepageheight + designheight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
			}
		} else if (isDesign == 'N') {
			if($('#designpage').height() == designheight) {
				$('#designpage').animate({"height": 0}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
				$('.wrapper').animate({"height": dHeight + homepageheight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
			}
		}
	}
	function _isAboutOpen(nextNav) {
		if($(".wrapper").scrollTop() == 0 || nextNav == NavHome) {
			currentHeight = $(".wrappersub").height() + screenheight + navheight;
			if(currentNav == NavHome) {
				currentHeight = homepageheight + screenheight + navheight;
				if($('.wrapper').height() == currentHeight) {
					if(nextNav == NavHome) {
						_hideOpenNav(NavHome, 'Y');
					}
				} else {
					if(nextNav == NavHome && $(".wrapper").scrollTop() != 0) {
						_hideOpenNav(NavHome, 'Y');
					} else {
						$('.wrapper').animate({"height": currentHeight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
						$(".wrapper").animate({"scrollTop": aboutheight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false, complete: function() {
							if(nextNav == NavHome) {
								_hideOpenNav(NavHome, 'Y');
							}
						}});
					}
				}
				currentScrollTop = aboutheight;
			} else {
				if(nextNav == NavHome) {
					_hideOpenNav(NavHome, 'Y');
				}
			}
		} else {
			if($('#designpage').height() != 0 && currentNav == NavHome) {
				currentHeight = homepageheight + aboutheight + screenheight + navheight;
				var dHeight = maxScrollTop - currentScrollTop + navheight;
				$('#designpage').animate({"height": 0}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
				$('.wrappersub').animate({"height": homepageheight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
				$('.wrapper').animate({"height": dHeight + homepageheight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false, complete: function() {
					$('.wrapper').animate({"height": currentHeight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
					$(".wrapper").animate({"scrollTop": 0}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
				}});
			} else {
				currentHeight = $(".wrappersub").height() + aboutheight + screenheight + navheight;
				$('.wrapper').animate({"height": currentHeight}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
				$(".wrapper").animate({"scrollTop": 0}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
			}
			currentScrollTop = 0;
		}
	}
	function _scrollToTop() {
		$("body").animate({"scrollTop": 0}, {duration: animationSpeed, easing: 'easeInOutQuad', queue: false});
	}
	
	$('.wrapper').scrollTop(aboutheight);
	currentScrollTop = aboutheight;
	maxScrollTop = aboutheight + screenheight;
	$('#designpage').css({height: 0});
	$('#homepage').css({top: 0});
	currentHeight = screenheight + navheight + homepageheight;
	$('.wrapper').css({height: currentHeight});
	$('.wrappersub').css({height: homepageheight});
});
