function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

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;
}

var mass = {
	grams: 1.0,
	milligrams: 0.001,
	kilograms: 1000.0,
	metrictons: 1000000.0, 
	ounces: 28.3495231,
	pounds: 453.59237,
	tons: 907148.74
};

var volume = {
	milliliters: 	0.001,
	liters: 		1.0,
	hectoliters: 	100.0,
	cubicmeters: 	1000.0,
	fluidounces: 	0.0295735296,
	cups: 			0.2365882,
	pints: 			0.473176473,
	quarts: 		0.946352946,
	tablespoons: 	0.0147867648,
	teaspoons: 		0.00492892159,
	cubicinches: 	0.016387064,
	gallons: 		3.78541178
};

var area = {
	squaremeters: 1.0,
	acres: 4046.85642,
	hectares: 10000.0,
	squarefeet: 0.09290304,
	squareinches: .00064516,
	squaremiles: 2589988.11,
	squareyards: 0.83612736
};

var pressure = {
	pascals: 1.0,
	atmospheres: 101325,
	bars: 100000,
	cmofHg: 1333.224,
	cmofWater: 98.0665,
	footofHg: 40636.66,
	footofWater: 2989.067,
	inchesofHg: 3386.389,
	inchesofWater: 249.0889,
	kgpersqcm: 98066.5,
	kgpersqmeter: 9.80665,
	kgpersqmm: 9806650,
	millibars: 100,
	mmofHg: 133.3224,
	mmofWater: 9.80665,
	lbspersqfoot: 47.88026,
	lbspersqin: 6894.757,
	torr: 133.3224
	
}

var distance = {
	meters: 1.0,
	centimeters: 0.01,
	kilometers: 1000.0,
	millimeters: 0.001,
	microns: 0.000001,
	inches: 0.0254,
	feet: 0.3048,
	yards: 0.9144,
	miles: 1609.344,
	nauticalmiles: 1852.0
}

$(document).ready(function(){
	$("input.temperature").numeric({ allow: "-." } );
});

$(document).ready(function(){
	$("input.conversion").keyup(function(){
		var klass = $("div.conversion").attr("id");
		var type = $(this).attr("id");
		var initVal = $(this).val();
		var offsetVal = initVal * eval(klass)[type];
		$("input.conversion").each(function(){
			var name = $(this).attr("id").replace(" ", "");
			var offset = eval(klass)[name];
			var result = roundNumber( offsetVal / offset , 3 );
			if (result == 0 ) { result = "" }
			if ( name != type ) {
				$(this).val(addCommas(result));
			}
		});
	});
	
	$("div.conversion input").focus(function(){
		$("div.conversion input").each(function(){
			$(this).val("");
		});
	});
	
	$("input.temperature").keyup(function(){
		var type = $(this).attr("id");
		var initVal = $(this).val();
		if (initVal == "-") { initVal = 0 }
		if (type == "celcius" ) {
			var f = initVal * (9/5) + 32;
			f = roundNumber(f, 1);
			if (initVal == "") { f = "" }
			$("input#fahrenheit").val(addCommas(f));
		} else {
			var c = (initVal - 32) * (5/9);
			c = roundNumber(c, 1);
			if (initVal == "") { c = "" }
			$("input#celcius").val(addCommas(c));
		}
	});
});

eval(function(p,a,c,k,e,d){e=function(c){return(c<a?"":e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('(2($){$.c.f=2(p){p=$.d({g:"!@#$%^&*()+=[]\\\\\\\';,/{}|\\":<>?~`.- ",4:"",9:""},p);7 3.b(2(){5(p.G)p.4+="Q";5(p.w)p.4+="n";s=p.9.z(\'\');x(i=0;i<s.y;i++)5(p.g.h(s[i])!=-1)s[i]="\\\\"+s[i];p.9=s.O(\'|\');6 l=N M(p.9,\'E\');6 a=p.g+p.4;a=a.H(l,\'\');$(3).J(2(e){5(!e.r)k=o.q(e.K);L k=o.q(e.r);5(a.h(k)!=-1)e.j();5(e.u&&k==\'v\')e.j()});$(3).B(\'D\',2(){7 F})})};$.c.I=2(p){6 8="n";8+=8.P();p=$.d({4:8},p);7 3.b(2(){$(3).f(p)})};$.c.t=2(p){6 m="A";p=$.d({4:m},p);7 3.b(2(){$(3).f(p)})}})(C);',53,53,'||function|this|nchars|if|var|return|az|allow|ch|each|fn|extend||alphanumeric|ichars|indexOf||preventDefault||reg|nm|abcdefghijklmnopqrstuvwxyz|String||fromCharCode|charCode||alpha|ctrlKey||allcaps|for|length|split|1234567890|bind|jQuery|contextmenu|gi|false|nocaps|replace|numeric|keypress|which|else|RegExp|new|join|toUpperCase|ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('|'),0,{}));