$(function(){
	
	// header links
	var allTabsOpen = [];
	$('#header > ul > li').each( function () {
		var $this = $(this),
			l = $('>a', $this),
			m = $('>dl', $this).add( $('>a span',$this) ).hide(),
			hideTimer = false,
			open = false,
		
			openTab = function() {
				hideTimer && clearTimeout(hideTimer);
				if (!open) {
					open = true;
					var i;
					while(i = allTabsOpen.pop()) {
						if (m != i) i.hide();
					}
					allTabsOpen.push(m);
				}
				m.fadeTo(90, 1).css('display','block');
			},
			closeTab = function() {
				open = hideTimer = false;
				m.fadeTo(400, 0, function(){ if (!open) m.hide();});
			};
		
		l			.mouseenter(openTab);
		$this	.mouseenter(openTab)
					.mouseleave(function() {
			open = false;
			hideTimer && clearTimeout(hideTimer);
			hideTimer = setTimeout(closeTab, 500);
		});
		
		// Set remove fall-back CSS class
		$this.parent().removeClass('nojs');
	});
	
	// label fading
	$('.label-holder').each( function () {
		var $this = $(this),
			label = $('label', $this),
			form  = $('input[name="'+label.attr('for')+'"]', $this),
			input = form.get(0);
		
		// don't operate on a null label
		if (!input)
			form = $('textarea[name="'+label.attr('for')+'"]', $this);
		if (!form) 
			return;
		else
			input = form.get(0); 
		
		form
			.bind('focus', function() {
				if (input.value == '')
					label.stop().show().fadeTo(200, .3);
				else
					label.hide();
			})
			.bind('blur', function() {
				if (input.value == '')
					label.stop().show().fadeTo(400, 1);
				else
					label.hide();
			})
			.bind('keydown', function(e) {
				if ( (e.keyCode == 8 || e.keyCode == 46) && input.value.length <= 1)
					label.stop().show().fadeTo(400, .3);
				else 
					label.hide();
			});
		label
			.bind('click', function() {
				if (input) input.focus();
			});

		if (input && input.value != '') {
			label.hide();
		}
	});
	
	
});