$(function() {
    var productos = $('.productoCotizacion');
    if (
        productos.length <= 0
    ) {
        $('.procesarCotizacion').toggle();
    }

    $('.navigation').addClass('art-vmenu');

    var options = {
        success:       showResponse,
        datatype:      'json'
    }

    $('.ajaxForm').ajaxForm(options);

//    $('.ajaxForm').live('blur', function(event) {
//        $(this).ajaxForm(options);
//    });

    $('.ajaxLink').live('click', function(event) {
        var enlace = $(this);
        $.ajax({
            type: 'post',
            url: this.href,
            dataType: 'json',
            success: function(data){
                if (data.eliminado) {
                    $(enlace).parents('.productoCotizacion').remove();
                    var productos = $('.productoCotizacion');
                    if (
                        productos.length <= 0
                        &&
                        $('.procesarCotizacion').css('display') != 'none'
                    ) {
                        $('.procesarCotizacion').toggle();
                    }
                } else {
                    $(enlace).parent().find('.cantidadCotizador').html('Cantidad: ' + data.value);
                }
            }
        });
        return false;
    });

    function showRequest(formData, jqForm, options) {
        var queryString = $.param(formData);
        alert('About to submit: \n\n' + queryString);
        return true;
    }

    function showResponse(responseText, statusText, xhr, $form)  {
        if (responseText.clave) {
            var box = $('#estadoCotizacion');
            var cantidad = box.children().length;


            $('.procesarCotizacion').css('display', 'inline');

            $('#' + responseText.clave).parent().remove();

            if (cantidad >= 5) {
                box.children().first().remove();
            }

            var img = responseText.img.split('/');
            img = img[img.length - 1];

            var div = $(box).append(
                '<div class="responseTextCotizacion">'
                + '<img width="50" src="/recursos/imagenes/productos/thumbnails/50x68/' + img  + '" />'
                + '<strong id="' + responseText.clave + '"> ' + responseText.clave + '</strong>'
                + '<h4>' + responseText.producto + '</h4>'
                + '</div>'
            );

            $.each(responseText.presentaciones, function() {
                $('.responseTextCotizacion:last').append(
                    '<div>'
                    + '<p>' + this.nombre + '<br />'
                    + '<span class="cantidadCotizador">Cantidad: ' + this.cantidad + '</span>'
                    + '&nbsp;&nbsp;&nbsp;<a class="ajaxLink" href="/producto/eliminar/id/' + responseText.clave + '/p/' + this.presentacion.replace(/\s/g, '-') + '">'
                    + '<img src="/images/eliminar.png" /> '
                    + '</a>&nbsp;&nbsp;&nbsp;'
                    + '<a class="ajaxLink" href="/producto/sumar/id/' + responseText.clave + '/p/' + this.presentacion.replace(/\s/g, '-') + '">'
                    + '<img width="10" src="/images/plu.png" /> '
                    + '</a>&nbsp;&nbsp;&nbsp;'
                    + '<a class="ajaxLink" href="/producto/restar/id/' + responseText.clave + '/p/' + this.presentacion.replace(/\s/g, '-') + '">'
                    + '<img width="10" src="/images/minu.png" /> '
                    + '</a>'
                    + '</p>'
                    + '</div>'
                );

//
//                $.each(this, function(key, value) {
//                    $('.presentacionProducto:last').append(
//                        '<span>Cantidad: ' + value + '</span><a href="">Eliminar</a></p>'
//                    )
//                });

            });
        }
    }

});
