$(function(){

	function formatItem_collegekeyword(row) {
		return row[0] + "<br/><span class='location'>" + row[1] + ", " + row[2] + "</span>";
	}
	function formatResult_collegekeyword(row) {
		return row[0];
	}
	
	$("#txtSearch").autocomplete("_handlers/SchoolAutosuggest.aspx", {
		width: 428,
		max: 50,
		scroll: true,
		cacheLength:10,
		matchContains:true,
		scrollHeight: 300,
		formatItem:formatItem_collegekeyword,
		formatResult:formatResult_collegekeyword,
		minChars:4}).result(SelectCollege);
		
	$("#btViewSchool").click(function()
	{
		
		if ($.trim($("#txtSearch").val()))
		{
			jQuery.ajax({
				// try to leverage ajaxQueue plugin to abort previous requests
				mode: "abort",
				// limit abortion to this input
				port: "autocomplete",
				dataType: "text",
				url: "_handlers/SchoolAutosuggest.aspx",
				data: "q=" + escape($("#txtSearch").val()) + "&limit=55",
				success: ViewCollegeNow
			});	
		}
	});
	
});

function SelectCollege(event,data)
{
	if(data)
	{
		if ($("#selectedSchools").length > 0)
		{
			$("#selectedSchools").mask("Adding College...");
			$("#txtSearch").val("");
			SelectSchool(data, true, "selectedSchools");
			refreshSchoolCount("selectedSchools", "selectedSchoolsCount");
			$("#selectedSchools").unmask();
		}
		else
		{
			window.location.href='collegeprofile.aspx?institutionid=' + data[3];
		}
	}
}


function ViewCollegeNow(data)
{
	var rows, school
	
	data = $.trim(data);
	rows = data.split("\n");
	
	if (data)
	{
		if (rows.length == 1)
		{
			school = rows[0].split("|");
			window.location.href='collegeprofile.aspx?institutionid=' + school[3];
		}
		else
		{
			displayMessage("viewCollegeMessage", "More than one college matches the name or keyword provided. Please narrow your search.");
		}
	}
	else
	{
		displayMessage("viewCollegeMessage", "No colleges match name or keyword provided.");
	}
			
}
