var searchControl= {
	init: function() {
		// Move labels into elements. Use with usability-driven care.
		$('#search label').each(function(){
			var ctl = '#' + $(this).attr('for');
			if($(ctl).val() == '') {
				$(ctl).val($(this).html()).addClass('islabeled');
			}
			
			$('#livesearch').hide();
		
		});
		
		$('.islabeled').focus(function(){

			if ($(this).attr('id') == 'habari_password')
				document.getElementById('habari_password').type = 'password';

			$(this).filter('.islabeled')
				.val('')
				.removeClass('islabeled');
				
			$('#livesearch').show();
			
			searchControl.postFetch();
		}).blur(function(){
			$('label[for='+$(this).attr('id')+']').hide()

			if ($(this).val() == '') {
				$('label[for='+$(this).attr('id')+']').removeClass('popup')
								
				if ($(this).attr('id') == 'habari_password')
					document.getElementById('habari_password').type = 'text';

				$(this)
					.addClass('islabeled')
					.val($('#search label[for=' + $(this).attr('id') + ']').html())
			} else {
				$('label[for='+$(this).attr('id')+']').addClass('popup')
			}
			
			$('#livesearch').hide();
			
		});
		
		$('.islabeled').parents('form').submit(function(){
			$('.islabeled').val('');
		});
	},
	postFetch: function() {
		$('#livesearch ol li.directions').click(function() {
			alert('lol');
			$('#livesearch').submit();
		});
	}
}

$(document).ready(function(){
	searchControl.init();
});