var basket;
$(document).ready(function(){
  basket = new _basket();
  basket.setup();
  $(document).on("cartupdate",function(){
    cart.updateCart();
  });
  $(".couponcheck .inputbtn").on("click",function(){
    basket.checkupCoupon();
  });
});

function _basket(){
  this.items=[];
  this.$parent = $(".topnav .cart");
  this.total=0;
  this.$total = $("#basket .summary .baskettotal");

  this.setup = function(){
      this.total = 0;
      $("#basket").find(".orderitem ").each(function(){
        var handler = $(this).data("handler");
        if(isset(handler)){
          this.total = handler.total+this.total;
        }

      });
  };

  this.removeItem = function(item){

    var $item = $(item).closest(".orderitem");
    //console.log($item.attr("data-id"));
    makeryJson(getBaseUrl()+"cart/removeItem/"+$item.attr("data-id"),function(data){
      if(data.status>0){
        $item.slideUp(function(){
          $(this).remove();
        });
        basket.updateTotal(data);
      }
    });
  };

  this.updateTotal = function(data){
    $(".itemtotal.shipping").html(data.shipping);
    $(".itemtotal.event").html(data.event);
    $(".itemtotal.gift").html(data.gift);
    $(".baskettotal").html(data.total);
    if(data.coupon>0){
      $(".itemtotal.coupon").html(data.coupon);
      $(".itemtotal.coupon").closest(".couponreduce").show();
    }else{
      $(".itemtotal.coupon").html(data.coupon);
      $(".itemtotal.coupon").closest(".couponreduce").hide();
    }
    if(isset(data.discountrow)){
      $("#discountrow").replaceWith(data.discountrow);
    }
  };

  this.checkupCoupon = function(){
      var code = $("#couponcode").val();
      makeryJson(getBaseUrl()+"cart/couponcheck/"+code,function(data){
        //console.log(data);
        if(data.status <1){
          $("#couponcode").closest(".input").find(".errorinfo").html(data.msg);
        }else{
          $("#couponcode").val("");
          basket.updateTotal(data);
        }

        if(data.special == 1 && $("#couponcode").length == 0){
          $(".itemtotal.coupon").closest(".couponreduce").after('<div className="row subtotal specialcp" id="specialcp"><div></div><div className="title text-17-24"><u>Nur für 2 Artikel einlösbar</u></div></div>');
        }
      });
  };
  this.setItems = function(){
    // $("#basket").find(".orderitem ").each(function(){
    //   var item = {};
    //   item.total = parseInt($(this).find(".totalprice").html());
    //   item.price = parseInt($(this).find(".singleprice").html());
    //   item.amount = parseInt($(this).find(".counter").attr("data-cnt"));
    //   basket.items.push(item);
    // });
  };

  this.update = function(ref){
    $(ref).removeClass("error");
    var handler = $(ref).data("handler");
    makeryJson(getBaseUrl()+"cart/updateItemAmount/"+handler.id+"/"+handler.amount,function(data){
      if(data.status>0){
        basket.updateTotal(data);
      }else{
        $(ref).addClass("error");
        $(ref).find(".ticketerror").html(data.msg);
        $(ref).data("handler").amount=data.amount;
        $(ref).data("handler").update(data.amount,false);
        if(isset(data.max)){
          $(ref).find(".makerycount").attr("data-max",data.max).attr("data-cnt",data.amount);
          $(ref).find(".makerycount .counterval").html(data.amount);
          $(ref).data("handler").update(data.amount,false);
        }
      }
    });
  };

  this.getTotal = function(){

  };
  this.getCartCnt = function(){
    var $this = this;
    makeryJson(getBaseUrl()+"cart/getItemCount",function(data){
      $this.updateCart(data.count);
    });
  };
}
// function countdown(id,timer){
// //  console.log(id);
//   if (window.jQuery) {
//     $.each($(".timer"+id),function(i,el){
//       var eventTime = $(el).attr('data-dt');
//       var currentTime = moment().unix();
//       var leftTime =  currentTime-eventTime-timer;
//    //   console.log("ecenttime"+eventTime+" currenttime"+currentTime+" timeleft "+leftTime);
//       var duration = moment.duration(leftTime, 'seconds');
//       var interval = 1000;
//       $(el).html(formatt(duration.minutes())+':'+formatt(duration.seconds()));
//      // var timeArray = presentTime.split(/[:]+/);
//      //  var m = timeArray[0];
//      //  var s = timeArray[1];
//      //  var timer = moment(m+":"+s,"HH:mm");
//      //  timer = timer.add('seconds', 1);
//      //  console.log(timer);
//      //  $("#timer"+id).html(timer.format('mm:ss'));
//      if(leftTime<0){
//         setTimeout(function() {
//            countdown(id,timer);
//        }, 1000);
//      }else{
//        removeCartItem(id);
//      }
//     });
//
//   }else{
//     setTimeout(function() { countdown(id,timer); }, 50);
//   }
// }
// function formatt(i){
//   return pad(Math.abs(i),2);
// }
//
// function pad(num, size) {
//     var s = num+"";
//     while (s.length < size) s = "0" + s;
//     return s;
// }
