function crc_recalculate()
{
	$.getJSON("/crc_calculate?" + $(":input").serialize(),
        function(calc_results){
			$("#crc_calc_total_emissions").html(calc_results.total_emissions_lbs_co2);
			$("#crc_calc_chart_image").attr("src", "/crc_chart?params=" + calc_results.chartparams);
		});
}

function crc_vehicle_calculation_changed()
{
	/* what was selected? */
	var vehicle_calculation = $("#crc_calculator_form input[name='vehicle_calculation']:checked").val();
	
	/* Store  which selection was made  */
	$("#edit-crc-form-vehicle-sub-mode").val(vehicle_calculation);
	
	/* show/hide the relevant divs */

	$("#vehicle_detailed_" + vehicle_calculation).css("display", "block");
	$(".vehicle_detailed_subform:not(#vehicle_detailed_" + vehicle_calculation + ")").css("display", "none");
	
	crc_recalculate();
}

function car_count_changed()
{
	/* hide or show the vehicle basic car choices */
	var max_cars = Number($("#edit-max-num-cars").val());	
	var num_cars = Number($("#edit-car-count-selection").val());	
	for(var car_index = 0; car_index < max_cars; car_index++){
		car_select_id = "#edit-car-" + car_index ;
		$(car_select_id).css("display", (car_index < num_cars) ? "block" : "none");
	}
	/*
	$("#per_vehicle_table").css("display", (num_cars > 0) ? "block" : "none");
	*/
	
	row_style="table-row";
	table_style="table";
	if (navigator.appName == "Microsoft Internet Explorer") {
		row_style="block";
		table_style="block";
	}
	
	/* vehicle - detailed */
	var max_cars_detailed = Number($("#edit-max-num-cars-detailed").val());	
	var num_cars_detailed = Number($("#edit-car-count-selection-detailed").val());	
	for(var car_index = 0; car_index < max_cars_detailed; car_index++){
/*
		$("#edit-mpg-" + car_index).css("display", (car_index < num_cars_detailed) ? "table-cell" : "none");
		$("#edit-miles-" + car_index).css("display", (car_index < num_cars_detailed) ? "table-cell" : "none");	
*/
		$("#detailed_car_row_" + car_index).css("display", (car_index < num_cars_detailed) ? row_style : "none");
				
	}
	$("#per_vehicle_table").css("display", (num_cars_detailed > 0) ? table_style : "none");
	
	
	crc_recalculate();
}

function primary_calculator_tab_clicked(primary_tab)
{
	/* Store  which tab we are on */
	$("#edit-crc-form-mode").val(primary_tab);

	/* show only the selected primary div */
	$(".crc_primary_tab_content:not(#crc_calc_" + primary_tab + ")").css("display", "none");
	$("#crc_calc_" + primary_tab ).css("display", "block");

	/* mark only the selected primary tab as selected */
	$("#crc_calc_" + primary_tab + "_tab").addClass("selected");
	$("#crc_calc_primary_tabs li:not(#crc_calc_" + primary_tab + "_tab)").removeClass("selected");
	
	/* Get the secondary tab for the selected primary */
	var secondary_tab = $("#edit-crc-form-" + primary_tab + "-mode").val();
	
	/* show only the selected secondary div */
	$(".crc_secondary_tab_content:not(#crc_calc_" + primary_tab + "_" + secondary_tab + ")").css("display", "none");	
	$("#crc_calc_" + primary_tab + "_" + secondary_tab).css("display", "block");
	
	/* mark only the selected secondary tab as selected */
	$("#crc_calc_" + primary_tab + "_" + secondary_tab + "_tab").addClass("selected");
	$(".crc_calc_secondary_tabs :not(#crc_calc_" + primary_tab + "_" + secondary_tab + "_tab)").removeClass("selected");
	
}

function secondary_calculator_tab_clicked(secondary_tab)
{
	/* Get the primary tab */
	var primary_tab = $("#edit-crc-form-mode").val();	

	/* Store which secondary tab we are on */
	$("#edit-crc-form-" + primary_tab + "-mode").val(secondary_tab);
	
	/* show only the selected secondary div */
	$(".crc_secondary_tab_content:not(#crc_calc_" + primary_tab + "_" + secondary_tab + ")").css("display", "none");	
	$("#crc_calc_" + primary_tab + "_" + secondary_tab).css("display", "block");
	
	/* mark only the selected secondary tab as selected */
	$("#crc_calc_" + primary_tab + "_" + secondary_tab + "_tab").addClass("selected");
	$(".crc_calc_secondary_tabs :not(#crc_calc_" + primary_tab + "_" + secondary_tab + "_tab)").removeClass("selected");
		
	/* Need to recalculate, since there may be different values on this tab */
	crc_recalculate();
}

function help_button_clicked(button_id)
{
	$(button_id).css("display", "block");

	var the_width = 400;
	var the_height = 300;
	
	if(button_id == "#flight_distances"){
		the_width = 400;
		the_height = 250;
	} else if(button_id == "#length_matter"){
		the_width = 300;
		the_height = 200;
	} else if(button_id == "#usage_data"){
		the_width = 300;
		the_height = 200;
	}
	
	$(button_id).dialog({ 
	    buttons: { 
	        "Ok": function() { 
				$(button_id).css("display", "none");
	            $(this).dialog("close"); 
	        } 
	    },
		position: [ 50, 100 ],
		width: the_width,
		height: the_height,
		modal:true
	});
}

$(document).ready(function(){

	/* Initialize jquery.bubble */
/*	
	$('div.bubbleInfo').bubble({
		'trigger' : '.trigger', // selector for the trigger element
		'popup' : '.popup', // selector for the actual bubbe (within div.bubble)
		'distance' : 10, // distance in px it will travel
		'hideDelay' : 500, // time before hiding
		'effectTime' : 250 // total time for effect
	});
*/
	/* Initialize jquery.checkbox */
/*	
	$('#crc_calculator_form input[type=checkbox]').checkbox({cls:'jquery-safari-checkbox',empty:'/modules/esource_crc/jquery_checkbox/empty.png'});
*/	
	/* Hide the unused vehicle drop down lists */
	car_count_changed();
	
	/* Simulate a click on the home tab in order to force the system to re-evaluate tab & content visibility */
	primary_calculator_tab_clicked('home');
	
	/* Initialize jquery.combobox */
	/*
	$('#crc_calculator_form select').combobox({
		comboboxContainerClass: "comboboxContainer",
		comboboxValueContentContainerClass: "comboboxValueContainer",
		comboboxValueContentClass: "comboboxValueContent",
		comboboxDropDownClass: "comboboxDropDownContainer",
		comboboxDropDownButtonClass: "comboboxDropDownButton",
		comboboxDropDownItemClass: "comboboxItem",
		comboboxDropDownItemHoverClass: "comboboxItemHover",
		comboboxDropDownGroupItemHeaderClass: "comboboxGroupItemHeader",
		comboboxDropDownGroupItemContainerClass: "comboboxGroupItemContainer"
	});
	*/
	
	/* Initialize jquery.droppy */
	$('#crcmenu').droppy({speed: 1});
	
	/* Initialize jquery cluetip */
	var cluetip_options = {
		showTitle:        false,
		positionBy:       'mouse',
		local:			true,
		leftOffset: 100,
		topOffset: 150,
		tracking: false,
		fx: {             
                      open:       'show', // can be 'show' or 'slideDown' or 'fadeIn'
                      openSpeed:  '300'
		}
	}
	$('div.bubbleInfo').cluetip(cluetip_options);
	
	/* Initialize jquery.typewatch  */
	var options = {
	    callback:crc_recalculate,  
	    wait:750,          // milliseconds
	    highlight:true,     // highlight text on focus
	    enterkey:true     // allow "Enter" to submit data on INPUTs
	}
	$("#crc_calculator_form input[type='text']").typeWatch( options );	

	if (navigator.appName == "Microsoft Internet Explorer") {
		var b_version=navigator.appVersion;
		var version=parseFloat(b_version);
		if(version < 7){
			$("#tab_learn_more, #tab_learn_more .level2").hover(function(){			
				$(".crc_secondary_tab_content select").css({ visibility: "hidden" });
			}, function(){
				$(".crc_secondary_tab_content select").css({ visibility: "visible" });
			});
			
			$(".pcmenu li").hover(
				function(){
					$(this).addClass("iehover");
				}, 
				function(){
					$(this).removeClass("iehover");
				}
			);
		}
	}
});


