SizingCalculator = function ()
{
	this.options = {
		suction : [0, 5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 700],
		discharge : [ 65, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 800, 900, 1000, 1250, 1500, 1750, 2000, 2500, 3000 ]
	}
	
	// e3m3
	this.dataTable = {
		60 : {
			0   : { 65: 8.1,  100: 5.8,  150: 5.0,  200: 4.0,  250: 3.3, 300: 2.4 },
			5   : { 65: 11.7, 100: 8.5,  150: 7.4,  200: 5.5,  250: 4.6, 300: 3.7 },
			10  : { 65: 15.2, 100: 11.1, 150: 8.4,  200: 7.3,  250: 5.9, 300: 4.6 },
			15  : {           100: 13.8, 150: 9.6,  200: 8.2,  250: 7.1, 300: 5.2 },
			20  : {           100: 16.6, 150: 11.6, 200: 9.5,  250: 7.4 },
			30  : {           100: 21.8, 150: 15.4, 200: 11.7, 250: 8.6 },
			40  : {                      150: 19.3, 200: 13.6, 250: 9.8 },
			50  : {                      150: 23.0, 200: 16.4, 250: 11.8 },
			60  : {                      150: 23.8, 200: 18.6, 250: 13.5 },
			70  : {                      150: 27.2, 200: 21.0, 250: 15.1 },
			80  : {                                 200: 23.0, 250: 17.0 },
			90  : {                                 200: 25.5, 250: 17.9 },
			100 : {                                 200: 27.0, 250: 19.0 },
			meta : {
				link :    '/products/60-hp/',
				product : '60 HP'
			}
		}, 
		90 : { 
			0   : { 65: 13.7, 100: 13.3, 150: 10.3, 200: 6.5,  250: 5.6,  300: 4.2 },
			5   : { 65: 19.8, 100: 16.5, 150: 12.6, 200: 9.6,  250: 6.8,  300: 6.5,  350: 5.6 },
			10  : { 65: 25.9, 100: 20.9, 150: 15.8, 200: 11.8, 250: 9.1,  300: 8.8,  350: 7.7,  400: 6.0 },
			15  : {           100: 24.6, 150: 18.2, 200: 11.9, 250: 11.5, 300: 10.2, 350: 8.1,  400: 7.8 },
			20  : {           100: 28.2, 150: 21.8, 200: 14.2, 250: 13.8, 300: 12.3, 350: 9.9,  400: 9.6 },
			30  : {           100: 37.4, 150: 26.3, 200: 19.0, 250: 17.0, 300: 13.9, 350: 11.5, 400: 10.5 },
			40  : {                      150: 32.9, 200: 23.8, 250: 18.1, 300: 17.6, 350: 14.6, 400: 11.7 },
			50  : {                      150: 39.3, 200: 28.6, 250: 21.8, 300: 18.3, 350: 17.8, 400: 14.8 },
			60  : {                      150: 42.0, 200: 30.7, 250: 25.6, 300: 21.5, 350: 18.9, 400: 16.6 },
			70  : {                      150: 47.9, 200: 35.4, 250: 29.3, 300: 24.7, 350: 21.2, 400: 17.9 },
			80  : {                                 200: 39.8, 250: 32.9, 300: 27.9, 350: 23.5, 400: 19.8 },
			90  : {                                 200: 44.2, 250: 36.6, 300: 31.0, 350: 26.2, 400: 22.1 },
			100 : {                                 200: 48.7, 250: 40.2, 300: 34.1, 350: 28.5, 400: 23.8 },
			110 : {                                 200: 53.1, 250: 43.9, 300: 37.2, 350: 31.1, 400: 26.7 },
			120 : {                                            250: 47.6, 300: 40.3, 350: 33.7, 400: 29.3 },
			130 : {                                            250: 51.3, 300: 43.4, 350: 36.3, 400: 32.3 },
			meta : {
				link :    '/products/90-hp/',
				product : '90 HP'
			}
		},
		135 : { 
			0   : { 65: 24.0, 100: 19.2, 150: 14.0, 200: 9.4,  250: 7.8,  300: 6.7 },
			5   : { 65: 31.9, 100: 25.1, 150: 18.9, 200: 13.4, 250: 11.6, 300: 10.2, 350: 8.4 },
			10  : { 65: 39.4, 100: 30.2, 150: 22.9, 200: 16.6, 250: 14.1, 300: 11.9, 350: 11.4, 400: 0.0 },
			15  : {           100: 36.6, 150: 26.2, 200: 20.0, 250: 17.7, 300: 15.0, 350: 13.4, 400: 9.9 },
			20  : {           100: 42.5, 150: 29.9, 200: 21.8, 250: 18.5, 300: 18.0, 350: 15.7, 400: 11.8 },
			30  : {           100: 55.1, 150: 37.7, 200: 29.0, 250: 24.8, 300: 21.9, 350: 17.7, 400: 13.1 },
			40  : {                      150: 47.4, 200: 31.7, 250: 28.8, 300: 24.2, 350: 20.6, 400: 15.9 },
			50  : {                      150: 48.7, 200: 38.1, 250: 33.6, 300: 26.7, 350: 24.8, 400: 20.1 },
			60  : {                      150: 56.8, 200: 44.5, 250: 38.4, 300: 29.7, 350: 29.1, 400: 23.1 },
			70  : {                      150: 64.9, 200: 50.9, 250: 43.6, 300: 34.2, 350: 31.8, 400: 25.7 },
			80  : {                                 200: 57.3, 250: 49.6, 300: 38.6, 350: 35.5, 400: 28.2 },
			90  : {                                 200: 63.6, 250: 55.2, 300: 42.9, 350: 39.2, 400: 30.4 },
			100 : {                                 200: 70.0, 250: 62.0, 300: 47.2, 350: 43.2, 400: 33.5 },
			110 : {                                 200: 76.4, 250: 68.3, 300: 51.5, 350: 47.2, 400: 36.6 },
			120 : {                                            250: 74.8, 300: 55.8, 350: 51.7, 400: 40.5 },
			130 : {                                            250: 81.4, 300: 60.2, 350: 56.3, 400: 44.5 },
			meta : {
				link :    '/products/135-hp/',
				product : '135 HP'
			}
		},
		200 : { 
			0   : { 65: 33.7, 100: 27.9, 150: 22.0, 200: 15.2, 250: 11.9, 300: 9.6 },
			5   : { 65: 44.8, 100: 36.4, 150: 28.9, 200: 21.4, 250: 16.6, 300: 13.1, 350: 12.3 },
			10  : { 65: 55.4, 100: 44.4, 150: 34.4, 200: 26.5, 250: 19.5, 300: 18.0, 350: 14.5, 400: 14.1 },
			15  : {           100: 52.5, 150: 39.8, 200: 30.0, 250: 25.0, 300: 21.6, 350: 18.3, 400: 17.8 },
			20  : {           100: 61.0, 150: 46.6, 200: 33.2, 250: 30.5, 300: 24.5, 350: 22.0, 400: 19.6 },
			30  : {           100: 80.1, 150: 59.0, 200: 42.6, 250: 35.3, 300: 30.3, 350: 27.0, 400: 25.9 },
			40  : {                      150: 71.8, 200: 52.2, 250: 38.9, 300: 34.6, 350: 33.9, 400: 31.6 },
			50  : {                      150: 84.0, 200: 62.4, 250: 46.7, 300: 42.1, 350: 40.0, 400: 35.3 },
			60  : {                      150: 98.3, 200: 72.8, 250: 54.6, 300: 49.6, 350: 44.8, 400: 39.8 },
			70  : {                      150:111.4, 200: 83.9, 250: 62.5, 300: 57.1, 350: 48.9, 400: 42.7 },
			80  : {                                 200: 95.3, 250: 70.4, 300: 64.6, 350: 55.4, 400: 48.4 },
			90  : {                                 200:106.9, 250: 78.4, 300: 71.9, 350: 61.2, 400: 53.5 },
			100 : {                                 200:118.7, 250: 86.3, 300: 79.0, 350: 66.8, 400: 58.4 },
			110 : {                                 200:129.6, 250: 94.3, 300: 86.3, 350: 74.7, 400: 65.8 },
			120 : {                                            250:102.3, 300: 93.5, 350: 80.9, 400: 71.3 },
			130 : {                                            250:110.3, 300:100.8, 350: 88.2, 400: 77.8 },
			meta : {
				link :    '/products/200-hp/',
				product : '200 HP'
			}
		},
		325 : { 
			0   : { 65: 56.6, 100: 45.3, 150: 37.1, 200: 24.1, 250: 19.5, 300: 16.6 },
			5   : { 65: 75.0, 100: 60.6, 150: 48.1, 200: 35.3, 250: 30.4, 300: 24.1, 350: 20.9 },
			10  : { 65: 94.3, 100: 75.9, 150: 58.9, 200: 42.5, 250: 38.2, 300: 30.3, 350: 29.0, 400: 25.2 },
			15  : {           100: 89.2, 150: 68.0, 200: 53.8, 250: 44.2, 300: 38.2, 350: 35.6, 400: 32.6 },
			20  : {           100:104.8, 150: 77.9, 200: 60.6, 250: 49.0, 300: 46.3, 350: 40.7, 400: 36.8 },
			30  : {           100:135.9, 150: 96.3, 200: 73.6, 250: 63.7, 300: 57.2, 350: 48.1, 400: 44.5 },
			40  : {                      150:120.9, 200: 87.8, 250: 76.5, 300: 65.7, 350: 57.8, 400: 51.0 },
			50  : {                      150:141.6, 200:104.8, 250: 88.4, 300: 74.6, 350: 66.0, 400: 60.4 },
			60  : {                      150:167.4, 200:125.2, 250:103.4, 300: 85.2, 350: 72.5, 400: 70.8 },
			70  : {                      150:196.5, 200:144.7, 250:116.1, 300: 96.6, 350: 73.1, 400: 74.2 },
			80  : {                                 200:164.3, 250:133.7, 300:107.6, 350: 94.6, 400: 84.2 },
			90  : {                                 200:184.1, 250:150.1, 300:118.4, 350:105.9, 400: 93.1 },
			100 : {                                 200:203.9, 250:166.8, 300:137.4, 350:117.0, 400:102.0 },
			110 : {                                 200:230.2, 250:182.7, 300:150.1, 350:127.4, 400:110.5 },
			120 : {                                            250:199.4, 300:164.3, 350:138.2, 400:120.9 },
			130 : {                                            250:217.2, 300:179.8, 350:149.0, 400:130.6 },
			meta : {
				link :    '/products/325-hp/',
				product : '325 HP'
			}
		},
		425 : { 
			0   : { 65: 76.5, 100: 61.7, 150: 48.7, 200: 31.5, 250: 24.4, 300: 19.8 },
			5   : { 65: 99.1, 100: 79.3, 150: 64.8, 200: 46.0, 250: 34.5, 300: 29.1, 350: 27.7 },
			10  : { 65:124.6, 100: 97.7, 150: 77.3, 200: 56.2, 250: 47.0, 300: 39.9, 350: 38.5, 400: 32.5 },
			15  : {           100:117.2, 150: 90.4, 200: 64.1, 250: 55.1, 300: 50.7, 350: 45.8, 400: 39.4 },
			20  : {           100:135.9, 150:103.4, 200: 73.8, 250: 63.0, 300: 61.6, 350: 52.3, 400: 44.7 },
			30  : {           100:178.2, 150:131.1, 200: 98.6, 250: 84.9, 300: 73.3, 350: 62.1, 400: 60.5 },
			40  : {                      150:160.5, 200:118.0, 250:101.5, 300: 85.2, 350: 77.9, 400: 75.2 },
			50  : {                      150:190.7, 200:138.3, 250:116.0, 300: 96.6, 350: 93.3, 400: 81.9 },
			60  : {                      150:218.1, 200:161.1, 250:134.4, 300:112.2, 350:104.1, 400: 95.4 },
			70  : {                      150:251.9, 200:187.3, 250:150.6, 300:128.8, 350:113.3, 400: 100.7 },
			80  : {                                 200:214.9, 250:173.6, 300:145.5, 350:126.6, 400: 113.9 },
			90  : {                                 200:243.8, 250:195.4, 300:162.2, 350:139.6, 400: 124.2 },
			100 : {                                 200:274.6, 250:216.9, 300:178.6, 350:154.1, 400: 137.2 },
			110 : {                                 200:300.3, 250:239.3, 300:194.8, 350:170.6, 400: 150.3 },
			120 : {                                            250:262.2, 300:211.2, 350:184.9, 400: 165.2 },
			130 : {                                            250:285.6, 300:227.6, 350:201.5, 400: 178.2 },
			meta : {
				link :    '/products/425-hp/',
				product : '425 HP'
			}
		},
		637 : { 
			0   : { 65:107.2, 100: 90.9, 150: 70.6, 200: 55.4, 250: 39.4, 300: 36.2 },
			5   : { 65:147.7, 100:118.4, 150: 89.2, 200: 72.7, 250: 59.2, 300: 52.4 },
			10  : { 65:184.6, 100:145.2, 150:117.6, 200: 82.7, 250: 79.2, 300: 62.1, 350: 38.5, 400: 37.3 },
			15  : {           100:169.2, 150:133.1, 200:100.1, 250: 89.5, 300: 69.5, 350: 49.3, 400: 48.0 },
			20  : {           100:199.9, 150:157.6, 200:120.1, 250: 98.0, 300: 78.9, 350: 60.1, 400: 58.8 },
			30  : {           100:262.1, 150:197.1, 200:150.1, 250:122.1, 300: 96.3, 350: 81.7, 400: 80.3 },
			40  : {                      150:241.1, 200:177.5, 250:146.9, 300:113.9, 350:103.5, 400:102.0 },
			50  : {                      150:286.2, 200:207.5, 250:169.5, 300:143.3, 350:125.2, 400:123.7 },
			60  : {                      150:323.3, 200:241.1, 250:194.0, 300:167.0, 350:147.1, 400:145.5 },
			70  : {                      150:379.9, 200:281.0, 250:220.6, 300:187.1, 350:169.1, 400:150.6 },
			80  : {                                 200:323.9, 250:250.1, 300:208.8, 350:191.1, 400:170.3 },
			90  : {                                 200:367.4, 250:283.1, 300:232.1, 350:213.2, 400:190.1 },
			100 : {                                 200:412.8, 250:317.3, 300:257.8, 350:235.3, 400:205.3 },
			110 : {                                 200:461.1, 250:352.8, 300:284.7, 350:257.5, 400:224.8 },
			120 : {                                            250:389.3, 300:312.5, 350:279.1, 400:244.4 },
			130 : {                                            250:428.4, 300:341.4, 350:300.8, 400:269.6 },
			meta : {
				link :    '/products/637-hp/',
				product : '637 HP'
			}
		},
		2000 : {
			meta : {
				link : '/products/637-2000-hp/',
				product : '2000 HP'
			}
		}
	}
	return true;
}

SizingCalculator.prototype = {
	//conversionRate: 35.31, //e3m3 to mcf
	//conversionRate: 0.035494, //e3m3 to mmcf
	conversionRate: 0.03531, //e3m3 to mmcf
	errors: ['Sorry, your choosen conditions are outside the settings of our sizing tool.'],
	findCompressor : function (suction, discharge, flow, flow_unit) {
		var dt = this.dataTable;
		
		// convert flow rate 
		if (flow_unit == 'mmcf') {
			flow = flow / this.conversionRate;
		}
		//console.log(flow_unit, flow);
		
		try {
			for (obj in dt)
			{
				var comp = dt[obj];
				if ((comp[suction] != undefined) && (comp[suction][discharge] != undefined))
				{
					if (parseFloat(flow) <= parseFloat(comp[suction][discharge]))
					{
						return { 
							status: 'ok',
							link : comp.meta.link,
							product: comp.meta.product
						}
					}
				}
			}
			// try again with a forumla
			//if ((discharge < 300 && suction > 100) || (discharge == 300 && suction > 15) || discharge > 300) {
				
				hp = ((Math.log((discharge + 14.5) / (suction + 14.5)) * 64) * (flow*this.conversionRate));
				
				/*
				// loop the database to find an appropriate compressor
				for (obj in dt) {
					if (parseFloat(hp) <= parseFloat(obj)) {
						var comp = dt[obj];
						return { 
							status: 'ok',
							//link : comp.meta.link,
							product: comp.meta.product
						}						
					}
				}*/
				return { 
					status: 'ln',
					product: Math.round(hp) + ' HP'
				}
			//}
		} catch (e) {
			// console.log(e);
			return {
				status : 'error',
				message: this.errors[0]
			}
		}

		return {
			status : 'error',
			message: this.errors[0]
		}
	}
};

(function($) {
	
	// wrapper to kickstart the calculator
	$.fn.sizingCalculator = function() {
		return $.each(this, function() {
			$.sizingCalculator(this);
		});	
	}
	
	$.sizingCalculator = function(obj) {
		var self = $(obj);
		
		$('<form class="calculator-keypad" onsubmit="return false">' +
			'<p><label>Suction:</label> <span class="suction-psig-value">0</span> psig<br />' +
			'<select class="f_suction" name="f_suction"></select></p>' +
			'<p><label>Discharge:</label> <span class="discharge-psig-value">0</span> psig<br />' +
			'<select class="f_discharge" name="f_discharge"></select></p>' +	
			'<dl class="form-table">' +
				'<dt><label>Flow</label></dt>' +
				'<dd><input type="text" class="f_flow" name="f_flow" /> <select class="f_flow_unit" name="f_flow_unit"><option value="mmcf">MMcf/d</option><option value="e3m3">e3m3/d</option></select></dd>' +
				'<dd><button class="btn_calulate_size" name="btn_calulate_size"><span class="hide">Calculate Size</span></button></dd>' +
			'</dl>' +
			'</form>' +
			'<div class="calculator-display">' +
				'<div class="content-padding">' +
					'<div class="calc_results">' +
						'<!--p>You need a <a href="#">99 HP</a> Gas Compressor.<br /><a href="#">See Detail &raquo;</a></p-->' +
					'</div>' +
				'</div>' +
			'</div>').appendTo(self);		
		
		calc = new SizingCalculator();
		
		// add suction options
		var suction = calc.options.suction;
		for (i=0; i<suction.length; i++)
		{
			var opt = new Option();
		 	$(opt).val(suction[i]).text(suction[i]);
			$('.f_suction', self).append(opt);
		}

		// add discharge options
		var discharge = calc.options.discharge;
		for (i=0; i<discharge.length; i++)
		{
			var opt = new Option();
		 	$(opt).val(discharge[i]).text(discharge[i]);
			$('.f_discharge', self).append(opt);
		}
		
		// convert selects to sliders
		if ($.fn.slider) {
			// f_suction
			var s_select = $('.f_suction', self).hide();
			var s_values = $.map(s_select[0].options, function(n) { return n.value; });
			$('<div class="calc-slider"></div>')
				.slider({
					min: 0,
					max: s_values.length-1,
					step: 1,
					slide: function(event, ui) {
						s_select[0].selectedIndex = ui.value;
						$('.suction-psig-value').text(s_select.val());
					}
				})
				.insertAfter(s_select);
				
			// f_discharge
			var d_select = $('.f_discharge', self).hide();
			var d_values = $.map(d_select[0].options, function(n) { return n.value; });
			$('<div class="calc-slider"></div>')
				.slider({
					min: 0,
					max: d_values.length-1,
					step: 1,
					slide: function(event, ui) {
						d_select[0].selectedIndex = ui.value;
						$('.discharge-psig-value').text(d_select.val());
					}
				})
				.insertAfter(d_select);
		}		

		$('.btn_calulate_size', self).click(function(e) {
			e.preventDefault();
			
			results = calc.findCompressor(
				$('.f_suction', self).val(),
				$('.f_discharge', self).val(),
				$('.f_flow', self).val(),
				$('.f_flow_unit', self).val()
			);

			var val = '';
			switch (results.status) {
				case 'ok' :
					val = '<p>You need a ' + (results.link ? '<a href="' + results.link + '">' : '') + results.product + (results.link ? '</a>' : '') + ' gas compressor.<br />' + (results.link ? '<a href="' + results.link + '">See Detail &raquo;</a>' : '') + '</p>';
					break;
				case 'ln' :
					val = '<p>You require approximately ' + results.product + ' for compression.</p><p style="font-size:1em;">For an exact calculation, please<br /><a href="/contact-sage/">contact Sage</a></p>';
					break;
				default:
					val = '<p class="' + results.status + '">' + results.message + '</p>';
					break;	
			}
			
			$('.calc_results', self)
				.hide()
				.html(val)
				.animate({opacity: 'show'}, 600)
				.closest('.calculator-display')
				.animate({opacity: '1'});

		});	

	}		
	
})(jQuery);