/*
FMInput uses Ajax to find a food from the given string/substring, then displays
the appropriate units to be used and allows the user to input the qty and unit.
*/
function FMInput (cntrId) {
try {
	var thisObj = this;

	// The parent element in which the FMUInput is rendered
	this.ffParentId = cntrId;
	this.parentDiv = getElement(this.ffParentId);

	// listeners to handle the "Add/Submit button click"
	this.submitListeners = new Array();

	// css ids of items that make up the food finder.

	// The food suggestions drop down here - an empty div ...
	this.ffCntrId = cntrId + "_ffCntr";

	// The actual input tag connected to the YAutoComplete
	this.ffId = cntrId + "_ffInp";

	// Qty and Measuring unit to complete a FoodMeasure
	this.qtyId = cntrId + "_ffQty";
	this.usId = cntrId + "_ffUs";

	var foodSuggestionsDiv = createElementWithId("div", this.ffCntrId); 
	// foodSuggestionsDiv.setAttribute("class", "ingleft");

	this.ffInput = createFoodInput(this.ffId);
	this.unitSelector = createUnitSelector(this.usId);
	this.unitSelector.disabled=true;
	this.qtyInput = createQtyInput(this.qtyId);
	this.qtyInput.disabled=true;
	this.submitBtn = createDOMElement("input");
	this.submitBtn.type = "button";
	this.submitBtn.value = "Add Item";
	this.submitBtn.onclick = callback(thisObj.submitSelection, {bind: thisObj});
	this.setDefaultState();


	var ffWrapper = createDOMElement("div");

	// Create the div for food suggestions - the input and empty div 
	// for the suggestions.
	this.tempD = createDOMElement("div");
	this.tempD.id = "myAutoComplete";
	this.tempD.setAttribute("class", "ingleft");
	this.tempD.appendChild(this.ffInput);
	this.tempD.appendChild(foodSuggestionsDiv);

	// Now create a div for the qty and Unit Selection
	var tmp1 = createDOMElement("div");
	// tmp1.setAttribute("class", "ingleft");

	tmp1.appendChild(this.qtyInput);
	tmp1.appendChild(this.unitSelector);
	tmp1.appendChild(this.submitBtn);

	// First add the div with the input control
	ffWrapper.appendChild(this.tempD);

	// Now the wrapper div with the qty and Unit Selection controls.
	ffWrapper.appendChild(tmp1);

	this.parentDiv.appendChild(ffWrapper);

	this.foodInfo = null;
	var itemSelectHandler = callback(thisObj.setSelectedFood, {bind: thisObj});
	this.foodFinder = new FoodFinder(this.ffId, this.ffCntrId, itemSelectHandler);
} catch (ex) {
	alert(ex);
}
}

function _getFoodId() {
	return this.foodInfo.fdAliasId;
}
function _getFoodName() {
	return this.foodInfo.fdAliasName;
}
function _getUnitSelected() {
	var selVar = this.unitSelector;
    var selIx = selVar.selectedIndex;
    var selOpt = selVar.options[selIx];
    var selTxt = selOpt.text;
	return selTxt;
}
function _getUnitIdSelected() {
	var selVar = this.unitSelector;
    var selIx = selVar.selectedIndex;
    var selOpt = selVar.options[selIx];
    var selVal = selOpt.value;
	return selVal;
}
function _getQty() {
	return this.qtyInput.value;
}
function _getFoodInfo() {
	return this.foodInfo;
}

// Anyone interested in knowing when the submit button is clicked...
// Event that is called out by the FMInput...
function _addSubmitListener (sListener, cbData) {
	var ix = this.submitListeners.length;
	this.submitListeners[ix] = { listener:sListener, cbData:cbData};
}

// Update all registered listeners and reset the widget...
function _submitSelection () {
	var thisObj = this;
	for (var ix=0;ix<this.submitListeners.length;ix++) {
		var func = this.submitListeners[ix].listener;
		var cbData = this.submitListeners[ix].cbData;
		func.call(null, cbData);
	}

	this.setDefaultState();
}

/**
 * Callback when an item is selected in the autocomplete dropdown...
 * Capture the units to be used and the foodId and foodName...
 * eventType = String representing the event.
 * ui = object literal of data for the result
 */
function _setSelectedFood(eventType, ui) {
	

	// foodInfo = foodName, foodId, ssUnitsArr 
	this.foodInfo = ui.item;
	try {
		var id = this.foodInfo.fdAliasId;
		var ssUnits = this.foodInfo.servingUnits;

		var selVar = this.unitSelector;
		selVar.disabled = false;
		this.qtyInput.disabled = false;
		this.submitBtn.disabled = false;

		// reset any existing units ...
    	selVar.options.length = 0;
		// .. and now set the new unit options...
    	for (var ix=0;ix<ssUnits.length;ix++) {
        	var ssunit = ssUnits[ix];
        	selVar.options[ix] = new Option(ssunit.unitName, ssunit.unitId, ix==0, false);
		}
	} catch (e) {
   		alert(e);
	}
}

// reset the widgets so that they are ready to accept more input...
function _setDefaultState()  {
	this.ffInput.value = "";
	this.qtyInput.value = "";
	this.unitSelector.options.length = 0;
	this.unitSelector.options[0] = new Option("Unit");

	this.unitSelector.disabled = true;
	this.qtyInput.disabled = true;
	this.submitBtn.disabled = true;
}
FMInput.prototype.getUnitSelected = _getUnitSelected;
FMInput.prototype.getUnitIdSelected = _getUnitIdSelected;
FMInput.prototype.getQty = _getQty;
FMInput.prototype.getFoodInfo = _getFoodInfo;
FMInput.prototype.getFoodName = _getFoodName;
FMInput.prototype.getFoodId = _getFoodId;
FMInput.prototype.addSubmitListener = _addSubmitListener;
FMInput.prototype.submitSelection = _submitSelection;
FMInput.prototype.setSelectedFood = _setSelectedFood;
FMInput.prototype.setDefaultState = _setDefaultState;

	

