/*--------------------------------------------------------------------------.
|  Software: SolarSaver Calculator                                  		|
|   Version: 1.0                                                            |
|   Contact: sander@lighthousesolar.com      								|                
| ------------------------------------------------------------------------- |
|     Admin: Sander Pick (project admininistrator)                          |
|   Authors: Sander Pick                         							|
| Copyright (c) 2009-Today Lighthouse Solar. All Rights Reserved.       	|                     
| ------------------------------------------------------------------------- |
|   License: Distributed under the General Public License (GPL)             |
|            http://www.gnu.org/licenses/licenses.html#GPL                  |
| This program is distributed in the hope that it will be useful - WITHOUT  |
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or     |
| FITNESS FOR A PARTICULAR PURPOSE.                                         |
'--------------------------------------------------------------------------*/
$(function() {
	$('#calc-form').submit(function() { return false; });
	$("#bill").bind("click",function() { $.scrollTo("#left",500); });
	var running=false; var regex=/^\d{1,7}$/; var down=0; var max=1000; var b=0; var e=0.1; var ppa=0; // e is xcel's cost per kWh
	$("#slider-range-min").slider({
		range: "min",
		value: down,
		min: 0,
		max: max,
		slide: function(event, ui) {
			down = ui.value;
			$("#amount").html('$' + addCommas(down));
			energy();
		}
	});
	$("#amount").html('$' + addCommas(down));
	$("#bill").bind("change",function() { energy(); }); energy();
	//––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
	function energy(){
		if(!running) {
			running = true;
			b = $("#bill").val();
			if(regex.test(b) && b.substring(0,1)!='0' && b.length<4) {
				$("#bill").css("color","#666");
				ppa = new PPA(parseInt(b)*1000/e,0.07,20,down);
				max = Math.round(ppa.worth*.1);
				$("#size").html(Math.round(ppa.power/10)/100 + ' kW');
				$("#price").html('$' + addCommas(Math.round(ppa.worth)));
				$("#slider-range-min").slider('option', 'max', max);
				updatePPA(); updateLease();
			} else { $("#bill").css("color","red"); }
			running = false;
		}
	}
	function updatePPA() {
		var cents = Math.round(ppa.rate*100)/100;
		if(cents==.1) cents = "0.10";
		$("#ppa-rate").html('$' + cents);
		$("#ppa-savings").html('$' + addCommas(ppa.ppaSavings));
	}
	function updateLease() {
		$("#lease-rate").html('$' + Math.round(ppa.fm_pay));
		$("#lease-savings").html('$' + addCommas(ppa.leaseSavings));
	}
	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;
	}
});
