/**
 * TPEventCalc application object
 *
 * Depends on jQuery, jQuery.scrollTo, JSON, and Cookie objects.
 */

var TPEventCalc = {

///////////////
// Constants //
///////////////

EVENT_MODE:      'evt',
WEDDING_MODE:    'wed',

////////////////
// Properties //
////////////////

mode: null,

/////////////
// Methods //
/////////////

init: function( mode ) {

	Number.prototype.toFormattedString = function() {
		var numStr = String(Math.floor(this));
		var displayStr = '';

		for(var pos=0; numStr.length > pos; pos++){

			if( pos != 0 && (numStr.length - pos) % 3 == 0){
				displayStr += ',';
			}
			displayStr += numStr.charAt(pos);
		}

		return displayStr;
	}

	TPEventCalc.mode = mode || TPEventCalc.EVENT_MODE;

	$(document).ready(function(){

		TPEventCalc.loadFootprint();
		TPEventCalc.updateFootprint(true);

		$('#calc_event_footprint').click(function(){
			TPEventCalc.updateFootprint();
			TPEventCalc.saveFootprint();
		});
	});
},

calcFootprint: function() {
	var computedTotal = TPEventCalc.Air.calcFootprint()
	 + TPEventCalc.Road.calcFootprint()
	 + TPEventCalc.Hotel.calcFootprint();
	var baselineTotal = TPEventCalc.Event.calcFootprint();

	if(computedTotal > 0) {
		return computedTotal + baselineTotal;
	} else {
		return 0;
	}
},

updateFootprint: function(noAnimate) {
	var co2Lbs = Math.round( TPEventCalc.calcFootprint() || 0 );
	var roundedCo2Klbs = co2Lbs > 0 ? Math.ceil(co2Lbs / 1000) : 0.

	if(co2Lbs > 0) {

		// Show CO2 total and update the value

		if(noAnimate) {
			$('#total_section').show();
		} else {
			if($('#total_section').is(':hidden') || !$('#total_section').attr('_notFirstTime')) {
				$('#total_section').attr('_notFirstTime', true);
				$('#total_section').slideDown('slow', function() {
					$.scrollTo($(this), 800);
				});
			}
		}
		$('#co2_display').html(co2Lbs.toFormattedString());

		// Select CO2 product SELECT option

		var $options = $('#co2_product option');
		var optLen = $options.length;

		if(roundedCo2Klbs >= $options.eq(optLen - 1).val()) {
			$options.eq(optLen - 1).attr('selected', 'selected');
		} else {
			for(var i=0; i < optLen; i++) {
				if(roundedCo2Klbs <= $options.eq(i).val()) {
					$options.eq(i).attr('selected', 'selected');
					break;
				}
			}
		}
		
		
		
	} else {
		if(noAnimate) {
			$('#total_section').hide();
		} else {
			$('#total_section').slideUp('slow');
		}
		$('#co2_display').html('');
		$('#co2_product option').eq(0).attr('selected', 'selected');
	}
	
	//set the hidden form field:
		document.getElementById('attr4').value=roundedCo2Klbs;
},

saveFootprint: function() {
	var elemIds = ['nshort', 'nmedium', 'nlong', 'ncars', 'triplen', 'nrooms'];
	var numIds = elemIds.length;
	var obj;
	var key, val;

	for(var i=0; i < numIds; i++) {
		key = elemIds[i];
		val = parseInt($('#' + key).val());
		if(!isNaN(val) && val > 0) {
			if(!obj) {
				obj = {};
			}
			obj[key] = val;
		}
	}

	if(obj) {
		return TPEventCalc.User.saveProfile(obj);
	} else {
		return TPEventCalc.User.deleteProfile();
	}
},

loadFootprint: function() {
		var profile = TPEventCalc.User.getProfile();
		if(profile) {
			$('#nshort').val(profile.nshort);
			$('#nmedium').val(profile.nmedium);
			$('#nlong').val(profile.nlong);
			$('#ncars').val(profile.ncars);
			$('#triplen').val(profile.triplen);
			$('#nrooms').val(profile.nrooms);
		}
},

/**
 * TPEventCalc.User sub-object
 */

User: {
	getProfile: function() {
		var name = TPEventCalc.mode + '_calc';
		var val = Cookie.get(name);
		return val ? JSON.parse(val) : false;
	},

	saveProfile: function(profile) {
		var name = TPEventCalc.mode + '_calc';
		return Cookie.set(name, JSON.stringify(profile)) > 0;
	},

	deleteProfile: function(profile) {
		var name = TPEventCalc.mode + '_calc';
		return Cookie.remove(name);
	}
},

/**
 * TPEventCalc.Air sub-object
 */

Air: {

	CO2_LBS_PER_SHORT_FLIGHT:    500,
	CO2_LBS_PER_MEDIUM_FLIGHT:  1250,
	CO2_LBS_PER_LONG_FLIGHT:    2000,

	calcFootprint: function() {
		var numShort = parseInt($('#nshort').val()) || 0;
		var numMed   = parseInt($('#nmedium').val()) || 0;
		var numLong  = parseInt($('#nlong').val()) || 0;
		var co2Air1 = numShort * TPEventCalc.Air.CO2_LBS_PER_SHORT_FLIGHT;
		var co2Air2 = numMed * TPEventCalc.Air.CO2_LBS_PER_MEDIUM_FLIGHT;
		var co2Air3 = numLong * TPEventCalc.Air.CO2_LBS_PER_LONG_FLIGHT;

		return co2Air1 + co2Air2 + co2Air3;
	}
},

/**
 * TPEventCalc.Road sub-object
 */

Road: {

	AVG_MPG:              20,	// Average car MPG
	CO2_LBS_PER_GAL:      19.564,	// Lbs CO2 per gallon

	calcFootprint: function() {
		var numCars  = parseInt($('#ncars').val()) || 0;
		var tripLen  = parseInt($('#triplen').val()) || 0;
		return 2 * numCars * tripLen * TPEventCalc.Road.CO2_LBS_PER_GAL * 1.0 / TPEventCalc.Road.AVG_MPG;
	}
},

/**
 * TPEventCalc.Hotel sub-object
 */

Hotel: {
	CO2_LBS_PER_ROOM:     62.6,	// Lbs CO2 per hotel room per night
	NUM_NIGHTS:           1,	// Number of nights per guest

	calcFootprint: function() {
		var numRooms = parseInt($('#nrooms').val()) || 0;
		return numRooms * TPEventCalc.Hotel.CO2_LBS_PER_ROOM * TPEventCalc.Hotel.NUM_NIGHTS;
	}
},

/**
 * TPEventCalc.Event sub-object
 */

Event: {
	CO2_LBS_PER_SQFT:     0.063,	// Lbs CO2 per square foot of event space
	AVG_SQFT:             2300,	// Average square feet
	NUM_DAYS:             1,	// Number of event days

	calcFootprint: function() {
		if(TPEventCalc.mode == TPEventCalc.WEDDING_MODE) {
			return TPEventCalc.Event.AVG_SQFT * TPEventCalc.Event.CO2_LBS_PER_SQFT * TPEventCalc.Event.NUM_DAYS;
		} else {
			return 0;
		}
	}
}

}
