$(document).ready(function(){ 
	function alignGrid(/*string*/ id, /*int*/ cols, /*int*/ cellWidth, /*int*/ cellHeight, /*int*/ padding) {

	    var x = 0;
	    var y = 0;
	    var count = 1;

	    jQuery("#" + id).each(function() {
	        jQuery(this).css("position", "relative");

	        jQuery(this).children("div").each(function() {
	            jQuery(this).css("width", cellWidth + "em");
	            jQuery(this).css("height", cellHeight + "em");
	            jQuery(this).css("position", "absolute");

	            jQuery(this).css("left", x + "em");
	            jQuery(this).css("top", y + "em");

	            if ((count % cols) == 0) {
	                x = 0;
	                y += cellHeight + padding;
	            } else {
	                x += cellWidth + padding;
	            }

	            count++;
	        });
	    });

	   var parentpos = jQuery("#" + id).offset();
	   var childpos = jQuery("#" + id).children("div:last").offset();
	   var childheight  = jQuery("#" + id).children("div:last").height(); 
	   jQuery("#" + id).css({"height": (childpos.top - parentpos.top) + childheight + 25 + "px"});

	}
	
	
	alignGrid("profiles", 2, 23, 8, 1);   
	
	$("select#child_age, select#child_program, input[name='child_gender']").change(function() { 
		$("#profiles").html("<img src='/_img/spinner3.gif' alt='searching'/>"); 
	
		var age = $('#child_age option:selected').val();
		var program = $('#child_program option:selected').val();
		var gender = $("input[@name='child_gender']:checked").val(); 
         $("#pagination").remove();
	    $('#profiles').load('/_inc/ajax-profiles.php',{ 'age':age, 'program':program, 'gender':gender },
		function(){ 
			
			alignGrid("profiles", 2, 23, 8, 1); 
			
			}); 

	}); 
	
	
	$("a.random").click(function(){
		  $('#profiles').load('/_inc/ajax-profiles.php',{ 'random':"true"});
	});

});