jQuery(function($){
	if (document.body.id == 'home-page') {
		$('#query').focus();
		$('#top-people select').change(function(e){e.target.form.submit();});
	}
	
	if (document.body.id == 'results-page') {
	    if ($('#add_facet').length > 0) {
	        var $addFacetBtn = $('#add_facet input[type=submit]');
	        $addFacetBtn.hide();
	        $('#add_facet select').change(function(e){
                $(e.target.form).append("<input type='hidden' name='add_facet' value='Add' />");
	            e.target.form.submit();
	            $('#facets').block({
	                message: 'Adding...', 
	                overlayCSS: {'-moz-border-radius': '8px', '-webkit-border-radius': '8px'}
	            });
	            $('.blockMsg').css('top', '160px');
	        });
	    }
	    
	    $('#facets .remove_facet').click(function(e){
	        $('#facets').block({
                message: 'Removing...', 
                overlayCSS: {'-moz-border-radius': '8px', '-webkit-border-radius': '8px'}
            });
            $('.blockMsg').css('top', '160px');
	    });
	    
        var showFacetAction = function(){$('#facets .action-edit').show()};
        var hideFacetAction = function(){$('#facets .action-edit').hide()};         
        $('#facets').hover(showFacetAction, hideFacetAction);   
        hideFacetAction();
	    
	    // set the block UI defaults how we want them
	    $.blockUI.defaults.css = $.extend($.blockUI.defaults.css, {
	        border: 'none',
			'-webkit-border-radius': '10px',
			'-moz-border-radius': '10px',
			padding: '30px 15px',
			backgroundColor: '#000',
			opacity: '0.8',
			color: '#fff',
			fontFamily: 'arial',
			fontWeight: 'bold'
	    });
		$.blockUI.defaults.overlayCSS.opacity = '0.2';
		$.blockUI.defaults.fadeIn = 0;
		
		// Ideally this would use the change event but IE does not propagate 
		// it. Thanks, IE!
		$('#facets').click(function(e){
			// This is ugly, we basically need to reimplement the label/checkbox
			// functionality...
			var $target = $(e.target), $chk = null;
			if ($target.is('input[type=checkbox]')) {
				$chk = $target;
			} else {
				$chk = $target.siblings('input[type=checkbox]');
				if ($chk.length) {
					e.preventDefault();
					$chk.attr('checked', !$chk.attr('checked'));
				}
			}
			
			if ($chk.length) {
				$chk.blur();
                $($chk.attr('form')).append('<input type="hidden" name="commit" value="refine" />');
                $chk.attr('form').submit();
				$.blockUI({
					message: 'Loading...',
					css: {
            			fontSize: '32px'
					}
				});
			}
		});		
	}
});
