var DocumentSearch = function() {
	var path;
	
	function init(web_path) {
		// Store the root path for links
		path = web_path;
		
		// Bind the event for clicking on the text search button
		$('#textSearchBut').bind('click', textSearch);
		$('#textSearchBut2').bind('click', textSearch);
		// Bind the event for typing in the search box to capture the enter key press
		$('#searchText').bind('keydown', function(e) {
			if (e.which == 13) {
				textSearch();
				return false;
			}
		});
		
		// Bind the event for clicking on a tag cloud keyword
		$('#tagCloud .tag').bind('click', selectKeyword);
		initTooltips('tagCloud');
		
		// Initialize the resource category search form
		$('.ex').hide();
		
		$('.addToForm').bind('click', function() {
			$('#theForm').append('<input class="rem" type="hidden" value="' + this.rel + '" name="q" id="' + this.name + '">');
			$('#theForm').ajaxSubmit({ 
				beforeSubmit: showLoading,  
				success: function(data) {
					$('#filterDiv').append(data);
					hideLoading();
				}
			}); 
			return false;
		});
		
		return false;
	}
	
	function initTooltips(container) {
		$('#'+container+' .tag').Tooltip('#fff', '#cc0000', { border:'solid 1px #000', padding:'2px 5px' });
	}
	
	function hideAllTooltips() {
		$('.tag').mouseout();
	}
	
	function showLoading(){
		$('#searchMain').block('<span id="delayMessage"><img src="'+path+'/site/default/images/nqc/indicator_yellow.gif" width="16" height="16"> Processing</span>', { border: '3px solid #cdcdcd' }); 
	}
	
	function hideLoading() {
		$('#searchMain').unblock();
	}
	
	function textSearch() {
		search('textSearch');
	}
	
	function selectKeyword(e) {
		var keyword = e.target.innerHTML.replace('&nbsp;', ' ');
		showLoading();
		
		hideAllTooltips();
		
		$.get(
			path + '/ajax/documentResults.cfm?keyword='+keyword+'&edgeid='+DocumentSearch.edgeid,
			{},
			onSearch
		);
		
		return false;
	}
	
	function selectCat(e) {
		showLoading();
		
		hideAllTooltips();
		
		$.get(
			e.target.href,
			{},
			onSearch
		);
		
		return false;
	}
	
	function onSearch(data) {
		$('#docContainer').hide();
		$('#searchContainer').hide();
		$('#searchInside').html(data).show();
		
		//setTimeout("$('#resultTable .tag').bind('click', DocumentSearch.selectKeyword);DocumentSearch.initTooltips('resultTable');", 50);
		setTimeout("$('#resultTable .catLink').bind('click', DocumentSearch.selectCat);", 50);
		
		hideLoading();
	}
	
	function removeFilter(filterID, hiddenFieldID) {
		$('#'+hiddenFieldID).remove();
		
		var nextDivs = $('#'+filterID).next('.removeMe');
		var prevDivs = $('#'+filterID).prev('.removeMe');
		
		if (!prevDivs.length) {
			$('#theForm .rem').remove();
			$('#filterDiv .removeMe').remove();
		}
		else if (nextDivs.length) {
			$('#theForm').append('<input type="hidden" id="refresh" name="refresh" value="1">');
			
			$('#theForm').ajaxSubmit({ 
				beforeSubmit: showLoading,
				success: function(data) {
					$('#refresh').remove();
					$('#filterDiv').html(data);
					hideLoading();
				}
			});
		}
		else {
			$('#'+filterID).hide('fast', function() { $('#'+filterID).remove(); });
		}
	}
	
	function search(form_id) {
		$('#'+form_id+'_form').ajaxSubmit({ 
			beforeSubmit: showLoading,  
			success: onSearch
		});
	}
	
	function displayDoc(id) {
		showLoading();
		
		$.post(
			path+'/ajax/documentDisplay.cfm',
			{ id:id },
			function(data) {
				$('#searchInside').hide();
				$('#docContainer').html(data).show();
				
				hideLoading();
				
				setTimeout("$('#resourceWrapper .tag').bind('click', DocumentSearch.selectKeyword);DocumentSearch.initTooltips('resourceWrapper');", 50);
			}
		);
		
		return false;
	}
	
	function backToSearchResults() {
		$('#docContainer').hide();
		$('#searchInside').show();
		$('#page_title').html(DocumentSearch._oldTitle);
	}
	
	function updatePageTitle(title) {
		DocumentSearch._oldTitle = $('#page_title').html();
		$('#page_title').html(title);
	}
	
	return {
		init: init,
		removeFilter: removeFilter,
		showLoading: showLoading,
		search: search,
		displayDoc: displayDoc,
		selectKeyword: selectKeyword,
		selectCat: selectCat,
		initTooltips: initTooltips,
		backToSearchResults: backToSearchResults,
		updatePageTitle: updatePageTitle
	};
}();