$(document).ready(function() {

		// Form things
		$('.button-submit').click(function() {
				$(this).parents('form').submit();
			});
/*
		$('input:text, input:password').keypress(function(event) {
				if(event.keyCode == '13') {
						$(this).parents('form').trigger('submit');
					}
			});
*/
		// Menu
		$('#navigation li').children('ul').hide();
/*
		$('#navigation li').hover(function() {
				$(this).children('ul').stop(true, true);
				$(this).children('ul').animate({
						opacity: 1,
						height: 'toggle',
					}, 600);
			},
			function() {
				$(this).children('ul').stop(true, true);
				$(this).children('ul').animate({
						opacity: 0,
						height: 'toggle',
					}, 200);
			});
*/
		$('#navigation li').hover(function() {
				$(this).children('ul').show();
			},
			function() {
				$(this).children('ul').hide();
			});
/*
		$('#navigation li').hover(function() {
				$(this).children('ul').animate({
						opacity: 1,
						height: 'toggle',
					}, 600);
			},
			function() {
				$(this).children('ul').stop(false, true);
				$(this).children('ul').animate({
						opacity: 0,
						height: 'toggle',
					}, 200);
				$(this).children('ul').stop(true, true);
			});
*/
		// Pointer!
		var current_element = $('#navigation li.current-parent');
		var pointer = $('#pointer');
		var speed = 400;

		pointer.css('left', getPointerPositionFor(current_element));
		if(null == current_element.position()) {
				pointer.css('opacity', 0);
			}
		pointer.show();

		// Getting out of menu: fade (if main page etc) or go back to current position
		$('#navigation ul').hover(function() {
			}, function() {
				if(null == current_element.position()) {
						pointer.animate({
								opacity: 0
							}, speed);
					}
				else {
						pointer.animate({
								left: getPointerPositionFor(current_element),
							}, speed);
					}
			});

		// Mouse on menu element: animate
		$('#navigation ul').children('li.top').hover(function() {
				// Main page etc (no default menu element selected)
				if(null == current_element.position()) {
						// Mouse over menu element, came from out of menu
						if(0 == pointer.css('opacity')) {
								pointer.css('left', getPointerPositionFor($(this)));
								pointer.animate({
										opacity: 1,
									}, speed);
							}
						// Mouse over menu element, came from other menu element
						else {
								pointer.animate({
										opacity: 1,
										left: getPointerPositionFor($(this)),
									}, speed);
							}
					}
				// Default - just change the pointer position
				else {
						pointer.animate({
								left: getPointerPositionFor($(this)),
							}, speed);
					}
			}, function() {
				pointer.stop(true,false);
			});

		function getPointerPositionFor(element) {
				if($(element).length > 0) {
						// Typical, but not to use here
						//var pointer_pos = element.position().left + (element.width() - $('#pointer').width()) / 2;
						//alert('li pos: ' + element.position().left + ', li width: ' + element.width() + ', pntr w: ' + $('#pointer').width())
						var pointer_pos = element.position().left + (element.width() - $('#pointer').width()) / 2;
						return pointer_pos;
					}
				return -99999;
			}
	});

// FUNCTIONS
	
	// Search input labels etc.
	function registerLabel(element, text) {
			$(element).focus(function() {
					if($(this).attr('value') == text) {
							$(this).attr('value','');
						}
				});
			$(element).blur(function() {
					if($(this).attr('value') == '') {
							$(this).attr('value',text);
						}
				});
			// Don't submit element's value if default
			$(element).parents('form').submit(function() {
					if($(element).attr('value') == text)
						{
							$(element).attr('value','');
						}
					return true;
				});
		}
