function options(s) {
    var ids = [], r = new String
    ids = s.split(',')
    for(var key in ids)  r = r + '<option>' + ids[key] + '</option>'
    return r
}

function round(value) {
r = Math.round(100*value)/100
return r
}

function ro() {
      var r
        switch($("#metal").val()) {
           case "углеродистая сталь":
           r = 7850
           break;
           case "нержавеющая сталь":
           r = 8000
           break;
           case "дюралюминий сталь":
           r = 2690
           break;
           case "медь":
           r = 8960
           break;
           case "латунь":
           r = 8600
           break;
           case "бронза":
           r = 8500
           break;
       }
    return r
}

function s() {
    var m = [
     [20,3,1.13],
     [20,4,1.46],
     [25,3,1.43],
     [25,4,1.86],
     [28,3,1.62],
     [30,3,1.74],
     [30,4,2.27],
     [32,3,1.86],
     [32,4,2.43],
     [35,3,2.04],
     [35,4,2.67],
     [35,5,3.28],
     [40,3,2.35],
     [40,4,3.08],
     [40,5,3.79],
     [45,3,2.65],
     [45,4,3.48],
     [45,5,4.29],
     [50,3,2.96],
     [50,4,3.89],
     [50,5,4.80],
     [50,6,5.69],
     [56,4,4.38],
     [56,5,5.41],
     [63,4,4.96],
     [63,5,6.13],
     [63,6,7.28],
     [70,4.5,6.20],
     [70,5,6.86],
     [70,6,8.15],
     [70,7,9.42],
     [70,8,10.67],
     [75,5,7.39],
     [75,6,8.78],
     [75,7,10.15],
     [75,8,11.50],
     [75,9,12.83],
     [80,5.5,8.63],
     [80,6,9.38],
     [80,7,10.85],
     [80,8,12.30],
     [90,6,10.61],
     [90,7,12.28],
     [90,8,13.93],
     [90,9,15.60],
     [100,6.5,12.82],
     [100,7,13.75],
     [100,8,15.60],
     [100,10,19.24],
     [100,12,22.80],
     [100,14,26.28],
     [100,16,29.68],
     [110,7,15.15],
     [110,8,17.20],
     [125,8,19.69],
     [125,9,22.00],
     [125,10,24.33],
     [125,12,28.89],
     [125,14,33.37],
     [125,16,37.77],
     [140,9,24.72],
     [140,10,27.33],
     [140,12,32.49],
     [160,10,31.43],
     [160,11,34.42],
     [160,12,37.39],
     [160,14,43.57],
     [160,16,49.07],
     [160,18,54.79],
     [160,20,60.40],
     [180,11,38.80],
     [180,12,42.19],
     [200,12,47.10],
     [200,13,50.85],
     [200,14,54.60],
     [200,16,61.98],
     [200,20,76.54],
     [200,25,94.29],
     [200,30,111.54],
     [220,14,60.38],
     [220,16,68.58],
     [250,16,78.40],
     [250,18,87.72],
     [250,20,96.96],
     [250,22,106.12],
     [250,25,119.71],
     [250,28,133.12],
     [250,30,141.96],
     [250,35,163.71]
    ]
     for (i=0; i<m.length; i++) {
     ar = m[i]
     if (ar[0] == $('#v31a').val() && ar[1] == $('#v33a').val()) r = ar[2]
     }
    return r
}

    function s2() {
    var m = [
     [25,16,3,1.16],
     [32,20,3,1.49],
     [32,20,4,1.94],
     [40,25,3,1.89],
     [40,25,4,2.47],
     [45,28,3,2.14],
     [45,28,4,2.8],
     [50,32,3,2.42],
     [50,32,4,3.17],
     [56,36,4,3.58],
     [56,36,5,4.41],
     [63,40,4,4.04],
     [63,40,5,4.98],
     [63,40,6,5.9],
     [63,40,8,7.68],
     [70,45,5,5.59],
     [75,50,5,6.1],
     [75,50,6,7.25],
     [75,50,8,9.46],
     [80,50,5,6.36],
     [80,50,6,7.54],
     [80,50,8,0],
     [90,56,5.5,7.86],
     [90,56,6,8.54],
     [90,56,8,11.17],
     [100,63,6,9.59],
     [100,63,7,11.08],
     [100,63,8,12.57],
     [100,63,10,15.41],
     [110,70,6.5,11.44],
     [110,70,8,13.89],
     [125,80,7,14.01],
     [125,80,8,15.92],
     [125,80,10,19.75],
     [125,80,12,23.31],
     [140,90,8,17.96],
     [140,90,10,22.29],
     [160,100,9,22.93],
     [160,100,10,25.22],
     [160,100,12,30.06],
     [160,100,14,34.78],
     [180,110,10,28.28],
     [180,110,12,33.63],
     [200,125,11,34.9],
     [200,125,12,37.83],
     [200,125,14,43.82],
     [200,125,16,49.81],
     [250,160,12,48.28],
     [250,160,16,63.57],
     [250,160,18,71.08],
     [250,160,20,78.6]
    ]
     for (i=0; i<m.length; i++) {
     ar = m[i]
     if (ar[0] == $('#v31').val() && ar[1] == $('#v32').val() && ar[2] == $('#v33').val() ) r = ar[3]
     }
    return r
}

  $(document).ready(function(){

    $('.calc').calculator({showOn: 'button',buttonImage: 'lib/calculator.png'});
    $("#tabs>ul").tabs({fxFade:true,fxSpeed:'slow'})

    $('#bv31').hide();
    $('#bv32').hide();
    $('#bv33').hide();
    $('#br31').hide();

    $('#v31').html(options('25,32,40,45,50,56,63,70,75,80,90,100,110,125,140,160,180,200,250'))
    $('#v32').html(options('16'))
    $('#v33').html(options('3'))


    $('#v31a').html(options('20,25,28,32,35,40,45,50,56,63,70,75,80,90,100,110,125,140,160,180,200,220,250'))
    $('#v33a').html(options('3,4'))

    $("#v30").change(function () {
    if ($(this).val() == 'неравнополочный') {
      $('#bv31').show()
      $('#bv32').show()
      $('#bv33').show()
      $('#br31').show()
      $('#bv31a').hide()
      $('#bv33a').hide()
      $('#br31a').hide()
      } else {
      $('#bv31').hide();
      $('#bv32').hide();
      $('#bv33').hide();
      $('#br31').hide();
      $('#bv31a').show()
      $('#bv33a').show()
      $('#br31a').show()
      }
    })


    $("#e1").click(function(){
    $("#r11").attr({value: round(ro() * 3.14 * $("#v11").val() * $("#v12").val() / 1000000 ) })
    $("#r12").attr({value: round($("#r11").val() * $("#v13").val()) })
    $("#r13").attr({value: Math.ceil($("#v14").val() / $("#r12").val()) })
    })

    $("#e2").click(function(){
    $("#r21").attr({value: round(ro() * ($("#v21").val() * $("#v22").val() * $("#v23").val()) / 1000000000) })
    $("#r22").attr({value: $("#r21").val() * 100 })
    $("#r23").attr({value: Math.ceil($("#v24").val() / $("#r21").val())})
    })

    $("#e3").click(function(){
      if ($("#v30").val() == 'равнополочный') $("#r31a").attr({value: round(ro() * s() / 10000)}); else $("#r31").attr({value: round(ro() * s2() / 10000)})

    })

    $("#e4").click(function(){
    $("#r41").attr({value: round(ro() * 3.14 / 4 * $("#v41").val() * $("#v41").val() / 1000000)})
    $("#r42").attr({value: round($("#r41").val() * $("#v42").val())})
    })

    $("#e5").click(function(){
    $("#r51").attr({value: round(ro() * $("#v51").val() * $("#v51").val() / 1000000 )})
    $("#r52").attr({value: round($("#r51").val() * $("#v52").val())})
    })

    $("#e6").click(function(){
    $("#r61").attr({value: round(ro() * 2 * Math.sqrt(3) * $("#v61").val() * $("#v61").val() / 4 / 1000000)})
    $("#r62").attr({value: round($("#r61").val() * $("#v62").val())})
    })


    $("#v31a").change(function () {
          switch($(this).val()) {
           case "20":
                $('#v33a').html(options('3,4'))
           break;
           case "25":
                $('#v33a').html(options('3,4'))
           break;
           case "28":
                $('#v33a').html(options('3'))
           break;
           case "30":
                $('#v33a').html(options('3,4'))
           break;
           case "32":
                $('#v33a').html(options('3,4'))
           break;
           case "35":
                $('#v33a').html(options('3,4,5'))
           break;
           case "40":
                $('#v33a').html(options('3,4,5'))
           break;
           case "45":
                $('#v33a').html(options('3,4,5'))
           break;
           case "50":
                $('#v33a').html(options('3,4,5,6'))
           break;
           case "56":
                $('#v33a').html(options('4,5'))
           break;
           case "63":
                $('#v33a').html(options('4,5,6'))
           break;
           case "70":
                $('#v33a').html(options('4.5,5,6,7,8'))
           break;
           case "75":
                $('#v33a').html(options('5,6,7,8,9'))
           break;
           case "80":
                $('#v33a').html(options('5.5,6,7,8'))
           break;
           case "90":
                $('#v33a').html(options('6,7,8,9'))
           break;
           case "100":
                $('#v33a').html(options('6.5,7,8,10,12,14,16'))
           break;
           case "110":
                $('#v33a').html(options('7,8'))
           break;
           case "125":
                $('#v33a').html(options('8,9,10,12,14,16'))
           break;
           case "140":
                $('#v33a').html(options('9,10,12'))
           break;
           case "160":
                $('#v33a').html(options('10,11,12,14,16,18,20'))
           break;
           case "180":
                $('#v33a').html(options('11,12'))
           break;
           case "200":
                $('#v33a').html(options('12,13,14,16,20,25,30'))
           break;
           case "220":
                $('#v33a').html(options('14,16'))
           break;
           case "250":
                $('#v33a').html(options('16,18,20,22,25,28,30,35'))
           break;
        }
    })


    $("#v31").change(function () {

          switch($(this).val()) {
           case "25":
                $('#v32').html(options('16'))
                $('#v33').html(options('3'))
           break;
           case "32":
                $('#v32').html(options('20'))
                $('#v33').html(options('3,4'))
           break;
           case "40":
                $('#v32').html(options('25'))
                $('#v33').html(options('3,4'))
           break;
           case "45":
                $('#v32').html(options('28'))
                $('#v33').html(options('3,4'))
           break;
           case "50":
                $('#v32').html(options('32'))
                $('#v33').html(options('3,4'))
           break;
           case "56":
                $('#v32').html(options('36'))
                $('#v33').html(options('4,5'))
           break;
           case "63":
                $('#v32').html(options('40'))
                $('#v33').html(options('4,5,6,8'))
           break;
           case "70":
                $('#v32').html(options('45'))
                $('#v33').html(options('5'))
           break;
           case "75":
                $('#v32').html(options('50'))
                $('#v33').html(options('5,6,8'))
           break;
           case "80":
                $('#v32').html(options('50'))
                $('#v33').html(options('5,6,8'))
           break;
           case "90":
                $('#v32').html(options('56'))
                $('#v33').html(options('5.5,6,8'))
           break;
           case "100":
                $('#v32').html(options('63'))
                $('#v33').html(options('6,7,8,10'))
           break;
           case "110":
                $('#v32').html(options('70'))
                $('#v33').html(options('6.5,8'))
           break;
           case "125":
                $('#v32').html(options('80'))
                $('#v33').html(options('7,8,10,12'))
           break;
           case "140":
                $('#v32').html(options('90'))
                $('#v33').html(options('8,10'))
           break;
           case "160":
                $('#v32').html(options('100'))
                $('#v33').html(options('9,10,12,14'))
           break;
           case "180":
                $('#v32').html(options('110'))
                $('#v33').html(options('10,12'))
           break;
           case "200":
                $('#v32').html(options('125'))
                $('#v33').html(options('11,12,14,16'))
           break;
           case "250":
                $('#v32').html(options('160'))
                $('#v33').html(options('12,16,18,20'))
           break;
          }

    })

        $('#tabs').show();
  })
