/*VARIABLES*/
var nb_home_slide=0;
var home_slideshow_timer;
var home_slideshow_pos = 1;
var home_slideshow_delay = 5000;

jQuery(document).ready(function() {
    (function($) {
        /*SLIDESHOW*/
        if($('.home-slideshow').length>0) {
            $('.home-slideshow li').each(function() {
                nb_home_slide++; 
                $(this).attr('id', 'slide'+nb_home_slide);
            });
            
            for(var i = 0; i < nb_home_slide; i++) {
                if(i==0) {
                    $('.home-slideshow-thumb').append('<div class="active slide-btn" id="slide-btn-'+(i+1)+'" onclick="gotoHomeSlide('+(i+1)+');">'+(i+1)+'</div>');
                }
                else {
                    $('.home-slideshow-thumb').append('<div class="slide-btn" id="slide-btn-'+(i+1)+'" onclick="gotoHomeSlide('+(i+1)+');">'+(i+1)+'</div>');
                }
            }
            
            home_slideshow_timer = window.setTimeout("doHomeSlide()", home_slideshow_delay);
        }
        
        /*CUFON*/
        Cufon.replace('.bradley', {fontFamily: 'Bradley Hand ITC TT'});
        Cufon.replace('.special-price span.price', {fontFamily: 'Garamond'});
        Cufon.replace('.regular-price span.price', {fontFamily: 'Garamond'});
        
        /*HOVER ITEM PRODUCT LIST*/
        $('.products-grid .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();
        });
        
        
        /*+ et - sur les quantités*/
        $('input.qty').each(function() {
            $(this).wrap('<div class="wrap-qty"></div>');
            $(this).parent().append('<div class="qty-plus" onclick="qtyPlus(this);">+</div>');
            $(this).parent().append('<div class="qty-moins" onclick="qtyMoins(this);">-</div>');            
        });
        
        /*-----nyroImage-----*/
        Shadowbox.init();

        /**/
        $('.top-cart').hover(function(){
            $('.top-cart-hover').show();
        }, function() {
            $('.top-cart-hover').hide();
        });
        
        /*PRODUCT ALERT*/
        if($('.advisa-product-alert').length>0)
        {
            $('.add-to-box').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;"></div>');
                }
            });
        }
        
        /*NARROW OPEN FILTER*/
        $('#narrow-by-list dt').click(function() {
            if(!$(this).hasClass('layer-dt-manufacturer') && !$(this).hasClass('layer-dt-color'))
            {
                $(this).next().toggle();
            }
        });

    })(jQuery);
});

function closeAlertProduct()
{
    jQuery('.advisa-product-alert').hide();
    jQuery('#IB_overlay').hide();
}

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);
}

function doHomeSlide() {
    (function($) {
        
        var next_pos = home_slideshow_pos+1;
        if(home_slideshow_pos == nb_home_slide) {
            next_pos = 1; //retour au début
        }

        $('#slide'+next_pos).css({zIndex: 1}).show();
        $('#slide'+home_slideshow_pos).css({zIndex: 2}).fadeOut();
        
        $('.home-slideshow-thumb .slide-btn').removeClass('active');
        $('.home-slideshow-thumb #slide-btn-'+next_pos).addClass('active');


        home_slideshow_pos = next_pos;
        home_slideshow_timer = window.setTimeout("doHomeSlide()", home_slideshow_delay);
    
    })(jQuery);
}

function gotoHomeSlide(position) {
    (function($) {
        if(position != home_slideshow_pos) {
            
            window.clearTimeout(home_slideshow_timer);

            var next_pos = position;

            $('#slide'+next_pos).css({zIndex: 1}).show();
            $('#slide'+home_slideshow_pos).css({zIndex: 2}).fadeOut();

            $('.home-slideshow-thumb .slide-btn').removeClass('active');
            $('.home-slideshow-thumb #slide-btn-'+next_pos).addClass('active');


            home_slideshow_pos = next_pos;
            home_slideshow_timer = window.setTimeout("doHomeSlide()", home_slideshow_delay);
            
        }
    
    })(jQuery);    
}




/*home little slide*/

var nb_slide = new Array();
nb_slide['home-nouveautes'] = 0;
nb_slide['home-promotions'] = 0;
nb_slide['home-avis'] = 0;

var current_pos = new Array();
current_pos['home-nouveautes'] = 0;
current_pos['home-promotions'] = 0;
current_pos['home-avis'] = 0;

var timer = new Array();
timer['home-nouveautes'];
timer['home-promotions'];
timer['home-avis'];

var delay = new Array();
delay['home-nouveautes'] = 4000;
delay['home-promotions'] = 4000;
delay['home-avis'] = 4000;

var animation_delay = new Array();
animation_delay['home-nouveautes'] = 400;
animation_delay['home-promotions'] = 400;
animation_delay['home-avis'] = 400;

var locked = new Array();
locked['home-nouveautes'] = false;
locked['home-promotions'] = false;
locked['home-avis'] = false;

jQuery(document).ready(function() {
    (function($) {
        
        $('.slider .thumbs a').click(function(event) {
            event.preventDefault();
        });
        
        $('.slider').each(function() {
            
                var current_id = $(this).attr('id');
            
                $('.slide-thumb', $(this)).each(function() {
                    if(nb_slide[current_id] == 0) {
                        $(this).addClass('current');
                    }
                    nb_slide[current_id]++;
                });
                if(nb_slide[current_id]>1) { //doSlide
                    timer[current_id] = window.setTimeout(function() { doLittleSlide(current_id) }, delay[current_id]);
                    
                    $('#'+current_id+' .thumbs a').click(function(event) {
                        gotoLittleSlide($(this).attr('id').replace('slide-thumb', ''), current_id);
                    });
                }
                else { //hide arrows
                    $('#'+current_id+' .thumbs a').hide();
                    $('#'+current_id+' .thumbs a').hide();
                }        
            
        });
        
        
        
    })(jQuery);    
});



function doLittleSlide(slideid) {
    (function($) {
        locked[slideid] = true;
        var next_pos = current_pos[slideid]+1;
        if(next_pos>=nb_slide[slideid]) {
            next_pos=0;
        }
        
        if(next_pos > current_pos[slideid]) {
            $('#'+slideid+' .slide'+current_pos[slideid]).animate({left: '-200px'}, animation_delay[slideid], function() {
                $(this).removeClass('current');
                locked[slideid] = false;
            });
            $('#'+slideid+' .slide'+next_pos).css({left: '200px'}).animate({left: '0'}, animation_delay[slideid]);
        }
        else {
            $('#'+slideid+' .slide'+current_pos[slideid]).animate({left: '200px'}, animation_delay[slideid], function() {
                $(this).removeClass('current');
                locked[slideid] = false;
            });
            $('#'+slideid+' .slide'+next_pos).css({left: '-200px'}).animate({left: '0'}, animation_delay[slideid]);
        }
        
        $('#'+slideid+' .slide-thumb').removeClass('current');
        $('#'+slideid+' #slide-thumb'+next_pos).addClass('current');
        current_pos[slideid] = next_pos;
        
        timer[slideid] = window.setTimeout(function() { doLittleSlide(slideid) }, delay[slideid]);
    })(jQuery);
}


function gotoLittleSlide(position, slideid) {
    (function($) {
        if(position!=current_pos[slideid] && !locked[slideid]) {
            
                window.clearTimeout(timer[slideid]);
            
                locked[slideid] = true;
                var next_pos = position;

                if(next_pos > current_pos[slideid]) {
                    $('#'+slideid+' .slide'+current_pos[slideid]).animate({left: '-200px'}, animation_delay[slideid], function() {
                        $(this).removeClass('current');
                        locked[slideid] = false;
                    });
                    $('#'+slideid+' .slide'+next_pos).css({left: '200px'}).addClass('current').animate({left: '0'}, animation_delay[slideid]);
                }
                else {
                    $('#'+slideid+' .slide'+current_pos[slideid]).animate({left: '200px'}, animation_delay[slideid], function() {
                        $(this).removeClass('current');
                        locked[slideid] = false;
                    });
                    $('#'+slideid+' .slide'+next_pos).css({left: '-200px'}).addClass('current').animate({left: '0'}, animation_delay[slideid]);                    
                }
                $('#'+slideid+' .slide-thumb').removeClass('current');
                $('#'+slideid+' #slide-thumb'+next_pos).addClass('current');
                
                current_pos[slideid] = next_pos;

                timer[slideid] = window.setTimeout(function() { doLittleSlide(slideid) }, delay[slideid]);
                
        }
    })(jQuery);    
}
