jQuery(document).ready(function() {
    (function($) {
        
        /*MENU*/
        $('#nav li').hover(function() {
            if(!$(this).parent().hasClass('secondary') && !$(this).hasClass('li-secondary')) {
                $('#nav li.active').removeClass('active').addClass('active-bup');
                if($(this).hasClass('active-bup')) {
                    $(this).removeClass('active-bup').addClass('active');
                }
            }
        }, function() {
            if(!$(this).parent().hasClass('secondary') && !$(this).hasClass('li-secondary')) {
                $('#nav li.active-bup').removeClass('active-bup').addClass('active');
            }            
        });
        $('#nav li a').mouseover(function() {
            if($(this).attr('rel')!='') {
                var src_img = $(this).attr('rel');
                
                $(this).parents('#nav').find('li.level0 .category-image').html('<img src="'+src_img+'" alt=" " />');
            }
        });
        
        /*HOVER PRODUCT*/
        $('.products-grid .item').hover(function() {
            $(this).addClass('item-hover');
        }, function() {
            $(this).removeClass('item-hover');
        });
        $('.product-listing .item').hover(function() {
            $(this).addClass('item-hover');
        }, function() {
            $(this).removeClass('item-hover');
        });
        

        /*-----ANIMATION ONGLETS-----*/
        $('.tab-effect .tab').click(function(){
            $(this).parent().children('.tab').removeClass('selected');
            $(this).addClass('selected');
            var tab_id = $(this).attr('rel');
            $(this).parent().parent().children('.tab-content').children('.content-div').hide();
            $(this).parent().parent().children('.tab-content').children('.content'+tab_id).show();
        });
        
        /*STARS*/
        $('.rel-star1').hover(function() {
            $('.star').hide();
            $('.star1').show();
        }, function() {
            $('.star').hide();            
            $('.star0').show();
        });
        $('.rel-star2').hover(function() {
            $('.star').hide();
            $('.star2').show();
        }, function() {
            $('.star').hide();            
            $('.star0').show();
        });
        $('.rel-star3').hover(function() {
            $('.star').hide();
            $('.star3').show();
        }, function() {
            $('.star').hide();            
            $('.star0').show();
        });
        $('.rel-star4').hover(function() {
            $('.star').hide();
            $('.star4').show();
        }, function() {
            $('.star').hide();            
            $('.star0').show();
        });
        $('.rel-star5').hover(function() {
            $('.star').hide();
            $('.star5').show();
        }, function() {
            $('.star').hide();            
            $('.star0').show();
        });
        
        /*BESTSELLER*/
        count_best_slide = $('.home-bestseller ul li').length;
        timer_best = window.setTimeout("doBestseller()", 6000); 
        
        $('.best-previous').click(function() {
            if(!best_lock) {
                var $left = parseInt($('.home-bestseller ul').css('left').replace('px', ''));
                if($left < 0) {
                    best_lock = true;
                    $('.home-bestseller ul').animate({left: ($left+198)+'px'}, 200, function() {
                        best_lock = false;
                    });
                }
            }
            
            window.clearTimeout(timer_best);
            timer_best = window.setTimeout("doBestseller()", 6000); 
        });
        $('.best-next').click(function() {
            if(!best_lock) {
                var $left = parseInt($('.home-bestseller ul').css('left').replace('px', ''));
                if(count_best_slide>2 && $left > ((count_best_slide-2)*-198)) {
                    best_lock = true;
                    $('.home-bestseller ul').animate({left: ($left-198)+'px'}, 200, function() {
                        best_lock = false;
                    });
                }
            }
            
            window.clearTimeout(timer_best);
            timer_best = window.setTimeout("doBestseller()", 6000); 
        });
        
        /*FULL SCREEN*/
        $('.fancy-fullscreen').click(function(e) {
            e.preventDefault();
            $('#fancyfull .fancycontainer .zoom-image').html('<img src="'+$(this).attr('href')+'" />');
            
            $('#fancyfull').css({opacity: 0, top:'-1500px'}).show().animate({top:'0px', opacity: 1}, 1000, function() {
                
            });
            
            $('.zoom-tumbnails').html('<ul>'+$('.more-views ul').html()+'</ul>');
            
            $(window).scrollTop(0);
            
                    /*-----MOUSE OVER THUMB-----*/
                    $('.zoom-tumbnails li').mouseover(function() {
                        $('#fancyfull .fancycontainer .zoom-image img').attr('src', $('img', $(this)).attr('relbig'));
                    });
                    $('.zoom-tumbnails li a').click(function(e) {
                        e.preventDefault();
                    });
        });

        /*-----MOUSE OVER THUMB-----*/
        $('.more-views li').click(function() {
            $('.more-views li.active').removeClass('active');
            $(this).addClass('active');
            $('.product-image #image').attr('src', $('img', $(this)).attr('rel'));
            $('.product-image #imageHover').attr('src', $('img', $(this)).attr('relhover'));
            $('.product-image .fancy-fullscreen').attr('href', $('img', $(this)).attr('relbig'));
        });
        
        
        /*TAB HOVER*/
        $('.tab-header .tab').hover(function() {
            $(this).toggleClass('hover')
        });
        

        /*PRODUCT ALERT*/
        if($('.advisa-product-alert').length>0)
        {
            $('.add-to-cart').each(function() {
                $(this).append('<div class="advisa-product-alert" style="display:block">'+$('.advisa-product-alert').html()+'</div>');
                if($('#IB_overlay').length==0)
                {
                    $('body').prepend('<div id="IB_overlay" style="opacity: 0.30;background-color: #000 !important;" onclick="closeAlertProduct();"></div>');
                }
            });
        } 
        
        /*PROMOTION MENU*/
        $('.promotion-menu').each(function() {
            $(this).html($('#promotion-menu').html());
        });
        
        
    })(jQuery);
});

function closeAlertProduct()
{
    jQuery('.advisa-product-alert').hide();
    jQuery('#IB_overlay').hide();
}


var count_best_slide
var best_lock = false;
var timer_best;
var best_direction = "right";
function doBestseller() {
    (function($) {
        
        if(best_direction=="right") {
                if(!best_lock) {
                    if($('.home-bestseller ul').length>0)
                    {
                        var $left = parseInt($('.home-bestseller ul').css('left').replace('px', ''));
                        if($left < 0) {
                            best_lock = true;
                            $('.home-bestseller ul').animate({left: ($left+198)+'px'}, 200, function() {
                                best_lock = false;
                            });
                        }
                        else {
                            best_direction = "left";
                        }
                    }
                }
        }
        if(best_direction=="left") {

                    if(!best_lock) {
                        var $left = parseInt($('.home-bestseller ul').css('left').replace('px', ''));
                        if(count_best_slide>2 && $left > ((count_best_slide-2)*-198)) {
                            best_lock = true;
                            $('.home-bestseller ul').animate({left: ($left-198)+'px'}, 200, function() {
                                best_lock = false;
                            });
                        }
                        else {
                            best_direction = "right";
                        }
                    }
        }
        
        window.clearTimeout(timer_best);
        timer_best = window.setTimeout("doBestseller()", 6000); 
        
        
    })(jQuery);
}

function showCommentsTab() {
    (function($) {
        $('.tab-header .selected').removeClass('selected');
        $('.tab-header .tab[rel=3]').addClass('selected');
        $('.tab-content .content-div').hide();
        $('.tab-content .content3').show();
    })(jQuery);
}

function showDescTab() {
    (function($) {
        $('.tab-header .selected').removeClass('selected');
        $('.tab-header .tab[rel=4]').addClass('selected');
        $('.tab-content .content-div').hide();
        $('.tab-content .content4').show();
    })(jQuery);
}

function showEntretienTab() {
    (function($) {
        $('.tab-header .selected').removeClass('selected');
        $('.tab-header .tab[rel=5]').addClass('selected');
        $('.tab-content .content-div').hide();
        $('.tab-content .content5').show();
    })(jQuery);
}
        
/*+ et - sur les quantités*/
function qtyPlus(elem) {
    (function($) {
        $('input.qty', $(elem).parent()).val(parseInt($('input.qty', $(elem).parent()).val())+1);
        if($('input.qty', $(elem).parent()).val() == 'NaN' || $('input.qty', $(elem).parent()).val() < 0) {
            $('input.qty', $(elem).parent()).val('0');
        }
    })(jQuery);
}
function qtyMoins(elem) {
    (function($) {
        $('input.qty', $(elem).parent()).val(parseInt($('input.qty', $(elem).parent()).val())-1);

        if($('input.qty', $(elem).parent()).val() == 'NaN' || $('input.qty', $(elem).parent()).val() < 0) {
            $('input.qty', $(elem).parent()).val('0');
        }
    })(jQuery);
}     


/*PRODUIT CONFIGURABLE SWTICH IMAGE*/
function switchSelect(elem) {
    //on selectionne l'option associée si elle existe
    if(elem != null) {
        jQuery('.product-view select option[value='+jQuery(elem).attr('rel')+']').attr("selected", "selected");
    }
}

function switchMedia(elem) {
    (function($) {
        $('.more-views li[rel='+$(elem).val()+']').each(function() {
            $('.more-views li.active').removeClass('active');
            $(this).addClass('active');
            $('.product-image #image').attr('src', $('img', $(this)).attr('rel'));
            $('.product-image #imageHover').attr('src', $('img', $(this)).attr('relhover'));
            $('.product-image .fancy-fullscreen').attr('href', $('img', $(this)).attr('relbig'));
        });
    })(jQuery);
}


