

search_string_for_sort = "";



function getData(v){

	var ft = document.getElementById("ft").checked;

	var pt = document.getElementById("pt").checked;

	var flex = document.getElementById("flex").checked;

	var all_types = document.getElementById("all_types").checked;

	var sTitle, sId, sType, sCode;

	var re = new RegExp("\\b" + v,"i");

	var matchingCourses = [];

	var matched_pt = false;

	var matched_ft = false;

	var matched_flex = false;

	//  matchingCourses(id: is set to the array index from the complete JSON data array or -1 for the headings,

	//  matchingCourses(value: is the course description,

	//  matchingCourses(url: is the url of the course,

	//  matchingCourses(info: used by jqueryAutocomplete.js to set the class name of the course when displaying the course in the SAYT list)



	if (all_types) {

		pt = true;

		ft = true;

		flex = true;

	}



	for(var i=0;i<course_data.length;i++)

	{

		sTitle = course_data[i].title;

		sType = course_data[i].type;

		sCode = course_data[i].code;

		sId = course_data[i].id;

		

		if(sTitle.match(re))

		{

			if (ft && sType == "F") {

				PushCourse(matchingCourses, sTitle, sType, sId, sCode);

				matched_ft = true;

			} else if (pt && IsPtType(sType)) {

				PushCourse(matchingCourses, sTitle, sType, sId, sCode);

				matched_pt = true;

			} else if (flex && IsFlexType(sType)) {

				PushCourse(matchingCourses, sTitle, sType, sId, sCode);

				matched_flex = true;

			} 		

		}

	}

	

	if (pt && matched_pt) {

		matchingCourses.push({id:-1,value:"Part Time Courses",url:"http://www.abcol.ac.uk/courses/part-time/index.cfm",info:"pt",group:2});

	}

	if (ft && matched_ft) {

		matchingCourses.push({id:-1,value:"Full Time Courses",url:"http://www.abcol.ac.uk/courses/full-time/index.cfm",info:"ft",group:1});

	}

	if (flex && matched_flex) {

		matchingCourses.push({id:-1,value:"Self Study",url:"http://www.abcol.ac.uk/courses/self_study/index.cfm",info:"flex",group:3});

	}

		

	search_string_for_sort = v.toLowerCase();

	matchingCourses.sort(CompareByTitlePreferingSearchString);

	return matchingCourses;

}



function PushCourse(matchingCourses, sTitle, sType, sId, sCode){

	if (sType == "F") {

		matchingCourses.push(

			{

				id:sId,

				value:sTitle,

				url:"http://www.abcol.ac.uk/courses/full-time/course-detail.cfm?course_id_arg="+sId,

				info:"ft",

				group:1				

			}

		);

	} else if (sType == "P") {

		matchingCourses.push(

			{

				id:sId,

				value:sTitle,

				url:"http://www.abcol.ac.uk/courses/part-time/course-detail.cfm?course_id_arg="+sId,

				info:"pt",

				group:2

			}

		);

	} else if (sType == "O") {

		matchingCourses.push(

			{

				id:sId,

				value:sTitle,

				url:"http://www.abcol.ac.uk/courses/open-learning/course-detail.cfm?course_id_arg="+sId,

				info:"flex",

				group:3

			}

		);

	} else if (sType == "C") {

		matchingCourses.push(

			{

				id:sId,

				value:sTitle,

				url:"http://www.abcol.ac.uk/community/course-detail.cfm?course_id_arg="+sId,

				info:"pt",

				group:2

			}

		);

	} else if (sType == "I") {

		matchingCourses.push(

			{

				id:sId,

				value:sTitle,

				url:"http://www.abcol.ac.uk/courses/it-centre/course-detail.cfm?course_id_arg="+sId,

				info:"flex",

				group:3

			}

		);

	} else if (sType == "E") {

		matchingCourses.push(

			{

				id:sId,

				value:sTitle,

				url:"http://www.abcol.ac.uk/courses/e-learning/course-detail.cfm?course_id_arg="+sId,

				info:"flex",

				group:3

			}

		);

	} 

}



function IsFlexType(sType) {

	return (sType == "O" || sType == "I" || sType == "E");

}

function IsPtType(sType) {

	return (sType == "P" || sType == "C");

}



function CompareByTitlePreferingSearchString(a, b) {

	if (a.group < b.group){

		return -1;	

	}else if (a.group > b.group){

		return +1;

	}else if (a.id == -1 && b.id != -1){

		return -1;

	}else if (a.id != -1 && b.id == -1){

		return +1;

	}

	var value_a = a.value.toLowerCase();

	var value_b = b.value.toLowerCase();

	var a_at_start = (value_a.indexOf(search_string_for_sort) == 0);

	var b_at_start = (value_b.indexOf(search_string_for_sort) == 0);

	if (a_at_start && !b_at_start) {

		return -1;

	} else if (b_at_start && ! a_at_start) {

		return +1;

	} else if (value_a < value_b) {

		return -1;

	} else if (value_a > value_b) {

		return +1;

	} else {

		return 0;

	}

}


$(document).ready(function() {

	$('#theSearch').autocomplete({

		get:getData,

		multi:false,

		delay:0,

		height:180,

		noresults:"No courses match your search criteria, go to our <span class=\"nowrap\"><a id=\"atoz\" href=\"/courses/index.cfm\">main course section</a> for more options.</span>"

	});

});
