$(document).ready(function(){
	
	
    // bouton -
    $('.lessProd,.lessProdExpand,.lessProdRecap').click(function(e){
        e.preventDefault();
        
        
        if ( $(this).attr('class') == 'lessProdExpand' ) {
            showMaskLoader();
            var elemPieces = $(this).attr('id').split('_');
            var prodId =  elemPieces[1];
            countCartj($('#lessProdRecap_'+prodId),-1);
            updateCart(1,$(this));
            moreProd($(this), -1);
        }
        else if ( $(this).attr('class') == 'lessProdRecap') {
            showMaskLoader();
            countCartj($(this),-1);
            if ($('#expandCard').length > 0 ) {
                var elemPieces = $(this).attr('id').split('_');
                var prodId =  elemPieces[1];
                updateCart(1,$('#lessProdExpand_'+prodId));
            } else {
                updateCart(1,$(this));
            }
        } else {
            moreProd($(this), -1);
        }
        
    });
    // bouton +
    $('.moreProd,.moreProdExpand,.moreProdRecap').click(function(e){
        e.preventDefault();
        
        var elemPieces;
        var prodId;
        if ( $(this).attr('class') == 'moreProdExpand' ) {
            showMaskLoader();
            updateCart(1,$(this));
        }
        else if ( $(this).attr('class') == 'moreProdRecap') {
            showMaskLoader();
            if ($('#expandCard').length > 0 ) {
                elemPieces = $(this).attr('id').split('_');
                prodId =  elemPieces[1];
                updateCart(1,$('#moreProdExpand_'+prodId),$(this));
            } else {
                countCartj($(this),+1);
                updateCart(1,$(this));
            }
        } else {
            moreProd($(this), 1);
        }
    });
  
  
    // ajout au panier
    $('.prodaddtoSelection').click(function(e){
        e.preventDefault();
        showMaskLoader();  
        var val = "quantity=" + $('input', $(this).parent()).val();
        updateCart(val,$(this));
    
    
    });

    function updateCart(val,elem,elem2) {

        $.ajax({
            type: "POST",
            dataType: 'json',
            async: true,
            url: elem.attr('href'),
            data: val,
            //cache: false,
            success: function(e){
                
                // panier étendu
                if ($('#expandCard').length > 0 ) {
                    
                    if ( e.stock_status == "stock") {
                        if ( elem.hasClass("moreProdExpand")) {
                            var elemPieces = elem.attr('id').split('_');
                            var prodId =  elemPieces[1];
                            countCartj($('#moreProdRecap_'+prodId),+1);
                            moreProd(elem, 1);
                        }
                        if ( elem.hasClass("moreProdRecap")) {
                            if ( elem2 != undefined) {
                                countCartj(elem2,1);
                            } else {
                                countCartj(elem,1);
                            }
                        }
                        
                        refreshExpandCart();
                        
                        $('.noticeCart').effect("highlight", {
                            color:"#eeecee"
                        }, 2000);
                         
                        $('.noticeCart').fadeOut(1000);
                       
                    } else {
                        $('.warningCart').effect("highlight", {
                            color:"#eeecee"
                        }, 2000);
                        $('.warningCart').fadeOut(1000);
                        
                    }
                } else {
                    if ( e.stock_status == "stock") {
                        //mise à jour panier normal
                        $('#dynamicOrderInfo').load(
                            '/order/countProduct?new=1&random='+new Date().getTime(),
                            null,
                            function(e){
                                $('.noticeCart').effect("highlight", {
                                    color:"#eeecee"
                                }, 2000);
                                $('.noticeCart').fadeOut(1000);
                                $('.endCommande').load('/order/finishLink');
                            }
                            );
                    } else {
                        $('.warningCart').effect("highlight", {
                            color:"#eeecee"
                        }, 2000);
                        $('.warningCart').fadeOut(1000);
                    }
                }
                
                hideMaskLoader();
                
            }
        });
    
    }

    function moreProd(elt, inc)
    {
        var eltParent = elt.parent();
        var val = parseInt($('input', eltParent).val());
        if ((val > 1 && inc == -1) || inc == 1){
            $('input', eltParent).val(val + inc);
        }
    }


    /**
 * mise à jour panier étendu
 * @return void
 */
    function refreshExpandCart()
    {


        $.get('/order/refreshExpandCart',{
            "no_cache": new Date().getTime()
            },function(response) {
            $('#expandCard').html(response);
            $('.moreProdExpand').click(function(e){
                e.preventDefault();
                showMaskLoader();
                updateCart(1,$(this));
            });
            $('.lessProdExpand').click(function(e){
                e.preventDefault();
                showMaskLoader();
                var elemPieces = $(this).attr('id').split('_');
                var prodId =  elemPieces[1];
                countCartj($('#lessProdRecap_'+prodId),-1);
                moreProd($(this), -1);
                updateCart(1,$(this));
            });

        });


    }

    function countCartj(bt, inc)
    {
        var input = $('input', bt.parent());
        var qte = parseInt(input.val()) + inc;
        var id = input.attr('rel');

        
        $('#zoneCart').load(
            "/catalog/countCart",
            {
                qte: qte,
                id: id
            },function(){
                $('.lessProdRecap').click(function(e){
                    e.preventDefault();
                    showMaskLoader();
                    countCartj($(this),-1);
                    if ($('#expandCard').length > 0 ) {
                        
                        var elemPieces = $(this).attr('id').split('_');
                        var prodId =  elemPieces[1];
                        updateCart(1,$('#lessProdExpand_'+prodId));
                    } else {
                        updateCart(1,$(this));
                    }
                });
                
                // bouton +
                $('.moreProdRecap').click(function(e){
                    e.preventDefault();
                    showMaskLoader();
                    if ($('#expandCard').length > 0 ) {
                        var elemPieces = $(this).attr('id').split('_');
                        var prodId =  elemPieces[1];
                        updateCart(1,$('#moreProdExpand_'+prodId));
                    } else {
                        countCartj($(this),+1);
                        updateCart(1,$(this));
                    }
                });
            });

    }
    
    function showMaskLoader() {
        $('#mask_loader').show();
        
        
    }
    
    function hideMaskLoader() {
        $('#mask_loader').hide();
        
        
    }

  
});





