$(document).ready(function() {
  var Cache = new Array();
  $(".variation_select").each(function(e) {
    var tmp = $(this).attr("class").split('row_key_');
    var key = tmp[1];
    $(this).attr('key',key);
    $(this).find("option").each(function(i) {
      var variation_id = $(this).attr("value");

        if ( ! Products_variations_attr[key][variation_id].is_shippable ) {
          $(this).css("color","#f00");
        }

        if ( $(this).attr("selected") ) {
          Cache[key] = i;
        }
    });

    $(this).change(function (event) {
      var variation_id = $(this).attr("value");
      var current_key = $(this).attr('key');
      if ( ! Products_variations_attr[current_key][variation_id].is_shippable ) {
        var current_index = Cache[current_key];

        $(this).find("option").each(function(i) {
          if ( i == current_index ) {
              $(this).attr('selected', 'selected')
          }
        });

        var label = Products_variations_attr[current_key][variation_id].item;
        alert(label+" ist derzeit nicht lieferbar");
      }  else {
        // save the selected index
        $(this).find("option").each(function(i) {
          if ( $(this).attr('selected') ) {
               Cache[current_key] = i;
          }
        });

        // update html display
        var price = Products_variations_attr[current_key][variation_id].price;
        var unit_price = Products_variations_attr[current_key][variation_id].unit_price;
//         var pkg_unit = Products_variations_attr[current_key][variation_id].pkg_unit;
//         if (pkg_unit) {
//           pkg_unit = ' / ' + pkg_unit;
//         }
        var reference_price = Products_variations_attr[current_key][variation_id].reference_price;
        $('#row_'+current_key).find('.col3').html(price);
        $('#row_'+current_key).find('.col5').html(unit_price);
        $('#row_'+current_key).find('.col6').html(reference_price);
      }
    });

    key++;
  });


  $('td.amount button.qtychange').each(function(e) {
    $(this).click(function(event) {
			event.preventDefault();

      var current_input = $(this).parent().find('input.dynvalue');
      var current_value = parseInt(current_input.attr('value'));

      var new_value = 1;
      if($(this).children().attr('class') == 'add') {
        new_value = (current_value + 1);
      } else if($(this).children().attr('class') == 'remove') {
        new_value = (current_value - 1);
      }

      if (new_value < 1) {
        new_value = 1;
      }

      if (current_value != new_value) {
        current_input.attr('value', new_value);
      }
		});
	});


  $(".less").each(function(e) {
    $(this).css("display","none");
  });

  $(".long_description").each(function(e) {
    $(this).css("display","none");
  });

  $(".more").click(function(e) {
    e.preventDefault();
    $(".long_description").css("display","block");
    $(".short_description").css("display","none");
    $(".more").css("display","none");
    $(".less").css("display","block");
  });


  $(".less").click(function(e) {
    e.preventDefault();
    $(".long_description").css("display","none");
    $(".short_description").css("display","block");
    $(".more").css("display","block");
    $(".less").css("display","none");
  });

  $('.variation_select').each(function(e) {
    $(this).change(function(event) {
      if (this.options[this.selectedIndex].text == 'Mix') {
        $('#select_mix_text').show();
      } else {
        $('#select_mix_text').hide();
      }
    })
  })
});

