/* Sitewide JS for axisflex.com   
   Author: Wess Willis  lucentpdx.com */

$(function() {

	$('.closed').css({"width" : "160", "height" : "46"});

	// OPEN NAV MENU
	$('.trigger').click(function(){ 
		if ($(this).parent().hasClass('closed')) {
        	$(this).parent().toggleClass('open').toggleClass('closed'); 
        	$(this).parent().animate({width: 314, height:334},300);
        	$(this).blur();
        } else {
        	clearNav();
        }
		return false;
	});

  	// KEEP MENU OPEN IF CLICKING INSIDE
  	$('nav').click(function() {
		return false;
  	});
  	
  	// NAVIGATE TO CLICKED LINK
  	$('nav > ul > li > a').click(function() {
  		window.location.href = this;
  		setTimeout(function(){clearNav()},250);
  	});
  	
	// CLOSE MENU BY CLICKING OUTSIDE OF IT
	$('html').click(function() {
		clearNav();
	});

	// CLOSE ALL NAVIGATION MENUS
	function clearNav() {
		if ($('.open').length) {
			$('.open').toggleClass('closed').toggleClass('open'); 
			$('.trigger').parent().animate({width: 160, height:46},300 );
			$('.trigger').blur();
		}
	}

  
  
  /* HOME PAGE SLIDER 
  ------------------------------------------------*/ 
	
	var slides = $('.slide');
	var slideHeight = 120;
	
	// containing box
	var move = $('#move');
	var interval = 0;
	var timeout = 0;
	
	if(slides.length) {
				
		if(slides.length <= 4) {
			// too few slides, disable animation
			$('.slideControls').css('display','none');
			$('#slideshow').css('margin',0);
			
		} else {
			// more than 4 slides; ANIMATE!
			animate();

			// set up click event
			$('.slideControls').click(function() {
				
				// temporarily pause interval
				window.clearTimeout(timeout);
				window.clearInterval(interval);
				timeout = window.setTimeout(function(){ animate()},5000)
				
				// move slider!
				if( $(this).attr('id') === 'ctrlTop' ) {
					moveSlides('+',300,'easeOutExpo')
				}
				if( $(this).attr('id') === 'ctrlBottom' ) {
					moveSlides('-',300,'easeOutExpo')
				}
			});
			
		}
	}
	
	function animate() {
		moveSlides('-',2000,'easeInOutCubic');
		interval = window.setInterval(function() {
			moveSlides('-',2000,'easeInOutCubic');
		}, 3000);
		return interval;
	}
	
	function moveSlides(direction,duration,easing) {
		// move down
		clearTimeout(move.timeout);
		
		if(!move.animating) {
			if(direction === '+') {
				reOrder(direction)
				$(move).animate({top: direction+'='+slideHeight}, duration, easing);
			} else {
				$(move).animate({top: direction+'='+slideHeight}, duration, easing, function() {
					$(this).after(reOrder('-'))
				});
			}
		}
		
		// disable event
		move.animating = true; 
		move.timeout = setTimeout(function() {
			move.animating=false;
		},duration);
			
	}

	function reOrder(direction) {
		(direction == '+') ? adjust='-':adjust='+';
		if(direction === '+') {
			var slide = $('.slide:last');
			var parent = $(slide).parent();
			$(slide).detach();
			$(slide).prependTo(parent);
			if(adjust == '+')
				$(move).css({top: ($(move).position().top + slideHeight) + "px"});
			else
				$(move).css({top: ($(move).position().top - slideHeight) + "px"});
				
		} else {
			var slide = $('.slide:first');
			var parent = $(slide).parent();
			$(slide).detach();
			$(slide).appendTo(parent);
			$(move).animate({top: adjust+'='+slideHeight}, 0);
		}
	}




	/* SINGLE PAGE SCROLLING (ELIGIBLE EXPENSES)
	------------------------------------------------*/ 

	// BACK TO TOP
	$(".toplink").each(function(i) {
		var targetID = this.href.substr(this.href.indexOf('#'));
		$(this).click(function(){
			$(window).scrollTo(targetID, 500, {onAfter: function(){ $(window).scroll();}});
			return false;
		});
	});	
	
	// NAVIGATION
	var navLinks = $("#alphaNav > li > a");
	$(navLinks).each(function(i) {
		var targetID = this.href.substr(this.href.indexOf('#'));
		// store section offsets in link object
		navLinks[i].offset = $(targetID).offset();
		$(this).click(function(){
			$(window).scrollTo(
				targetID, 
				750,
				{offset: {top:-50, left:0}, onAfter: function(){} }
			);
			return false;
		});
	});
	
	var navBarPosition = $("#alphaNavContainer").offset();
	if(navBarPosition) {
		$(window).scroll(function() {

			var navBar = $("#alphaNav");
			var scrollTop = $(window).scrollTop();

		    if($('body').hasClass('iphone')){
    			if(navBarPosition.top <= scrollTop) {
    			    $(navBar).addClass('fixed');
                    $("#alphaNavContainer").offset({top:window.pageYOffset, left: navBarPosition.left});
                }else{
                    $("#alphaNavContainer").offset({top:navBarPosition.top, left: navBarPosition.left});
                    $(navBar).removeClass('fixed');
                }

            }else{
    			// FIX SUBPAGE_NAV AT THE TOP OF THE PAGE
    			if(navBarPosition.top <= scrollTop) {
    			    $(navBar).addClass('fixed');
            	} else {
    				$(navBar).removeClass('fixed');
    			}
            }
            	
			// UPDATE "SELECTED" NAVIGATION BASED ON SCROLLING
			var windowOffset = 50;
			$(navLinks).each(function(i) {
				// if section is in view and there is no next section, or
				// section is in view and the next section isn't in view, and
				// it is not at the top of the page
				if(((navLinks[i].offset.top < scrollTop + windowOffset && !navLinks[i+1]) ||	
					(navLinks[i].offset.top < scrollTop + windowOffset && navLinks[i+1].offset.top > scrollTop + windowOffset)) &&
					scrollTop > 0) {
					// if section has scrolled into view
					$(this).addClass('selected');
				} else {
					$(this).removeClass('selected');
				}
				
			});
	
		});
	}
	
});
