/* Constants */

var Protein_energy = 4;
var Lipid_energy = 9;
var Glucide_energy = 4;

/* Global */
var QuickadminClass = Class.create();
var quickadmin;

QuickadminClass.prototype = {

    initialize: function() {
    },

    update_food_qty_and_unit: function(o) {
        var option;
        record_measure = $('record_measure');
        $A(record_measure.options).each(function(opt){
            if (opt.value == o.getAttribute('measure_id')) {
                opt.selected= "selected";
                option = opt;
            }
        });
        quickadmin.set_food_qty_and_unit(option);
    },

    set_food_qty_and_unit: function(o) {
        $('record_quantity').value = o.getAttribute('quantity');
        $('record_unit').value = o.getAttribute('unit');
    },

    get_measures: function(food_id, portion_id) {
        var elementHTML =  'measure_' + portion_id;
        var o_options = "{asynchronous:true, evalScripts:true}"
        new Ajax.Updater(elementHTML,"/quickadmin/meals/get_all_measures?food_id=" + food_id, o_options);
    },

    update_attrs: function(flag) {
        var sumK = 0;
        var sumG = 0;
        var sumL = 0;
        var sumP = 0;
        var pG = 0;
        var pL = 0;
        var pP = 0;
        var sumQty = 0;
        var ratio = 1;
        var decimal= 1;
        var percent_decimal=0;
        var amountInput,measureSelect;
        var kcal,glucides,lipids,proteins,food_qty,measure_qty,amount;

        $$('input.food-input').each(function(o){
            portion_id = o.getAttribute('portion_id');
            unitQuantityInput = $('unit_quantity_' + portion_id);
            amountInput = $('amount_' + portion_id);
            measureSelect = $('measure_' + portion_id);
            if($A(measureSelect.options).size()) {
                glucides = parseFloat(o.getAttribute('glucides'));
                lipids = parseFloat(o.getAttribute('lipids'));
                proteins = parseFloat(o.getAttribute('proteins'));
                food_qty = parseFloat(o.getAttribute('quantity'));
                measure_qty = parseFloat(measureSelect.options[measureSelect.options.selectedIndex].getAttribute('unit_quantity'));
                amount = parseFloat(amountInput.value);

                unitQuantityInput.value = measure_qty;

                if(food_qty && isNaN(amount)) {
                    unitQuantityInput.value = food_qty;
                    amountInput.value = amount = 1;
                } else if(isNaN(amount)) {
                    amount = 0;
                }
                if(food_qty){
                    sumQty += amount * measure_qty;
                    ratio = amount * measure_qty / food_qty;
                }


                if(proteins) sumP += ratio * proteins;  
                if(lipids) sumL += ratio * lipids; 
                if(glucides) sumG += ratio * glucides; 
            }
        });


        if(flag == 'add'){
            o = $('associated_id').options[$('associated_id').options.selectedIndex]
            sumP += parseFloat(o.getAttribute('proteins'));
            sumL += parseFloat(o.getAttribute('lipids'));
            sumG += parseFloat(o.getAttribute('glucides'));
            sumQty += parseFloat(o.getAttribute('quantity'));
        }

        if($$('p.meal-attrs')) {
            $$('p.meal-attrs').each(function(o, index){
                if(o.getAttribute('kcal') != ''){
                    sumP += parseFloat(o.getAttribute('proteins'));
                    sumL += parseFloat(o.getAttribute('lipids'));
                    sumG += parseFloat(o.getAttribute('glucides'));
                    sumQty += parseFloat(o.getAttribute('quantity'));
                }
            });
        }



        sumK = sumP * Protein_energy + sumL * Lipid_energy + sumG * Glucide_energy;
        sumPLG = sumP + sumL + sumG
        if(sumPLG){
            pP = sumP / sumPLG * 100;
            pL = sumL / sumPLG * 100;
            pG = sumG / sumPLG * 100;
        }          
        $('record_kcal').value = sumK.toFixed(0);
        $('record_proteins').value = "" + sumP.toFixed(decimal) + " (" + pP.toFixed(percent_decimal) + " %)";
        $('record_lipids').value = "" + sumL.toFixed(decimal) + " (" + pL.toFixed(percent_decimal) + " %)";
        $('record_glucides').value = "" + sumG.toFixed(decimal) + " (" + pG.toFixed(percent_decimal) + " %)";
    },

    update_food_attrs: function(elem, value, input_name, portion_id) {
        $(input_name).value = value.getAttribute('id');
        elem.setAttribute('kcal', value.getAttribute('kcal'));
        elem.setAttribute('proteins', value.getAttribute('proteins'));
        elem.setAttribute('lipids', value.getAttribute('lipids'));
        elem.setAttribute('glucides', value.getAttribute('glucides'));
        elem.setAttribute('quantity', value.getAttribute('quantity'));
        elem.setAttribute('measure_id', value.getAttribute('measure_id'));
        quickadmin.get_measures(value.getAttribute('id'), portion_id);
    }
}

// START:observer
Event.observe(window, 'load', function() {
    if($('login')) $('login').focus();
    quickadmin = new QuickadminClass();
});
// END:observer

