$(document).ready(function()
{

	var mouseX = 0;
	var mouseY = 0;

	$().mousemove( function(e)
	{
		mouseX = e.pageX;
		mouseY = e.pageY;
	});

	/**
	 * Mandatory form fields
	 */
	$('form').each(function()
	{
		$(this).submit(function(e)
		{
			if($(this).is('#seminaarilomake'))
			{
				var mandatory_alert = 'Ole hyvä ja täytä kaikki pakolliset kentät.';
			}
			else
			{
				var mandatory_alert = 'Please fill in all mandatory form fields.';
			}
			$(this).find('.mandatory').each(function()
			{
				var val = $(this).val();
				if ( ! val.length)
				{
					e.preventDefault();
					$(this).focus();
					$(this).addClass('warn_input');
					alert(mandatory_alert);
					setTimeout(clear_warn_inputs, 1000);
					return false;
				}
			});
		});
	});

	function clear_warn_inputs()
	{
		$('input, textarea').removeClass('warn_input');
	}



	$('.back_link').click(function(e)
	{
		e.preventDefault();
		window.history.back() ;
	});



	/**
	 * Set blank target to all off-site links
	 */
	/*
	$('a').each(function()
	{
		if( ! _root || _root == '') return;

		if($(this).attr('href'))
		{
			if($(this).attr('href').indexOf(_root) < 0)
			{
				$(this).attr('target', '_blank');
			}
		}
	});
	*/



	/**
	 * Clear certain inputs on focus
	 */
	$('.emptyme').each(function()
	{
		$(this).attr('original_value', $(this).val());

		$(this).focus(function()
		{
			var my_original_value = this.getAttribute('original_value');
			if(this.value == my_original_value)
			{
				this.value = '';
			}
		});

		$(this).blur(function()
		{
			var my_original_value = this.getAttribute('original_value');
			if(this.value == '')
			{
				this.value = my_original_value;
			}
		});
	});



	/**
	 * Autofocus on elements with ID focus
	 */
	if(document.getElementById('focus'))
	{
		document.getElementById('focus').focus();
	}



	/**
	 * Front page navigation
	 */
	$('body.home #navigation').addClass('js');
	 $('body.home #navigation').hover(
		  function () {
		    $('body.home #navigation').animate({height: "100px",marginTop: "-40px"}, 120);
		  },
		  function () {
		    $('body.home #navigation').animate({height: "50px",marginTop: "10px"}, 90);
		  }
	);

	$('body.home #navigation ul li ul li').mouseout(function()
	{
		//$(this)
	});


	$(function()
	{
		if ($.browser.msie && $.browser.version < 7) return;

		$('#front_page_nav li ul li').removeClass('highlight').find('a').append('<span class="hover" />').each(function()
		{
			var $span = $('> span.hover', this).css('opacity', 0);

			$(this).hover(function()
			{
				// on hover
				$span.stop().fadeTo(50, 1);
			}, function()
			{
				// off hover
				$span.stop().fadeTo(850, 0);
			});
		});
	});





	/**
	 * Monthly Question
	 */
	$('.poll_answer').click(function(e)
	{
		e.preventDefault();

		$('#poll_answers').css({opacity : .5});
		$(this).click(function(e){return false;});

		var poll_id = $('#poll_answers').attr('rel');

		var values = 'vote=' + $(this).attr('rel') + '&poll_id=' + poll_id;

		$.post(_root + 'poll',
				values,
				function(result)
				{
					if(result == 'error')
					{
						alert('Something went wrong, please try again later.');
					}
					else
					{
						$('#monthly_question_wrapper').html(result);
					}
				});
	});



	/**
	 * Front page case carousel
	 */
	$('#case_slider').carousel({
			animSpeed: "slow",
			loop: true,
			autoSlide: true,
			autoSlideInterval: 4000
		});



	/**
	 * Footer contact form
	 */
	$('a.toggle_footer_contact_form').click(function(e)
	{
		e.preventDefault();

		$('#subscribe_newsletter_link').removeClass('active');
		$('#subscribe_newsletter_form').hide();

		if($('#footer_contact_form').is(':visible'))
		{
			$('#toggle_footer_contact_form_link').removeClass('active');
		}
		else
		{
			$('#toggle_footer_contact_form_link').addClass('active');
		}

		$('#footer_contact_form').toggle('slow');
		setTimeout(scroll_down, 500);
	});



	/**
	 * Newsletter subscribing link
	 */
	$('#subscribe_newsletter_link').click(function(e)
	{
		e.preventDefault();

		$('#toggle_footer_contact_form_link').removeClass('active');
		$('#footer_contact_form').hide();

		if($('#subscribe_newsletter_form').is(':visible'))
		{
			$(this).removeClass('active');
		}
		else
		{
			$(this).addClass('active');
		}

		$('#subscribe_newsletter_form').toggle('slow');
		setTimeout(scroll_down, 800);
	});

	if(document.getElementById('subscribe_newsletter_form_form'))
	{
		document.getElementById('subscribe_newsletter_form_form').onsubmit = function()
		{
			$.post(_root + 'newsletter',
			$('#subscribe_newsletter_form_form').serialize(),
				function(result)
				{
					if(result == 'ok')
					{
						$('#subscribe_newsletter_form_form fieldset').html('Thank You!');
						$('#newsletter_submit').hide();
					}
					else
					{
						alert('There was a problem, please check that both fields are filled in and that the email address is valid.');
					}
				});

				return false;
		}
	}

	$('#close_newsletter_form').click(function(e)
	{
		e.preventDefault();
		$('#subscribe_newsletter_form').hide('slow');
		$('#subscribe_newsletter_link').show('slow');
		$('#subscribe_newsletter_link').removeClass('active');
	});
	$('#close_contact_form').click(function(e)
	{
		e.preventDefault();
		$('#footer_contact_form').hide('slow');
		$('#toggle_footer_contact_form_link').removeClass('active');
	});



	/**
	 * Hide and toggle(rs)
	 */
	$('.hide').hide();
	$('.toggler').click(function(e)
	{
		e.preventDefault();
		$('#' + $(this).attr('rel')).toggle('slow');
	});

	$('.pipit_toggler').click(function(e)
	{
		e.preventDefault();
		$('#' + $(this).attr('rel')).toggle('slow');
	});





	/**
	 * Employees
	 */
	$('.photo').fadeTo(0, 0.5);
	$('#employee_list li').mouseenter(function(e)
	{
		if($(this).find('.descr').is(':visible'))
		{
			$(this).find('.descr').fadeOut('fast');
			$('.photo').fadeTo(0, 0.5);
		}
		else
		{
			$('.descr').hide();
			$(this).find('.descr').fadeIn('fast');
			$(this).find('.photo').fadeTo(1000, 1);
		}
	});



	/**
	 * Print button
	 */
	$('#print_button').click(function(e)
	{
		e.preventDefault();
		$('#sub_page .left').printElement({
			pageTitle: document.title
		});
	});

});



function scroll_down()
{
	var scroll_to = $('#footer').offset().top;
	$('html, body').animate({scrollTop: scroll_to}, 2000);
}

