$(document).ready(function() {
	/*
	 * Toggle Login form
	 */
	$('#login-toggle').bind('click', function(){
		$(this).next().toggle();
		$(this).toggleClass('active');
		$(this).next().toggleClass('active');
		return false;
	});
	
	/*
	 * Make App menu items adjust to the surrounding space
	 */ 
	// get available width
	var availableWidth = $('.navigation').width();
	
	// count nr of menu items
	var count = $("#menu-nav").children().length;
	
	// set a menu item's width to the result of width/nrofmenuitems
	var menuWidth = availableWidth / count;
	$("#menu-nav li.first-level").width(menuWidth);
	var dropdownWidth = (menuWidth > 194) ? (menuWidth - 194) : (menuWidth - 194);
	$("#menu-nav li.first-level").hover(function(){
		$(this).children('.drop-down').css('left', dropdownWidth/2);
	});
	$('.navigation').show();
	
	/*
	 * Add user menu to the logged in message container
	 * Hide the logout submit button and replace it with a link that submits the form
	 */
	var userMenu = $('#user-menu').html();
	var logoutLink = '<li><a id="logout-link" href="#"><span>Logg ut</span></a></li>';
	
	// append userMenu to logged in message
	$('#logout-wrapper').append(userMenu);
	$('#logout-wrapper ul').append(logoutLink);
	$('#logout-link').unbind().bind('click', function(){
		$('#logout-submit').click();
		return false;
	});
	
});
