

window.appendLoadEvent(function () {
    
    window.vpc = {};
    
    var self = window.vpc;
    
    window.vpc.$ = {
		initState: 'closed',  // at init time, basket should be 'closed' or 'opened' !
        timer: {
            animation: {
                delay: 1500
            }
        },
        init: {
            addProductLink: function () {
                if (window.navigator
                        && window.navigator.vendor
                        && window.navigator.vendor.toLowerCase().trim() == 'firefox') {
                    var vs = window.navigator.vendorSub.split('.');
                    if (((vs.length < 2) && (vs[0] < 2))
                            || ((vs[0] < 2) && (vs[1] < 2))) {
                        return true;
                    }
                }
                var contener = $coalesce(arguments[0], self);
                
                //alert('contener:' + contener.id);
                //alert(contener.innerHTML);
                
                $(contener).find('a[@href*=/vpc/basket.php?action=]').each(function () {
                    //alert('found link:' + this.href);
                    
                    var self = this;
                    
                    $(this).find('img').each(function () {
                        this.sources = {
                            original : this.src,
                            waiting : '/ressources/_global/images/bt_ajouter_aupanier_encours.gif',
                            complete : '/ressources/_global/images/bt_ajouteraupanier_ok.gif',
                            add: '/ressources/_global/images/bt_ajouter_aupanier_encours.gif',
                            remove: '/ressources/_global/vpc/images/ajax-indicator.small.gif',
                            animation : {
                                delay : window.vpc.$.timer.animation.delay,
                                perform : function () {
                                    
                                    if ($.browser.msie) {
                                        this.src = this.sources.original;
                                        return;
                                    }
                                    
                                    $(this).fadeOut('fast', function () {
                                        this.src = this.sources.original;
                                        $(this).fadeIn('slow');
                                    });

                                    
                                    
                                }
                            }
                        };
                        
                        if (0 <= self.href.indexOf('action=remove')) {
                            this.sources.waiting = this.sources.remove;
                        }
                        
                    });
                    
                    
                    
                    this.originalHref = this.href;
                    
                    this.href += '&ajax=on';
                    
                    //alert('new link:' + this.href);
                    
                    this.$ = {};
                    
                    this.$.ajax = {
                        inAction : false,
                        addProduct : function () {
                            var imgLength = $(self).find('img').each(function () {
                                this.src = this.sources.waiting;
                            }).size();
                            
                            
                            if (imgLength < 1) {
                                $(self).empty().append('<img src="/ressources/_global/vpc/images/ajax-indicator.small.gif"  alt="..." />');
                                
                            }
                            
                            setTimeout(function () {
                                $.get(self.href, {}, function (data) {
                                    var main = document.createElement('div');
                                    
                                    
                                    // le caddie existe deja ?
                                    var basketIsEmpty = window.vpc.$.basket.isEmpty();
                                    
                                    
                                    $(main).append(data);
                                    
                                    // les erreurs
                                    var errors = $(main).find('div.errors_contener').children().get();
                                    if (0 < errors.length) {
                                        document.location = self.originalHref;
                                        self.$.ajax.inAction = false;
                                        return;
                                    }
                                    $(main).find('div.errors_contener').remove();
                                    
                                    
                                    // panier cacher ?
                                    var basketIsShown = true;
                                    if (window.vpc.$.init.miniBasketIsHidden() == true) {
                                        $(main).find('div.toggle_mini_basket_hide').hide();
                                        $(main).find('div.toggle_mini_basket_show').show();
                                        
                                        $(main).find('div[@id=bloc_panier_effect]').hide();
                                        
                                        basketIsShown = false;
                                    } else {
                                        $(main).find('div.toggle_mini_basket_hide').show();
                                        $(main).find('div.toggle_mini_basket_show').hide();
                                        $(main).find('div[@id=bloc_panier_effect]').show();
                                        
                                    }
                                    
                                    $('#bloc_panier').empty().append(main.innerHTML);
                                    
                                    
                                    $(self).find('img').each(function () {
                                        var thisImage = this;
                                        if (!thisImage.sources) {
                                            return;
                                        }
                                        thisImage.src = thisImage.sources.complete;
                                        setTimeout(function () {
                                            thisImage.sources.animation.perform.apply(thisImage);
                                            
                                        }, thisImage.sources.animation.delay);
                                    });
                                    
                                    
                                    var target = $id('bloc_panier_center');
                                    if (target) {
                                        window.vpc.$.init.addProductLink(target);
                                        window.vpc.$.init.setInitToggleEffect();
                                    }
                                    
                                    
                                    self.$.ajax.after(basketIsEmpty, basketIsShown);
                                    
                                    self.$.ajax.inAction = false;
                                    
                                    return;
                                    
                                });
                            }, 10);
                            
                            return true;
                            
                        },
                        after: function (oldIsEmpty, basketIsShown) {
                            //window.vpc.$.init.setToggleEffect();
                            if (oldIsEmpty == true && window.vpc.$.basket.isEmpty() != true) {
                                window.vpc.$.init.showEffect();
                                
                            } else {
                                // on le montre !!
                                if (!window.vpc.$.basket.isEmpty()) {
                                    window.vpc.$.init.showEffect();
                                } else {
                                    $('#bloc_panier_top').find('div.toggle_mini_basket_hide').hide();
                                    $('#bloc_panier_top').find('div.toggle_mini_basket_show').hide();
                                }
                                
                                
                                
                            }
                            
                        },
                        removeProduct : function () {
                            return self.$.ajax.addProduct();
                            
                        },
                        performAction : function () {
                            if (self.$.ajax.inAction == true) {
                                return false;
                            }
                            self.$.ajax.inAction = true;
                            
                            //alert(self.href + '\n' + self.href.indexOf('action=add'));
                            
                            
                            if (-1 < self.href.indexOf('action=add')) {
                                self.$.ajax.addProduct();
                                
                            } else {
                                self.$.ajax.removeProduct();
                                
                            }
                        }
                    }
                    
                    
                    
                }).bind('click', function () {
                    this.$.ajax.performAction();
                    
                    return false;
                });
                
                
            },
            
            selectProductLink: function () {
                if (window.navigator
                        && window.navigator.vendor
                        && window.navigator.vendor.toLowerCase().trim() == 'firefox') {
                    var vs = window.navigator.vendorSub.split('.');
                    if (((vs.length < 2) && (vs[0] < 2))
                            || ((vs[0] < 2) && (vs[1] < 2))) {
                        return true;
                    }
                }
                if (!window.$user || !window.$user.logged) {
                    return;
                }
                
                
                var contener = $coalesce(arguments[0], self);
                
                $(contener).find('a[@href*=/vpc/users/suggestions/ma-selection.php?action=]').each(function () {
                    
                    this.originalHref = this.href;
                    
                    this.href += '&ajax_product=on';
                    
                    var self = this;
                    
                    
                    $(this).find('img').each(function () {
                        this.sources = {
                            original : this.src,
                            state: 'add',
                            add: {
                                normal: '/ressources/_global/images/images_vpc/bt_ajouter_selection.gif',
                                waiting: '/ressources/_global/images/images_vpc/bt_selection_ajoutencours.gif',
                                complete: '/ressources/_global/images/images_vpc/bt_ajouter_selection_ok.gif'
                            },
                            remove: {
                                normal: '/ressources/_global/images/images_vpc/bt_retirer_selection.gif',
                                waiting: '/ressources/_global/images/images_vpc/bt_selection_suppencours.gif',
                                complete: '/ressources/_global/images/images_vpc/bt_retirer_selection_ok.gif'
                            },
                            current: null,
                            animation : {
                                delay : window.vpc.$.timer.animation.delay,
                                perform : function () {
                                    this.sources.action = (this.sources.action == 'add') ? 'remove' : 'add';
                                    this.sources.current = this.sources[this.sources.action];
                                    
                                    if ($.browser.msie) {
                                        this.src = this.sources.current.normal;
                                        return;
                                    }
                                    
                                    $(this).fadeOut('fast', function () {
                                        this.src = this.sources.current.normal;
                                        $(this).fadeIn('slow');
                                    });

                                    
                                    
                                }
                            }
                        };
                        
                        this.sources.action = (0 <= self.href.indexOf('action=remove')) ? 'remove' : 'add';
                        
                        this.sources.current = this.sources[this.sources.action];
                    });
                    
                    self.$ = {
                        ajax : {
                            inAction : false,
                            addProduct : function () {
                                $(self).find('img').each(function () {
                                    this.src = this.sources.current.waiting;
                                });
                                
                                
                                setTimeout(function () {
                                    $.get(self.href, {}, function (data) {
                                        var main = document.createElement('div');
                                        //alert(data);
                                        $(main).append(data);
                                        // les erreurs
                                        var errors = $(main).find('div.errors_contener').children().get();
                                        if (0 < errors.length) {
                                            document.location = self.originalHref;
                                            self.$.ajax.inAction = false;
                                            return;
                                        }
                                        
                                        
                                        /*
                                        echo '<div class="info_contener" >';
                                        echo '<input type="hidden" name="url" value="' . $link_basket . '" />';
                                        echo '<input type="hidden" name="img" value="' . $img_src . '" />';
                                        echo '</div>';
                                        */
                                        
                                        var info = $(main).find('div.info_contener').find('input[@name=url]').get();
                                        if (0 < info.length) {
                                            self.href = info[0].value;
                                        }
                                        
                                        $(self).find('img').each(function () {
                                            var thisImage = this;
                                            if (!thisImage.sources) {
                                                return;
                                            }
                                            
                                            thisImage.src = thisImage.sources.current.complete;
                                            setTimeout(function () {
                                                thisImage.sources.animation.perform.apply(thisImage);
                                                
                                            }, thisImage.sources.animation.delay);
                                        });
                                        
                                        
                                        
                                        self.$.ajax.inAction = false;
                                    });
                                }, 10);
                                
                                return true;
                                
                            },
                            removeProduct : function () {
                                return self.$.ajax.addProduct();
                                
                            },
                            performAction : function () {
                                if (self.$.ajax.inAction == true) {
                                    return false;
                                }
                                self.$.ajax.inAction = true;
                                
                                //alert(self.href + '\n' + self.href.indexOf('action=add'));
                                
                                if (-1 < self.href.indexOf('action=add')) {
                                    self.$.ajax.addProduct();
                                    
                                } else {
                                    self.$.ajax.removeProduct();
                                    
                                }
                            }
                        }
                    }
                    
                    
                    
                    
                    
                    
                }).bind('click', function () {
                    this.$.ajax.performAction();
                    return false;
                });
            
                
                
            },
            
            setInitToggleEffect: function () {
                
                
                window.vpc.$.init.setToggleEffect();
            },
            
            hideEffect: function () {
                if (window.vpc.$.init.miniBasketIsHidden() == true) {
                    return;
                }
                
                var self = arguments[0] || $('#bloc_panier_top').find('div.toggle_mini_basket_hide').find('a').get()[0];
                
                if (window.vpc.$.basket.isEmpty()) {
                    return false;
                }
                
                
                self.$inAction = $coalesce(self.$inAction, false);
                if (self.$inAction == true) {
                    return false;
                }
                
                self.$inAction = true;
                
                var basketHide = $('#bloc_panier_top').find('div.toggle_mini_basket_hide').get();
                if (basketHide.length < 1) {
                    return true;
                }
                basketHide = basketHide[0];
                
                var basketShow = $('#bloc_panier_top').find('div.toggle_mini_basket_show').get();
                if (basketShow.length < 1) {
                    return true;
                }
                basketShow = basketShow[0];
                
                // il faut cacher
                //$(basketHide).hide();
                $(basketHide).css({visibility: 'hidden'});
                /*
                if ($.browser.msie) {
                    $(basketHide).hide();
                    
                } else {
                    $(basketHide).hide();
                    //$(basketHide).fadeOut('slow', function () {
                    //    $(basketHide).hide();
                    //});
                }
                */
                
                $('#bloc_panier_effect:visible').slideToggle('slow', function () {
                    self.$inAction = false;
                    
                    setTimeout(function () {
                        $(basketHide).css({visibility: 'visible', display: 'none'});
                        $(basketShow).css({visibility: '', display: ''});
                    }, 10);
                    /*
                    if ($.browser.msie) {
                        $(basketShow).show();
                    
                    } else {
                        $(basketShow).fadeIn('slow', function () {});
                        
                    }
                    */
                    
                });
                
            },
            
            showEffect: function () {
                if (window.vpc.$.init.miniBasketIsHidden() != true) {
                    return;
                }
                
                var self = arguments[0] || $('#bloc_panier_top').find('div.toggle_mini_basket_show').find('a').get()[0];
                
                if (window.vpc.$.basket.isEmpty()) {
                    return false;
                }
                
                self.$inAction = $coalesce(self.$inAction, false);
                if (self.$inAction == true) {
                    return false;
                }
                
                self.$inAction = true;
                
                var basketHide = $('#bloc_panier_top').find('div.toggle_mini_basket_hide').get();
                if (basketHide.length < 1) {
                    return true;
                }
                basketHide = basketHide[0];
                
                var basketShow = $('#bloc_panier_top').find('div.toggle_mini_basket_show').get();
                if (basketShow.length < 1) {
                    return true;
                }
                basketShow = basketShow[0];
                
                // il faut cacher
                //$(basketShow).hide();
                $(basketShow).css({visibility: 'hidden'});
                
                /*
                if ($.browser.msie) {
                    $(basketShow).hide();
                    
                } else {
                    $(basketShow).hide();
                    
                    //$(basketShow).fadeOut('slow', function () {
                    //    $(basketShow).hide();
                    //});
                }
                */
                
                $('#bloc_panier_effect:hidden').slideToggle('slow', function () {
                    self.$inAction = false;
                    setTimeout(function () {
                        $(basketShow).css({visibility: 'visible', display: 'none'});
                        $(basketHide).css({visibility: '', display: ''});
                    }, 10);
                    
                    /*
                    if ($.browser.msie) {
                        $(basketHide).show();
                    
                    } else {
                        $(basketHide).fadeIn('slow', function () {});
                        
                    }*/
                });
            },
            
            toggleEffect: function () {
                if (window.vpc.$.basket.isEmpty()) {
                    return;
                }
                
                if (window.vpc.$.init.miniBasketIsHidden() != true) {
                    // il faut le cacher
                    window.vpc.$.init.hideEffect();
                    
                } else {
                    // il faut le montrer
                    window.vpc.$.init.showEffect();
                    
                }
                
            },
            
            miniBasketIsHidden: function () {
                if (0 < $('#bloc_panier_top').find('div.toggle_mini_basket_hide').filter(':visible').get().length) {
                    // il faut le cacher
                    return false;
                    
                }
                
                return true;
            },
            
            setToggleEffect: function () {
                $('#bloc_panier_top').find('div.toggle_mini_basket_hide').find('a').unbind('click').bind('click', function () {
                    window.vpc.$.init.toggleEffect();
                    //window.vpc.$.init.hideEffect(this);
                    
                    return false;
                    
                });
                
                
                $('#bloc_panier_top').find('div.toggle_mini_basket_show').find('a').unbind('click').bind('click', function () {
                    window.vpc.$.init.toggleEffect();
                    //window.vpc.$.init.showEffect(this);
                    return false;
                });
            }
            
            
            
            
            
        },
        basket: {
            count: function () {
                var counter = $id('basket_items_counter');
                if (counter == null) {
                    return 0;
                }
                return Number.parse.integer(counter.innerHTML, 0);
            },
            isEmpty: function () {
                return window.vpc.$.basket.count() < 1 ? true : false;
            }
        }
    }
    
    
});


// bloc ajout de produit dans le panier
window.appendLoadEvent(function () {
    
    
    var target = $id('add_produit');
    if (target) {
        window.vpc.$.init.addProductLink(target);
        window.vpc.$.init.selectProductLink(target);
    }
    
    
    target = $id('bloc_panier_center');
    if (target) {
        window.vpc.$.init.addProductLink(target);
    }
    
    window.vpc.$.init.setInitToggleEffect();
    
    /*
    if ( target && window.vpc.$.initState == 'closed' ) {
        $('#bloc_panier_top').find('div.toggle_mini_basket_hide').find('a').click();
    }
    */
    
    /*
    if (window.vpc.$.basket.isEmpty()) {
        $('div.toggle_mini_basket_show').css('display','none');
    }
    */
});


