/**
 * Description.
 * @module co2calc
 * @requires jQuery, tbelt, json
*/

(function($j, $tb) {

    /**
    * A widget that displays the combined totals/averages from the other Widgets in co2calc.widgets.
    * @namespace co2calc.widget
    * @class Total
    * @extends co2calc.widget.Widget
    */
    co2calc.widget.Total = (function() {

        /**
         * Employs the Widget constructor.
         * @constructor
         * @param opts {Object} An object whose properties are copied into the instance.
         */
        function Total(opts) {
            var I = this;
            co2calc.widget.Widget.apply(I, arguments);

        }

        $j.extend(Total.prototype, co2calc.widget.Widget.prototype,
        {
            /**
             * Overrides Widget.name; 
             * @property name
             * @type String
             * @default "Total"
             */
			name: "Total",
			/**
			 * Returns an empty object, because nothing in this Widget needs to be saved.
			 * @method toJSON
			 */
			toJSON:function(){return {}}

        });

        return Total;

    })();


    /**
    * Represents the Quick View of the Total widget. 
    * @namespace co2calc.widget
    * @class TotalView
    * @extends co2calc.widget.WidgetView
    */
    co2calc.widget.TotalView = (function() {

        /**
        * Employs the WidgetView constructor.
        * @constructor
        * @param {Object} opts {required} An object whose properties are copied to the TotalView instance.
        */
        function TotalView(opts) {
            var I = this;
            co2calc.widget.WidgetView.apply(I, arguments);
            //console.log(I.calculate);
        }

        $j.extend(true, TotalView.prototype, co2calc.widget.WidgetView.prototype,
        {
            /**
             * Overrides WidgetView.name; The name of this TotalView instance.
             * @property name
             * @type String
             * @default "Quick"
             */
			name: "Quick",
			
			/**
			 * Overrides WidgetView.data;
			 * @property data
			 * @type Object
			 * @default {}
			 */
            data: {},
			
			/**
			 * Overrides WidgetView.init(); Employs WidgetView.init(); 
			 * Binds to the Widget.update event of the widgets in co2calc.widgets; 
			 * Removes the callback function for WidgetView.formHandler.onError/onSuccess.
			 * @method init
			 * @return TotalView This TotalView instance.
			 */
            init: function() {
                var I = this, widgetList, widget;
                co2calc.widget.WidgetView.prototype.init.apply(I, arguments);

                I.formHandler.onError = function() { };
                I.formHandler.onSuccess = function() { };
                widgetList = co2calc.widgets.list();
                for (var w = 0; w < widgetList.length; w++) {
                    widget = widgetList[w];
                    if (widget.jI && widget.name != "Total") {
						
						var doWidgetUpdates=function(evt){
							I.widget.update();
						}
						widget.jI.bind(widget.className + ".total", doWidgetUpdates);
						//widget.jI.bind(widget.className + ".average", doWidgetUpdates);
						
					}	
                }
				
				return I;

            },
			
			/**
			 * Overrides WidgetView.calculate(); Calculates the total/average of the other widgets in co2calc.widgets. 
			 * @method calculate
			 * @param isAverage {Boolean} Determines whether to calculate based on the user's values or the typical values.
			 */
            calculate: function(isAverage) {
                var I = this, co2Total = 0.00, 
				widgetList = co2calc.widgets.list(), 
				widget;
               
                //get totals from the other widgets
                for (var w = 0; w < widgetList.length; w++) {
                    widget = widgetList[w];
                    if (widget != I.widget) {
                        co2Total += (isAverage) ? widget.average() : widget.total();
                    }
                }
				if(co2Total==null||co2Total==undefined) co2Total = (isAverage) ? I.average : I.total;
				else if(isAverage) I.average = co2Total;
				else I.total=co2Total;

                return co2Total;

            }
        });

        return TotalView;

    })();




})(jQuery, tbelt);





