


function filter_table(term, _id, cellNr){
	var suche = term.value.toLowerCase();
	var table = document.getElementById(_id);
	var ele;
	for (var r = 1; r < table.rows.length; r++){
		ele = table.rows[r].cells[cellNr].innerHTML.replace(/<[^>]+>/g,"");
		if (ele.toLowerCase().indexOf(suche)>=0 )
			table.rows[r].style.display = '';
		else table.rows[r].style.display = 'none';
	}
}

var $j = jQuery.noConflict();

var edited = new Array();

$j(document).ready(function(){
	
 

	//$j('.edit').css('background', 'red');
	
   $j("tbody tr:odd").css("background", "url('http://progres2.hekko.pl/dieta/wp-content/themes/dieta/img/row_background.png')"); 
   
	$j(".edit").prev().prev().prev().css("text-align", "left");
    
    $j('.edit').editable({
	submit:'OK',
	onSubmit:calculateRow
	});
    
    
   
});

function calculateRow(content)
{
	//alert("calculate!");
	var x = content.current.split(' ', 10);
	//console.log(x);
	
	var newValue = parseFloat(x[0].replace(',', '.'));
	var jednostka = x[1];
	
	//console.log(jednostka);
	
	var id = this.prev().prev().prev().prev();
	
	var masa_porcji = this.prev();
	var kj = this.next();
	var kcal = kj.next();
	var bialko = kcal.next();
	var tluszcz = bialko.next();
	var weglowodany = tluszcz.next();
	var choles = weglowodany.next();
	var blonnik = choles.next();
	
	//var newValue = parseFloat(content.current);
	
	if (newValue==0) return 0;
	
	if (edited[id.html()]) 
	{
		//MAMY TE DANE W "BAZIE"
		
		
	}
	else
	{
		//alert("Info "+id.html()+" "+kj.html()+" "+kcal.html()+" "+bialko.html()+" "+tluszcz.html()+" "+weglowodany.html()+" "+choles.html()+" "+blonnik.html()+" ");
		
		edited[id.html()] = new Object;
		
		edited[id.html()].kj = parseFloat(kj.html());
		edited[id.html()].kcal = parseFloat(kcal.html());
		edited[id.html()].bialko = parseFloat(bialko.html());
		edited[id.html()].tluszcz = parseFloat(tluszcz.html());
		edited[id.html()].weglowodany = parseFloat(weglowodany.html());
		edited[id.html()].choles = parseFloat(choles.html());
		edited[id.html()].blonnik = parseFloat(blonnik.html());
		edited[id.html()].masa_porcji = parseFloat(masa_porcji.html());
		
		
		
		//console.log(edited);
	}
	
	if(jednostka=='g')
	{
		kj.html		(Math.round(parseFloat(edited[id.html()].kj*(newValue/edited[id.html()].masa_porcji))*100)/100);
		kcal.html	(Math.round(parseFloat(edited[id.html()].kcal*(newValue/edited[id.html()].masa_porcji))*100)/100);
		bialko.html	(Math.round(parseFloat(edited[id.html()].bialko*(newValue/edited[id.html()].masa_porcji))*100)/100);
		tluszcz.html	(Math.round(parseFloat(edited[id.html()].tluszcz*(newValue/edited[id.html()].masa_porcji))*100)/100);
		weglowodany.html(Math.round(parseFloat(edited[id.html()].weglowodany*(newValue/edited[id.html()].masa_porcji))*100)/100);
		choles.html	(Math.round(parseFloat(edited[id.html()].choles*(newValue/edited[id.html()].masa_porcji))*100)/100);
		blonnik.html	(Math.round(parseFloat(edited[id.html()].blonnik*(newValue/edited[id.html()].masa_porcji))*100)/100);
		
	}else if(jednostka=='porcja')
	{
		kj.html(Math.round(parseFloat(edited[id.html()].kj*newValue)*100)/100);
		kcal.html(Math.round(parseFloat(edited[id.html()].kcal*newValue)*100)/100);
		bialko.html(Math.round(parseFloat(edited[id.html()].bialko*newValue)*100)/100);
		tluszcz.html(Math.round(parseFloat(edited[id.html()].tluszcz*newValue)*100)/100);
		weglowodany.html(Math.round(parseFloat(edited[id.html()].weglowodany*newValue)*100)/100);
		choles.html(Math.round(parseFloat(edited[id.html()].choles*newValue)*100)/100);
		blonnik.html(Math.round(parseFloat(edited[id.html()].blonnik*newValue)*100)/100);
	}
	
}
