jQuery(document).ready(function(){
    $("#BotonConsultaEnviar").live("click", function(){
        var destino = $("#ConsultaProductoForm").attr('action');
        jQuery.ajax({
            url: destino,
            type: "POST",
            data: jQuery("#ConsultaProductoForm").serialize(),
            cache: false,
            success: function(html){
                jQuery("div#consulta-producto").html(html);
            },
            beforeSend: function(){
                jQuery("div#cargador-consulta").show();
            },
            complete: function(){
                jQuery("div#cargador-consulta").hide();
            }
        });
        return false;
    });
    $('#dialog').jqm();
	
    $("a.agregar-producto").click(function(){
        var id = $(this).attr('rel');
        var nro = jQuery("#ProductoCantidad"+id).attr("value");
        var stock = jQuery("#ProductoStock"+id).attr("value");
        var nroLimpio = nro.replace(/\D/g, "");
        var regex = /^[0-9]+$/;
        if (regex.test(nroLimpio) == false) {
            alert("Cantidad incorrecta. Debe entrar un número.");
            return false;
        } 
        var nuevoStock = parseInt(stock) - parseInt(nroLimpio);
        if (nuevoStock < 0) {
            alert("El stock es "+stock+". Por favor, seleccione una cantidad inferior");
            return false;
        } 

        jQuery.ajax({
            url: dir_raiz+'productos/agregar',
            type: "POST",
            data: {
                id: id,
                cantidad: nroLimpio
            },
            cache: false,
            success: function(html){
                $('#dialog').jqmShow();
                jQuery("#dialog").html(html);
                actualizarNro();
            },
            beforeSend: function(){
                jQuery("div#cargador").show();
            },
            complete: function(){
                jQuery("div#cargador").hide();		
            }
        });
        return false;
    });
	
    $("a.borrar-producto").live("click", function(){
        var id = $(this).attr('rel');
        jQuery.ajax({
            url: dir_raiz+'productos/quitar',
            type: "POST",
            data: {
                id: id
            },
            cache: false,
            success: function(html){
                jQuery("#dialog").html(html);
                actualizarNro();
            },
            beforeSend: function(){
                jQuery("div#cargador").show();
            },
            complete: function(){
                jQuery("div#cargador").hide();
                $('#dialog').jqmShow();
            }
        });
        return false;
    });
	
    $("div#btn-actualizar").live("click", function(){
        jQuery.ajax({
            url: dir_raiz+'productos/actualizar',
            type: "POST",
            data: jQuery("#ActualizarProductoForm").serialize(),
            cache: false,
            success: function(html){
                jQuery("#dialog").html(html);
            },
            beforeSend: function(){
                jQuery("div#cargador").show();
            },
            complete: function(){
                jQuery("div#cargador").hide();
                $('#dialog').jqmShow();
            }
        });
        return false;
    });
	
    $("#menu-cesta").live("click", function(){

        jQuery.ajax({
            url: dir_raiz+'productos/lista',
            type: "GET",
            cache: false,
            success: function(html){
                $('#dialog').jqmShow();
                jQuery("#dialog").html(html);
            },
            beforeSend: function(){
                jQuery("div#cargador").show();
            },
            complete: function(){
                jQuery("div#cargador").hide();			
            }
        });
        return false;
    });
	
    function actualizarNro() {
        jQuery.ajax({
            url: dir_raiz+'productos/numero',
            type: "GET",
            cache: false,
            success: function(html){
                jQuery("#menu-cesta").html(html);
            }
        });
    }
	
    $("#carrito-ordenar").live("click", function(){

        jQuery.ajax({
            url: dir_raiz+'pedidos/agregar',
            type: "GET",
            cache: false,
            success: function(html){
                jQuery("#dialog").html(html);
            },
            beforeSend: function(){
                jQuery("div#cargador").show();
            },
            complete: function(){
                jQuery("div#cargador").hide();	
                $(".pin").pin();
            }
        });
        return false;
    });
	
    $("#BotonOrdenar").live("click", function(){
        var destino = $("#PedidoAgregarForm").attr('action');
        jQuery.ajax({
            url: destino,
            type: "POST",
            data: jQuery("#PedidoAgregarForm").serialize(),
            cache: false,
            success: function(html){
                jQuery("#dialog").html(html);
            },
            beforeSend: function(){
                jQuery("div#cargador").show();
            },
            complete: function(){
                jQuery("div#cargador").hide();
            }
        });
        return false;
    });

    $("div.producto-agregar").hover(
        function () {
            $(this).find("div.aviso").show();
        },
        function () {
            $(this).find("div.aviso").hide();
        });
	
});

function mostrarBtnAct() {
    jQuery("div#btn-actualizar").show();
}

function cerrarCarrito() {
    $('#dialog').jqmHide();
}
