$( function() { 
		
		window.$my = {
				energySaved : parseFloat ( $('input[name=energySaved]').val() ),
				energySavedPerSecond : parseFloat ( $('input[name=energySavedPerSecond]').val() ),
				energy_saved : $('#energy-saved'),
				energy_saved_d : [],
				carbon_saved : $('#carbon-saved'),
				carbon_saved_d : [],
				foo: 1,
				used: 0, 
				max: 15,
				'interval': 5000
		}
		
		for ( var i = 0; i < 8; i ++ ) {
			var s = '<span class="digit-' + i + '"></span>';
			window.$my.energy_saved.append ( s );
			window.$my.energy_saved_d[i] = window.$my.energy_saved.find('.digit-' + i);

			window.$my.carbon_saved.append ( s );
			window.$my.carbon_saved_d[i] = window.$my.carbon_saved.find('.digit-' + i);
		}

		window.$my.energy_saved.append ( '<span class="digit-unit"></span>' );
		window.$my.energy_saved.find('.digit-unit').css ( 'background-position', '-264px' );

		window.$my.carbon_saved.append ( '<span class="digit-unit"></span>' );
		window.$my.carbon_saved.find('.digit-unit').css ( 'background-position', '-312px' );

 		energise();
 		setInterval ( 'energise();', 1000 );

		if ( $('#home-images').length > 0 ) {
			$.get( "/home_images?", 
				   function(data){
					   var items = data.split("\n");
				   
					   $.each(items, function ( k, v ) {
							   s = '<div class="home-image" style="background:url(' + v + '); "></div>';
							   $('#home-images').append(s);
						   });

					   $('#home-images .home-image').hide();
					   $('#home-images .home-image:first-child').show();
					   $('#home-images .home-image:first-child').addClass('home-image-active');
					   setInterval ( 'HomeRotate();', 5000 );
					   
				   });
		}

	});


function energise () {
	digitise ( window.$my.energy_saved_d, window.$my.energySaved );
	var carbon = parseFloat(window.$my.energySaved * 0.94);
	digitise ( window.$my.carbon_saved_d, carbon );
	window.$my.energySaved += window.$my.energySavedPerSecond;
}

function digitise( els, val ) {

	val = parseFloat(val);
	if ( val >= 10000 ) {
		val = val.toFixed(1) + '';
	}

	if ( val < 100000 ) {
		val = ' ' + val;
	}

	for ( i = 7; i >= 0; i-- ) {
		var d = val.substr ( i, 1 );
		if ( i == 0 && d == 0 ) {
			els[i].css ( 'background-position', ( '50px 0' ) );
		} else if ( '.' == d ) {
			els[i].css ( 'background-position', ( '-246px 0' ) );
		} else {
			var x = ( d * 24 );
			els[i].css ( 'background-position', ( '-' + x + 'px 0' ) );
		}

	}

}

function HomeRotate() {
 		var e = $('#home-images .home-image-active').next();
		if ( e.length == 0 ) {
				e = $('#home-images .home-image:first-child');
		}

		// only rotate a few times before stopping
		window.$my.used ++;
		if ( window.$my.used > window.$my.max ) {
				clearInterval ( window.$my.interval );
		}

  	$('#home-images .home-image-active').fadeOut( 1000, function() {
				$('#home-images .home-image-active').removeClass('home-image-active');
				$(e).fadeIn( 1000, function() {
						$(e).show();
				});
		    $(e).addClass('home-image-active');
		});

}

