var didClick = false;
var scrlSections = {};
var ua = navigator.userAgent;
var uaChk = {
	iphone: ua.match(/(iPhone|iPod|iPad)/),
	blackberry: ua.match(/BlackBerry/),
	android: ua.match(/Android/)
};

checkWindowView = function(notrk){
	var wt = $(window).scrollTop();
	var wh = Math.round($(window).height() / 2);
	var usePos = 0;
	for(var sect in scrlSections){
		if((scrlSections[sect] - wh) < wt) usePos = sect;
	}
	if(usePos !== 0){
		if(!$('#nav a[href="#' + usePos + '"]').is('.active')){
			$('#nav a.active').removeClass('active');
			$('#nav a[href="#' + usePos + '"]').addClass('active');
			if(!notrk) _gaq.push(['_trackEvent', 'MainNav', 'Scroll', $('#nav a[href="#' + usePos + '"]').attr('title')]);
		}
	}
};

$(document).ready(function(){
	
	$('.trans').fadeTo(0, 0.9);
	
	// IE 7/IE 8 Compatability fix
	if($.browser.msie && ((parseInt($.browser.version.substr(0, 1)) <= 7 && document.documentMode <= 7) || document.documentMode <= 7)){ //ie 7 only fixes
		$("hr").replaceWith('<div class="hr"></div>');
		$('#flt_bar').css('top', '-2px');
		$('#nav').css({'position':'relative','left':'2px'});
		$('html').css('overflow-x', 'hidden');
	}
	
	// main nav
	$('#nav a').click(function(e){
		e.preventDefault();
		didClick = true;
		$('#nav a.active').removeClass('active');
		$(this).addClass('active');
		_gaq.push(['_trackEvent', 'MainNav', 'Click', $(this).attr('title')]);
		$('html,body').stop().animate({scrollTop: $($(this).attr('href')).offset().top}, 650, 'easeOutQuint', function(){ setTimeout("didClick = false;", 100); });
		return false;
	});
	
	// sliding area
	$('#slidenav a').click(function(e){
		e.preventDefault();
		if($(this).not('.active')){
			$('#slidenav a.active').removeClass('active');
			
			var setInd = $('#slidenav a').index($(this));
			var mvLft = '-' + $('#slides .slider .slide').eq(setInd).position().left + 'px';
			
			$('#slides .slider').stop().animate({ left: mvLft }, 450, 'easeOutQuint');
			$(this).addClass('active');
			_gaq.push(['_trackEvent', 'FiveEssentialIdeals', 'Click', $(this).attr('title')]);
		}
		return false;
	});
	
	// custom links
	$('.scrl').click(function(e){
		e.preventDefault();
		$('html,body').stop().animate({scrollTop: $($(this).attr('href')).offset().top}, 650, 'easeOutQuint');
		return false;
	});
	
	$('.mdl').colorbox({
		onOpen: function(){
			_gaq.push(['_trackEvent', 'ModalLink', 'Click', $(this).attr('title')]);
		}
	});
	
	// detect mobile browser
	if(uaChk.iphone || uaChk.android){
		$('#flt_bar').css({'min-height' : $('#fixednav').outerHeight() + 'px', 'position' : 'absolute', 'top' : 'auto'});
		$('#logo').css('margin-bottom', '30px');
		$('#nav').css('margin-bottom', '0');
		$('#fsc').hide();
		$('#footer .content .fscmbl').show();
		$(window).scroll(function(){
			var wt = $(window).scrollTop();
			$('#flt_bar').css('top', wt + 'px');
		});
	}
	
	// detecet section change
	$('#nav a').each(function(i){
		var theHref = $(this).attr('href');
		scrlSections[theHref.substr(1)] = Math.round($(theHref).offset().top);
	});
	$(window).scroll(function(e){ if(didClick == false && !$('html,body').is(':animated')) checkWindowView(); });
	checkWindowView(true);
	
}); // document ready
