$(document).ready(function () {
   makeButtonsEvents();
});
function makeButtonsEvents()
{
  $('.btnRemove').each(function(){
    var href  = $(this).attr('href');
    var removeGood  = $(this).attr('gsId');
    $(this).click(function(){
      $.ajax({
        type: 'GET',
        url: href+'&ajax=1',
        dataType : "json",
        cache:  false,
        success: function (data, textStatus) {
          $('.basket-info-content span').html(data.short_info);
          $('#SummaItogo').html(data.summa);
          animateDeleteGoods($('#basketRow_'+removeGood));
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
          //alert("TECHNICAL ERROR: unable to refresh the cart.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
        }
      });
      return false;
    })
  });
  $('.btnrefresh').each(function(){
    var href  = $(this).attr('href');
    var refreshGood   = $(this).attr('gsId');

    var newQantityObj = $(this).parent().find('input');

    $(this).click(function(){
      qantity = newQantityObj.attr('value');
      if(qantity==0)
      {
        $('#basketRow_'+refreshGood+' .btnRemove').click();
      }
      else
      {
        $.ajax({
          type: 'GET',
          url: href+'&ajax=1&quantity='+ qantity,
          dataType : "json",
          cache:  false,
          success: function (data, textStatus) {
            $('.basket-info-content span').html(data.short_info);
            $('#SummaItogo').html(data.summa);
            $('#summa_'+refreshGood).html(data.summa_single_good)

          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
            //alert("TECHNICAL ERROR: unable to refresh the cart.\n\nDetails:\nError thrown: " + XMLHttpRequest + "\n" + 'Text status: ' + textStatus);
          }
        });
      }
      return false;
    })
  });

}
function animateDeleteGoods(aObj)
{
  aObj.animate({
      opacity: 0
    }, 300, "", function(){
      $(this).hide();
    });
}
function basketRefresh()
{
  var options = {
    url: "/basket/?action=refreshAll&ajax=1",
    type: "POST",
    dataType: 'json',
    success: function(data) {

      $('#FormBasketContainer').html(data.form);
      $('.basket-info-content span').html(data.info.short_info);
      $('#SummaItogo').html(data.info.summa);
      makeButtonsEvents();
      return false;
    }
  };
  $("#BasketRefreshForm").ajaxSubmit(options);
  return false;

}

