// Handle menu-toggle
$(document).ready(function() {		
	$("#menu dd").each(function() {
		if (this.id != "dd-" + $("body").attr("class")) {
			$(this).hide();
		}
	});
	$("#menu dt").css("cursor", "pointer");
		
	$("#menu dt").click(function() {
		$(this).next("dd").slideToggle("fast").siblings("dd:visible").slideUp("fast");
		$(this).toggleClass("active");
		$(this).siblings("dt").removeClass("active");
	});
});


// Print hyperlinks as footnotes
$(document).ready(function() {
	$("#content").footnoteLinks();
});


// Trick the strict (target=_blank)
$(document).ready(function() {
	$('#content a').each(function() {
		if ($(this).attr('rel') == 'external') {
			$(this).attr('target', '_blank');
		}
	});
});


// Poetry
$(function () {
	$('#poetrypage #content pre').hide();
	$('#poetrypage #content h2')
		.css('cursor', 'pointer')
		.click(function() {
			$(this).next('pre').slideToggle('fast').siblings("pre:visible").slideUp("fast");
			$(this).toggleClass('active');
			$(this).siblings('h2').removeClass('active');
		});
});


// Site search
$(document).ready(function() {
	$('.overlabel').each(function() {
		var $field;
		
		if (!$(this).attr('for') || !($field = $('#' + $(this).attr('for')))) {
			return false;
		}
		
		$(this)
			.removeClass('overlabel').addClass('overlabel-apply')
			.click(function () {
				var $field;
				
				if ($(this).attr('for') && ($field = $('#' + $(this).attr('for')))) {
					$field.focus();
				}
			});
		
		if ($field.val() !== '') {
			hideLabel($field.attr('id'), true);
		}
		
		$field
			.focus(function () {
				hideLabel($(this).attr('id'), true);
			})
			.blur(function () {
				if ($(this).val() === '') {
					hideLabel($(this).attr('id'), false);
				}
			});
	});
});

function hideLabel(field_id, hide) {
	$('label').each(function() {
		if ($(this).attr('for') == field_id) {
			$(this).css({textIndent:((hide) ? '-9999px' : '0px')});					
			return true;
		}
	});		
	return false;
}

jQuery.fn.indexOf = function(e) {
	for( var i=0; i<this.length; i++ ) {
		if( this[i] == e ) return i;
	}

	return -1;
};