$(document).ready(function(){
  $(".orderitem ").each(function(i,el){
    //console.log("setupcartitem");
    item = new _cartitem();
    item.setup(el);
  });

  $(document).on("cartupdate",function(){
    cart.updateCart();
  });
});

function _cartitem(){
  this.total=0;
  this.id="";
  this.single=0;
  this.amount=0;
  this.ref="";

  this.setup = function($el){
    this.ref = $($el);
    this.id = this.ref.attr("data-id");
    this.total = parseInt(  this.ref.find(".totalprice").html());
    this.price = parseInt(  this.ref.find(".singleprice").html());
    this.amount = parseInt(  this.ref.find(".counter").attr("data-cnt"));
    this.ref.find(".makerycount").on("changed",function(){
      var handler = $(this).closest(".orderitem").data("handler");
      //console.log("counterupdate");
      //console.log(handler);
      if(isset(handler)){
        handler.amount = parseInt($(this).attr("data-cnt"));
        handler.update();
      }
    });
    if(this.ref.find(".reserved").length){
      //console.log("hasreserved"+this.id);
      this.countdown();
    }
    this.ref.data("handler",this);
  };

  this.timedout = function(){
    var $item = this.ref;
    var msg = $item.find(".ticketerror").attr("data-timeout");
    makeryJson(getBaseUrl()+"cart/removeItem/"+this.id,function(data){
      if(data.status>0){
        $item.find(".order-inner").remove();
        $item.find(".reserved").remove();
        $item.addClass("error").find(".ticketerror").html(msg);
        // basket.update(data);
      }else{
        $item.find(".order-inner").remove();
        $item.find(".reserved").remove();
        $item.addClass("error").find(".ticketerror").html(msg);
      }
    });
  };

  this.countdown = function(){
    var el = this.ref.find(".timeritem");
    //console.log("start timer "+this.id);
    var timer = $(el).attr("data-max");
    var eventTime = $(el).attr('data-dt');
    //console.log("timer"+timer+" eventtime "+eventTime);
    var currentTime = moment().unix();
    var leftTime =  currentTime-eventTime-timer;
    if(leftTime>0){
      this.timedout();
      return;
    }
    //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'));
    var $this = this;
    if(leftTime<0){
        setTimeout(function() {
           $this.countdown();
       }, 1000);
     }
  };

  this.update = function(cnt,noupdate){
    this.total = this.amount * this.price;
    this.ref.find(".totalprice").html(this.total);
    if(!isset(noupdate)){
      basket.update(this.ref);
    }

  };
}

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;
}
