// Selected foodline
var foodline_selected = false;
var foodline_selected_last_dm = false;
var foodline_selected_current_dm = false;
var foodline_selected_last = false;
var foodline_selected_current = false;

function hotkeys(e)
{
	if($("#dialog1").is(":visible") || $("#fehlerPopup").is(":visible"))
	{
		return;
	}
  if (!e)
	{
    var e = window.event;
	}
  if (!e)
	{
    return;
	}
	var keymap = new Array;
	if (document.getElementById('m6'))
	{ // Extended mealtypes
		// key 1-8
		keymap[49] = 1;
		keymap[50] = 6;
		keymap[51] = 2;
		keymap[52] = 7;
		keymap[53] = 3;
		keymap[54] = 8;
		keymap[55] = 4;
		keymap[56] = 5;
		// key F1-F8
		keymap[97] = 1;
		keymap[98] = 6;
		keymap[99] = 2;
		keymap[100] = 7;
		keymap[101] = 3;
		keymap[102] = 8;
		keymap[103] = 4;
		keymap[104] = 5;
	} else { // Standard mealtypes
		// Key 1-5
		keymap[49] = 1;
		keymap[50] = 2;
		keymap[51] = 3;
		keymap[52] = 4;
		keymap[53] = 5;
		// Key F1-F5
		keymap[97] = 1;
		keymap[98] = 2;
		keymap[99] = 3;
		keymap[100] = 4;
		keymap[101] = 5;
	}
	if (keymap[e.keyCode])
	{
		window.location = "/index.php?id=205&mealtype=" + keymap[e.keyCode];
	}
	return true;
}

function update_foodxml()
{
	if (window.XMLHttpRequest)
		{
		xhttp=new XMLHttpRequest();
		}
	else // Internet Explorer 5/6
		{
		xhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	xhttp.open("GET","/scripts/foodxml.php",false);
	xhttp.send("");
	xmlDoc=xhttp.responseXML; 
}
update_foodxml();

function cns_infobox(id)
{
	// Get type
	var pattern= /^[a-z]+/gi;
	var type = id.match(pattern);

	// Check which data we have, and insert hardcoded data if needed
	if(get_value(id,'amount_text') == '-')
	{
		var amount_text = '{{LANG_2432}}';
	}
	else
	{
		var amount_text = get_value(id,'amount_text');
	}
	if(get_value(id,'name') == '-')
	{
		if(type == 'cd')
		{
			var name_text = get_value(id,'title');
		}
		if(type == 'm')
		{
			var name_text = get_value('meal_lang', id);
		}
	}
	else
	{
		var name_text = get_value(id,'name');
	}

	// Generate html
	var html = '<table style="width: 100%;">';
	html += '<tr><td style="font-weight: bold; padding: 0 5px;">'+name_text+'</td>';
	html += '<td style="vertical-align: top; text-align: right;"><img src="/gfx/cdnutrition.php?style=7&prot='+get_value(id,'pct_protein')+'&carb='+get_value(id,'pct_kulhydrat')+'&fat='+get_value(id,'pct_fedt')+'" height="19" width="19" /></td></tr>';
	html += '</table>';
	html += '<div style="padding: 5px;"><table class="infobox">';
	html += '<tr><td>'+amount_text+':</td><td class="ib_portion">'+get_value(id,'amount')+'</td><td class="ib_100g">{{LANG_1883}}</td></tr>';
	if (xmlDoc.getElementsByTagName('details_lang')[0])
	{
		y=xmlDoc.getElementsByTagName('details_lang')[0].childNodes; // Get food details
		for (i=0;i<y.length;i++)
		{ 
			if (y[i].nodeType==1)
			{
				html += '<tr><td>'+y[i].childNodes[0].nodeValue+':</td>';
				html += '<td class="ib_portion">'+get_value(id,y[i].nodeName)+'</td>';
				if(type == 'cd' || type == 'm')
				{
					html += '<td class="ib_100g">'+get_value(id+'_100',y[i].nodeName)+'</td></tr>';
				}
				else
				{
					html += '<td class="ib_100g">'+get_value('fo'+get_value(id,'food_id'),y[i].nodeName)+'</td></tr>';
				}
			} 
		}
	}
	html += '</table>';
	return html;
}

function get_value(id, element)
{
	if (xmlDoc.getElementsByTagName(id).length == 0)
	{
	//	return false;
		return false;
	}
	if (xmlDoc.getElementsByTagName(id)[0].getElementsByTagName(element).length == 0)
	{
	//	alert('fejl 2 - se 201.js!!');
		return '-';
	}
	var str = xmlDoc.getElementsByTagName(id)[0].getElementsByTagName(element)[0].childNodes[0].nodeValue;
	return str;
}

// Marked the line as selected, and popup dmenu
function foodline_click(id, menu_id, e)
{
	var menu = menu_id & 15;
	var submenu = menu_id & 240;
	if (!e)
		var e = window.event;
	// Check for legal id
	if (!document.getElementById(id))
		return;
	$('.bgYellow').removeClass('bgYellow');
	$('#'+id).addClass('bgYellow');
	switch (submenu)
	{
		case 16:
			var desc = '{{LANG_2554}}';
			break;
		case 32:
			var desc = '{{LANG_2555}}';
			break;
		case 64:
			var desc = '{{LANG_2556}}';
			break;
		case 128:
			var desc = '{{LANG_2557}}';
			break;
	}
	if (menu == 0 || menu == 4)
	{
		var context = $('#contextmenu_foodline');
		context.cutnpaste($('#calendar_cut'), $('#calendar_paste_foodline'));
		$('#a_goto').text(desc);
		$('#a_goto').data('sub_menu', submenu);
		$('#change_weight').show();
//		dm_ext_changeItem(menu, 1, 3, [desc, "javascript:menu_goto("+submenu+");", "goto.png" , "goto2.png", , , , , ]);
	}
	if (menu == 1)
	{
		var context = $('#contextmenu_exercise');
		context.cutnpaste($('#calendar_cut'), $('#calendar_paste_exercise'));
	}
	if (menu == 2)
	{
		var context = $('#contextmenu_mealheader');
		context.cutnpaste($('#calendar_cut'), $('#calendar_paste_mealheader'));
	}
	if (menu == 3)
	{
		var context = $('#contextmenu_exerciseheader');
		context.cutnpaste($('#calendar_cut'), $('#calendar_paste_exerciseheader'));
	}
	if (menu == 4)
	{
		$('#change_weight').hide();
	}
	context.trigger('popup_menu', e);
	return false;
}

function right(str, n)
{
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else {
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
}

// Detect selected meal and redirect to id 205
// allows the user to add more food
function menu_add_food()
{
	var id=$('.bgYellow').attr('id');
	if (!id)
	{
		return;
	}
	if (id.substring(0,1) == 'm')
	{
		window.location = "/index.php?id=205&mealtype="+id.substring(1,2);
	}
}

// Detect selected meal and redirect to id 208
// allows the user to add favorite
function menu_add_favorite(f_id)
{
	var id=$('.bgYellow').attr('id');
	if (id.substring(0,1) == 'm')
	{
		window.location = "/index.php?id=208&f_id="+f_id+"&mealtype="+id.substring(1,2);
	}
}

// Detect selected meal and redirect to id 210
// allows the user to add favorite
function menu_add_favorite_exercise(f_id)
{
	var id=$('.bgYellow').attr('id');
	if (id.substring(0,1) == 'm')
	{
		window.location = "/index.php?id=210&f_id="+f_id+"&mealtype="+id.substring(1,2);
	}
}

// Detect selected meal or foodline and move it
function menu_move(mealtype)
{
	var id=$('.bgYellow').attr('id');
	xajax_move_foodline(mealtype, id);
}

// Detect selected meal or foodline and it it
function menu_delete()
{
	var id=$('.bgYellow').attr('id');
	xajax_delete_foodline(id);
}

// Detect selected meal or foodline and change it
function menu_change_amount()
{
	var id=$('.bgYellow').attr('id');
	window.location = "/index.php?id=207&foodline="+id;
}

// Go to food, recipe, favorite or foodcalc
function menu_goto(menu_id)
{
	var id=$('.bgYellow').attr('id');
	window.location = "/index.php?id=240&menu_id="+$('#a_goto').data('sub_menu')+"&foodline="+id;
}

$(document).ready(function() {
	$(".contextmenu_day")
	.bind("click", function(e) {
		$('#dialog1').dialog('close');
		$("#contextmenu_day")
		.cutnpaste($('#calendar_cut'), $('#calendar_paste_day'))
		.trigger('popup_menu', e);
	});
	$("#contextmenu_foodline, #contextmenu_mealheader, #contextmenu_exerciseheader, #contextmenu_exercise, #contextmenu_foodline")
	.bind("hide_menu", function() {
		$('.bgYellow').removeClass('bgYellow');
	});
});

