var GasCO2 = 19.546544325;
var DieselCO2 = 22.1122825;

var FleetSize = $('input#val').attr('value');
var AverageMPG = $('input#val2').attr('value');
var AnnualMiles = $('input#val3').attr('value');
var PricePerGallon = $('input#val4').attr('value');




var FuelConsumption = 0;
var CO2Emissions = 0;

var FuelUseReduceA = 0;
var FuelUseReduceB = 0;
var FuelUseReduceC = 0;

var FuelSavingsA = 0;
var FuelSavingsB = 0;
var FuelSavingsC = 0;

var CO2ReduceA = 0;
var CO2ReduceB = 0;
var CO2ReduceC = 0;

var FleetFuelSavingsA = 0;
var FleetFuelSavingsB = 0;
var FleetFuelSavingsC = 0;

var FleetCO2ReduceA = 0;
var FleetCO2ReduceB = 0;
var FleetCO2ReduceC = 0;


function calculateGasolineSavings()
{
	FuelConsumption = AnnualMiles / AverageMPG;
	FuelCosts 		= FuelConsumption * PricePerGallon;
	CO2Emissions 	= FuelConsumption * GasCO2;

	FuelUseReduceA = FuelConsumption - (AnnualMiles /(AverageMPG * (0.05 + 1)));
	FuelUseReduceB = FuelConsumption - (AnnualMiles /(AverageMPG * (0.10 + 1)));
	FuelUseReduceC = FuelConsumption - (AnnualMiles /(AverageMPG * (0.15 + 1)));

	//Individual		
	FuelSavingsA = FuelUseReduceA * PricePerGallon;
	FuelSavingsB = FuelUseReduceB * PricePerGallon;
	FuelSavingsC = FuelUseReduceC * PricePerGallon;

	CO2ReduceA = FuelUseReduceA * GasCO2;
	CO2ReduceB = FuelUseReduceB * GasCO2;
	CO2ReduceC = FuelUseReduceC * GasCO2;		

	IFSKA = Math.round((FuelSavingsA));
	IFSKB = Math.round((FuelSavingsB));
	IFSKC = Math.round((FuelSavingsC));

	$('#iat10value').html("$" + addCommas(IFSKA) + "");
	$('#iat50value').html("$" + addCommas(IFSKB) + "");
	$('#iat100value').html("$" + addCommas(IFSKC) + "");	

	ICOPA = Math.round((CO2ReduceA));
	ICOPB = Math.round((CO2ReduceB));
	ICOPC = Math.round((CO2ReduceC));

	$('#icat10value').html(addCommas(ICOPA) + "");
	$('#icat50value').html(addCommas(ICOPB) + "");
	$('#icat100value').html(addCommas(ICOPC) + "");	

	//Fleet
	FleetFuelSavingsA = FuelSavingsA * FleetSize;
	FleetFuelSavingsB = FuelSavingsB * FleetSize;
	FleetFuelSavingsC = FuelSavingsC * FleetSize;

	FleetCO2ReduceA = CO2ReduceA * FleetSize;
	FleetCO2ReduceB = CO2ReduceB * FleetSize;
	FleetCO2ReduceC = CO2ReduceC * FleetSize;

	FSKA = Math.round((FleetFuelSavingsA));
	FSKB = Math.round((FleetFuelSavingsB));
	FSKC = Math.round((FleetFuelSavingsC));

	COPA = Math.round((FleetCO2ReduceA));
	COPB = Math.round((FleetCO2ReduceB));
	COPC = Math.round((FleetCO2ReduceC));

	$('#ffat10value').html("$" + addCommas(FSKA) + "");
	$('#ffat50value').html("$" + addCommas(FSKB) + "");
	$('#ffat100value').html("$" + addCommas(FSKC) + "");	

	$('#fcat10value').html(addCommas(COPA) + "");
	$('#fcat50value').html(addCommas(COPB) + "");
	$('#fcat100value').html(addCommas(COPC) + "");	

} // calculateGasolineSavings

function calculateDieselSavings()
{
	FuelConsumption	= AnnualMiles / AverageMPG;
	FuelCosts 		= FuelConsumption * PricePerGallon;
	CO2Emissions 	= FuelConsumption * DieselCO2;

	FuelUseReduceA = FuelConsumption - (AnnualMiles / (AverageMPG * (0.05 + 1)));
	FuelUseReduceB = FuelConsumption - (AnnualMiles / (AverageMPG * (0.10 + 1)));
	FuelUseReduceC = FuelConsumption - (AnnualMiles / (AverageMPG * (0.15 + 1)));

	//Individual		
	FuelSavingsA = FuelUseReduceA * PricePerGallon;
	FuelSavingsB = FuelUseReduceB * PricePerGallon;
	FuelSavingsC = FuelUseReduceC * PricePerGallon;

	CO2ReduceA = FuelUseReduceA * DieselCO2;
	CO2ReduceB = FuelUseReduceB * DieselCO2;
	CO2ReduceC = FuelUseReduceC * DieselCO2;

	IFSKA = Math.round((FuelSavingsA));
	IFSKB = Math.round((FuelSavingsB));
	IFSKC = Math.round((FuelSavingsC));

	$('#iat10value').html("$" + addCommas(IFSKA) + "");
	$('#iat50value').html("$" + addCommas(IFSKB) + "");
	$('#iat100value').html("$" + addCommas(IFSKC) + "");	

	ICOPA = Math.round((CO2ReduceA));
	ICOPB = Math.round((CO2ReduceB));
	ICOPC = Math.round((CO2ReduceC));

	$('#icat10value').html(addCommas(ICOPA) + "");
	$('#icat50value').html(addCommas(ICOPB) + "");
	$('#icat100value').html(addCommas(ICOPC) + "");

	//Fleet
	FleetFuelSavingsA = FuelSavingsA * FleetSize;
	FleetFuelSavingsB = FuelSavingsB * FleetSize;
	FleetFuelSavingsC = FuelSavingsC * FleetSize;

	FleetCO2ReduceA = CO2ReduceA * FleetSize;
	FleetCO2ReduceB = CO2ReduceB * FleetSize;
	FleetCO2ReduceC = CO2ReduceC * FleetSize;

	FSKA = Math.round((FleetFuelSavingsA));
	FSKB = Math.round((FleetFuelSavingsB));
	FSKC = Math.round((FleetFuelSavingsC));
	
	COPA = Math.round((FleetCO2ReduceA));
	COPB = Math.round((FleetCO2ReduceB));
	COPC = Math.round((FleetCO2ReduceC));

	$('#ffat10value').html("$" + addCommas(FSKA) + "");
	$('#ffat50value').html("$" + addCommas(FSKB) + "");
	$('#ffat100value').html("$" + addCommas(FSKC) + "");	

	$('#fcat10value').html(addCommas(COPA) + "");
	$('#fcat50value').html(addCommas(COPB) + "");
	$('#fcat100value').html(addCommas(COPC) + "");

} //calculateDieselSavings

function showFleetChart()
{

	$('#results-nav').show();

    $('#calc_header').hide();
	
	$('#thesliders').hide();

	$('#results-nav').html("<p class=\"left\"><a href=\"javascript:void(0)\" class=\"goback\" onclick=\"showSliders()\">Go Back</a></p><div class=\"results-tabs\"><ul id=\"calc-tabs\"><li id=\"btn_fleet\"><span class=\"wlk_distext\">fleet</span></li><li id=\"btn_indiv\" class=\"has_anchor\"><a href=\"javascript:void(0)\" onclick=\"showIndividualChart()\" title=\"Individual\"><span class=\"wlk_distext\">individual</span></a></li></ul></div>");

	$('#article_text_1').hide();

	$('#article_text_2').show();	
	

	if ($('#typeCar').attr("checked"))
	{
		calculateGasolineSavings();
	}
	else if ($('#typeCar2').attr("checked"))
	{
		calculateDieselSavings();	
	}

} // showFleetChart


function showIndividualChart()
{
	$('#results-nav').show();
	
	$('#thesliders').hide();

	$('#results-nav').html("<p class=\"left\"><a href=\"javascript:void(0)\" class=\"goback\" onclick=\"showSliders()\">Go Back</a></p><div class=\"results-tabs\"><ul id=\"calc-tabs\"><li id=\"btn_fleet\" class=\"has_anchor\"><a href=\"javascript:void(0)\" onclick=\"showFleetChart()\" title=\"Individual\"><span class=\"wlk_distext\">fleet</span></a></li><li id=\"btn_indiv\"><span class=\"wlk_distext\">individual</span></li></ul></div>");

	$('#article_text_2').hide();

	$('#article_text_1').show();	

	if ($('#typeCar').attr("checked"))
	{
		calculateGasolineSavings();
	}
	else if ($('#typeCar2').attr("checked"))
	{
		calculateDieselSavings();	
	}
} // showIndividualChart


function showSliders()
{
	$('#results-nav').hide();

    $('#calc_header').show();

	$('#thesliders').show();
	
	$('#results-nav').html("");

	$('#article_text_2').hide();	

	$('#article_text_1').hide();
} // showSliders


function roundNumber(num, dec) 
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
} // roundNumber


function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
} // addCommas


$(window).load(function() { // stuff that goes wrong if executed while elements are still loading 

        if(!$('#sidebar_calculator')) return;
	// Create the new slider instance

$('#bar').slider({
    min: 10,
    max: 10000,
    step: 100,
    orientation: 'horizontal',
    slide: function(event,ui) {
        FleetSize = ui.value
        $('#val').attr('value', FleetSize);
        }
});

$('#bar2').slider({
    min: 5,
    max: 40,
    orientation: 'horizontal',
    slide: function(event,ui) {
        AverageMPG = ui.value
        $('#val2').attr('value', AverageMPG);
        }
});

$('#bar3').slider({
    min: 5000,
    max: 40000,
    step: 1000,
    orientation: 'horizontal',
    slide: function(event,ui) {
        AnnualMiles = ui.value
        $('#val3').attr('value', AnnualMiles);
        }
});

$('#bar4').slider({
    min: 1.00,
    max: 6.00,
    step: .05,
    orientation: 'horizontal',
    slide: function(event,ui) {
        PricePerGallon = ui.value.toFixed(2)
        $('#val4').attr('value', PricePerGallon);
        }
});

$('#bar').slider('value', FleetSize);
$('#bar2').slider('value', AverageMPG);
$('#bar3').slider('value', AnnualMiles);
$('#bar4').slider('value', PricePerGallon);

$('#val').live('change', function(event) {
    FleetSize = this.value;
    $('#bar').slider('value', this.value);
});

$('#val2').live('change', function(event) {
    AverageMPG = this.value;
    $('#bar2').slider('value', this.value);
});

$('#val3').live('change', function(event) {
    AnnualMiles = this.value;
    $('#bar3').slider('value', this.value);
});

$('#val4').live('change', function(event) {
    PricePerGallon = this.value;
    $('#bar4').slider('value', this.value);
});


        


 
    

});

