$(function(){
 $("ul.categoria li").hover(function(){
  $(this).find("ul").show().parent().siblings().find("ul").hide();
  },function(){
   $(this).find("ul").hide();
   });
 
$("img.sumar,img.restar").click(function(){
   var elemento = new coreCarro($(this));
   if(elemento.clase=="sumar"){
    elemento.sumar();
   }else{
    elemento.restar();
   }
   elemento.ajax();
 });
 function coreCarro(elemento){
  this.objeto=elemento.parent().parent().parent();
  this.cantidad=this.objeto.find("input.cantidad").val();
  this.idP=this.objeto.find("input.id").val();
  this.clase=elemento.attr("class");
  this.sumar=function(){
    if(!isNaN(this.cantidad) && this.cantidad>=0){
     this.cantidad++;
    }else{
     this.cantidad=1;
    }
  };
  this.restar=function(){
  if(!isNaN(this.cantidad) && this.cantidad>0){
     this.cantidad--;
    }else{
     this.cantidad=0;
    }
  };
  this.ajax=function(){
     if(this.idP>0 && !isNaN(this.cantidad)){
       var id=this.idP;
       var cantidadc=this.cantidad;
       var obj=this.objeto;
       $.getJSON("peticion-carro.html",{idP:id,cantidad:cantidadc},
             function(resp){
              if(resp.datos.error!=undefined){
                console.log("error");
              }else{
               var divBody=$("div#infoCarro");
               if(divBody.length){
                divBody.html("<span>cantidad:"+resp.datos.cantidad+"</span>\
                             &nbsp;<a href='carro-compra.html' title='ver mi carro'>ver carro de compras</a>");
               }else{
                $("<div id='infoCarro'></div>").prependTo("div.menu_var").html("<span>cantidad:"+resp.datos.cantidad+"</span>&nbsp;<a href='carro-compra.html' title='ver carro'>ver mi carro</a>");
               }
               var strongCarro=$("strong#totalCarroPie");
               if(strongCarro.length)strongCarro.html(resp.datos.total);
               obj.find("input.cantidad").val(cantidadc);
               var strong=$("strong#totalCarro");
               if(strong.length)strong.html(resp.datos.total);
              }
              });
    }else{
     console.log("no se encontro el elemento");
    }
   };
 }
 
   
 });
