	showLoading = function() {
  		$("#loading").show();
	}
	
	hideLoading = function() {
  		$("#loading").hide();
	}

/// <reference path="jquery-1.4.2.min.js" />
var searchModal = function() {
	var pub = {};

	var searchModalSelector;

	pub.currentLetter = function() {
		var currentLetter = $(searchModalSelector).find(".letters .selected").text();
		if (currentLetter == "") {
			currentLetter = "a";
		}
		return currentLetter;
	};

	pub.moveLetter = function(letter) {
		if (letter == pub.currentLetter()) {
			return;
		}
		var $searchModal = $(searchModalSelector);
		$searchModal.find(".letters li").each(function() {
			var $this = $(this);
			if ($this.text() == letter) {
				$this.siblings().removeClass("selected");
				$this.addClass("selected");
				$searchModal.find(".handleContainer .handle").css("top", $this.position().top + 1);
				return false;
			}
		});
	};

	pub.showResultsForLetter = function(letter) {
		var $searchModal = $(searchModalSelector);
		var foundScroll = false;
		var charCode = letter.charCodeAt(0);
		$searchModal.find(".results li.result:visible").each(function() {
			var $this = $(this);
			if ($this.find("h3").text().toLowerCase().charCodeAt(0) >= charCode) {
				$searchModal.find(".resultsContainer").scrollTop($searchModal.find(".resultsContainer").scrollTop() + $this.position().top);
				$this.prevAll()
				foundScroll = true;
				return false;
			}
		});
		if (!foundScroll) {
			$searchModal.find(".resultsContainer").scrollTop(89823904032984389348);
		}
	};

	pub.loadResults = function(url) {
		$.get(url, function(result) {
			$(searchModalSelector).find(".results").html($(result).find(".results").html());
			pub.filterResults();
		});
	};

	pub.resetBox = function() {
		$(searchModalSelector).find(".searchTags").html("");
		$(searchModalSelector).find(".searchpageSubSearch .textBox").val("");
		$(searchModalSelector).find(".searchpageSubTypes li").removeClass("inactive");
		$(searchModalSelector).find(".searchpageSubTypes li span").text("");
		$(searchModalSelector).find(".searchCategories li").removeClass("inactive");
		$(searchModalSelector).find(".searchCategories li").removeClass("unchecked");
		$(searchModalSelector).find(".searchCategories li span").text("");
		pub.showResultsForLetter("a");
	};

	pub.filterResults = function() {
		var $searchModal = $(searchModalSelector);
		$searchModal.find(".results li.result:visible:last").css("padding-bottom", "");
		var includeDepartments	= !$(".searchpageSubTypes li:first").hasClass("inactive");
		var includePrograms		= !$(".searchpageSubTypes li:eq(1)").hasClass("inactive");
		var includeCenters		= !$(".searchpageSubTypes li:eq(2)").hasClass("inactive");
		var includeInstitutes	= !$(".searchpageSubTypes li:eq(3)").hasClass("inactive");
		var includeFaculty		= !$(".searchpageSubTypes li:last").hasClass("inactive");
		var includeProviders	= !$(".searchCategories li:first").hasClass("unchecked");
		var includeClinics		= !$(".searchCategories li:eq(1)").hasClass("unchecked");
		var includeSpecialties	= !$(".searchCategories li:last").hasClass("unchecked");

		$searchModal.find(".resultsContainer .noResults").hide();

		var filters = new Array();
		$searchModal.find(".searchTags li").each(function() {
			filters.push($(this).text().toLowerCase());
		});
		if (filters.length == 0) {
			$searchModal.find(".results li.result").addClass("included").hide();	// ".included" doesn't mean "shown" it means that it is part of the search results excluding the DPCIF filter.
			var $results = $searchModal.find(".results li.result");
			if (includeDepartments) {
				$results.filter(".department").show();
			}
			if (includePrograms) {
				$results.filter(".program").show();
			}
			if (includeCenters) {
				$results.filter(".center").show();
			}
			if (includeInstitutes) {
				$results.filter(".institute").show();
			}
			if (includeFaculty) {
				$results.filter(".faculty").show();
			}
			if (includeProviders) {
				$results.filter(".provider").show();
			}
			if (includeClinics) {
				$results.filter(".clinic").show();
			}
			if (includeSpecialties) {
				$results.filter(".specialty").show();
			}
		}
		else {
			$searchModal.find(".results li.result").each(function() {
				var matchesAll = true;
				var $this = $(this);
				$this.removeClass("included");
				$this.addClass("excluded");
				if (!includeDepartments && $this.hasClass("department")) {
					$this.hide();
				}
				else if (!includePrograms && $this.hasClass("program")) {
					$this.hide();
				}
				else if (!includeCenters && $this.hasClass("center")) {
					$this.hide();
				}
				else if (!includeInstitutes && $this.hasClass("institute")) {
					$this.hide();
				}
				else if (!includeFaculty && $this.hasClass("faculty")) {
					$this.hide();
				}
				else if (!includeProviders && $this.hasClass("provider")) {
					$this.hide();
				}
				else if (!includeClinics && $this.hasClass("clinic")) {
					$this.hide();
				}
				else if (!includeSpecialties && $this.hasClass("specialty")) {
					$this.hide();
				}
				else {
					var thisHtml = $(this).html().toLowerCase();
					for (var x = 0; x < filters.length; x++) {
						if (thisHtml.indexOf(filters[x]) < 0) {
							matchesAll = false;
							break;
						}
					}
					if (matchesAll) {
						$this.removeClass("excluded");
						$this.addClass("included");
						$this.show();
					}
					else {
						$this.hide();
					}
				}
			});
		}
		setTimeout(function() {
			$searchModal.find(".searchpageSubTypes li:first span").text($searchModal.find(".results .department.included").size());	// Removed ":visible"
			$searchModal.find(".searchpageSubTypes li:eq(1) span").text($searchModal.find(".results .program.included").size());
			$searchModal.find(".searchpageSubTypes li:eq(2) span").text($searchModal.find(".results .center.included").size());
			$searchModal.find(".searchpageSubTypes li:eq(3) span").text($searchModal.find(".results .institute.included").size());
			$searchModal.find(".searchpageSubTypes li:last span").text($searchModal.find(".results .faculty.included").size());
			$searchModal.find(".searchCategories li:first span").text($searchModal.find(".results .provider.included").size());	// Removed ":visible"
			$searchModal.find(".searchCategories li:eq(1) span").text($searchModal.find(".results .clinic.included").size());
			$searchModal.find(".searchCategories li:last span").text($searchModal.find(".results .specialty.included").size());
			var $last = $searchModal.find(".results li.result:visible:last");
			$last.css("padding-bottom", $searchModal.find(".resultsContainer").height() - $last.height() - 16);
			
			if($searchModal.find(".results .included").size() === 0) $searchModal.find(".resultsContainer .noResults").show();
		}, 100);
		
		pub.showResultsForLetter(pub.currentLetter());
	};

	pub.searchAll = function() {			
		var $searchModal = $(searchModalSelector);
		$searchModal.find(".results li.result:visible:last").css("padding-bottom", "");
		var includeDepartments	= !$(".searchpageSubTypes li:first").hasClass("inactive");
		var includePrograms		= !$(".searchpageSubTypes li:eq(1)").hasClass("inactive");
		var includeCenters		= !$(".searchpageSubTypes li:eq(2)").hasClass("inactive");
		var includeInstitutes	= !$(".searchpageSubTypes li:eq(3)").hasClass("inactive");
		var includeFaculty		= !$(".searchpageSubTypes li:last").hasClass("inactive");
		var includeProviders	= !$(".searchCategories li:first").hasClass("unchecked");
		var includeClinics		= !$(".searchCategories li:eq(1)").hasClass("unchecked");
		var includeSpecialties	= !$(".searchCategories li:last").hasClass("unchecked");

		$searchModal.find(".resultsContainer .noResults").hide();

		var filters = new Array();
		$searchModal.find(".searchTags li").each(function() {
			filters.push($(this).text().toLowerCase());
		});
		if(filters.length == 0) {
			$searchModal.find(".searchTagsHeader a").hide();
			var $results = $searchModal.find(".results li.result");
            $results.addClass("included").hide();
            if(includeDepartments) {
                $results.filter(".department").show();
            }
            if(includePrograms) {
                $results.filter(".program").show();
            }
            if(includeCenters) {
                $results.filter(".center").show();
            }
            if(includeInstitutes) {
                $results.filter(".institute").show();
            }
            if(includeFaculty) {
                $results.filter(".faculty").show();
            }
            if(includeProviders) {
                $results.filter(".provider").show();
            }
            if(includeClinics) {
                $results.filter(".clinic").show();
            }
            if(includeSpecialties) {
                $results.filter(".specialty").show();
            }
		}
		else {
			$searchModal.find(".searchTagsHeader a").show();
			$searchModal.find(".results li.result").each(function() {
				var matchesAll = true;
				var $this = $(this);
				$this.removeClass("included");
				$this.addClass("excluded");
				var thisHtml = $(this).html().toLowerCase();
				for (var x = 0; x < filters.length; x++) {
					if (thisHtml.indexOf(filters[x]) < 0) {
						matchesAll = false;
						break;
					}
				}
				if(matchesAll) {
					$this.removeClass("excluded");
					$this.addClass("included");
					$this.show();
				}
				else {
					$this.hide();
				}
				
				if (!includeDepartments && $this.hasClass("department")) {
					$this.hide();
				}
				else if (!includePrograms && $this.hasClass("program")) {
					$this.hide();
				}
				else if (!includeCenters && $this.hasClass("center")) {
					$this.hide();
				}
				else if (!includeInstitutes && $this.hasClass("institute")) {
					$this.hide();
				}
				else if (!includeFaculty && $this.hasClass("faculty")) {
					$this.hide();
				}
				else if (!includeProviders && $this.hasClass("provider")) {
					$this.hide();
				}
				else if (!includeClinics && $this.hasClass("clinic")) {
					$this.hide();
				}
				else if (!includeSpecialties && $this.hasClass("specialty")) {
					$this.hide();
				}
			});
		}
		
		setTimeout(function() {
			$searchModal.find(".searchpageSubTypes li:first span").text($searchModal.find(".results .department.included").size());	// Removed ":visible"
			$searchModal.find(".searchpageSubTypes li:eq(1) span").text($searchModal.find(".results .program.included").size());
			$searchModal.find(".searchpageSubTypes li:eq(2) span").text($searchModal.find(".results .center.included").size());
			$searchModal.find(".searchpageSubTypes li:eq(3) span").text($searchModal.find(".results .institute.included").size());
			$searchModal.find(".searchpageSubTypes li:last span").text($searchModal.find(".results .faculty.included").size());
			$searchModal.find(".searchCategories li:first span").text($searchModal.find(".results .provider.included").size());	// Removed ":visible"
			$searchModal.find(".searchCategories li:eq(1) span").text($searchModal.find(".results .clinic.included").size());
			$searchModal.find(".searchCategories li:last span").text($searchModal.find(".results .specialty.included").size());
			var $last = $searchModal.find(".results li.result:visible:last");
			$last.css("padding-bottom", $searchModal.find(".resultsContainer").height() - $last.height() - 16);
			
			if($searchModal.find(".results .included").size() === 0) $searchModal.find(".resultsContainer .noResults").show();
			
		}, 100);
		
		pub.showResultsForLetter(pub.currentLetter());
	};

	pub.addFilter = function(newFilter) {
		var $searchModal = $(searchModalSelector);
		$searchModal.find(".searchTags").append("<li>" + newFilter + "</li>");
		$searchModal.find(".searchTags li:last").click(function(event) {
			event.preventDefault();
			$(this).remove();
			pub.searchAll();
		});
	};
	
	pub.updateLetter = function() {
		var $searchModal = $(searchModalSelector);
		$searchModal.find(".results li.result:visible").each(function() {
			var $this = $(this);
			if ($this.position().top >= -40) {
				var letter = $.trim($this.find("h3").text()).toLowerCase().charAt(0);
				pub.moveLetter(letter);
				return false;
			}
		});
	}

	pub.setupModal = function(selector) {
		searchModalSelector = (selector);
		var $searchModal = $(searchModalSelector);
		$searchModal.find(".letters li").click(function(event) {
			event.preventDefault();
			pub.moveLetter($(this).text());
			pub.showResultsForLetter($(this).text());
		});

		var $filterButtons	= $searchModal.find(".searchpageSubTypes li");

		if ($filterButtons.length == 0) {
			$filterButtons	= $searchModal.find(".searchCategories li");

			$filterButtons.click(function(event) {
				event.preventDefault();
				var $this = $(this);
				$filterButtons.addClass("unchecked");
				$this.removeClass("unchecked");
				pub.searchAll();
			});
		}
		else {
			$filterButtons.click(function(event) {
				event.PreventDefault();
				var $this = $(this);
				$filterButtons.addClass("inactive");
				$this.removeClass("inactive");
				pub.searchAll();
			});
		}

		var ieTimer;


		if ($(".ie6,.ie7,.ie8").length == 0) {
			$searchModal.find(".resultsContainer").scroll(function() {
				clearTimeout(ieTimer);
				ieTimer = setTimeout(pub.updateLetter, 50);
			});
		}

		$searchModal.find(".searchpageSubSearch .textBox").keypress(function(event) {
			if (event.which == 13 || event.keyCode == 13) {
				var $this = $(this);
				if ($this.val() != "") {
					pub.addFilter($this.val());
					$this.val("");
					pub.searchAll();
				}
				return false;
			}
		});
        
        
        var $findButton	= $searchModal.find(".searchpageSubSearch a");
		$findButton.click(function(event) {
			event.preventDefault();
			var $findText = $searchModal.find(".searchpageSubSearch .textBox");
			if ($findText.val() != "") {
				pub.addFilter($findText.val());
				$findText.val("");
				pub.searchAll();
			}
		});
		
		var $specialityList = $searchModal.find(".searchpageSubSearch select");
		$specialityList.change(function() {
			var $findSelect = $searchModal.find(".searchpageSubSearch select");
			if ($findSelect.val() != "") {
				pub.addFilter($findSelect.val());
				$findSelect.val("");
				pub.searchAll();
			}
		});
		
		var $clearButton = $searchModal.find(".searchTagsHeader a");
		$clearButton.click(function(event) {
			event.preventDefault();
			pub.resetBox();
			pub.searchAll();
		});
	};


	return pub;
} ();

$(document).ready(function() {
  hideLoading();
});

