﻿
//--------------------------------------
//	General stats
//--------------------------------------
		var KWH_PER_BLOCK					= 100; 			// Monthly

//--------------------------------------
//	CO2 stats
//--------------------------------------
		var LBS_CO2_PER_KWH					= 1.21834;

//--------------------------------------
//	Car stats
//--------------------------------------
		var CAR_AVG_MILES_PER_YEAR			= 11856;		// Per car per year
		var CAR_AVG_MPG						= 19.7;
		var CAR_LBS_CO2_PER_GALLON			= 19.42605;

//--------------------------------------
//	Tree stats
//--------------------------------------
		var TREES_PER_KWH					= 0.052514655;
		
//--------------------------------------
//	State usage averages
//--------------------------------------
		var ST_AVG_KWH_CA					= 986.257424;
		var ST_AVG_KWH_ID					= 1085.926338;
		var ST_AVG_KWH_OR					= 950.0;
		var ST_AVG_KWH_UT					= 791.3998965;
		var ST_AVG_KWH_WA					= 1324.341337;
		var ST_AVG_KWH_WY					= 846.005306;


//--------------------------------------
//	Pricing
//--------------------------------------
		var PRICE_PER_BLOCK					= 1.95;
		var PRICE_PER_KWH					= 0.012;
		var PRICE_PER_QS_BLOCK				= 0.70;
		var PRICE_QS_MONTHLY_FEE			= 125.00;
		var PRICE_HABITAT_FEE				= 2.50;
		
		var MIN_QS_BLOCKS					= 101;
		
//------------------------------------------------------------------------------
//
//	Public Methods
//
//------------------------------------------------------------------------------
		
//--------------------------------------
//	kwhAsCars
//
//	Given a number of kwH, returns the
//	number of cars per year that would
//	produce the equivalent amount of
//	CO2.
//--------------------------------------
		function kwhAsCars(kwh)
		{
			// This uses car emissions for 1 year.
			return kwhAsCO2(kwh) / ( CAR_LBS_CO2_PER_GALLON / CAR_AVG_MPG * CAR_AVG_MILES_PER_YEAR );
		}
//--------------------------------------
//	kwhAsCO2
//
//	Given a number of kwH, returns the
//	number of pounds of CO2 created by 
//	the production of that much energy.
//--------------------------------------
		function kwhAsCO2(kwh)
		{
			return kwh * LBS_CO2_PER_KWH;
		}
		
		
//--------------------------------------
//	kwhAsTrees
//
//	Given a number of kwH, returns the
//	number of trees required to consume
//	the CO2 resulting from the
//	production of that much energy.
//--------------------------------------
		function kwhAsTrees(kwh)
		{
			return kwh * TREES_PER_KWH;
		}
		
		
//--------------------------------------
//	kwhAsMiles
//
//	Given a number of kwH, returns the
//	number of miles driven per year that 
//	would produce the equivalent amount 
//	of CO2.
//--------------------------------------
		function kwhAsMiles(kwh)
		{
			return kwhAsCO2(kwh) / ( CAR_LBS_CO2_PER_GALLON / CAR_AVG_MPG );
		}

//--------------------------------------
//	kwhByBlocks
//
//	Given a number of blocks, returns
//	the equivalent in kwH
//--------------------------------------
		function kwhByBlocks(blocks)
		{
			return blocks * KWH_PER_BLOCK;
		}

//--------------------------------------
//	blocksByKwh
//
//	Given a number of kwH, returns
//	the equivalent in blocks
//--------------------------------------
		function blocksByKwh(kwh)
		{
			return Math.floor(kwh / KWH_PER_BLOCK);
		}
		
//--------------------------------------
//	kwhByUsage
//
//	Returns a percentage of total kwH
//--------------------------------------
		function kwhByUsage(percent, usage)
		{
			return Math.ceil(percent * usage / 100) * 100;
		}
		
//--------------------------------------
//	blocksByCost
//
//	Given a dollar amount, returns the
//	equivalent in blocks, based on 
//	standard pricing.
//--------------------------------------
		function blocksByCost(cost)
		{
			return Math.floor(cost / PRICE_PER_BLOCK);
		}

//--------------------------------------
//	blocksByQSCost
//
//	Given a dollar amount, returns the
//	equivalent in blocks, based on QS
//	pricing.
//
//	If the number of blocks falls below
//	the minimum QS purchase, returns 0.
//--------------------------------------
		function blocksByQSCost(cost)
		{
			var blocks = Math.floor((cost - PRICE_QS_MONTHLY_FEE) / PRICE_PER_QS_BLOCK);
			return blocks >= MIN_QS_BLOCKS ? blocks : 0;
		}
		
		function blocksByQSLevel(level, usage)
		{
			var blocks = 0;
			switch (level)
			{
				case "Supporter" :
				{
					// 7 blocks for over 10000 kwH usage. 4 blocks otherwise.
					blocks = usage >= 10000 ? 7 : 4;
					break;
				}
				case "Champion" :
				{
					// Greater of 10% usage or 10 blocks.
					blocks = Math.max(blocksByKwh(usage * 0.1), 10);
					break;
				}
				case "Visionary" :
				{
					// Greater of 30% usage or 50 blocks.
					blocks = Math.max(blocksByKwh(usage * 0.3), 50);
					break;
				}
			}
			
			return blocks;
		}
		
		function getStateAverage(abbr)
		{
			if (eval("ST_AVG_KWH_" + abbr.toUpperCase())) {
				return eval("ST_AVG_KWH_" + abbr.toUpperCase());
			} else {
				return -1;
			}
		}
		
		function getBlockCost(blocks)
		{
			return blocks * PRICE_PER_BLOCK;
		}
		
		function getQSBlockCost(blocks)
		{
			return blocks >= MIN_QS_BLOCKS ? blocks * PRICE_PER_QS_BLOCK + PRICE_QS_MONTHLY_FEE : 0;
		}
		
		function getUsageCost(kwh, addHabitat)
		{
			addHabitat = (addHabitat == null) ? false : addHabitat;
			return kwh * PRICE_PER_KWH + (addHabitat ? PRICE_HABITAT_FEE : 0);
		}

// used as part of flash page as a simple way to grab url params.
		jQuery.extend({
		  getUrlVars: function(){
		    var vars = [], hash;
		    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		    for(var i = 0; i < hashes.length; i++)
		    {
		      hash = hashes[i].split('=');
		      vars.push(hash[0]);
		      vars[hash[0]] = hash[1];
		    }
		    return vars;
		  },
		  getUrlVar: function(name){
		    return jQuery.getUrlVars()[name];
		  }
		});