// JavaScript Document
var ajaxurl = "searchjson.php";
function EmployeeSearch(){
	new Ajax.Request(ajaxurl,
	  {
		method:'post',
		parameters: $('search').serialize(true),
		onSuccess: function(transport){
			//alert('Success');
			var response = eval(transport.responseText);
			parseAJAX(response);
		},
		onFailure: function(){ alert('Unable to connect to employee database') }
	  });
}

//parse the server response and build the html
function parseAJAX(phpJSONresponse){
	$('results').innerHTML = "";
	for(var i = 0; i < phpJSONresponse.length; i++){
		var thisresponse = phpJSONresponse[i];
		thisid = thisresponse.id;
		thisname = thisresponse.name;
		thistitle = thisresponse.title;
		thisimage = thisresponse.image;
		buildResultsRow(thisid,thisname,thistitle,thisimage);
	}	
}

//dynamically build vote content dom
function buildResultsRow(id,name,title,thumb){
	//build nodes individually
	if(! thumb) thumb = 'no_photo.jpg';
	var imgpath = "/images/employees/thumbnails/" + thumb;
	thumb = "images/employees/thumbnails/cbg.jpg";
	element =  Builder.node('div',{id:'employee'}); //top level
	imageelement = Builder.node('div',{className:'thumbnail'},[Builder.node('img',{src: imgpath})]);//status
	infoelement = Builder.node('div',{className:'info'},[name,  Builder.node('br') ,title]);//vote container
	employeelink = Builder.node('a',{href: 'employee.php?id=' + id}, [Builder.node('br'),"More Information"]);
	//append appropriate nodes to each other
	infoelement.appendChild(employeelink); //append vote link
	element.appendChild(imageelement);
	element.appendChild(infoelement);
	//alert("appending child node");
	$('results').appendChild(element);
}