$(document).ready(function() {

  $('.numeric-stepper-plusminus.checkout').live('mousedown', function(e){
    if(e.target==$(this).find('input')[0]) return;
    var input = $(this).find('input'),
    width = $(this).width();
    height = $(this).height();
    if((e.pageX-$(this).offset().left)>($(this).width()/2)) {
      //clicked in left half
      input.val(parseInt(parseInt(input.val())+1));
      if( ! $(this).parents('.error')) {
        $(this).css({'background-position':'100% -'+height+'px'});
      } else {
        $(this).css({'background-position':'0 -'+height+'px'});
      }
    } else {
      //…lower half
      input.val(Math.max(parseInt(input.val())-1,0));
      if( ! $(this).parents('.error')) {
        $(this).css({'background-position':'100% -'+(height*2)+'px'});
      } else {
        $(this).css({'background-position':'0 -'+(height*2)+'px'});
      }
    }
    return false;
  }).live('mouseup', function(event){
    if( ! $(this).parents('.error')) {
      $(this).css({'background-position':'100% 0'});
    } else {
      $(this).css({'background-position':'0 0'});
    }
    if($(this).hasClass('submit-on-blur') && event.target===this) {
      update_basket_from_numeric_stepper(this);
      }
    }).find('input').live('keypress',function(event) {
      return false;

  });
  $('.numeric-stepper-plusminus.submit-on-blur.checkout input').live('blur', function(){
    update_basket_from_numeric_stepper(this);
  });

  function update_basket_from_numeric_stepper(elem) {
    $element = $(elem);
    $element.attr('readonly', 'readonly');
    var inputElement = $element.find('.quantityIn');
    $('#page').fadeTo('fast', '0.5');

//    checkoutDesign.showLoader('li.article_details');
    var updateLink = $element.find('.hidden-update-url');
    var href = $(updateLink).attr('href');

    href = shopAction.appendUrlParam(href, "quantity", $(inputElement).val());
    shopAction.ajax(href,null,'li.article_details', function() {
      $('#page').fadeTo('fast', '1');
    });
  }
});
