$(document).ready(function() {
    // Focus/Blur des champs de quantité des produits
    $('.command').focus(function() { if ($(this).val() == '0') { $(this).val(''); } });
    $('.command').blur(function() { if ($(this).val() == '' || isNaN($(this).val())) { $(this).val('0'); } });

    // Show/Hide des produits
    $('h2.selector').click(function() {
        $('div[rel="prod' + $(this).attr('rel') + '"]').clearQueue();

        if ($(this).attr('class') == 'selector') {
            $(this).attr('class', 'selector open');
            $('div[rel="prod' + $(this).attr('rel') + '"]').fadeTo(0, 0).css('display', 'block').fadeTo('slow', 1);
        } else {
            $(this).attr('class', 'selector');
            $('div[rel="prod' + $(this).attr('rel') + '"]').fadeTo('slow', 0, function() { $(this).css('display', 'none'); });
        }
    });

    // Validation du formulaire de commande
    var validForm = true;

   $('#btnValidate').click(function() {
        fields = new Array('prenom', 'nom', 'courriel', 'telephone', 'adresse', 'ville', 'code-postal');
        nbrField = fields.length;
        validForm = true;
        validQuantity = false;

        for (i = 1; i <= nbrField; i++) {
             if ($('#' + fields[i - 1]).val() == '') {
                $('#' + fields[i - 1]).attr('style', 'border: #F00 solid 1px;');
                validForm = false;
            } else {
                $('#' + fields[i - 1]).attr('style', '');
            }
        }

        if (!validForm) {
            alert('Avant de nous faire parvenir votre commande,\nveuillez remplir tous les champs obligatoires du formulaire.');
        } else {
            validQuantity = false;

            $('.command').each(function() {
                if ($(this).val() == '') { $(this).val('0'); }

                if ($(this).val() != '0') {
                    validQuantity = true;
                    return;
                }
            });

            if (validQuantity) {
                $.ajax({
                    type: "POST",
                    url: "commande.php",
                    data: ({mgr: $('#commandeMGR').serialize()}),
                    success: function(msg) {
                        if (msg == 'ok') {
                            $('#commandeMGR input').val('');
                            $('#commandeMGR .command').val('0');
                            $('#ok').fadeTo(0, 0).css('display', 'block').fadeTo('slow', 1);
                            $('#btnCommande').fadeTo('slow', 0, function() {
                                $(this).css('display', 'none');
                            })
                        } else {
                            $('#error').fadeTo(0, 0).css('display', 'block').fadeTo('slow', 1);
                        }
                    }
                });
            } else {
                alert('Avant de nous faire parvenir votre commande,\nveuillez inscrire une quantité à un produit.');
            }
        }

       if (validForm == true) {
            $('#sec').attr({
                name: 'security',
                value: 'good'
            });
        }
    });

    // Fancybox
    $(".fancy").fancybox({
        'speedIn'        :    600,
        'speedOut'        :    200,
        'overlayShow'    :    true
    });
});
