var activeMeal = null;
var collapseSpeed = 250;

function initCollapseMeal()
{	
	$('.box.meal .body').each(function() {
		var labelRoot = $(this).find('.col1 ul.label>li');
		var dataRoot = $(this).find('.col2 ul.dataMain>li');
		
		labelRoot.each(function (key, value) {
			var labelEl = $(this);
			var dataEl = dataRoot.eq(key);

			var meal_id = $(this).attr('id').substr(5);
			
			if (activeMeal == null || activeMeal.id != meal_id)
			{
				labelEl.children('ul').height('0px');
				dataEl.children('ul.mealWrapper').height('0px');
			}
			else if (activeMeal.id == meal_id)
			{	
				labelEl.css('background-color', "#dadada");
				labelEl.css('background-image', "url('images/ui/meal_minus.png')");
				labelEl.css('background-position', "10px 11px");
				labelEl.css('background-repeat', "no-repeat");				
				
				activeMeal = {label: labelEl, data: dataEl, id:meal_id};
			}			
			
			$(this).children('p').click(
				function() { 
					var oldID = null;					
					ah = labelEl.find('ul li').length * 28;
					
					if (activeMeal != null)
					{						
						activeMeal.label.children('ul').animate({height: '0px'}, collapseSpeed);
						activeMeal.data.children('ul.mealWrapper').animate({height: '0px'}, collapseSpeed);
						
						activeMeal.label.css('background-color', "#dadada");
						activeMeal.label.css('background-image', "url('images/ui/meal_plus.png')");
						activeMeal.label.css('background-position', "10px 9px");
						activeMeal.label.css('background-repeat', "no-repeat");	
						
						oldID = activeMeal.id;
						
						if (activeMeal.id != meal_id)
						{
							labelEl.children('ul').animate({height: ah + 'px'}, collapseSpeed);
							dataEl.children('ul.mealWrapper').animate({height: ah + 'px'}, collapseSpeed);
							
							labelEl.css('background-color', "#dadada");
							labelEl.css('background-image', "url('images/ui/meal_minus.png')");
							labelEl.css('background-position', "10px 11px");
							labelEl.css('background-repeat', "no-repeat");

							activeMeal = {label: labelEl, data: dataEl, id:meal_id};
							setMealData(meal_id, oldID);
						}
						else
						{
							activeMeal = null;
							setMealData(null, oldID);
						}
					}
					else
					{
						labelEl.children('ul').animate({height: ah + 'px'}, collapseSpeed);
						dataEl.children('ul.mealWrapper').animate({height: ah + 'px'}, collapseSpeed);
						
						labelEl.css('background-color', "#dadada");
						labelEl.css('background-image', "url('images/ui/meal_minus.png')");
						labelEl.css('background-position', "10px 11px");
						labelEl.css('background-repeat', "no-repeat");

						activeMeal = {label: labelEl, data: dataEl, id:meal_id};
						
						setMealData(meal_id, oldID);
					}																				
				}
			);
		});
	});
	
}

function setMealData(newID, oldID)
{		
	var sumList = $('#summeringsBox .body .col2 .dataSub li input');

	if (oldID != null)
	{
		$.each(allMeals[oldID].provs ,function(key, value) {
			var prov = $('#prov_' + value.id + ' li.editable input');
			prov.val('');				
			delActiveProv(prov, value.id);						
		});	
	}	
	
	if (newID != null)
	{
		$.each(allMeals[newID].provs, function(key, value) {
			var prov = $('#prov_' + value.id + ' li.editable input');
			prov.val(value.M);				
			addActiveProv(prov, allProvs[value.id], value.M)
		});		
	}	
}
