$(document).ready(function() {
	
	var isiSomething = false;
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
		isiSomething = true;
	}
	
	var offset = 70,
		xloc = 0,
		fade = 0.7,
		transitionSpeed = 300;
	
	$('body').addClass('js-enabled');
	
	
	/* = MASK
	-------------------------------------------------------------- */
	$('.pane:not(#table-of-contents)').append('<div class="zorro"></div>');
	$('.focus .zorro').css({'opacity':'0', 'display':'none'});
	
	
	
	/* =NAVIGATION
	-------------------------------------------------------------- */
	var navMenu,
		urlSegment = 0, // the significant url segment (who, what, or how)
		pos,
		noFocus = true;
	
	var currentHref, currentPage, current, targetHref, targetPage, target;
	$('#primary a.nav-trigger').click(function(ev) {
		ev.preventDefault();
		navMenu = $(this).next('ol');
		if(navMenu.hasClass('hide')) {
			$('#primary .nav-menu:not(.hide)').addClass('hide');
			navMenu.removeClass('hide');
			navMenu.removeClass('hover');
			$('#nav-overlay').css({'display':'block', 'opacity':'1'});
		} else {
			navMenu.addClass('hide');
			if(!isiSomething) {
				navMenu.addClass('hover');
			}
			$('#nav-overlay').css({'display':'none', 'opacity':'0'});
		}
	});
	
		$('#nav-overlay').click(function() {
			$('#primary .nav-menu').addClass('hide');
			$(this).css({'display':'none','opacity':'0'});
		});
		
		if(!isiSomething){
			$('#primary a.nav-trigger').hover(function() {
				if($(this).next().hasClass('hide')) { $(this).next().addClass('hover'); }
			}, function() {
				if($(this).next().hasClass('hide')) { $(this).next().removeClass('hover'); }
			});
		}
		
		/* =JUMP TO PANE ON LOAD
		-------------------------------------------------------------- */
		currentHash = window.location.hash.substr(1);
		current = '.anchor-'+currentHash;
		if($(current).length > 0) {
			pos = $(current).position();
			xloc = pos.left - offset;
			setTimeout(function() {
				if(!$(current).hasClass('focus')) {
					$('.tray .focus .zorro').css({'display':'block'}).animate({'opacity':fade}, transitionSpeed, function() { $(this).parent('.focus').removeClass('focus'); });
				}
				$('.tray').animate({'left':'-'+xloc+'px'}, transitionSpeed);
				if(!$(current).hasClass('focus')) {
					$(current).children('.zorro').animate({'opacity':'0'}, transitionSpeed, function() {
			  		$(this).css({'display':'none'});
			  		$(this).parent().addClass('focus');
			  	});
				}
			}, 500);
		} else {
			xloc = $('#table-of-contents').width() - offset;
			setTimeout(function() { $('.tray').animate({'left':'-'+xloc+'px'}, transitionSpeed); }, 500);
		}
		
		/* =NAV CLICK
		-------------------------------------------------------------- */
		$('#primary .nav-menu a, #table-of-contents .nav-menu a').click(function(ev) {
			currentPage = $('body').attr('id');
			targetHref = $(this).attr('href').substring(1);
			targetPage = targetHref.split('/');
			
			if(currentPage == targetPage[urlSegment]) {
				ev.preventDefault();
				nextHash = $(this).attr('href').substr($(this).attr('href').indexOf('#') + 1);
				window.location.hash = nextHash;
				$('#primary .nav-menu:not(.hide)').addClass('hide');
				var anchorPos = $('.anchor-'+nextHash).position();
				xloc = anchorPos.left - offset;
				if(!$('.anchor-'+nextHash).hasClass('focus')) { switchPane($('.anchor-'+nextHash), xloc); }
			}
		});
			
			
		/* =KEY PRESS
		-------------------------------------------------------------- */
		$(this).keypress(function(e) {
			// right 39, up 38, left 37, down 40, space 32
			if((e.keyCode == 37 || e.keyCode == 39) && !$('.form-contact input').is(':focus')) { e.preventDefault(); }
		});
		$(this).keyup(function(e) {
			// left 37, right 39
			if(!$('.tray').is(':animated') && noFocus && (e.keyCode == 37 || e.keyCode == 39)) {
				var target, pos;
				if(e.keyCode == 37) { target = $('.pane.focus').prev('.pane'); } else { target = $('.pane.focus').next('.pane'); }
				if(target.length && target.attr('id') != 'table-of-contents') {
					pos = target.position();
					xloc = pos.left - offset;
					switchPane(target, xloc);
				}
			}
		});
		
		/* =TRAY
		-------------------------------------------------------------- */
		$('#table-of-contents .toc-trigger').click(function(ev) {
			ev.preventDefault();
			if(xloc == 0) { xloc = $('#table-of-contents').width() - offset; } else { xloc = 0; }
			$('.tray').animate({'left':'-'+xloc+'px'}, transitionSpeed);
		});
		
		$('.tray').delegate('.pane', 'click', function(ev) {
			if($(this).attr('id') != 'table-of-contents' && !$(this).hasClass('focus')) {
				var pos = $(this).position();
				xloc = pos.left - offset;
				switchPane($(this), xloc);
			}
		});
		
			function switchPane(target, xloc) {
				// if tips panel is open, close it, by triggering it's toggle function.
				if($('#tips-bg').css('top') == '-30px') { $('#tips').click(); }
				$('.tray .focus .zorro').css({'display':'block'}).animate({'opacity':fade}, transitionSpeed, function() { $(this).parent('.focus').removeClass('focus'); });
			  $('.tray').animate({'left':'-'+xloc+'px'}, transitionSpeed);
			  target.children('.zorro').animate({'opacity':'0'}, transitionSpeed, function() {
			  	$(this).css({'display':'none'});
			  	$(this).parent().addClass('focus');
			  });
			}
			
		$('#what .bookend a.go-up').click(function(ev) {
			ev.preventDefault();
			$('.anchor-portfolio').trigger('click');
		});
	
	
	/* =TIPS
	-------------------------------------------------------------- */
	$('#tips').toggle(function(evt) {
		evt.preventDefault();
		$('#tips-bg').animate({'top':'-30px'}, transitionSpeed, 'easeInOutQuad');
		$('#tips-content').animate({'top':'0px', 'opacity': '1'}, transitionSpeed);
	}, function() {
		$('#tips-bg').animate({'top':'-220px'}, transitionSpeed, 'easeInOutQuad');
		$('#tips-content').animate({'top':'-240px', 'opacity':'0'}, transitionSpeed);
	});
	
		
	
	
	/* =CONTACT FORM
	-------------------------------------------------------------- */
	$('.form-contact input').focus(function() {
		noFocus = false;
	}).blur(function() {
		noFocus = true;
	});
	
});




