/**
 * this is a widget that could be used on webpage within strfri.
 * By itself it will get the last entered food diary summary and display
 * this. It also provides a js api call that will accept a foodId/foodAliasId
 * and popup a form to add this food item to the food diary.
 * Usage model: 
 */
function FoodDiaryWidget (pId) {
	this.pId = pId;
	this.parentDiv = getElement(pId);
	this.myWrapper = createDOMElement("div");
	this.myWrapper.setAttribute("class", "fdwidget");
	this.parentDiv.appendChild(this.myWrapper);
	this.date = "10/11/2009";
	var thisObj = this;
    var cb = {
        success: function (res) {
            var fdSummary = YAHOO.lang.JSON.parse(res.responseText);
            thisObj.setSummary(fdSummary);
        },
        failure: function (res) {
            alert(res.statusText);
        }
    }
    var request = YUC.asyncRequest('POST', "get_fd_summary.jsp?day="+this.date, cb);
}

function _setFoodAliasId(fdAliasId) {
	var thisObj = this;
	this.fdAliasId = fdAliasId;
	var cb = {
    	success: function (res) {
        	var fmInfo = YAHOO.lang.JSON.parse(res.responseText);
        	thisObj.initDlg(fmInfo[0]);
    	},
    	failure: function (res) {
        	alert(res.statusText);
    	}
	}
	var request = YUC.asyncRequest('POST', "find_food_from_id.jsp?fdAliasId="+this.fdAliasId, cb);
}
FoodDiaryWidget.prototype.setFoodAliasId = _setFoodAliasId;

function _destroyDlg() {
	if (this.myDlg != null)
		this.myWrapper.removeChild(this.myDlg);
}
FoodDiaryWidget.prototype.destroyDlg = _destroyDlg;

	
function _initDlg(fmInfo) {
	this.destroyDlg();

	this.fmInfo = fmInfo;
	
	
	// create a div with a qty, unit input and time input...
	var editDlgId = "dlg__" + this.pId;
	var editDlg = createElementWithId("div", editDlgId);
	var nameHdr = createDOMElement("H4");
	var nameTxt = document.createTextNode(this.fmInfo.fdAliasName);
	nameHdr.appendChild(nameTxt);
	editDlg.appendChild(nameHdr);

	this.qtyInput = createQtyInput("qty", this.fmInfo.qty);
	this.usInput = createUnitSelector("us", this.fmInfo.servingUnits, this.fmInfo.unitId);
	editDlg.appendChild(this.qtyInput);
	editDlg.appendChild(this.usInput);	
	
	this.myDlg = editDlg;
	this.myWrapper.appendChild(editDlg);

    this.dlg = new Dialog(editDlgId, null);

	var thisObj = this;
	var addCB = callback(thisObj.addToFoodDiary, {bind: thisObj});

	this.dlg.setCallbacks(addCB);
    this.dlg.addSubmitAction("Add");
	this.dlg.addCancelAction("Cancel");

	this.dlg.setAutoControl(false);
	this.dlg.init();
	this.dlg.openDlg();
}
FoodDiaryWidget.prototype.initDlg = _initDlg;

function _addToFoodDiary() {
    var qty = this.qtyInput.value;
    var unitId = this.usInput.options[this.usInput.selectedIndex].value;
	var when = "Lunch";	// for now...
	var date = "10/11/2009";	// need to get this from the user too??
	
	var thisObj = this;
    var cb = {
        success: function (res) {
            var fdSummary = YAHOO.lang.JSON.parse(res.responseText);
            thisObj.setSummary(fdSummary);
        },
        failure: function (res) {
            alert(res.statusText);
        }
    }

    var selVar = this.usInput;
    var selIx = selVar.selectedIndex;
    var selOpt = selVar.options[selIx];
    var selTxt = selOpt.text;

	var asEntered = qty + " " + selTxt + " " + this.fmInfo.fdAliasName;
    var request = YUC.asyncRequest('POST', "add_to_fd.jsp?numberOfItems=1&fmQty0="+qty+"&fmUnitId0="+unitId+"&fmFAId0="+this.fdAliasId+"&fmAsEntered0="+asEntered+"&when0="+when+"&day="+date, cb);
}
FoodDiaryWidget.prototype.addToFoodDiary = _addToFoodDiary;

function _setSummary (summary) {
	if (this.sDiv != null)
		this.myWrapper.removeChild(this.sDiv);

	this.sDiv = createDOMElement("div");
	this.sDiv.setAttribute("class", "emSmall"); 
	var info = "Calories: " + summary.cal + ", " + "Carbs: " + summary.carb;
	var infoNode = document.createTextNode(info);
	this.sDiv.appendChild(infoNode);
	this.myWrapper.appendChild(this.sDiv);
}
FoodDiaryWidget.prototype.setSummary = _setSummary;

