var memb_pos = new Array();
memb_pos[0] = '0px -272px';
memb_pos[1] = '0px -272px';
memb_pos[2] = '0px -272px';
memb_pos[3] = '0px -272px';
memb_pos[4] = '0px -272px';

var use_pos = new Array();
use_pos[0] = '0px -272px';
use_pos[1] = '0px -272px';
use_pos[2] = '0px -272px';
use_pos[3] = '0px -272px';
use_pos[4] = '0px -272px';

$j = jQuery.noConflict();

function pad(number,length) {
    var str = '' + number;
    while (str.length < length)
        str = '0' + str;
    return str;
}

(function($j) {
	$j.extend($j.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $j.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
            
           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);

$j(function () {

	getFPData();
	setInterval("getFPData()", 30000);

	$j('#taf-button').bind('click', function() {
				
		if ($j("#hp-taf-email").val().match(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/)) {
			$data = "callback=?&email=" + $j("#hp-taf-email").val();
			$j.ajax({
			    url: '/data/taf.json',
			    type: 'POST',
			    dataType: 'json',
				data: $data,
			    timeout: 2500,
				error: function(json) {
					alert ("Sorry - unable to send an email at this time.");
				},
			    success: function(json) {
					alert ("We've sent an email to your friend. Thanks for spreading the word.");
					$j("#hp-taf-email").val("");
				}
			});
			
		} else {
			alert ("That doesn't look like a real Email address. Please try again.");
		}
		return false;
	});

});

function getFPData() {

	var txp;

	$data = "callback=?";
	$j.ajax({
	    url: 'http://status2.energyhive.co.uk/fp_data.json',
	    type: 'GET',
	    dataType: 'json',
		data: $data,
	    timeout: 2500,
	    success: function(json) {
			if (json.fp_data.members > 99999) {	json.fp_data.members = 99999;
			} else { json.fp_data.members = pad(json.fp_data.members, 5); }
			
			var watt_watt = Math.round( (json.fp_data.members * json.fp_data.avg) / 1000 ); 
			
			if (watt_watt > 99999) { watt_watt = 99999;
			} else { watt_watt = pad(watt_watt, 5); }
			
			for (i=0; i<5; i++) {
				
				txp = -272 + (json.fp_data.members.charAt(i) * 28);
			
				$j("#mem"+i)
					.stop()
					.css({backgroundPosition: memb_pos[i]})
					.animate(
							{backgroundPosition: "(0px " + txp + "px)"}, 
							{duration:2500});
							
				memb_pos[i] = '0px ' + txp + 'px';			
				
				txp = -272 + (watt_watt.charAt(i) * 28);
			
				$j("#use"+i)
					.stop()
					.css({backgroundPosition: use_pos[i]})
					.animate(
							{backgroundPosition: "(0px " + txp + "px)"}, 
							{duration:2500});
							
				use_pos[i] = '0px ' + txp + 'px';
				
			}

			if (json.fp_data.min > 1000) { $j('#low-val').html(Math.round(json.fp_data.min/10)/100 + "kW"); }
			else { $j('#low-val').html(json.fp_data.min + " W");}
			
			if (json.fp_data.min > 5000) { yc_pc = 300; } 
			else if ( json.fp_data.min < 0 ) { yc_pc = 0; }
			else { yc_pc = Math.round( ( json.fp_data.min / 5000 ) * 300 ); }
			
			$j('#slide1').animate({'width': + yc_pc + 'px'}, 1000);

			if (json.fp_data.avg > 1000) { $j('#avg-val').html(Math.round(json.fp_data.avg/10)/100 + "kW"); }
			else { $j('#avg-val').html(json.fp_data.avg + " W");}
			
			if (json.fp_data.avg > 5000) { yc_pc = 300; } 
			else if ( json.fp_data.avg < 0 ) { yc_pc = 0; }
			else { yc_pc = Math.round( ( json.fp_data.avg / 5000 ) * 300 ); }
			
			$j('#slide2').animate({'width': + yc_pc + 'px'}, 1000);

			if (json.fp_data.max > 1000) { $j('#high-val').html(Math.round(json.fp_data.max/10)/100 + "kW"); }
			else { $j('#high-val').html(json.fp_data.max + " W");}
			
			if (json.fp_data.max > 5000) { yc_pc = 300; } 
			else if ( json.fp_data.max < 0 ) { yc_pc = 0; }
			else { yc_pc = Math.round( ( json.fp_data.max / 5000 ) * 300 ); }
			
			$j('#slide3').animate({'width': + yc_pc + 'px'}, 1000);

	}
	});
	

}
