var slider_interval_id;

$(document).ready(function() {

	/* Switchers */

	$('.switcher > *').click(function() {
		// Verify we are truly switching pages.
		if($(this).parent().find('.active').index() == $(this).index())
			return false

		switchSlider($(this).parent().parent(), $(this).index());

		return false;
	});

	/* Cycling switchers */
    initializeSlider();

	$(window).focus(function() {
	    if (slider_interval_id == undefined) {
		    initializeSlider();
		}
    });


    $(window).blur(function() {
		clearInterval(slider_interval_id);
		slider_interval_id = undefined;
    });

	/*** Contact form ***/

	$('#contact').submit(function(e) {
		// Prevent sending form regularly.
		e.preventDefault();

		// Makes a pressed animation on the button when submitting.
		var submit = $(this).parent().find('.submit');
		$(submit).addClass('pressed');

		// Remove the pressed status after .2sec
		setTimeout(function() {
			$(submit).removeClass('pressed');
		}, 100, [submit]);

		// Don't continue if the e-mail is invalid.
		if (!verifyEmail($('#email').val())) {
			alert('The e-mail is invalid');
			return false;
		}

		// Post it!
		$.post('/php/email_send.php',
			{
				name: $('form#contact #name').val(),
				email: $('form#contact #email').val(),
				company: $('form#contact #company').val(),
				phone: $('form#contact #phone').val(),
				message: $('form#contact #message').val()
			},
			function(data) {
				if (data != '1') {
					alert(data);
				} else {
					// Clear the fields.
					$('form#contact #name').val('');
					$('form#contact #email').val('');
					$('form#contact #company').val('');
					$('form#contact #phone').val('');
					$('form#contact #message').val('');

					// Display the thank you note.
					$('.contact-box > form').fadeOut(300);
					$('.contact-box').animate({height: '90px'}, 300, function() {
						$('.contact-box > .thankyou').fadeIn(300);
					});
				}
			});
	});



	/*** Team members ***/

	$('.team-browse').mouseenter(function() {
		$('.team-browse-instructions').fadeOut(300);
	});

	setTimeout(function() {
		$('.team-browse-instructions').fadeOut(300);
	}, 3500)

	$('.team-browse > div > img').click(function() {
		$(this).parent().addClass('open');
	});

	$('.team-browse > div').mouseleave(function() {
		$(this).removeClass('open');
	});


	// Close card on Esc key.
	$(window).keydown(function(event) {
		// 27 = Esc
		if(event.keyCode == 27) {
			// Close any open card.
			$('.team-browse > div.open').removeClass('open');
		}
	});

	// Goodies. Slow-motion on team browse, when pressing shift
	$(window).keydown(function(event) {
		// 16 = Shift
		if(event.keyCode == 16) {
			// Add slow motion class.
			$('body').addClass('slow-motion');
		}
	});
	$(window).keyup(function(event) {
		// 16 = Shift
		if(event.keyCode == 16) {
			// Remove slow motion class.
			$('body').removeClass('slow-motion');
		}
	});


	/* Header Menu */

	$('header > .menu > div').mouseenter(function() {
		$(this).children('nav').slideDown(200);
	});

	$('header > .menu > div').mouseleave(function() {
		$(this).children('nav').slideUp(200);
	});
});


function switchSlider(obj, index) {
	// Make the current item active.
	$(obj).find('.switcher > .active').removeClass('active');
	$(obj).find('.switcher > div').eq(index).addClass('active');

	// Make the current page active.
	$(obj).find('.slides > div:visible').fadeOut(400);
	$(obj).find('.slides > div').eq(index).fadeIn(200);
}

function verifyEmail(value){
	var status = false;
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	if (value.search(emailRegEx) == -1) {
		return false;
	} else {
		return true;
	}
}

function initializeSlider() {
	$('.switcher.cycle').data('cycling_skip', 0);
	$('.switcher.cycle').data('cycling_allowed', true);

	// Cycle interval.
	slider_interval_id = setInterval(function() {
		$('.switcher.cycle').each(function() {
			if($(this).data('cycling_skip') > 0)
				$(this).data('cycling_skip', $(this).data('cycling_skip') - 1);

			if($(this).data('cycling_skip') == 0 && $(this).data('cycling_allowed')) {
				// Fetch the active index.
				var index = $(this).find('.active').index();

				// Move one index forward, start from 0 if exceeded the total of switchers.
				index ++;
				if(index >= $(this).find('div').length)
					index = 0;

				switchSlider($(this).parent(), index);
			}
		});
	}, 5000);

	// Skip cycling while hovering the switcher.
	$('.switcher.cycle > div').hover(function() {
		$(this).parent().data('cycling_allowed', false);
	}, function() {
		$(this).parent().data('cycling_allowed', true);
	});

	// Skip cycling while reading.
	$('.switcher.cycle > div').click(function() {
		$(this).parent().data('cycling_skip', 2);
	});
}

