/***************************************************************
 * BROWSE TAG SUGGESTION
 ***************************************************************/
var filterSuggestion_keyHandled = false;
var filterSuggestion_timeout;

//HELPER FUNCTIONS

function hideFilterSuggest() {
	setTimeout(function() { 
		$('#filterForm #tagSuggest').css('display', 'none'); 
	}, 150);
}

// END HELPER FUNCTIONS

$(document).ready(function() {
	
	/**
	 * Handle the various mouse events that can occur in the 
	 * Search Ingredient Suggestion division.
	 */
	$("#filterForm #tagSuggest p").live('mouseenter', function(event) {
		$(this).addClass('highlight');
		return false;
	});
	
	$("#filterForm #tagSuggest p").live('mouseleave', function(event) {
		$(this).removeClass('highlight');
		return false;
	});
	
	$("#filterForm #tagSuggest p").live('click', function(event) {
		var val = $("#filterForm .input").attr('value');
		var tags = val.split(',');
		
		var notTest = tags[tags.length-1].trim();
		if(notTest.indexOf('!') == 0) {
			tags[tags.length-1] = '!'+$(this).html();
		}
		else {
			tags[tags.length-1] = $(this).html();
		}
		
		var result = '';

		var i=0;	
		for(i = 0; i < tags.length; i++) {
				result += tags[i].trim()+", ";
		}
		
		hideFilterSuggest();
		$("#filterForm .input:first").attr('value', result);
		$("#filterForm .input:first").focus();
		return false;
	});
	
	/**
	 * Bind a function to handle a key event in the recipe ingredient search form.
	 */
	$("#filterForm .input").live('keydown', function(event) {
		filterSuggestion_keyHandled = true;
		if(event.keyCode == "13") { // enter
			if($("#filterForm #tagSuggest .highlight").exists() == false || $("#filterForm #tagSuggest").css('display') == 'none') {
				filterSuggestion_keyHandled = false;
				return;
			}
			event.preventDefault();
			$("#filterForm #tagSuggest .highlight").click();
			return false;
		}
		else if(event.keyCode == "27") { // esc
			hideFilterSuggest();
			return false;
		}
		else if(event.keyCode == "40") { // down
			if($("#filterForm #tagSuggest .highlight").exists() == false) {
				$("#filterForm #tagSuggest p:first").addClass('highlight');
			}
			else {
				$("#filterForm #tagSuggest .highlight").next("p:first").addClass('highlight');
				$("#filterForm #tagSuggest .highlight:first").removeClass('highlight');
			}
			return false;
		}
		else if(event.keyCode == "38") { // up
			if($("#filterForm #tagSuggest .highlight").exists() == false) {
				$("#filterForm #tagSuggest p:last").addClass('highlight');
			}
			else {
				$("#filterForm #tagSuggest .highlight").prev("p:first").addClass('highlight');
				$("#filterForm #tagSuggest .highlight:last").removeClass('highlight');
			}
			return false;
		}
		else {
			filterSuggestion_keyHandled = false;
		}
	});
	
	/**
	 * Bind the keyup event on the search input field to a handler function that produces
	 * the ingredient suggestions. 
	 */
	$("#filterForm .input").live('keyup', function(e) {
		if(filterSuggestion_keyHandled == true) {
			return;
		}
		
		var input = $("#filterForm .input").attr('value');
		
		if(input == '') {
			hideFilterSuggest();
			return false;
		}
		
		var position = $("#filterForm .input").offset();
		var height = $("#filterForm .input").outerHeight();
		
		var parentPosition = $("#filterForm").offset();
		
		var left = position.left - parentPosition.left;
		var top = position.top - parentPosition.top +height;
		
		$("#filterForm #tagSuggest").css('display', 'block');
		$("#filterForm #tagSuggest").css('top', top+'px');
		$("#filterForm #tagSuggest").css('left', left+'px');
		$("#filterForm #tagSuggest").html('Loading suggestions...');
		
		clearTimeout(filterSuggestion_timeout);
		filterSuggestion_timeout = setTimeout(function() {
			$.ajax({
				   type: "POST",
				   url: "/tags/suggest"+input,
				   data: input,
				   success: function(data){
						if(data.trim() == 'none') {
							hideFilterSuggest();
						}
						else {
							$("#filterForm #tagSuggest").html(data);
						}
				   }
				});
		}, 500);
		return false;
	});
	
	
	/**
	 * Function to bind handlers to events that should cause the Search Ingredient
	 * Suggestions division to hide itself.
	 */
	$("#filterForm .input").live('blur', function() {
		hideFilterSuggest(); 
		return false;
	});

});

