/**
 * Description.
 * @module co2calc
 * @requires jQuery, tbelt, json
*/

(function($j, $tb) {

    /**
    * A Widget extension allowing the user to calculate their CO2 output based on their air travel data.
    * @class AirTravel
    * @namespace co2calc.widget
    * @extends co2calc.widget.Widget
    */
    co2calc.widget.AirTravel = (function() {

        /**
        * Employs the Widget constructor.
        * @constructor
        * @param opts {Object} {required} An object whose properties are copied to the Household instance.
        */
        function AirTravel(opts) {
            var I = this;
            co2calc.widget.Widget.apply(I, arguments);

        }

        $j.extend(AirTravel.prototype, co2calc.widget.Widget.prototype,
        {
            /**
             * Overrides Widget.name;
             * @property name
             * @type String
             * @default "AirTravel"
             */
			name: "AirTravel"

        });

        return AirTravel;

    })();


    /**
    * AirTravelViewQuick Class Description
    * @namespace co2calc.widget
    * @class AirTravelViewQuick
    * @extends co2calc.widget.WidgetView
    */
    co2calc.widget.AirTravelViewQuick = (function() {

        /**
        * Description.
        * @constructor
        * @param opts {Object} {required} An object whose properties are copied to the AirTravelViewQuick instance.
        */
        function AirTravelViewQuick(opts) {
            var I = this;
            co2calc.widget.WidgetView.apply(I, arguments);
            //console.log(I.calculate);
        }

        $j.extend(true, AirTravelViewQuick.prototype, co2calc.widget.WidgetView.prototype,
        {
			name: "Quick",
			
			data:{
				flightHours:new co2calc.widget.WidgetViewDataItem({
					typical: function() { return co2calc.dataSet.TypicalValues.AirFlightHours; }	
				})
			},
			
			init:function(){
				var I = this;
				co2calc.widget.WidgetView.prototype.init.apply(I, arguments);
				
				
				
			},

            calculate: function(isAverage) {
                var I = this,
				co2Total = 0.00;
				
				if(isAverage && I.staticAverage>-1) return I.staticAverage;
				
				try{
					// STEPHEN!!!:  Set required variables below.  I have just used the typical passed in dataset:
		            var dataHours = (isAverage) ? I.data.flightHours.typical() : I.data.flightHours.user();
		            // End of required variables
					
					if (!isAverage && I.data.flightHours.element.value == "") {
						//calculation is 0 if user has not entered a value
					} else {
					
						// If hours is over 0 then add in flightCO2
						
						if (dataHours > 0) {
							co2Total += co2calc.dataSet.TypicalValues.FlightCO2lbs;
							
							// Add AirTravelHours
							co2Total += (dataHours - co2calc.dataSet.TypicalValues.AirFlightHours) * co2calc.dataSet.Adjustments.AirTravel;
						}
						
						//Convert to tonnes
						co2Total = co2calc.toTons(co2Total);
					}
	            
            	}catch(err){
					trace(I.className+".calculate("+(isAverage||false)+"): "+err);
				};
				
				if(co2Total==null||co2Total==undefined) co2Total = (isAverage) ? I.average : I.total;
				else if(isAverage) I.average = co2Total;
				else I.total = co2Total;
				
				I.jI.trigger(I.className+".calculate", (isAverage||false));	

            	return co2Total;

				
	
            }
        });

        return AirTravelViewQuick;

    })();




})(jQuery, tbelt);





