var lastQueryTime = 0;
var queuedRequestID = 0;
var throttlePeriod = 500; //miliseconds

function queueSuggestionRequest(limit) {

    now = new Date();
	
    if (now.getTime() - lastQueryTime > throttlePeriod) {
	refreshStartSuggestions(limit);
    } else {
	clearTimeout(queuedRequestID);
	queuedRequestID = setTimeout("refreshStartSuggestions("+limit+")", throttlePeriod - (now.getTime() - lastQueryTime));
    }
}

function refreshStartSuggestions(limit)
{
    lastQueryTimeObject = new Date();
    lastQueryTime = lastQueryTimeObject.getTime();

    str = document.getElementById("startSearchField").value;
    refreshSuggestedIngredientsList(str, limit);
    searchCompositions(str);
}

function searchCompositions(filterString)
{
    AjaxRequest.get(
    {
	'url': 'ajax/findStartCompositions.php',
	'parameters': {
	    'q': filterString,
	    'limit': 5
	},
	'onSuccess':function(req) {
	    loadIntoElement("compositionResultList", req.responseText);
	}
    }
    );
}

function newIngredientPopup() 
{
	if (userIsRegistered()) {
	/* TODO: wenn nur das eingabefeld auf der startseite die gleiche
		 * id hätte, bräuchte es diese funktion hier nicht ein zweites mal.
		 */ 
	searchTerm = document.getElementById("startSearchField").value;
	AjaxRequest.get({
	    'url': 'popups/newIngredientPopup.php',
	    'parameters': {
		'input': searchTerm
	    },
	    'onSuccess':function(req) {
		loadIntoElement("popUpDiv", req.responseText);
	    }
	});
	popup('popUpDiv');
    } else {
	afterLoginFunction = newIngredientPopup;
	afterLoginParameters = new Array(0);
	showLoginPopup();
    }
}

function addNewIngredient()
{
    if (document.getElementById("popup-newIngredientName").value != "")
    {
	nameField = document.getElementById("popup-newIngredientName");
	tagsField = document.getElementById("popup-newIngredientTags");
	connectionsField = document.getElementById("popup-newIngredientConnections");
		
	AjaxRequest.get(
	{
	    'url': 'ajax/addNewIngredientFromStart.php',
	    'parameters': {
		//pass the values, if the fields exist:
		'name': (nameField)?nameField.value:"",
		'tags': (tagsField)?tagsField.value:"",
		'connections': (connectionsField)?connectionsField.value:""
	    },
	    'onSuccess': function(req) {
		//TODO: wir brauchen hier 2 rückgabewerte: fehlerstatus und ingredient id. JSON?
		if (pageTracker) pageTracker._trackEvent('Ingredient', 'New', 'New Ingredient added: ' + nameField.value + ', Tags: ' + (tagsField)?tagsField.value:"");
		location.href = req.responseText;
	    }
	}
	);
    }
    else
    {
	document.getElementById("newIngredientFeedback").innerHTML = "Please enter an ingredient name";
    }
}

function refreshSuggestedIngredientsList(filterString, limit)
{
    AjaxRequest.get(
    {
	'url': 'ajax/getSuggestedStartIngredientsList.php',
	'parameters': {
	    'q': filterString,
	    'limit': limit
	},
	'onSuccess':function(req) {
	    loadIntoElement("suggestedIngredientsList", req.responseText);
	}
    });
}

function enterKeyStartSearchField()
{
    bestMatchingId = document.getElementById("bestMatch").value;
    if (bestMatchingId > 0) newCompositionWithIngredientIds(bestMatchingId);
}