jQuery(document).ready(function() {
	
	jQuery(document.body).append('<div id="solar-calc-result-container"></div>');
	jQuery(document.body).append('<div id="mask"></div>');
	
	var calculator = {
		panelSizes : [6,8,9,10,12,18,30],
		systemSizes : [1050,1400,1560,1750,2010,3150,5250],
		systemCosts : [11560,14080,15650,16230,18980,26370,37680],
		emissionElec : 1.07,
		emissionCar : 0.06,
		kMAverage : 20000,
		treesAbsorption : 20,
		solarCreditMarketValue : 30, // number of $ per credit
		calc : function(pcode,kw,currCost) {
			this.pcode = pcode;
			this.kw = kw;
			this.currCost = currCost;
			this._getZoneMultiplier();
			// make it work like the calc 
			var ret = {};
			ret.postcode = pcode;
			ret.system = 'BE' + kw;
			this._calcMWh();
			
			ret.energyProductionAnnual = (this.battIneff * 1000).toFixed(2);
			ret.energyProductionDaily = (ret.energyProductionAnnual / 365).toFixed(2);
			
			var RECS = Math.floor(this.deemedMW * 15) * this.solarCreditMarketValue;
			ret.credit = this._$(RECS);
			
			this._calcCost();
			ret.cost = this._$(this._nearest(this.sysCost,10));
			ret.nettCost = this._$(this._nearest(this.sysCost - RECS,10));
			
			ret.billSaving = this._$(this.battIneff * 1000 * currCost / 100);
			
			ret.co2Saved = (this.battIneff * this.emissionElec).toFixed(2);
			
			ret.carsOffRoad = Math.ceil((this.battIneff * 1000 * this.emissionElec) / (this.emissionCar * this.kMAverage));
			
			ret.numTrees = Math.ceil(ret.co2Saved * 1000 / this.treesAbsorption);
			
			return ret;
		},
		_calcCost : function() {
			for (var i=0; i<this.systemSizes.length; i++) {
				if (this.systemSizes[i] == this.kw) {
					this.sysCost = this.systemCosts[i];
					break;
				}
			}
		},
		_$ : function(num) {
			return '$' + num.toFixed(2);
		},
		_nearest : function(num,nearestTo) {
			return Math.round(num / nearestTo) * nearestTo;
		},
		_calcMWh : function() {
			var zoneMultiplier = this._getZoneMultiplier();
			var c,rawC = zoneMultiplier * this.kw / 1000;
			if (this.kw > 1500) {
				c = rawC * 5 + zoneMultiplier * (this.kw - 1500) / 1000;
			} else {
				c = Math.floor(rawC) * 5;
			}
			this.deemedMW = c;
			this.battIneff = rawC / 0.9 / 0.9;
		},
		_getZoneMultiplier : function() {
			var zoneMultipliers = [1.622,1.536,1.382,1.185];
			var zone,zoneTable = [0,799,3,800,869,2,870,879,1,880,1000,2,1001,2356,3,2357,2357,2,2358,2384,3,2385,2393,2,2394,2395,3,2396,2398,2,2399,2399,3,2400,2400,2,2401,2404,3,2405,2407,2,2408,2410,3,2411,2414,2,2415,2536,3,2537,2537,4,2538,2544,3,2545,2557,4,2558,2626,3,2627,2629,4,2630,2630,3,2631,2639,4,2640,2820,3,2821,2842,2,2843,2872,3,2873,2874,2,2875,2876,3,2877,2889,2,2890,2897,3,2898,2899,4,2900,2999,3,3000,3390,4,3391,3398,3,3399,3413,4,3414,3426,3,3427,3474,4,3475,3514,3,3515,3516,4,3517,3520,3,3521,3524,4,3525,3538,3,3539,3539,4,3540,3549,3,3550,3560,4,3561,3569,3,3570,3570,4,3571,3606,3,3607,3617,4,3618,3622,3,3623,3628,4,3629,3657,3,3658,3684,4,3685,3687,3,3688,3724,4,3725,3731,3,3732,3999,4,4000,4416,3,4417,4417,2,4418,4422,3,4423,4423,2,4424,4426,3,4427,4473,2,4474,4476,1,4477,4478,2,4479,4485,1,4486,4490,2,4491,4492,1,4493,4499,2,4500,4721,3,4722,4722,2,4723,4723,3,4724,4735,2,4736,4736,1,4737,4824,3,4825,4827,2,4828,4828,3,4829,4829,2,4830,5261,3,5262,5263,4,5264,5270,3,5271,5300,4,5301,5429,3,5430,5450,2,5451,5653,3,5654,5669,2,5670,5679,3,5680,5699,2,5700,5709,3,5710,5722,2,5723,5724,1,5725,5733,2,5734,5799,1,5800,6243,3,6244,6250,4,6251,6254,3,6255,6270,4,6271,6315,3,6316,6357,4,6358,6393,3,6394,6400,4,6401,6430,3,6431,6431,2,6432,6433,3,6434,6439,2,6440,6441,1,6442,6444,3,6445,6459,4,6460,6467,3,6468,6469,2,6470,6471,3,6472,6474,2,6475,6506,3,6507,6555,2,6556,6573,3,6574,6602,2,6603,6607,3,6608,6641,2,6642,6724,1,6725,6750,2,6751,6797,1,6798,6799,2,6800,6999,3,7000,8999,4,9000,9999,3];
			
			// lookup zone in zoneTable using postcode
			for (var i=1; i<zoneTable.length; i+=3) {
				if (this.pcode <= zoneTable[i]) {
					zone = zoneTable[i+1];
					break;
				}
			}
			return zoneMultipliers[zone-1];
		}
	};
	
	
	jQuery.get("http://www.braemacenergy.com.au/javascript/solar-calc-form.html", function(data) {
		jQuery("#calc-form-container").html(data);
		var systemSizes = calculator.systemSizes;
		var systemSelect = jQuery('#solar-system').get(0);
		if (!systemSelect) {
			jQuery("#calc-form-container").html('Solar Calculation form failed to load');
			return;
		}
		var txt;
		for (var i=0; i<systemSizes.length; i++) {
			txt = calculator.panelSizes[i] + ' Panel BE' + systemSizes[i];
			systemSelect.options[i] = new Option(txt, systemSizes[i]);
		}
		var doCalc = function(e) {
			//Cancel the link behavior
			e.preventDefault();
			
			//Get the form to validate
			var frm = jQuery(this).get(0).form;
			
			// filter and convert to number
			var pcode = parseInt(frm.postcode.value.replace(/[^\d]/g,''));
			
			if (isNaN(pcode) || pcode < 0 || pcode > 9999) {
				alert('Please enter a valid post code');
				jQuery(frm.postcode).addClass("error");
				jQuery(frm.postcode).get(0).focus();
				return;
			}
			jQuery(frm.postcode).removeClass("error");
			
			// filter and convert to number
			var currcost = parseInt(frm.currcost.value.replace(/[^\d]/g,''));
			if (isNaN(currcost)) currcost = 0;
			frm.currcost.value = currcost;
			
			var kw = frm.system.value;
			
			var goSolarResult = calculator.calc(pcode,kw,currcost);
			var varToSpanId = {
				postcode:"postcode",
				system:"system",
				cost:"cost",
				credit:"credit",
				nettCost:"nett-cost",
				energyProductionAnnual:"annual-energy",
				energyProductionDaily:"daily-energy",
				billSaving:"bill-saving",
				co2Saved:"co2",
				carsOffRoad:"cars",
				numTrees:"trees"
			}
			var prefix = "#solar-calc-result-";
			for (var v in varToSpanId) {
				jQuery(prefix + varToSpanId[v]).html(goSolarResult[v]);
			}
			jQuery(prefix + 'cars-str').html(goSolarResult.carsOffRoad == 1 ? 'car' : 'cars');
			jQuery(prefix + 'trees-str').html(goSolarResult.numTrees == 1 ? 'tree' : 'trees');
			
			// result window id
			var id = '#solar-calc-result-container';
		
			//Get the screen height and width
			var maskHeight = jQuery(document).height();
			var maskWidth = jQuery(window).width();
		
			//Set height and width to mask to fill up the whole screen
			jQuery('#mask').css({'width':maskWidth,'height':maskHeight});
			
			//transition effect		
			jQuery('#mask').fadeIn(600);	
			//jQuery('#mask').fadeTo("slow",0.8);	
		
			//Get the window height and width
			var winH = jQuery(window).height();
			var winW = jQuery(window).width();
				  
			//Set the popup window to center
			jQuery(id).css('top',  winH/2-jQuery(id).height()/2);
			jQuery(id).css('left', winW/2-jQuery(id).width()/2);
		
			//transition effect
			jQuery(id).fadeIn(600); 
			
		};
		systemSelect.form.onsubmit = doCalc;
	
		jQuery('.do-solar-calc').click(doCalc);
		jQuery.get("http://www.braemacenergy.com.au/javascript/solar-calc-result.html", function(data) {
			jQuery("#solar-calc-result-container").html(data);
			jQuery('.close').click(function (e) {
				//Cancel the link behavior
				e.preventDefault();
				
				jQuery('#mask').hide('fast');
				jQuery('#solar-calc-result-container').hide('fast');
			});		
		});
		
		//if close button is clicked
	});

	//if mask is clicked
	jQuery('#mask').click(function () {
		jQuery(this).hide('fast');
		jQuery('#solar-calc-result-container').hide('fast');
	});			
	
});