var initialLayerHeight = 0;
jQuery(document).ready(function() {
    (function($) {
        
        Shadowbox.init();
        
        /*HOVER ITEM*/
        $('.products-grid .item').hover(function() {
            $(this).addClass('over');
        }, function() {
            $(this).removeClass('over');
        });
        
        
        $('#home-slideshow .infos-silhouettes').hover(function() {
            $('.infos-silhouettes .slider-effect-leef-active').show();
            $('#home-slideshow .infos-silhouettes a.opener').addClass('opened');
        }, function() {
            $('.infos-silhouettes .slider-effect-leef').hide();
            $('#home-slideshow .infos-silhouettes a.opener').removeClass('opened');
        });
        
        /*FILTRES*/
        initialLayerHeight = $('.block-layered-nav .block-content').height();
        $('.layered-bar .block-title').click(function() {
            swithFiltres();
        });
        $('.layered-bar .no-filter-applied').click(function() {
            swithFiltres();
        });
        $('.layered-bar .layer-opener').click(function() {
            swithFiltres();
        });
        
        $('#narrow-by-list .one-filter:last').addClass('one-filter-last');
        
        
        var ua = $.browser;
        if ( !(ua.msie && ua.version.slice(0,2) == "7." ) && !(ua.msie && ua.version.slice(0,2) == "6." ) ) {
            $("select").selectBox();
        }
        
        
        /*-----MOUSE OVER THUMB-----*/
        $('.more-views li').mouseover(function() {
            $('.product-image #image').attr('src', $('img', $(this)).attr('rel'));
            $('.product-image .fancy-fullscreen').attr('href', $('img', $(this)).attr('relbig'));
        });
        
        $('.header-account-access').hover(function() {
            $(this).toggleClass('header-account-access-hover');
        })
        $('.header-account-access-logged').hover(function() {
            $(this).toggleClass('header-account-access-logged-hover');
        })
        
        /*MAP*/
        $('#Map-pv area').click(function() {
            showMagasin(this);
        });
        $('#Map-pv area').qtip({
           content: $(this).attr('tooltip'),
           show: 'mouseover',
           hide: 'mouseout',
           position: {
              corner: {
                 target: 'rightMiddle',
                 tooltip: 'bottomLeft'
              }
           }
        })
        
        /*GUIDE DES TAILLES*/
        $('.guide_taille').fancybox({
            content: $('#guide_taille_content').html()
        });
        
        
        /*FULL SCREEN*/
        $('.fancy-fullscreen').click(function(e) {
            e.preventDefault();
            $('#fancyfull .fancycontainer .zoom-image').html('<img src="'+$(this).attr('href')+'" />');
            $('#fancyfull').show();
            $(window).scrollTop(0);
        });
        
        /*KDO*/
    	$('#openKdo').fancybox({
            content: $('#kdo_content').html()
        });
        
        
        /*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;" onclick="closeAlertProduct();"></div>');
                }
            });
        }        
        
    })(jQuery);
});

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


function swithFiltres() {
    (function($) {    
        if($('.block-layered-nav').hasClass('closed')) {
            //on ouvre
            $('.block-layered-nav').removeClass('closed');
            $('.block-layered-nav .block-content').css({height: 0}).show().animate({height: initialLayerHeight+'px'}, 500, function() {
                
            });
        }
        else {
            //on ferme
            $('.block-layered-nav').addClass('closed');
            $('.block-layered-nav .block-content').animate({height: '0px'}, 500, function() {
                $(this).hide();
            });
        }
        
    })(jQuery);
}

function openBlog(elem) {
    (function($) {    
        if(!$(elem).hasClass('postWrapperOpened')) {
            $('.postWrapperOpened').removeClass('postWrapperOpened');
            $(elem).addClass('postWrapperOpened');
        }
    })(jQuery);
}

var already_opened = false;
function showMagasin(elem) {
    (function($) {
        
        if(!already_opened) {
            $('.illustration').animate({width: '0px'}, 300);
            $('.boutiques-list-wrap').animate({width: '628px'}, 300, function() {
                $('.boutique-list').hide();
                $('#boutique-'+$(elem).attr('href').replace('#', '')).fadeIn();
            });
            already_opened = true;
        }
        else {
            $('.boutique-list').hide();
            $('#boutique-'+$(elem).attr('href').replace('#', '')).fadeIn();
        }
        
    })(jQuery);    
}

function submitCartForm() {
    (function($) {
    	$('#gift_hidden').val($('.gift_message:last').val());
    	$('#cart_form').submit();
    })(jQuery);
}
