var selectedItem = 0;
var totalItems;
var btnsDisabled = false;
var ignoreHashChange = false;
var isScrolling = false;

(function($){
	$(document).ready(function(){
		var features = $('.feature');
		totalItems = features.length;

		$('#features-container').css('margin-left', '-3025px');

		if(totalItems > 1){
			features.eq(0).clone().appendTo('#features-container');
			features.eq(1).clone().appendTo('#features-container');
			features.eq(totalItems - 1).clone().prependTo('#features-container');
			features.eq(totalItems - 2).clone().prependTo('#features-container');
		} else if(totalItems == 1) {
			features.eq(0).clone().appendTo('#features-container');
			features.eq(0).clone().appendTo('#features-container');
			features.eq(totalItems - 1).clone().prependTo('#features-container');
			features.eq(totalItems - 1).clone().prependTo('#features-container');
		}
		
		$('.features-arrow-left').click(animateFeatures);
		$('.features-arrow-right').click(animateFeatures);

		$.waypoints.settings.scrollThrottle = 30;

		$('#header-wrapper').waypoint(
			function(e, d){
				$('#wrapper').toggleClass('sticky', d === 'down');
			}, 
			{
				offset: -480
			}
		);

		$('.subfeatures-wrapper').waypoint(
			function(e, d){
				$(this).toggleClass('sticky', d === 'down');
			}, 
			{
				offset: ($('body').hasClass('page-contact') ? 100 : 115)
			}
		);

		$('.subfeatures').cycle({
			fx: 'fade',
			timeout: 10000,
			speed: 800
		});

		$('.nav-marker').waypoint(
			function(e, d){
				if(!isScrolling){
					var hash = $(this).attr('id');
					ignoreHashChange = true;
					//changeHash(hash);
				}
			},
			{
				offset: 115
			}
		);

		$('#sub_nav_holder a').not('.ext').click(function(e){
			e.preventDefault();

			var hash = $(e.currentTarget).attr('href');
			//changeHash(hash);

			isScrolling = true;
			$.scrollTo(hash, 1000, { axis: 'y', offset: -135, easing: 'easeOutExpo', onAfter: function(){ isScrolling = false; } });
		});

		$('a.scroll').click(function(e){
			e.preventDefault();

			var hash = $(e.currentTarget).attr('href');
			//changeHash(hash);

			isScrolling = true;
			$.scrollTo(hash, 1000, { axis: 'y', offset: 0, easing: 'easeOutExpo', onAfter: function(){ isScrolling = false; } });
		});

		$(window).bind('hashchange', function() {
			window.location.href = window.location.hash;
			var hash = window.location.hash.replace('/', '');
			if(ignoreHashChange === false) {
				$.scrollTo(hash, 0, { axis: 'y' });
				//changeHash(hash);
			}

			ignoreHashChange = false;
		});

		//if($('body').hasClass('interior')){
			//$(window).scrollTop(480);

			//isScrolling = true;
			//$.scrollTo(480, 500, { axis: 'y', offset: -115, easing: 'easeOutExpo', onAfter: function(){ isScrolling = false; } });
		//}
	});

	$(window).load(function(){
		setTimeout(function(){
			if($('body').hasClass('interior') && $(window).scrollTop() < 480){
				//$(window).scrollTop(480);

				isScrolling = true;
				$.scrollTo(480, 500, { axis: 'y', offset: -115, easing: 'easeOutExpo', onAfter: function(){ isScrolling = false; } });
			}
		}, 100);
	});
})(jQuery);

function changeHash(hash){
	window.location.hash = hash.replace('#', '/');
}

function animateFeatures(e){
	e.preventDefault();

	if(!btnsDisabled){
		btnsDisabled = true;

		var direction = $(this).hasClass('prev') ? 'prev' : 'next';
		if(direction == 'next'){
			selectedItem++;
		} else {
			selectedItem--;
		}

		$('#features-container').animate({'margin-left': -(((selectedItem+1) * 1210 + 2420) - 605) + 'px'}, 1000, 'easeInOutExpo', function(){
			if(selectedItem < 0) selectedItem += totalItems;
			if(selectedItem > totalItems - 1) selectedItem -= totalItems;

			$(this).css('margin-left', -(((selectedItem+1) * 1210 + 2420) - 605) + 'px');
			btnsDisabled = false;
		});
	}
}
