
window.addEvent('domready', function() {
	makeTableHover();    
    $('searchForm').addEvent('submit', formSubmit);
    
    //add the auto submit to all the form select elements
    var formSelects = $('searchForm').getElements('select, input');
    formSelects.each(function(i){
    	i.addEvent('change', formSubmit);
    });
    
  });

 function formSubmit(e) {
	 
	new Event(e).stop();
		
		$('view-students').empty().addClass('ajax-loading');
		$('searchForm').set('send', {onComplete:receiveSearch });
		$('searchForm').send();	
 }
 
 function studentSearch(page){
    
    //make sure the process type is set to ajax
    $('processType').value = 'ajax';
    
    //now build or vars to send based on the type of search
    formVars = $('searchForm').toQueryString();

    //add the page we want to look at
    formVars += "&page=" + page;

    //we should have all the data we need now.  send it in w ajax
    url = "view-students/search";

    //add the loader graphic to contacts div
    $('view-students').empty().addClass('ajax-loading');
    $('rss-feed').innerHTML = '';
        	 
    var req = new Request({
		url: "view-students/search",
		data: formVars ,
		onComplete: receiveSearch
	});
	req.send();      	 
};

function receiveSearch(json){
    eval(json);
    var pagination = decodeURIComponent(result['pagination']);
    var students = result['students'];

    //pagination html
    $('view-students').removeClass('ajax-loading');
    $('pagination-top').innerHTML = pagination;
    $('pagination-bottom').innerHTML = pagination;
    $('rss-feed').innerHTML = result['rss'];
    $('view-students').innerHTML = students;
    makeTableHover();    
};

function makeTableHover(){
    //add hover / click behavior to each table row
    $('view-students').getElements('tr.studentsTR').each(function(t){
        //mouseover add class for hover state
        t.onmouseover = function(){
            t.addClass('tableHover');
        };
        
        //mouseout remove class for hover state
        t.onmouseout = function(){
            t.removeClass('tableHover');
        };
        
        //add the link for the table row
        t.onclick = function(){
            link = t.getElement('a').href;
            document.location = link;
        };
              
    });
}; 