 	"base"
 	"libs/platform"
 ], (App, platform) ->
+
+	App.factory "algoliaSearch", ->
+		if window.sharelatex?.algolia? and window.sharelatex.algolia?.indexes?.wiki?
+			client = new AlgoliaSearch(window.sharelatex.algolia?.app_id, window.sharelatex.algolia?.api_key)
+			wikiIdx = client.initIndex(window.sharelatex.algolia?.indexes?.wiki)
+			kbIdx = client.initIndex(window.sharelatex.algolia?.indexes?.kb)
+
+		service =
+			searchWiki: wikiIdx.search.bind(wikiIdx)
+			searchKB: kbIdx.search.bind(kbIdx)
+		
+		return service
 
 
 	App.controller 'ContactModal', ($scope, $modal) ->

				$scope.sent = true
				$scope.$apply()

		$scope.$watch 'form.subject', (newVal, oldVal) ->
			console.log newVal, oldVal

		$scope.close = () ->
			$modalInstance.close()

				controller: "SupportModalController"
			)

	App.controller 'SupportModalController', ($scope, $modalInstance, algoliaSearch) ->
		$scope.form = {}
		$scope.sent = false
		$scope.sending = false
		$scope.suggestions = [];

		_handleSearchResults = (success, results) ->			
			suggestions = for hit in results.hits
				page_underscored = hit.pageName.replace(/\s/g,'_')
				section_underscored = hit.sectionName.replace(/\s/g,'_')

				suggestion = 
					url :"/learn/#{page_underscored}##{section_underscored}"
					name : hit._highlightResult.pageName.value + " - " + hit._highlightResult.sectionName.value

			$scope.$applyAsync () -> 
				$scope.suggestions = suggestions

		$scope.contactUs = ->
			if !$scope.form.email?
				console.log "email not set"
