
var projects = new Array();


function addProject(name,type,major,sponsor,link,paid,isnew){
	isnew == isnew || false;
	projects[projects.size()] = new Project(name,type,major,sponsor,link,paid,isnew);
}

function addStudentProject(name,major,sponsor,link,paid,isnew){
	addProject(name,"Student",major,sponsor,link,paid,isnew);
}

function addFacultyProject(name,major,sponsor,link,paid,isnew){
	addProject(name,"Faculty",major,sponsor,link,paid,isnew);
}


/*
Name - the name of the project
Major - ASE, EE, ME, CHEM, BIO, CIVIL
Short - short description of the project
long - detailed description of the project
sponsor - person or organization
link - links to external website or email address 

*/
function Project(name, type, major, sponsor, link,paid,isnew){
	this.major = major;
	this.sponsor = sponsor;
	this.link = link;
	this.name = name;
	this.paid = paid;
	this.type = type;
	this.isnew = isnew;
}

var tableType = 0;
function updateFilters(){
	var type = $("type_select");
	var major = $("major_select");
	if(type.selectedIndex > 0 && major.selectedIndex > 0){
		var buildList = new Array();
		if(type.selectedIndex == 1){
			for(var x = 0; x < projects.size(); x++){
				var curr = projects[x];
				if((curr.major == 'all' || majorEquals(curr.major,major.options[major.selectedIndex].innerHTML)) && curr.type == "Student"){
					buildList[buildList.size()] = curr;
				}
			}
			tableType = 0;
		}
		else if(type.selectedIndex == 2){
			for(var x = 0; x < projects.size(); x++){
				var curr = projects[x];
				if((curr.major == 'all' || majorEquals(curr.major,major.options[major.selectedIndex].innerHTML)) && curr.type == "Faculty"){
					buildList[buildList.size()] = curr;
				}
			}
			tableType = 0;
		}
		else{
			for(var x = 0; x < projects.size(); x++){
				var curr = projects[x];
				if(curr.major == 'all' || majorEquals(curr.major,major.options[major.selectedIndex].innerHTML)){
					buildList[buildList.size()] = curr;
				}
			}
			tableType = 1;
		}
		buildTable(buildList);
	}
}

function buildTable(list){
	$("display_div").style.display = 'none';
	if($("results_table")){
		$("results_table").parentNode.removeChild($("results_table"));
	}
	if(list.size() > 0){
		var table = $C("table")
		table.setAttribute("id","results_table");
		table.setAttribute("border",1);
		table.setAttribute("cellPadding","5");
		table.setAttribute("class","results_table");
		table.setAttribute("className","results_table");
		table.setAttribute("align","center");
		var tbody = $C("tbody");
		var tr = $C("tr");
		
		var td = $C("td");
		td.innerHTML = "<center>Research Opportunities</center>";
		if(tableType == 1)
			td.setAttribute("colSpan",6)
		else
			td.setAttribute("colSpan",5);
		td.setAttribute("class","results_other");
		td.setAttribute("className","results_other");
		tr.appendChild(td);
		tbody.appendChild(tr);
		
		tr = $C("tr");
		td = $C("td");
		td.setAttribute("class","results_header");
		td.setAttribute("className","results_header");
		td.innerHTML = "&nbsp;";
		tr.appendChild(td);
		
		td = $C("td");
		td.setAttribute("class","results_header");
		td.setAttribute("className","results_header");
		var link = $C("a");
		td.innerHTML = "Name";
		tr.appendChild(td);
		
		
		td = $C("td");
		td.setAttribute("class","results_header");
		td.setAttribute("className","results_header");
		td.innerHTML = "Link";
		tr.appendChild(td);
		
		td = $C("td");
		td.setAttribute("class","results_header");
		td.setAttribute("className","results_header");
		td.innerHTML = "Sponsor";
		tr.appendChild(td);
		
		td = $C("td");
		td.setAttribute("class","results_header");
		td.setAttribute("className","results_header");
		td.innerHTML = "Paid";
		tr.appendChild(td);
		
		if(tableType == 1){
			td = $C("td");
			td.setAttribute("class","results_header");
			td.setAttribute("className","results_header");
			td.innerHTML = "Type";
			tr.appendChild(td);
		}
		
		tbody.appendChild(tr);
		
		for(var x = 0; x < list.size(); x++){
			var name = list[x].name;
			var short = list[x].short;
			var sponsor = list[x].sponsor;
			
			tr = $C("tr");
			td = $C("td");
			td.setAttribute("class","results_other");
			td.setAttribute("className","results_other");
			td.innerHTML = (list[x].isnew == true ? "New" : "&nbsp;");
			tr.appendChild(td);
			td = $C("td");
			td.setAttribute("class","results_other");
			td.setAttribute("className","results_other");
			td.innerHTML = name;
			tr.appendChild(td);
			
			
			td = $C("td");
			td.setAttribute("class","results_other");
			td.setAttribute("className","results_other");
			var link = $C("a");
			if((""+list[x].link).indexOf("@")>=0){
				link.setAttribute("href","mailto:"+list[x].link);
				link.innerHTML = list[x].link;
			}
			else {
				link.setAttribute("href","#");
				link.listItem = list[x];
				link.innerHTML = "Website";
				link.onclick = function(){
					var win = new Window({className: "alphacube", title: this.listItem.name,width: 450, height: 350,url: this.listItem.link,showEffect:Effect.Appear, hideEffect: Effect.Fade, draggable:true, wiredDrag: true, destroyOnClose: true,showEffectOptions: {duration:1.5}})
					win.showCenter();
				};
			}
			td.appendChild(link);
			tr.appendChild(td);
			
			td = $C("td");
			td.setAttribute("class","results_other");
			td.setAttribute("className","results_other");
			td.innerHTML = sponsor;
			tr.appendChild(td);
			
			td = $C("td");
			td.setAttribute("class","results_other");
			td.setAttribute("className","results_other");
			td.innerHTML = list[x].paid;
			tr.appendChild(td);
			
			if(tableType == 1){
				td = $C("td");
				td.setAttribute("class","results_other");
				td.setAttribute("className","results_other");
				td.innerHTML = list[x].type;
				tr.appendChild(td);
			}
			tbody.appendChild(tr);
			
		}
		table.appendChild(tbody);
		table.setAttribute("width","750");
		$("display_div").appendChild(table);
	}
	else{
		$("display_div").innerHTML = "<table id='results_table' cellSpacing='5'><tr><td>There are no results to display.<br><br><br><br><br></td></tr></table>";
	}
	Effect.Appear("display_div");
}

function makeFilters(parent){
	var table = $C("table");
	var tbody = $C("tbody");
	var tr = $C("tr");
	var td = $C("td");
	td.innerHTML = "<select id='type_select' onchange=\"updateFilters()\"><option>Project Type:</option><option>Student<option>Faculty</option><option>All Projects</option></select>";
	tr.appendChild(td);
	
	var td = $C("td");
	td.innerHTML = "<select id='major_select' onchange=\"updateFilters()\"><option>Major:</option><option>Aerospace<option>Biomedical<option>Chemical<option>Civil<option>Electrical<option>Mechanical</select>";
	
	tr.appendChild(td);
	
	tbody.appendChild(tr);
	table.appendChild(tbody);
	$(parent).appendChild(table);
}

function layout(){
	
	makeFilters("filter_div");
	
}

function majorEquals(abbrev, normalname){
	
	abbrev = (""+abbrev).toLowerCase();
	normalname = (""+normalname).toLowerCase();
	if(abbrev == 'ase' && normalname == 'aerospace')
		return true;
	if(abbrev == 'ee' && normalname == 'electrical')
		return true;
	if(abbrev == 'chem' && normalname == 'chemical')
		return true;
	if(abbrev == 'civil' && normalname == 'civil')
		return true;
	if(abbrev == 'me' && normalname == 'mechanical')
		return true;
	if(abbrev == 'bme' && normalname == 'biomedical')
		return true;
	return false;
}
