$(function(){

	$("#btAddSchool").click(function()
	{
	
		//$("#addCollegeMessage").hide();
		
		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=100",
				success: AddSchool
			});	
		}
	});
		
	function AddSchool(data)
	{
	
		var rows, school
		
		data = $.trim(data);
		rows = data.split("\n");
		
		if (data)
		{
		
			if (rows.length == 1)
			{
				$("#selectedSchools").mask("Adding College...");
				school = rows[0].split("|");
				$("#txtSearch").val("");
				SelectSchool(school, true, "selectedSchools");
				refreshSchoolCount("selectedSchools", "selectedSchoolsCount");
				$("#selectedSchools").unmask();
			}
			else
			{
				displayMessage("addCollegeMessage", "More than one college matches the name or keyword provided. Please narrow your search.");
			}
		}
		else
		{
			displayMessage("addCollegeMessage", "No colleges match name or keyword provided.");
		}
				
	}

	$("#btResultsViewSchool").click(function()
	{
		if ($("#searchResults input:checked").length > 0)
		{
			$("#searchResults input:checked").each(function(){
				window.location.href='collegeprofile.aspx?institutionid=' + this.value;
			})
		}
		else
		{
			alert("Please select college you would like to view from the list above.")
		}
		
		
		
	})
	
	$("#btAddColleges").click(function()
	{
		
		$("#selectedSchools").mask("Adding College(s)...");
		$("#compareSchoolResultsBox #searchResults li.selected").removeClass("selected").prependTo("#selectedSchools");	
		$("#selectedSchools").unmask();
		refreshSchoolCount("selectedSchools", "selectedSchoolsCount");
		refreshSchoolCount("searchResults", "searchResultCount");

	});
	
	$("#btSearch").click(function()
	{
	
		//$("#searchMessage").hide()
		//$("#searchMessage").html("")
	
		if (searchCriteriaSelected())
		{
		
			$("#searchResults").mask("Loading Colleges...");
			
			jQuery.ajax({
				// try to leverage ajaxQueue plugin to abort previous requests
				mode: "abort",
				// limit abortion to this input
				port: "autocomplete",
				dataType: "text",
				url: "_handlers/SchoolSearch.aspx",
				data: "z=" + escape($("#txtZip").val()) + "&zr=" + $("#cmbRadius option:selected").val() + "&so=" + $("#cmbState option:selected").val() + "&sec=" + $("#cmbType option:selected").val() + "&is1=" + $("#cmbSize option:selected").val().split(",")[0]+ "&is2=" + $("#cmbSize option:selected").val().split(",")[1] + "&ss=" + $("#cmbSchoolSystem option:selected").val(),
				success: LoadResults
			});
			
		}
		else
		{
			displayMessage("searchMessage", "No search options have been provided. Please indicate search option(s).")
		}

	});
	
	function searchCriteriaSelected()
	{
	
		if (jQuery.trim($("#txtZip").val()) != "" || $("#cmbState").attr("selectedIndex") != 0 || $("#cmbSize").attr("selectedIndex") != 0 || $("#cmbType").attr("selectedIndex") != 0) {
			return true;
		} else if ($("#cmbSchoolSystem").length != 0 && $("#cmbSchoolSystem").attr("selectedIndex") != 0) {
			return true;
		} else {
			return false;
		}

	}

	function LoadResults(data)
	{
		var rows, school
				
		data = $.trim(data);
		rows = data.split("\n");
		
		if (data)
		{

			// verify no more than 200 results are displayed				
			if (rows.length > 200)
			{
				$("#searchResults").unmask()
				displayMessage("searchMessage", "Please limit search criteria. No more than 200 colleges may be returned for a search. Currently your search would result in " + rows.length + " colleges being returned.")
				return;
			}

			// empty search results table
			$("#searchResults").empty();
					
			for (var i=0; i < rows.length; i++)
			{
				school = rows[i].split("|");
				LoadSchool(school, false, "searchResults");
			}
			
			$("#searchResults").show();
			
			// bind event for choose a college search results box
			$("#findSchoolResultsBox #searchResults li").click(function()
			{
			
				$("#findSchoolResultsBox #searchResults input:checkbox").attr("checked","");
				$("#findSchoolResultsBox #searchResults li").removeClass("selected");
				
				$(this).addClass("selected");
				$(this).find("input:checkbox").attr("checked","checked");
				
			});

			// bind event for compare schools search results box
			$("#compareSchoolResultsBox #searchResults li").click(function()
			{
				
				if (!$(this).find("input:checkbox").attr("checked"))
				{
					$(this).addClass("selected");
					$(this).find("input:checkbox").attr("checked","checked");
				}
				else
				{
					$(this).removeClass("selected");
					$(this).find("input:checkbox").attr("checked","");
				}
				
			});
			
			// bind event for compare schools search results box
			$("#compareSchoolResultsBox input:checkbox").click(function()
			{
				this.checked = !this.checked;
			});
						
			$(".schoolResultsBox").show();
			
		}
		else
		{
			$(".schoolResultsBox").hide();
			displayMessage("searchMessage", "No colleges match search criteria provided.")
		}
		
		refreshSchoolCount("searchResults", "searchResultCount");
		$("#searchResults").unmask();
				
	}
	
	function LoadSchool(school, schoolSelected, controlname)
	{
		$("#" + controlname).append("<li class='school_" + school[3] + "'><input type='checkbox' class='controlBox' name='chk" + controlname + "' " + (schoolSelected ? "checked='checked'" : "") + "value='" + school[3] + "' /><div class='school'>" + school[0] + "<br/><span class='location'>" + school[1] + ", " + school[2] + "</span><span class='remove'><a href='javascript:void(0)' onclick='removeSchool(" + school[3] + ")'>remove</a></span></div></li>");
	}

	$("#compareSelectAll").click(function(){
		$("#compareSchoolResultsBox #searchResults li").addClass("selected").find("input:checkbox").attr("checked","checked");	
	});
	$("#compareSelectNone").click(function(){
		$("#compareSchoolResultsBox #searchResults li").removeClass("selected").find("input:checkbox").attr("checked","");	
	});
		
	$("#btCompare").click(function(){
			
		if ($("#selectedSchools li").length > 0){
		

			if ($("#selectedSchools li").length < 2)
			{		
				alert("You must select at least two colleges to compare.");
			}
			else if ($("#selectedSchools li").length > 100)
			{		
				displayInstitutionMaximumAlert();
			}
			else
			{
			
				var arrSelectedInstitutions = new Array();
				
				$("#selectedSchools input:checkbox").each(
					function()
					{
						arrSelectedInstitutions[arrSelectedInstitutions.length] = this.value;
					}
				);
				
				window.location.href='search1ba.aspx?institutionid=' + arrSelectedInstitutions.join(",");

			}

		}

	});
	
});

function displayMessage(ctl, message)
{
	alert(message);
	//$("#" + ctl).html(message);
	//$("#" + ctl).show("normal");
}

function displayInstitutionMaximumAlert()
{
	alert("NO MORE THAN 100 COLLEGES CAN BE COMPARED\n=========================================\nThere are currently " + $("#selectedSchools li").length + " colleges that have been selected for comparison. Please reduce the number of colleges you wish to compare to 100 or fewer colleges.");
}

function refreshSchoolCount(container, counter)
{
	if ($("#" + container + " li").length)
	{
		$("#" + counter).html($("#" + container + " li").length);
	}
	else
	{
		$("#" + counter).html("0");
	}
}

function SelectSchool(school, schoolSelected, controlname)
{
	$("#" + controlname).prepend("<li class='school_" + school[3] + "'><input type='checkbox' class='controlBox' name='chk" + controlname + "' " + (schoolSelected ? "checked='checked'" : "") + "value='" + school[3] + "' /><div class='school'>" + school[0] + "<br/><span class='location'>" + school[1] + ", " + school[2] + "</span><span class='remove'><a href='javascript:void(0)' onclick='removeSchool(" + school[3] + ")'>remove</a></span></div></li>");
}

function removeSchool(id)
{
	$("#selectedSchools li.school_" + id).remove();
	refreshSchoolCount("selectedSchools", "selectedSchoolsCount");
}