$(document).ready(function()
{
	/*
	* NAVIGATION
	*/
	var navActive 		= false;
	var popupMenu 		= $( '.primarynav-popup ul' );
	var tagline			= $( '.tagline' );
	var calltoaction	= $( '.calltoaction' );
	var initMessage;
	
	$('#primary-nav a')			.mouseenter( function(){ popUp($(this)) } );
	$('.primarynav-popup ul')	.mouseleave( function(){ popOut() } );
	$('#header-wrap')			.mouseleave( function(){ popOut() } );
	
	//REPOSITIONS ALL POP UP MENUS TO HIDE
	popupMenu.each(function()
	{   
		var newPos = $( this ).height() + $( this ).outerHeight();
    	$(this).css( {bottom: -newPos});
        $('.primarynav-popup').css({opacity:1});
  	});
  	//REPOSITIONS ALL POP UP MENUS TO HIDE W/ ANIMATION
	function hidePop()
	{
		popupMenu.each(function()
		{
			navActive	= false;
			var newPos	= $( this ).height() + $( this ).outerHeight();
			$( this ).stop().animate( { bottom: -newPos }, 400 );
  		});	
	}
	function popUp( btn )
	{
		if( navActive == true ) hidePop();
		navActive = true;
		$( '#' + btn.attr( 'id' ) +'-pop' ).stop().animate({ bottom:0 }, 300 );
		
		if( initMessage != 'hidden' )
		{
			initMessage = 'hidden';
			tagline.animate( { opacity:0 },500 );
			calltoaction.animate( { opacity:0 },500 );
		}
		showFeature( btn );
	}
	function popOut()
	{
		hidePop();
	}
	/*
	* FEATURE
	*/
	var nextSlideNum		= 1;
	var currentSlide 		= $('.feature-'+nextSlideNum);
	var featureInterval;
	var checkingNavActivity = false;
	var activeNavInterval;
	var selectedSlide;
	var showFeatureStart	= false;
	
	setInt();
	currentSlide.insertAfter( currentSlide.parent().children().last() );
	function setInt()
	{
		tagline.animate( { opacity:1 },1000 );
		calltoaction.animate( { opacity:1 },1000 );
		$('.feature-description').css({display:'none'});
		initMessage = null;
		
		featureInterval = window.setInterval(function()
		{
			featureCycle();							
		},3000);
	}
	function featureCycle()
	{
		
		if( nextSlideNum < 4 )
		{
			nextSlideNum = nextSlideNum + 1;
		}
		else
		{
			nextSlideNum = 1;
		}
		currentSlide = $( '.feature-' + nextSlideNum );
		currentSlide.css( { opacity:0 } );
		currentSlide.insertAfter( currentSlide.parent().children().last() );
		currentSlide.animate( { opacity:1 },1000 );
	}
	function showFeature( btn )
	{
		//STOP SLIDE
		if( featureInterval != null )
		{
			clearInterval( featureInterval );
			featureInterval = null
		}
		if( checkingNavActivity == false )
		{
			checkingNavActivity = true;
			activeNavInterval = window.setInterval( function()
			{
				if( navActive == false )
				{
					setInt();
					clearInterval( activeNavInterval );
					activeNavInterval 	= null;
					checkingNavActivity = false;
				}
			},1000);
		}
		var selectedSlide 	= $( '#' + btn.attr( 'id' ) + '-feature' );
		nextSlideNum 		= Number( selectedSlide.attr( 'class' ).replace( "feature-" , "" ) );
		currentSlide 		= $( '.feature-' + nextSlideNum );
		
		if( currentSlide.attr( 'class' ) != currentSlide.parent().children().last().attr( 'class' ) )
		{
			currentSlide.css( { opacity:0 } );
			currentSlide.insertAfter( currentSlide.parent().children().last() );
			currentSlide.stop().animate( { opacity:1 },500 );	
		}
		currentSlide.children('.feature-description').css( {display:'block'});
	}
});
