jQuery.noConflict();
jQuery(function($) {

//initiales ausblenden der Fehlermeldung ms@forty-four.de
    $('#fehler').hide();
    
  var widths = {
    0 : [100, 120, 140, 160, 200],
    1 : [100, 120, 140, 160, 180, 200],
    2 : [80, 90, 100, 120, 140, 160, 180, 200],
    3 : [140, 200]
  };
  var prices = {
    0 : {
      basic : {
        0 : [295, 369, 409, 449, 529],
        1 : [339, 409, 449, 509, 589]
      },
      plus : {
        0 : [449, 569, 659, 709, 859],
        1 : [499, 629, 689, 769, 939]
      },
      premium : {
        0 : [539, 649, 765, 879, 1099],
        1 : [609, 689, 829, 969, 1189]
      },
      visco : {
        0 : [729, 979, 1119, 1239, 1459],
        1 : [779, 1089, 1189, 1329, 1519]
      },
      platinum : {
        0 : [998, 1179, 1259, 1465, 1799, 1929]
      },
      /*platinumslim : {
        0 : [789, 959, 1099, 1229, 1550, 1650]
      },*/
      travel : {
        0 : [259, 279, 299, 389, 429, 459, 479, 499],
        1 : [499, 669]
      },
      travelcomfort : {
        0 : [299, 319, 349, 459, 499, 539, 579, 629]
      }
    },
    1 : {
      basic : {
        0 : [349, 419, 479, 539, 639],
        1 : [409, 479, 529, 589, 699]
      },
      plus : {
        0 : [529, 659, 759, 829, 969],
        1 : [589, 719, 799, 879, 1049]
      },
      premium : {
        0 : [609, 699, 849, 939, 1159],
        1 : [689, 779, 899, 998, 1249]
      },
      visco : {
        0 : [809, 998, 1219, 1339, 1559],
        1 : [889, 1159, 1299, 1398, 1649]
      },
      platinum : {
        0 : [1119, 1299, 1389, 1579, 1995, 2199]
      },
      /*platinumslim : {
        0 : [959, 1099, 1279, 1429, 1798, 1959]
      },*/
      travel : {
        0 : [309, 329, 359, 499, 539, 559, 579, 599],
        1 : [559, 729]
      },
      travelcomfort : {
        0 : [359, 379, 399, 539, 579, 619, 679, 699]
      }
    }
  };
  var width_map = {
    basic : [0, 0],
    plus : [0, 0],
    premium : [0, 0],
    visco : [0, 0],
    platinum : [1],
    /*platinumslim : [1],*/
    travel : [2, 3],
    travelcomfort : [2]
  };
  $('#konfigurator input').bind('click keyup', function(e) {
	if ($(this).attr('name') == 'preisliste') {
      $('#konfigurator #form-info span.infotext').text($(this).parent().attr('title'));
	}
        
	var result = calculate();
    if (result.laenge_info) {
      $('#konfigurator .dimensionen .info:hidden em').text(result.max_laenge + 'cm').parent().show();
        //simples ein- und ausblenden der fehlermeldung bzw. preisausgabe ms@forty-four.de
      $('#fehler').show();
      $('#preisausgabe').hide();
    }
    else {
      $('#konfigurator .dimensionen .info:visible').hide();
      //simples ein- und ausblenden der fehlermeldung bzw. preisausgabe ms@forty-four.de
       $('#fehler').hide();
       $('#preisausgabe').show();
    }
    $('#konfigurator .preis strong').text(result.price);
  });

  function calculate() {

    var handarbeit = Number($('#konfigurator #preisliste input:checked').val() == 'special');
    var typ = $('#konfigurator .typ input:checked').val();
    var breite = $('#konfigurator #breite').val();
    var laenge = $('#konfigurator #laenge').val();
    var laenge_set = Number(laenge > 200);
    var laenge_info = false;
    var max_laenge = 200;
//Breitenbegrenzung eingehackt und länge auf 211 gestellt ms@forty-four.de
    if (laenge >= 211 || breite >=201) {
      
      laenge_info = true
      max_laenge = 211;
    }
    if (width_map[typ][laenge_set] == undefined) {
      laenge_set = 0;
    }
    var widthset = widths[width_map[typ][laenge_set]];

    var found_index = 0;
    $.each(widthset, function(index, value) {
      if (breite > value) {
        found_index = (index + 1);
      }
    });

    var price = prices[handarbeit][typ][laenge_set][found_index];
    return {
      price : price,
      laenge_info : laenge_info,
      max_laenge : max_laenge
    }
  }
});
