/* ENERGY CALCULATOR JAVASCRIPT */

jQuery(document).ready(function($) {
	$('body').data({a: 0, b: 0, c: 0, d: 0}); // Set default data values for calculation
		
	function calcEnergySavings() {
		a = $('body').data('a');
		b = $('body').data('b');
		c = $('body').data('c');
		d = $('body').data('d');
		var e = (a-b)*c*d*(24/1000); 
		var energy_savings = e.toFixed(0);
		var cost_savings = e*0.1395;
		if (a != '' && b != '' && c != '' && d != '' && !isNaN(c)) {
			energy_savings = energy_savings+' kWh per year';
			cost_savings = '&pound;'+cost_savings.toFixed(0)+' per year';
		} else if (isNaN(c)) {
			energy_savings = 'Please enter numbers only.';
			cost_savings = '...';
		} else {
			energy_savings = '...';
			cost_savings = '...';
		}
		$('.energy_savings').html(energy_savings);
		$('.cost_savings').html(cost_savings);
		//console.log(a,b,c,d,e);
	}
	$('.calc_col_1 > ul > li').click(function() {
		//$('body').data('a', $('a',this).attr('rel'));
		$('.calc_col_1 > ul > li').removeClass('calc_col_1_hi');
		$(this).addClass('calc_col_1_hi');
		$('body').data('a', $('a',this).attr('rel'));
		calcEnergySavings();
	});
	$('.calc_u').click(function() {
		$('.calc_u').removeClass('calc_u_hi');
		$(this).addClass('calc_u_hi');
		$('body').data('b', $(this).attr('rel'));
		calcEnergySavings();
	});
	$('.glass_area').keyup(function() {
		$('body').data('c', $(this).val());
		calcEnergySavings();
	});
	$('.location').change(function() {
		$('body').data('d', $(this).val());
		calcEnergySavings();
	});
	$('.calc_col_1 > ul > li').hover(
		function () {
			$(this).addClass('calc_col_1_hover');
		}, 
		function () {
			$(this).removeClass('calc_col_1_hover');
		}
	);
	$('.calc_u').hover(
		function () {
			$(this).addClass('calc_u_hover');
		}, 
		function () {
			$(this).removeClass('calc_u_hover');
		}
	);
	
	
	
	
	
	
/* UVALUES CODE */	
	$('.open_calculator').click(function() {
		$(".ucalculator").css("display","block");
		$(".uvalues").css("display","none");
		$('.close_calculator').css("display","block");
	});
	
	$('.close_calculator').click(function() {
		$(".uvalues").css("display","block");
		$(".ucalculator").css("display","none");
		$('.close_calculator').css("display","none");
	});
	
	$('.open_calculator,.close_calculator').hover(
		function () {
			$(this).css('cursor','pointer');
		}, 
		function () {
			$(this).css('cursor','auto');
		}
	);
	

	
	$('.ubutton').hover(
		function () {
			var rel = $(this).attr('rel');
			$(".uhoverpanel").css("background-image","url(/wp-content/plugins/energycalculator/images/uvalue-"+rel+"-hover.png)");
			$(this).css('cursor','pointer');
		}, 
		function () {
			$(".uhoverpanel").css("background-image","none");
			$(this).css('cursor','auto');
		}
	);
	
	$('.ubutton').toggle( // Added click / toggle support for touch devices like iPhone etc
		function () {
			var rel = $(this).attr('rel');
			$(".uhoverpanel").css("background-image","url(/wp-content/plugins/energycalculator/images/uvalue-"+rel+"-hover.png)");
		}, 
		function () {
			$(".uhoverpanel").css("background-image","none");
		}
	);
	
	
});
