var slideTopFeatureInterval;
var benefitsInProgress = false;

function slideTopFeature() {
	var curr = $('#top-feature img.selected');
	var next = curr.next();
	var speed = 700;
	if (!next.length) {
		next = $('#top-feature img:first');
		speed = $('#top-feature img').length * speed;
	}
	curr.removeClass('selected');
	next.addClass('selected');
	$('#top-feature').scrollTo(next, speed, {
		easing: 'easeInOutCubic'
	});
}

$(document).ready(function() {
	
	//Static Fiction Edit added ('#topBarContentWrapper') to page-scrolling to account for new wrapper divs
	//eg... $.scrollTo($('#benefits'), 700, ......
	//is now: $('#topBarContentWrapper').scrollTo($('#benefits'), 700, ......											
	
	$('#top-feature img:first').addClass('selected');
	$('#top-feature img').click(function () {
		clearInterval(slideTopFeatureInterval);
		slideTopFeature();
		slideTopFeatureInterval = setInterval(slideTopFeature, 8000);
	}).css('cursor', 'pointer');
	slideTopFeatureInterval = setInterval(slideTopFeature, 8000);
	
	// init
	$('div#benefits-container').height($('div#benefits-container li.selected').height());
	
	$('a.up-section:first').hide().next().hide();
	$('a.up-section').click(function () {
		var prev = $($(this).closest('div.section').prevAll('.section')[0]);
		$('#topBarContentWrapper').scrollTo(prev, 700, {
			easing: 'easeInOutCubic'
		}); 
		return false;
	});
	$('a.down-section:last').hide().prev().hide();
	$('a.down-section').click(function () {
		var prev = $($(this).closest('div.section').nextAll('.section')[0]);
		$('#topBarContentWrapper').scrollTo(prev, 700, {
			easing: 'easeInOutCubic'
		}); 
		return false;
	});
	
	$('.view-benefits').click(function () {
		$('#topBarContentWrapper').scrollTo($('#benefits'), 700, {
			easing: 'easeInOutCubic'
		}); 
		return false;
	});
	$('.scroll-top a').click(function () {
		$('#topBarContentWrapper').scrollTo(0, 700, {
			easing: 'easeInOutCubic'
		}); 
		return false;
	});
	
	$('div#news-container li').hide();
	$('div#news-container li.selected').show();
	$('div#news-control a.next').click(function () {
		var curr = $('div#news-container li.selected');
		var next = curr.next();
		if (!next.length) {
			next = $('div#news-container li:first');
		}
		curr.removeClass('selected');
		next.addClass('selected');
		curr.fadeOut(500, function () {
			next.hide();
			$('div#news-container').scrollTo(next, 0);
			next.fadeIn(500);
		})
		return false;
	});
	$('div#news-control a.prev').click(function () {
		var curr = $('div#news-container li.selected');
		var next = curr.prev();
		if (!next.length) {
			next = $('div#news-container li:last');
		}
		curr.removeClass('selected');
		next.addClass('selected');
		curr.fadeOut(500, function () {
			next.hide();
			$('div#news-container').scrollTo(next, 0);
			next.fadeIn(500);
		})
		return false;
	});
	
	$('div#benefits-control a.next, #benefits-images img').click(function () {
		
		if(benefitsInProgress) {
			return false;
		}
		
		var curr = $('div#benefits-container li.selected');
		var next = curr.next();
		var currImg = $('div#benefits-images img.selected');
		var nextImg = currImg.next();
		if (next.length) {
			
			benefitsInProgress = true;
			
			currImg.removeClass('selected');
			nextImg.addClass('selected');
			curr.removeClass('selected');
			next.addClass('selected');
			curr.fadeOut(700, function () {
				next.fadeOut(0);
				$('div#benefits-container').scrollTo(next, 0);
				next.fadeIn(700, function() {
					benefitsInProgress = false;
				});
			});
			$('div#benefits-images').scrollTo(nextImg, 700);
			$('div#benefits-container').animate({
				height: $('div#benefits-container li.selected').height()
			}, {
				duration: 700,
				queue: false,
				specialEasing: {
					height: 'easeInOutCubic'
				}
			});
		}
		return false;
	});
	$('div#benefits-control a.prev').click(function () {
		
		if(benefitsInProgress) {
			return false;
		}
		
		var curr = $('div#benefits-container li.selected');
		var next = curr.prev();
		var currImg = $('div#benefits-images img.selected');
		var nextImg = currImg.prev();
		if (next.length) {
			
			benefitsInProgress = true;
			
			currImg.removeClass('selected');
			nextImg.addClass('selected');
			curr.removeClass('selected');
			next.addClass('selected');
			curr.fadeOut(700, function () {
				next.fadeOut(0);
				$('div#benefits-container').scrollTo(next, 0);
				next.fadeIn(700, function() {
					benefitsInProgress = false;
				});
			});
			$('div#benefits-images').scrollTo(nextImg, 700);
			$('div#benefits-container').animate({
				height: $('div#benefits-container li.selected').height()
			}, {
				duration: 700,
				queue: false,
				specialEasing: {
					height: 'easeInOutCubic'
				}
			});
		}
		return false;
	});
	
	$('tr.promo').find('td:first').each(function () {
		$(this).prepend('<div style="background: #FFFED2;position: absolute;margin: -20px 0 0 -10px;width: 10px;padding: 20px 0">&nbsp;</div>');
	});
	
	$('a.map').fancybox({
		'width'			:	'50%', 
		'height'		:	'70%', 
		'type' 			:   'iframe',
		'centerOnScroll':   true,
		'overlayColor'	:   '#000'
	});
	$('a.buy-now').fancybox({
		'width'			:	'80%', 
		'height'		:	'80%', 
		'type' 			:   'iframe',
		'centerOnScroll':   true,
		'overlayColor'	:   '#000'
	});
});
