
/**
 * Nutrition-class
 * @class Nutrition
 * @property {array} foods IDs of the selected foods
 * @property {string} nutrisource Source of nutritional information
 */
function Nutrition(){

    that = this;
    this.foods = new Array;
    this.nutrisource = "usda";

	this.groups = new Array(27);
	this.groups = [
		{ 'id': '0300', 'name': "Baby Foods" },
		{ 'id': '1800', 'name': "Baked Products" },
		{ 'id': '1300',	'name': "Beef Products" },
		{ 'id': '1400', 'name': "Beverages" },
		{ 'id': '0800', 'name': "Breakfast Cereals" },
		{ 'id': '2000', 'name': "Cereal Grains and Pasta" },
		{ 'id': '0100', 'name': "Dairy and Egg Products" },
		{ 'id': '3500', 'name': "Ethnic Foods" },
		{ 'id': '2100', 'name': "Fast Foods" },
		{ 'id': '0400', 'name': "Fats and Oils" },
		{ 'id': '1500',	'name': "Finfish and Shellfish Products" },
		{ 'id': '0900', 'name': "Fruits and Fruit Juices" },
		{ 'id': '1700', 'name': "Lamb, Veal, and Game Products" },
		{ 'id': '1600', 'name': "Legumes and Legume Products" },
		{ 'id': '2200', 'name': "Meals, Entrees, and Sidedishes" },
		{ 'id': '1200', 'name': "Nut and Seed Products" },
		{ 'id': '1000', 'name': "Pork Products" },
		{ 'id': '0500', 'name': "Poultry Products" },
		{ 'id': '0700', 'name': "Sausages and Luncheon Meats" },
		{ 'id': '2500', 'name': "Snacks" },
		{ 'id': '0600', 'name': "Soups, Sauces, and Gravies" },
		{ 'id': '0200', 'name': "Spices and Herbs" },
		{ 'id': '1900', 'name': "Sweets" },
		{ 'id': '1100', 'name': "Vegetables and Vegetable Products" },
	];

	this.srorders = [
		{"start" : 0,
		 "range" : [100,300,400,600,700,800,1000,
		 1100,1200,1500,1600,1700,1800,1900,2000,2100,2200],
		 "detail" : { "name": "Proximates", "htmlstring" : ""}},
		{"start" : 5300,
		 "range" : [5300,5400,5500,5600,5700,5800,5900,6000,6100,6200,6240],
		 "detail" : { "name": "Minerals", "htmlstring" : ""}},
		{"start" : 6300,
		 "range" : [6300,6400,6500,6600,6700,6800,6900,7000,7100,7200,7220,7270,7300,
		 7340,7400,7499,7700,7900,7920,8000,8100,8200,8700,8800],
		 "detail" : { "name": "Vitamins", "htmlstring" : ""}},
		{"start" : 9700,
		 "range" : [9700,9800,9900,10000,10100,10300,10400,10500,10600,10700,10800,10900,
		 11100,11200,11300,11400,11500,11600,11700,11800,11900,12000,12100,12200,12300,
		 12400,12500,12600,12700,12800,12900,13100,13200,13300,13600,13700,13800,13900,
		 14000,14100,14200,14300,14400,14500,14600,14700,14900,15000,15100,15160,15200,
		 15300,15400,15500,15600,15700,15800,15900,16000,16200],
		 "detail" : { "name": "Lipids", "htmlstring" : ""}},
		{"start" : "16300",
		 "range" : [16300,16400,16500,16600,16700,16800,16900,17000,17100,17200,17300,
		 17400,17500,17600,17700,17800,17900,18000],
		 "detail" : { "name": "Amino acids", "htmlstring" : ""}},
		{"start" : "18100",
		 "range" : [18100,18200,18300,18400,18500,18600,18700,18800,18900],
		"detail" : { "name": "Others", "htmlstring" : ""}},
	];

    /** Searches for foods containing given searchstring in their name (iphone-version)
     * @function searchFoods_IP
     * @param {string} searchstring
     * @param {string} nutrisource
     * @param {int} firstrow
     */
    this.searchFoods_IP = function(searchstring, nutrisource){
    
        searchstring = escape(search.checkSearchstring(searchstring));

        $.getJSON("http://datacalmers.hoito.org/nutritiontactician/loadcontent.php?jsoncallback=?",
			{
				action: "searchfoodnames",
	            nutrisource: nutrisource,
            	searchstring: searchstring,
            	standalone: true,
            	clienttype: 'iphone'
			},
			function(json, statustext) {

				var htmlstring = new Array(27);
				$.each(json.foods, function(i, food) {
					htmlstring[food.group] = "";
				});

				$.each(json.foods, function(i, food) {

					htmlstring[food.group] += "<li class=\"listfood\""
//					+ "onmouseover='$(this).css(\"background-color\", \"#dddddd\")' "
//					+ "onmouseout='$(this).css(\"background-color\", \"#ffffff\")'>"
					+ "<a href=\"javascript: nutrition.showNutritions_IP('" + food.group + "', '"
					+ food.foodnumber + "_9', '" + escape(food.fooddescescaped) + "','usda')"
					+ "\">" + food.fooddescescaped + "</a>";
					htmlstring[food.group] += "</li>";

				});

				var groupstring = "";
				for (var i in that.groups) {
					if (htmlstring[that.groups[i].id] && htmlstring[that.groups[i].id].length != "") {
						groupstring += "<h1 class=\"listfoodtitle\" "
						+ "onmouseover='$(this).css(\"cursor\", \"pointer\")' "
						+ "onmouseout='$(this).css(\"cursor\", \"default\")' "
						+ "onclick=\"nutrition.selectFoodgroup('" + that.groups[i].id + "', 'fromleft')\">"
						+ that.groups[i].name + "</h1>";
					}
				}
				$("#nutrition").append("<div id=\"foodgroups\">" + groupstring + "</div>");

				for (var i in that.groups) {
					if (htmlstring[that.groups[i].id] && htmlstring[that.groups[i].id].length != "") {
						$("#nutrition").append("<ul class=\"listfood\" id=\"fdgrpcd" + that.groups[i].id + "\">"
						+ htmlstring[that.groups[i].id] + "</ul>");
					}
				}

			}
        );
       
    };

    /**
     * Shows nutritions for selected foods
     * @function showNutritions_IP
     * @param {command} command
     * @param {string} foodids
     * @param {string} nutrisource
     */
    this.showNutritions_IP = function(groupid, foodids, foodname, nutrisource){
    
        $.getJSON("http://datacalmers.hoito.org/nutritiontactician/loadcontent.php?jsoncallback=?",
			{
				action: "shownutrition",
	            nutrisource: nutrisource,
            	foodids: foodids,
            	command: 0,
            	standalone: true,
            	clienttype: 'iphone'
			},
			function(json, statustext) {

				var htmlstring = new Array(6);
				$.each(json.nutritions, function(i, nutrition) {
					htmlstring[nutrition.srorder] = "";
				});

				$.each(json.nutritions, function(i, nutrition) {

					htmlstring[nutrition.srorder] += "<td width='200' align='left' class='listfood'>"
					+ nutrition.nutrdesc + "</td>";
					htmlstring[nutrition.srorder] += "<td width='60' nowrap='nowrap' align='right' class='listfood'>"
					+ nutrition.nutrval + "</td>";
					htmlstring[nutrition.srorder] += "<td width='40' nowrap='nowrap' align='left' class='listfood'>"
					+ nutrition.units + "</td>";

				});

				var nutritionstring = "";

				$.each(that.srorders, function(i, srorder) {
					nutritionstring += "<h1 class=\"listnutritiontitle\">" + srorder.detail.name + "</h1>"
					+ "<table cellpadding='0' cellspacing='0' width='100%'><tbody>";
					$.each(srorder.range, function(j, number) {
						if (htmlstring[number] && htmlstring[number].length != "") {
							nutritionstring += "<tr align='left' valign='top'>"
							nutritionstring += htmlstring[number];
							nutritionstring += "</tr>";
						}
/*
						else {
							nutritionstring += "This kind of nutritional information is unavailable for this food."
						}
*/
					})
					nutritionstring += "</tr></tbody></table>";
				});

				$("#nutrition").append("<div id=\"nutritiondetails\">"
				+ "<h3>" + foodname + " (100g)</h3>" + nutritionstring + "</div>");

				$('#toolbarfoodsbutton').animate({left : '0px', opacity : 'toggle'}, 'slow');
				$('#toolbargroupsbutton').animate({left : '-100%', opacity : 'toggle'}, 'slow')
				$('#toolbarfoodsbutton').one("click", function(){nutrition.selectFoodgroup(groupid, 'fromright')});
				$('#toolbartitle').html("id: " + foodids.substring(0,5));
				$('#fdgrpcd'+groupid).animate({left : '-100%', opacity : 'toggle'}, 'slow');
				$('#nutritiondetails').show().animate({left : '8px'}, 'slow');

			}
        );
       
    };

    /**
     * Change to the selected foodgroup -view
     * @function selectFoodgroup
     * @param {string} groupid
     * @param {string} direction
     */

	this.selectFoodgroup = function(groupid, direction) {

		if (direction == "fromright") {
			$('#toolbarfoodsbutton').animate({left : '-77px', opacity : 'toggle'}, 'slow');
			$('#toolbargroupsbutton').animate({left : '0px', opacity : 'toggle'}, 'slow');
			$('#toolbartitle').html(this.getGroupname(that.groups, groupid));
			$('#nutritiondetails').animate({left : '100%', opacity : 'toggle'}, 'slow').remove();
			$('#fdgrpcd'+groupid).animate({left : '8px', opacity : 'toggle'}, 'slow');
		}
		else if (direction == "fromleft") {
			$('#toolbargroupsbutton').animate({left : '0px', opacity : 'toggle'}, 'slow')
			.one("click", function(){nutrition.returnToFoodgroups(groupid)});
			$('#toolbartitle').html(this.getGroupname(that.groups, groupid).substring(0,16) + "...");
			$('#foodgroups').animate({left : '-100%', opacity : 'toggle'}, 'slow');
			$('#fdgrpcd'+groupid).animate({left : '8px', opacity : 'toggle'}, 'slow');
		}
		
	}

    /**
     * Return to foodgroup selection -view
     * @param {string} groupid
     * @function returnToFoodgroups
     */

	this.returnToFoodgroups = function(groupid) {

		$('#toolbargroupsbutton').animate({left : '-77px', opacity : 'toggle'}, 'slow')
		$('#toolbartitle').html("Nutrition tactician <span style=\"font-size: 7px;\">v1.00</span>");
		$('#foodgroups').animate({left : '14px', opacity : 'toggle'}, 'slow');
		$('#fdgrpcd'+groupid).animate({left : '100%', opacity : 'toggle'}, 'slow');
		
	}
    
    /**
     * Find an object from an array
     * @param {Array} arr
     * @param {string} groupid
     * @function getGroupname(arr, groupid) {
     * 
     */

	this.getGroupname = function(arr, groupid){

		var i = 0;
		while (i < arr.length) {
			
			if (arr[i].id == groupid) return arr[i].name;
			i++;
			
		}
	
	}	
	
}
