  // Set defaults
  var throw_ratio_min = 0;
  var throw_ratio_max = 0;
  var throw_ratio = 0;
  var destroy = 0;
  var standard_conversion = 39.37;
  var measure_unit = 'Standard';
  var aspect_ratio = '16:9';
  var width_multiplier = .87;
	var height_multiplier = .49;
  var unit = 'in';
  var dimension = 'diagonal';
  var dimension_value = 1;
  var aspect_ratio = '16:9';
  
  $(document).ready(function() {
    //alert(throw_ratio_min);
    //alert(throw_ratio_max);
    //alert(throw_ratio);
    //alert(measure_unit);
    // Check the values from the url to overwrite if necessary
    //var query_array = window.location.search.toString().split("&");
    // alert(query_array);

    // Using jquery.url.js now
    measure_unit = (jQuery.url.param('units') == undefined)?(measure_unit):(jQuery.url.param('units'));
    aspect_ratio = (jQuery.url.param('aspect_ratio') == undefined)?(aspect_ratio):(jQuery.url.param('aspect_ratio'));
    //dimension = (jQuery.url.param('dimension') == undefined)?(dimension):(query_array[3].match(/\w*/));
    //dimension_value = (query_array[3] == undefined)?(dimension_value):(query_array[3].replace(/\w*=/, "") - 0);
    
    // determine what dimension is used by parsing the query string
    var dimensions = Array('diagonal', 'height', 'width');
    var dimension = null;

    var query = jQuery.url.attr('query');
    if (query != null) {
      dimension = jQuery.url.attr('query').match('diagonal');
      
      var cnt = 1;
      while (dimension == null && cnt < 3) {
        dimension = jQuery.url.attr('query').match(dimensions[cnt]);
        cnt++;
      }
    }
    
    dimension = (dimension == null) ? ('diagonal') : (dimension);
    dimension_value = (jQuery.url.param(dimension) == undefined)?(dimension_value):(jQuery.url.param(dimension) - 0);

    //alert(measure_unit);
    //alert(aspect_ratio);
    //alert(dimension);
    //alert(dimension_value);
    
    // If the url contains values, apply the element selections
    if (measure_unit != 'Standard' || measure_unit == '') {
      // Select cm
      $("#calculator-container input.measure-unit[value='Standard']").removeAttr("checked");
      $("#calculator-container input.measure-unit[value='Metric']").attr("checked", "checked");
      unit = 'cm';
    }
    // Select correct aspect ratio
    if (aspect_ratio != '16:9' || aspect_ratio == '') {
      //$("#calculator-container input.aspect-ratio").removeAttr("checked");
      $("#calculator-container input.aspect-ratio[value='"+aspect_ratio+"']").attr("checked", "checked");
    }
    if (dimension != 'diagonal' || dimension == '') {
      // Select the appropriate dimension
      $("#calculator-container input.left[value='diagonal']").removeAttr("checked");
      $("#calculator-container input.left[value='"+dimension+"']").attr("checked", "checked");
    }
    
    if (dimension_value != 1 && dimension_value != 0) // We have already selected a product before
      dimension_value = (measure_unit == "Standard")?(dimension_value / standard_conversion):(dimension_value / 100);
  
    throw_ratio = (throw_ratio_min + throw_ratio_max) / 2;

    if (throw_ratio > 0) {
      $('#slider-callout').hide();
      destroy = 1;
      calculate(dimension_value, dimension);
      var slide_value = (measure_unit == 'Standard')?(($('#diagonal').val() / standard_conversion) * 10):($('#diagonal').val() / 100);
      screenSize(slide_value);
      
      // On click, calculate buttons
      $('.calculate').click(function(){
        var input_select = $('[name=base-measure]:checked').val();
        var value = $('#' + input_select).val();
        destroy = 1;
        if (measure_unit == 'Standard') {
          value = value / standard_conversion;
        } else {
          value = value / 100;
        }
        calculate(value, input_select);
      });
      
      // On click, calculator tabs
      /*
      $('.calculator-tab').click(function() {
      calculator_tab = $(this).val();
      destroy = 1;
      calculate(1, 'diagonal');
      });
      */
      // On click, aspect ratio radios
      $('.aspect-ratio').click(function(){
        aspect_ratio = $(this).val();
        if (aspect_ratio == '16:9') {
          width_multiplier = .87;
          height_multiplier = .49;
        }
        else {
          width_multiplier = .8;
          height_multiplier = .6;
        }
        destroy = 1;
        calculate(dimension_value, dimension);
      });
      
      // On click, unit of measure
      $('.measure-unit').click(function(){
        measure_unit = $(this).val();
        if (measure_unit == 'Metric') {
          unit = 'cm'
        }
        else {
          unit = 'in'
        }
        destroy = 1;
        calculate(dimension_value, dimension);
      });
      
      // Slider functions
      $('#throw-calculator').slider({
        handle: '.ui-slider-handle',
        min: 10,
        max: 100,
        start: function(e, ui){
          $('#slider-callout').fadeIn('fast');
        },
        stop: function(e, ui){
          $('#slider-callout').fadeOut('fast');
        },
        slide: function(e, ui){
          $('#slider-callout').css('left', ((ui.value * .9 - 10) * 2.5) + 1.25);
          if (measure_unit == 'Metric') {
            $('#slider-callout').html(myRound((((ui.value * .1) * width_multiplier) * 100) * throw_ratio, 0) + unit);
          }
          else {
            $('#slider-callout').html(myRound((((ui.value * .1) * width_multiplier) * standard_conversion) * throw_ratio, 1) + unit);
          }
        },
        change: function(e, ui){
          var diagonal = myRound(ui.value * .1, 2);
          calculate(diagonal, 'diagonal');
        }
      });
      
      
      $('#calculator-prompt').html(Drupal.settings.throw_ratio_calc.drag_projector_text);
      $('#calculator-inputs-prompt').html(Drupal.settings.throw_ratio_calc.click_dimension_text);
      $('#slider-prompt').html(Drupal.settings.throw_ratio_calc.proj_dist_range_text);
      $('#projector-disclaimer').html(Drupal.settings.throw_ratio_calc.before_placing_order_text);
    } else {
      $('#projector-disclaimer').html(Drupal.settings.throw_ratio_calc.contact_sim2_sales_text);
    }
  });
  
  // Calculation/display reset function
  function calculate(value, selected){
    var diagonal;
    var width;
    var height;
    var distance;
    var max_field;
    var min_field;
    
    //tabSwitch();
    
    // Field calculations
		if(selected == 'diagonal') {
			diagonal = value;
			width = myRound(value * width_multiplier, 2);
			height = myRound(value * height_multiplier, 2);
			distance = myRound(width * throw_ratio, 2);
      max_field = 1000;
      min_field = 100;
		} else if(selected == 'width') {
			width = value;
			diagonal = myRound(value / width_multiplier, 2);
			height = myRound(diagonal * height_multiplier, 2);
			distance = myRound(width * throw_ratio, 2);
      max_field = myRound(1000 * width_multiplier, 2);
      min_field = myRound(100 * width_multiplier, 2);
		} else if(selected == 'height') {
			height = value;
			diagonal = myRound(value / height_multiplier, 2);
			width = myRound(diagonal * width_multiplier, 2);
			distance = myRound(width * throw_ratio, 2);
      max_field = myRound(1000 * height_multiplier, 2);
      min_field = myRound(100 * height_multiplier, 2);
		} else if(selected == 'distance') {
			distance = value;
			width = myRound(value / throw_ratio, 2);
			diagonal = myRound(width / width_multiplier, 2);
			height = myRound(diagonal * height_multiplier, 2);
      max_field = myRound(1000 * width_multiplier * throw_ratio, 2);
      min_field = myRound(100 * width_multiplier * throw_ratio, 2);
		}
		    
    // Range checking
    if(diagonal > 10 && measure_unit == 'Metric') {
      alert('Your '+selected+' is too high, it is out of range. \n'+max_field+unit+' is the maximum value for '+selected+'.');
      return false;
    } else if(diagonal < 1 && measure_unit == 'Metric') {
      alert('Your '+selected+' is too low, it is out of range. \n'+min_field+unit+' is the minimum value for '+selected+'.');
      return false;
    } else if(diagonal > 10 && measure_unit == 'Standard') {
      alert('Your '+selected+' is too high, it is out of range. \n'+((max_field / 100) * standard_conversion)+unit+' is the maximum value for '+selected+'.');
      return false;
    } else if(diagonal < 1 && measure_unit == 'Standard') {
      alert('Your '+selected+' is too low, it is out of range. \n'+((min_field / 100) * standard_conversion)+unit+' is the minimum value for '+selected+'.');
      return false;
    }
    
    // Set minimum and maximum distances
		//var min_distance = myRound(((10 * .1) * width_multiplier) * throw_ratio, 2);
    //var max_distance = myRound(((100 * .1) * width_multiplier) * throw_ratio, 2);
		if (throw_ratio_min == throw_ratio_max) {
      $('#distance2').css("width","50px");
    } else {
      $('#distance2').css("width","150px");
    }
    // Output
    $('.measure-unit-out').html(unit);
		if (measure_unit == 'Standard') {
      $('#diagonal').attr("value", myRound(diagonal * standard_conversion, 2));
      $('#width').attr("value", myRound(width * standard_conversion, 2));
      $('#height').attr("value", myRound(height * standard_conversion, 2));
      $('#distance').attr("value", myRound(distance * standard_conversion, 2));
      if (throw_ratio_min == throw_ratio_max) {
        $('#distance2').html(myRound(distance * standard_conversion, 2) + unit);
      } else {
        $('#distance2').html(myRound((width * throw_ratio_min * standard_conversion) + 6, 2) + unit + " to " + myRound((width * throw_ratio_max * standard_conversion) - 6, 2) + unit);
      }
      //$('#minimum-distance').html(myRound(min_distance * standard_conversion, 2));
      //$('#maximum-distance').html(myRound(max_distance * standard_conversion, 2));
    } else {
      $('#diagonal').attr("value", myRound(diagonal * 100, 2));
      $('#width').attr("value", myRound(width * 100, 2));
      $('#height').attr("value", myRound(height * 100, 2));
      $('#distance').attr("value", myRound(distance * 100, 2));
      if(throw_ratio_min == throw_ratio_max){
        $('#distance2').html(myRound(distance * 100, 2) + unit);
      } else {
        $('#distance2').html(myRound((width * throw_ratio_min * 100) + ( 6 * .3937), 2)+unit+" to "+ myRound((width * throw_ratio_max * 100) - (6 * .3937), 2)+unit);
      }
      //$('#minimum-distance').html(min_distance);
      //$('#maximum-distance').html(max_distance);
    }
    
    var slide_value = diagonal * 10;
    screenSize(slide_value);
    
    // If display needs to be reset, destroy & rebuild slider with new values
    if (destroy == 1) {
      $('#throw-calculator').slider("destroy");
      $('#throw-calculator').slider({
        startValue: slide_value,
        handle: '.ui-slider-handle',
        min: 10,
        max: 100,
        start: function(e, ui) {
          $('#slider-callout').fadeIn('fast');
        },
        stop: function(e, ui) {
          $('#slider-callout').fadeOut('fast');
        },
        slide: function(e, ui) {
          $('#slider-callout').css('left', ((ui.value*.9 - 10) * 2.5)+1.25);
          if (measure_unit == 'Metric') {
            $('#slider-callout').html(myRound((((ui.value * .1) * width_multiplier) * 100) * throw_ratio, 0) + unit);
          } else {
            $('#slider-callout').html(myRound((((ui.value * .1) * width_multiplier) * standard_conversion) * throw_ratio, 1) + unit);
          }
        },
        change: function(e, ui) {
          var diagonal = myRound(ui.value * .1, 2);
          calculate(diagonal, 'diagonal');
        }
      });
      destroy = 0;
    }
    
  }
  
  // Screen resizing
  function screenSize(value) {
    var height_add;
    if(aspect_ratio == '4:3'){
      height_add = 70;
    } else {
      height_add = 45;
    }
    $("#screen-top").attr("style", function() {
      var screenstyle = "height:"+(myRound((value/6), 0)+9)+"px\; width:"+(myRound(value * .76, 0)+40)+"px";
      return screenstyle;
    })
    $("#screen-main").attr("style", function() {
      var screenstyle = "height:"+(myRound(value * .4, 0)+height_add)+"px\; width:"+(myRound(value * .76, 0)+39)+"px";
      return screenstyle;
    })
    $("#screen-bottom").attr("style", function() {
      var screenstyle = "height:"+(myRound((value/6), 0)+15)+"px\; width:"+(myRound(value * .76, 0)+40)+"px";
      return screenstyle;
    })
    /*
    $("#vertical-bar").attr("style", function() {
      var screenstyle = "height:"+(myRound(value * .3, 0)+140)+"px";
      return screenstyle;
    })
    $("#horizontal-bar").attr("style", function() {
      var screenstyle = "width:"+(myRound(value * .9, 0)+130)+"px";
      return screenstyle;
    })
    */
  }
  
  /*
  // Tabbing
  function tabSwitch(){
    if(calculator_tab == 'projection distance'){
      $('#calculator-prompt').html("At this image size your distance should be:");
      $('#calculator-inputs-prompt').html("Click on the desired dimension, then type in an image size:");
      $('.image-size-inputs').css("visibility", 'hidden');
      $('.projection-distance-inputs').css("visibility", 'visible');
      $('#projection-distance:button').attr("class", "calculator-tab tab-selected");
      $('#image-size:button').attr("class", "calculator-tab tab-unselected");
      $('[value=diagonal]:radio').css("visibility", "visible");
      $('[value=width]:radio').css("visibility", "visible");
      $('[value=height]:radio').css("visibility", "visible");
      $('[value=distance]:radio').css("visibility", "hidden");
      $('[value=distance]:radio').attr("checked", "");
      $('[value=diagonal]:radio').attr("checked", "CHECKED");
      $('#diagonal').attr("class", "calc-field left");
      $('#width').attr("class", "calc-field left");
      $('#height').attr("class", "calc-field left");
      $('#distance').attr("class", "hide calc-field left");
    } else {
      $('#calculator-prompt').html("Drag the projector to the desired distance from the screen or type in a distance:");
      $('#calculator-inputs-prompt').html("At this distance your image size will be:");
      $('.image-size-inputs').css("visibility", 'visible');
      $('.projection-distance-inputs').css("visibility", 'hidden');
      $('#projection-distance:button').attr("class", "calculator-tab tab-unselected");
      $('#image-size:button').attr("class", "calculator-tab tab-selected");
      $('[name=base-measure]').css("visibility", "hidden");
      $('[value=diagonal]:radio').attr("checked", "");
      $('[value=distance]:radio').attr("checked", "CHECKED");
      $('#diagonal').attr("class", "hide calc-field left");
      $('#width').attr("class", "hide calc-field left");
      $('#height').attr("class", "hide calc-field left");
      $('#distance').attr("class", "calc-field left");
    }
  }
  */

  // Rounding
  function myRound(number, places) {
    var multiplier = Math.pow(10, places);
    return Math.round(number * multiplier)/multiplier;
  }