$(document).ready(function() {
	// Add title helpers for text inputs
	$('input[type="text"], textarea').each(function(index, input) {
		if (!$(input).attr('value') || $(input).attr('value') == $(input).attr('title')) {
			$(input).attr('value', $(input).attr('title'));
			$(input).addClass('empty');
		}
	}).focus(function() {
		if ($(this).attr('value') == $(this).attr('title')) {
			$(this).attr('value', '');
			$(this).removeClass('empty');
		}
	}).blur(function() {
		if (!$(this).attr('value')) {
			$(this).attr('value', $(this).attr('title'));
			$(this).addClass('empty');
		}
	}).parents('form').submit(function() {
		$(this).find('input[type="text"], textarea').each(function(index, input) {
			if ($(input).attr('value') == $(input).attr('title')) {
				$(input).attr('value', '');
			}
		});
	});
	

	// Menu
	var menu = $('#menu #menu-scroller');
	var menuList = $('#menu #blog-list');
	var menuSlider = $('#menu #menu-slider');
	var selectedMenuItem = $('#menu li.selected');

	if (menu) {
		var selectedTop = 0;
		if (selectedMenuItem.position()) {
			selectedTop = Math.round(selectedMenuItem.position().top - menu.height() / 2 / selectedMenuItem.height() * selectedMenuItem.height());
			menu.scrollTop(selectedTop);
		}
		
		menuHeight = menuList.height();
	
		if (menuSlider) {
			menuSlider.slider({
				min: 0,
				max: menuHeight,
				orientation: 'vertical',
				value: menuHeight - selectedTop
			});

			menuSlider.bind('slide', function(event, ui) {
				menu.scrollTop(menuHeight - ui.value);
			});
		}

		// Listen to mouse scrolls over the menu
		menu.bind('mousewheel', function(event, delta) {
			var top = (menu.scrollTop() - (delta > 0 ? 20 : -20));
			menu.scrollTop(top);
			menuSlider.slider('value', menuHeight - top);
			return false;
		});
	}
	
	// Fix external links to open in new window
	$('a[href^="http://"]').each(function(index, a) {
		a = $(a);
		if (a.attr('rel') == 'external' || a.hostname != location.hostname) {
			a.click(function() {
				window.open(a.attr('href'));
				return false;
			});
		}
	});
});
