﻿$(document).ready(function () {
	$('#length-converter').unitConverter({
		sourceUnitsId: 'length-source-units',
		sourceTypeId: 'length-source-type',
		destinationUnitsId: 'length-destination-units',
		destinationTypeId: 'length-destination-type',
		definitionsUrl: 'definitions.xml',
		definitionType: 'length'
	});
	$('#weight-converter').unitConverter({
		sourceUnitsId: 'weight-source-units',
		sourceTypeId: 'weight-source-type',
		destinationUnitsId: 'weight-destination-units',
		destinationTypeId: 'weight-destination-type',
		definitionsUrl: 'definitions.xml',
		definitionType: 'weight'
	});
	$('#weight-calculator').weightCalculator({
		formulaTypeId: 'shape-type',
		materialTypeId: 'material-type',
		dimensionsPlaceHolderId: 'dimensions-placeholder',
		definitionsUrl: 'definitions.xml'
	});
	$('#round-to-square-calculator').roundToSquareCalculator({
		minimumDiameterId: 'minimum-diameter',
		desiredHeightId: 'desired-height',
		desiredWidthId: 'desired-width'
	});
	$('#decimal-to-fraction-converter').decimalToFractionConverter({
		decimalInputId: 'decimal',
		fractionOutputId: 'fraction'
	});
	$('#fraction-to-decimal-converter').fractionToDecimalConverter({
		fractionInputId: 'fraction-input',
		decimalOutputId: 'decimal-output'
	});
	$('#pipe-schedule-lookup-content').xmlLookup({
		definitionsUrl: 'definitions.xml',
		lookup: [
			{
				prompt: 'NPS (Nominal Pipe Size): ',
				selector: 'pipe-schedule > pipe',
				valueAttribute: 'nps',
				display: [
					{ prompt: 'Outer diameter: ', valueAttribute: 'outer-diameter' },
					{ prompt: 'DN (diamètre nominal): ', valueAttribute: 'dn' }
				]
			},
			{
				prompt: 'Schedule: ',
				selector: 'schedule',
				valueAttribute: 'name',
				display: [
					{ prompt: 'Wall thickness: ', valueAttribute: 'wall-thickness' }
				]
			}
		]
	});
	$('#sheet-gauge-lookup-content').xmlLookup({
		definitionsUrl: 'definitions.xml',
		lookup: [
			{
				prompt: 'Sheet Metal Gauge: ',
				selector: 'sheet-metal-gauges > gauge',
				valueAttribute: 'name',
				display: [
					{ prompt: 'Steel Thickness: ', valueAttribute: 'steel-thickness' },
					{ prompt: 'Aluminum Thickness: ', valueAttribute: 'aluminum-thickness' },
					{ prompt: 'Galvanized Steel Thickness: ', valueAttribute: 'galvanized-steel-thickness' }
				]
			}
		]
	});
	
    $(document).ready(function () {
	    $('#temperature-converter').temperatureConverter({
		    definitionsUrl: 'definitions.xml',
		    inputTypeId: 'temperature-from-types',
		    inputValueId: 'temperature-input',
		    outputTypeId: 'temperature-to-types',
		    outputId: 'temperature-output'
	    });
    });	

	var hardnessTypes = [
		{ name: 'Rockwell A', attr: 'rockwellA' },
		{ name: 'Rockwell B', attr: 'rockwellB' },
		{ name: 'Rockwell C', attr: 'rockwellC' },
		{ name: 'Rockwell D', attr: 'rockwellD' },
		{ name: 'Rockwell E', attr: 'rockwellE' },
		{ name: 'Rockwell F', attr: 'rockwellF' },
		{ name: 'Rockwell 15-N', attr: 'rockwell15N' },
		{ name: 'Rockwell 30-N', attr: 'rockwell30N' },
		{ name: 'Rockwell 45-N', attr: 'rockwell45N' },
		{ name: 'Rockwell 30-T', attr: 'rockwell30T' },
		{ name: 'Brinell 3000kg', attr: 'brinell3000' },
		{ name: 'Brinell 500kg', attr: 'brinell500' },
		{ name: 'Vickers 136', attr: 'vickers' },
		{ name: 'Shore', attr: 'shore' },
		{ name: 'Approximate Tensile Strength (psi)', attr: 'psi' }
	];

	var root = $('#hardness-lookup-content');
	for (var hardnessIndex = 0; hardnessIndex < hardnessTypes.length; hardnessIndex++) {
		var destinationHardnessTypes = [];
		for (var destinationHardnessIndex = 0; destinationHardnessIndex < hardnessTypes.length; destinationHardnessIndex++) {
			if (destinationHardnessIndex == hardnessIndex) {
				continue;
			}

			destinationHardnessTypes[destinationHardnessTypes.length] = {
				prompt: hardnessTypes[destinationHardnessIndex].name + ':',
				valueAttribute: hardnessTypes[destinationHardnessIndex].attr
			};
		}

		var sourceHardnessType = hardnessTypes[hardnessIndex];
		var className = 'hardness-lookup-' + sourceHardnessType.attr + '-content';

		root.append('<div class="' + className + '"></div>')
			.find('.' + className)
			.xmlLookup({
				definitionsUrl: 'definitions.xml',
				lookup: [
					{
						prompt: sourceHardnessType.name + ':',
						selector: 'flat-hardness-chart > row',
						valueAttribute: sourceHardnessType.attr,
						display: destinationHardnessTypes
					}
				]
			})
	}
});
