// NIWidget - Displays given Nutrition Info in a table. 
// The parentDiv where the table is to be displayed in passed to this class.
// Each food element is also editable or deletable. - this should be an option?

function NIWidget(pId) {
	this.pId = pId;
	this.parentDiv = getElement(pId);
	
	this.showNutritionInfo = true;
	this.showCalorieInfo = true;
	this.showProteinInfo = true;
	this.showFatInfo = true;
	this.showCarbInfo = true;
}

function _setPreferences(prefs) {
	if (prefs.showCalorieInfo != undefined)
		this.showCalorieInfo = prefs.showCalorieInfo;
	if (prefs.showProteinInfo != undefined)
		this.showProteinInfo = prefs.showProteinInfo;
	if (prefs.showFatInfo != undefined)
		this.showFatInfo = prefs.showFatInfo;
	if (prefs.showCarbInfo != undefined)
		this.showCarbInfo = prefs.showCarbInfo;
	if (prefs.showNutritionInfo != undefined) {
		this.showNutritionInfo = prefs.showNutritionInfo;
		this.showCalorieInfo = prefs.showNutritionInfo;
		this.showFatInfo = prefs.showNutritionInfo;
		this.showCarbInfo = prefs.showNutritionInfo;
		this.showProteinInfo = prefs.showNutritionInfo;
	}
}
NIWidget.prototype.setPreferences = _setPreferences;

function _removeOldWrapper() {
	var oldWrapper = document.getElementById("niTableWrapper"+this.pId);
	if (oldWrapper != null)
		this.parentDiv.removeChild(oldWrapper);
}
NIWidget.prototype.removeOldWrapper = _removeOldWrapper;

function _createWidget() {

	// Create the structure into which the nutrition info table will be rendered
	this.niWrapper = createDOMElement("div");
	this.niWrapper.id="niTableWrapper" + this.pId;
	this.niInfoDiv = createDOMElement("div");
	this.niInfoDiv.id = "niTable" + this.pId;
	this.niWrapper.appendChild(this.niInfoDiv);

	this.parentDiv.appendChild(this.niWrapper);
}
NIWidget.prototype.createWidget = _createWidget;

function _getNumberOfValidItemsInFoodInfo() {
	var ret = 0;
	for (var ix=0;ix<this.foodInfo.length;ix++) {
		var fInfo = this.foodInfo[ix];
		if (fInfo.fdAliasId != undefined && fInfo.fdAliasId != null)
			ret++;
	}
	return ret;
}
NIWidget.prototype.getNumberOfValidItemsInFoodInfo = _getNumberOfValidItemsInFoodInfo;

function _createNITable (fmInfoArr) {
	this.removeOldWrapper();

	this.foodInfo = fmInfoArr;
	var numValid = this.getNumberOfValidItemsInFoodInfo();
	if (numValid == 0)
		return;

	this.createWidget();
	this.table = createDOMElement("table");
	this.tblBody = createDOMElement("tbody");

	var hdr = createDOMElement("tr");
	addTH(hdr, "Food");


	if (this.showNutritionInfo) {
		if (this.showCalorieInfo)
			addTH(hdr, "Calories");
		if (this.showProteinInfo)
			addTH(hdr, "Proteins (g)");
		if (this.showFatInfo)
			addTH(hdr, "Fats (g)");
		if (this.showCarbInfo)
			addTH(hdr, "Carbs (g)");
	}
	this.tblBody.appendChild(hdr);
	
	this.niInfoDiv.appendChild(this.table);

	this.table.appendChild(this.tblBody);

	var totalsInfo = null;
	for (var ix=0;ix<fmInfoArr.length;ix++) {
		
		var fmInfo = fmInfoArr[ix];
		// var niInfo = fmInfoArr[ix].ni;	
		var fdId = fmInfo.fdAliasId;
		if (fdId == null)
			totalsInfo = fmInfo.ni;
		else {
			var row = createDOMElement("tr");
			this.createNiCells(row, fmInfo, ix);
	
			var niInfo = fmInfo.ni;
			if (niInfo != undefined) {
				if (this.showCalorieInfo)
					addTD(row, niInfo.cal);	
				if (this.showProteinInfo)
					addTD(row, niInfo.prot);	
				if (this.showFatInfo)
					addTD(row, niInfo.fat);	
				if (this.showCarbInfo)
					addTD(row, niInfo.carb);	
			}
	
			this.tblBody.appendChild(row);

			if (row.eipDiv != null) {
				row.eipDiv.init();
			}
		}
	}
	if (totalsInfo != null)
		this.addTotals(totalsInfo);
}
NIWidget.prototype.createNITable =  _createNITable;

function _addTotals(niInfo) {
	var totals = createDOMElement("tr");
	addTD(totals, "Total");
	addTD(totals, niInfo.cal);
	addTD(totals, niInfo.prot);
	addTD(totals, niInfo.fat);
	addTD(totals, niInfo.carb);

	this.tblBody.appendChild(totals);	
}
NIWidget.prototype.addTotals =  _addTotals;

function _enableEIP(flag, editCBFunc, editCBData) {
	var thisObj = this;
	this.enableEIP = flag;
	this.editCB = callback(editCBFunc, {bind: thisObj, args: editCBData});
}
NIWidget.prototype.enableEIP = _enableEIP;

function _enableDelete(flag, delCB, delCbData) {
	this.enableDelete = flag;
	this.deleteCB = callback(delCB, {args: delCbData});
}
NIWidget.prototype.enableDelete = _enableDelete;

function _createNiCells (row, fmInfo, ix) {
	var cell = createDOMElement("td");
	var cellDiv = createDOMElement("div");
	var thisObj = this;

	var niInfo = fmInfo.ni;
	var fdAliasId = fmInfo.fdAliasId;

	// put the delete link if enabled...
	if (this.enableDelete) {
		var delLnk = createDOMElement("a");
		delLnk.href = "javascript:void(0);";
		delLnk.innerHTML = "x";
		delLnk.title = "remove " + fmInfo.fdAliasName;
	
		delLnk.onclick = function () {
			thisObj.deleteCB(ix);
		}
		cellDiv.appendChild(delLnk);
	}

	if (this.enableEIP) {
		var editDlgId = "dlg_" + this.pId + fmInfo.fdAliasId + "_" + ix;
		var ctrlId = "dlgCtrl_" + this.pId + fmInfo.fdAliasId + "_" + ix;
	
		var editLnk = createElementWithId("a", ctrlId);
		editLnk.href = "javascript:void(0);";
		editLnk.innerHTML = fmInfo.qty + " " + fmInfo.unitName;
		editLnk.title = "Change Quantity";
	
		var editDlg = createElementWithId("div", editDlgId);
		var qtyInput = createQtyInput("qty" + fdAliasId, fmInfo.qty);
		var usInput = createUnitSelector("us" + fdAliasId, fmInfo.servingUnits, fmInfo.unitId);
		editDlg.appendChild(qtyInput);
		editDlg.appendChild(usInput);
		cellDiv.appendChild(editLnk);
	}
	
	var txtElem = document.createTextNode(" " + fmInfo.fdAliasName);
	cellDiv.appendChild(txtElem);

	if (this.enableEIP) {
		cellDiv.appendChild(editDlg);
		var dlg = new Dialog(editDlgId, ctrlId);
		dlg.setCallbackData (ix);
		dlg.setAutoControl(true);
		var changeCB = callback (thisObj.changeFMU, {bind: thisObj});
		dlg.setCallbacks(changeCB);
		dlg.addSubmitAction("Change");
		dlg.addCancelAction("Cancel");
		row.eipDiv = dlg;
	}
	cell.appendChild(cellDiv);
	row.appendChild(cell);
}
NIWidget.prototype.createNiCells =  _createNiCells;

function _changeFMU(ix) {
	var thisObj = this;
	var tmp = this.foodInfo[ix].ni;	
	var qtyElem = getElement("qty" + this.foodInfo[ix].fdAliasId);
	var qty = qtyElem.value;
	var us = getElement("us" + this.foodInfo[ix].fdAliasId);
	var unitId = us.options[us.selectedIndex].value;


	if (this.editCB != null) 
		this.editCB	(qty, unitId, ix);

	// var niCbHandler = callback(thisObj.createNITable, {bind: thisObj});
	// this.getNutritionInfo(niCbHandler);

}
NIWidget.prototype.changeFMU = _changeFMU;

function _getNITotals() {
	return this.niTotals;
}


NIWidget.prototype.getNITotals = _getNITotals;


function addTH(row, txt) {
	var cell = createDOMElement("th");
	var txtNode = document.createTextNode(txt);
	cell.appendChild(txtNode);
	row.appendChild(cell);
}
function addTD(row, txt) {
	var cell = createDOMElement("td");
	var txtNode = document.createTextNode(txt);
	cell.appendChild(txtNode);
	row.appendChild(cell);
}

function createBtn () {
	var ret = createDOMElement("input");
	ret.type="button";
	ret.value="Get Nutrition Info";
	return ret;
}

