﻿/// <reference path="..\jquery-1.3.2-vsdoc.js"/>
/// <reference path="TypeAhead.js"/>
/// <reference path="jquery.autocomplete.js"/>

(function($) {
	var CollegeOptions = {
		_baseOptions: {
			minLength: 0,
			mustMatch: false,
			selectFirst: true,
			noDataMessage: "No matching colleges available.  Please use the official name of the college.",
			parseItem: function(college, highlighter) {
				var alias = college.Alias;
				alias = ($.trim(alias) == "") ? "" : " (" + alias + ")";
				var collegeName = college.Name;

				return {
					data: college,
					value: collegeName,
					label: highlighter.highlightSearchTerm(collegeName + alias),
					isReadOnly: false
				};
			}
		},

		getCollegeOptions: function(typeAheadControlId, collegeIdField, url, callback) {
			var options = CollegeOptions._baseOptions;

			$("#" + typeAheadControlId).keydown(function(e) {
				var code = (e.keyCode || e.which);
				if ((code == 8) || ((code > 31) && (code < 126)) || (code > 128))
					$("#" + collegeIdField).val('');
			});

			options.sourceUrl = url;
			options.itemSelected = function(data) {
				$("#" + collegeIdField).val(data.InstitutionId);
				if (typeof callback == "function") callback(data);
			};

			return $.getTypeAheadOptions().extendBaseServerOptions(options);
		},

		getAllCollegeOptions: function(typeAheadControlId, collegeIdField, url, callback) {
		    var options = CollegeOptions.getCollegeOptions(typeAheadControlId, collegeIdField, url, callback);

        	options.buildSourceUrlData = function() {
				return {
					portalOnly: "false"
				};
			};

			return $.getTypeAheadOptions().extendBaseServerOptions(options);
		},

		getBaseCollegeEquivOptions: function(typeAheadControlId, collegeIdField, url, callback) {
			var options = CollegeOptions.getCollegeOptions(typeAheadControlId, collegeIdField, url, callback);

			options.isComboBox = true;
			options.formatItem = function(item) {
				var data = item.data;
				var noMatchesPostfix = "";
				var cssClass = "";
				if (data.HasMatchingEquivalencies != null && !data.HasMatchingEquivalencies) {
					cssClass = " class='ta-strikeThrough'";
					noMatchesPostfix = " (no matches)";
				}
				return "<span" + cssClass + ">" + item.label + "</span>" + noMatchesPostfix;
			};

			return $.getTypeAheadOptions().extendBaseServerOptions(options);
		},

		getCollegeEquivSourceOptions: function(typeAheadControlId, collegeIdField, particCollegeFieldId, targetCollegeFieldId, url, callback) {
			var options = CollegeOptions.getBaseCollegeEquivOptions(typeAheadControlId, collegeIdField, url, callback);

			options.buildSourceUrlData = function() {
				return {
					particCollegeOnly: $("#" + particCollegeFieldId).is(":checked"),
					targetCollegeId: $("#" + targetCollegeFieldId).val()
				};
			};

			return $.getTypeAheadOptions().extendBaseServerOptions(options);
		},

		getCollegeEquivTargetOptions: function(typeAheadControlId, collegeIdField, sourceCollegeFieldId, url, callback) {
			var options = CollegeOptions.getBaseCollegeEquivOptions(typeAheadControlId, collegeIdField, url, callback);

			options.buildSourceUrlData = function() {
				return {
					sourceCollegeId: $("#" + sourceCollegeFieldId).val()
				};
			};
			return $.getTypeAheadOptions().extendBaseServerOptions(options);
		},

		refreshTypeAhead: function(typeAheadClass) {
			var selector = "." + typeAheadClass;
			$(selector).addClass("ui-autocomplete-loading", "normal", function callback() {
				setTimeout(function() {
					$(selector).removeClass("ui-autocomplete-loading");
				}, 1500);
			});
		},

		refreshTargetTypeAhead: function() {
			CollegeOptions.refreshTypeAhead("collegeTargetTypeAhead");
		},

		refreshSourceTypeAhead: function() {
			CollegeOptions.refreshTypeAhead("collegeSourceTypeAhead");
		}
	}

	$(function() {
		$.getTypeAheadOptions().addTypeAheadOptions(CollegeOptions);
	});
})(jQuery);
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();