/*_____________________________________________________________________________________

FILE NAME:          app.js

DESCRIPTION:        Clock Four v2.0 Javascripts

SITE URL:           http://www.clockfour.com

AUTHOR:             C4 Tech Team

STRUCTURE:
                    1.0 LAST CHILD CLASS CONTROL
                    2.0 HOMEPAGE BLOCKS
                    3.0 SEARCH
                    4.0 PAGE NAVIGATION
                    5.0 CASE STUDIES
                    6.0 CAREERS
                    7.0 CONTACT
                    8.0 FOURSIGHT


_______________________________________________________________________________________
                                                                                     */

// declare global timeout vars to avoid ie js errors
var wait, hold, pause;


// check for windows as font rendering is throwing off alignment of top sub nav
$(function(){
	if( navigator.appVersion.indexOf('Win') != -1 ){ $('.page-nav').addClass('windows'); }
});


/*_____________________________________________________________________________________
1.0 LAST CHILD CLASS CONTROL                                                         */

$(function(){
	$('.section-nav li').last().addClass('last-child');
	$('.sub-nav').each(function(){
		$(this).find('li').last().addClass('last-child');
	});
	$('.footer-nav li').last().addClass('last-child');
	$('.case-study-teasers p').last().addClass('last-child');
	$('.description').last().addClass('last-child');
});



/*_____________________________________________________________________________________
2.0 HOMEPAGE BLOCKS                                                                  */

$(function(){
	$('.hero-blocks').bind({
		mouseleave: function(){
		wait = setTimeout(function(){ 
				$('#homepage-callout').fadeIn(1200);
			}, 200);
		},
		mouseenter: function(){
			$('#homepage-callout').clearQueue().fadeOut(200);
			clearTimeout(wait);
		}
	});
	
	$('.hero-trigger').bind({
		mouseenter: function(){
			$(this).parent().find('.hero-cover').fadeIn(600);
		},
		mouseleave: function(){
			$('.hero-cover').hide();
		}
	});
	
});



/*_____________________________________________________________________________________
3.0 SEARCH                                                                           */

$(function(){
	$('#search').bind('focus blur', function(e){
		$('.search-button').toggleClass('active');
	});
});



/*_____________________________________________________________________________________
4.0 PAGE NAVIGATION                                                                  */

$(function(){
	
	var highlight = $('#nav-highlight');
	var sectionNav = $('.section-nav li');
	var originalIndex = showSubNav();
	var originalPosition = sectionNav.eq(originalIndex).position().left;
	
	if($('b').hasClass('homepage') === true){ $('b, .sub-nav').hide(); }
	
	// Sets original position and width of highlight on dom ready
	highlight.css({
		'left'	: originalPosition,
		'width'	: sectionNav.eq(originalIndex).find('a').innerWidth()
	});
	
	// Resets to original position after mouseleave of 1500ms if applicable
	$('.page-nav').bind({
		mouseenter: function(){
			clearTimeout(hold);
		},
		mouseleave: function(){
			
			hold = setTimeout(function(){
				if($('b').hasClass('homepage') === true){
					$('b, .sub-nav').fadeOut(600);
					return
				}
				
				highlight.animate({
						left: sectionNav.eq(originalIndex).find('a').position().left,
						width: sectionNav.eq(originalIndex).find('a').innerWidth()
					}, 300);
					
					sectionNav.removeClass('active');
					sectionNav.eq(originalIndex).addClass('active');
					showSubNav();
					
				}, 1500);
		}
	});
	
	// Controls slider width/position and calls function controlling sub-nav display
	$('.section-nav a').mouseenter(function(){
		var link = $(this);
		var newPosition = $(this).parent().index();
		
		if($('b').hasClass('homepage') === true){ $('b').fadeIn(600); }
		
		highlight.clearQueue().animate({
			left: link.position().left,
			width: link.innerWidth()
		}, 300);
		
		sectionNav.removeClass('active');
		sectionNav.eq(newPosition).addClass('active');
		showSubNav();
	});
});



// Worker to determine which sub-nav to display
function showSubNav(){
	var position = $('.section-nav').find('.active').index();
	var theSubNav = $('.sub-nav').eq(position);
	
	$('.sub-nav').hide();
	theSubNav.show();
	
	return position
}



/*_____________________________________________________________________________________
5.0 CASE STUDIES                                                                     */

$(function(){
	
	if($('#slideshow').length)
	{
		
		// Initialize case studies slideshow
		var slideshow = $('#slideshow').slideshow({
			transition: 'push(left)',
			selector: '> .slide',
			initialIndex: 0,
			autoStyle: true,
			autoPlay: true,
			delay: 8000,
			duration: 800
		}).data('slideshow');

		// Pause slideshow on mouseenter, restart on 5000ms after mouseleave
		$('#slideshow').bind({
			mouseenter: function(){
				slideshow.stop();
				clearTimeout(pause);
			},
			mouseleave: function(){
				pause = setTimeout(function(){
					slideshow.play(true);
				}, 5000);
			}
		});

		// Slideshow navigation
		$('#next-slide').click(function(e){
			e.preventDefault();
			slideshow.show('next');
		});
		$('#prev-slide').click(function(e){
			e.preventDefault();
			slideshow.show('previous', { transition: 'push(right)'});
		});
		
		// Stopping undesirable behavior on window blur
		$(window).bind({
			blur: function(){
				slideshow.stop();
				clearTimeout(pause);
			},
			focus: function(){
				pause = setTimeout(function(){
					slideshow.play(true);
				}, 5000);
			}
		});
		
	}
	
	// Case study nav
	var triggers = $('.case-nav a');
	var labels = $('.case-nav p');
	
	triggers.bind({
		mouseenter: function(){
			var thisIndex = $(this).index('.case-nav a');
			labels.eq(thisIndex).fadeIn();
		},
		mouseleave: function(){
			labels.hide();
		}
	});
	
});



/*_____________________________________________________________________________________
6.0 CAREERS                                                                          */

$(function(){
	$('.role-description').hide();
	
	$('.position a').click(function(event){
		event.preventDefault();
		
		var thisOne = $(this).next('.role-description');
		
		if($(this).hasClass('expanded') === false)
		{ 
			$('.position a').removeClass('expanded');
			$(this).addClass('expanded');
		} else {
			$(this).removeClass('expanded');
		}
		
		$('.role-description').not(thisOne).slideUp(1000, 'easeOutQuart');
		thisOne.slideToggle(1000, 'easeOutQuart');
	});
});



/*_____________________________________________________________________________________
7.0 CONTACT                                                                          */

$(function(){
	var maps = $('#sf-map, #utah-map');

	$('.map-trigger').click(function(){		
		thisMap = $(this).index('.map-trigger');
		maps.hide();
		var iframeContent = maps.eq(thisMap).html();
		maps.eq(thisMap).html(iframeContent).show();
	});
	
});


/*_____________________________________________________________________________________
8.0 FOURSIGHT                                                                        */

// FourSight clear fields after click
$(function(){
	$('#mc-embedded-subscribe').click(function(){
		$('#mce-FNAME, #mce-LNAME, #mce-EMAIL, #mce-COMPANY').val('');
	});
});




