var menuContactLinkHover = false;
var menuContactPopupHover = false;

var PRESS_ANIMATION_INTERVAL = 3000;
var PRESS_ANIMATION_DURATION = 400;
var pressAnimationCurrentIndex = 0;
var pressAnimationInterval;

var hideContactPopupTimer;
function hideContactPopup() {
	if (!menuContactLinkHover && !menuContactPopupHover) {
		$('#menu-contact-popup').hide();
	}
}
function resetContactTimer(){
	clearTimeout(hideContactPopupTimer);
}
function preparePopup() {
	$('#menu-contact-link').click(function(e){
		e.preventDefault();
	});
	$('#menu-contact-link').mouseenter(function(){
		menuContactLinkHover = true;
		resetContactTimer();
		var trigger = $(this);
		var popup = $('#menu-contact-popup');
		popup.appendTo('body').show();
		popup.css({
			'top': trigger.position().top + trigger.outerHeight() + 25,
			'left': trigger.offset().left - popup.width()/2 + 15,
			'z-ndex': '250'
		});
	});
	$('#menu-contact-popup').mouseenter(function(){
		resetContactTimer();
		menuContactPopupHover = true;
	});

	$('#menu-contact-link').mouseleave(function(){
		menuContactLinkHover = false;
		hideContactPopupTimer = setTimeout(hideContactPopup, 500);
	});
	$('#menu-contact-popup').mouseleave(function(){
		menuContactPopupHover = false;
		hideContactPopupTimer = setTimeout(hideContactPopup, 500);
	});
}

function prepareInvitationForm(){
	$('.subscribeForm').submit(function(e){
		e.preventDefault();
		var self = this;
		$.ajax({
			'async': true,
			'url': $(self).attr('action'),
			'data': $(self).serialize(),
			'type' : 'post',
			'success': function(html){
				//$(self).find('input').hide();
				$(self).find('label').text("You've been successfully added to our update list.");
			}
		});
		$(self).find('input').attr('disabled', 'disabled');
	});
}

function preparePressAnimation() {
	pressAnimationInterval = window.setInterval(animatePress, PRESS_ANIMATION_INTERVAL);
}

function animatePress() {
	var nextFrameId = pressAnimationCurrentIndex + 1;
	var framesCount = $('.homePressLogo a').length;
	if (nextFrameId >= framesCount) nextFrameId = 0;
	animateTo(nextFrameId);
}

function animateTo(index) {
	if (pressAnimationCurrentIndex == index) return;
	
	$('.homePressLogo a').eq(pressAnimationCurrentIndex).fadeOut(PRESS_ANIMATION_DURATION);
	$('.homePressLogo a').eq(index).fadeIn(PRESS_ANIMATION_DURATION, function(){
		pressAnimationCurrentIndex = $('.homePressLogo a').index(this);
		$('.homePressDots .dot').removeClass('activeDot');
		$('.homePressDots .dot').eq(pressAnimationCurrentIndex).addClass('activeDot');
	});
}

$(function(){
	preparePopup();
	prepareInvitationForm();

	preparePressAnimation();
	$('.homePressDots .dot').click(function(){
		window.clearInterval(pressAnimationInterval);
		$('.homePressLogo img').stop();
		var index = $('.homePressDots .dot').index(this);
		animateTo(index);
		preparePressAnimation();
	});
	
	// Press quarters
	$('.quarters > li > h4').click(function(e){
		e.preventDefault();
		var current = $(this).closest('li');
		$(this).closest('.quarters').find('> li').not(current).removeClass('active');
		current.toggleClass('active');
		$(document).scrollTop(current.offset().top);
	});
});

