$(document).ready(function(){
    $("select").sb({ fixedWidth: true });
    if($("#headers-box .header-banner-box").length > 1){
        setTimeout(rotate_banner, 3000);    
    }
    
    $("#reload-other-products").click(function(){
        if( ! $(this).parent().hasClass('loading')) {
            $(this).parent().addClass('loading');
            
            $.get(base_url + 'collections/reloadproducts/'+$(this).attr('rel'), function(data){
               $('#model-other-models').html(data);
            })
            .complete(function(){
               $("#reload-other-products").parent().removeClass('loading');
            });
        }
    });
    
    $(".size-chart-link").fancybox({
            'width'		: 900,
            'height'		: 450,
            'autoScale'         : false,
            'transitionIn'      : 'none',
            'transitionOut'     : 'none',
            'type'		: 'iframe'
    });
    
    $("body").click(function(){
        if($("#facebook-box").hasClass('opened')){
            $('#facebook-box .facebook-opener').click();
        }
    });
    
    $('#facebook-box .facebook-opener').click(function(e){
        if($("#facebook-box").hasClass('opened')){
            $("#facebook-box").removeClass('opened').find(".facebook-content").hide();
        }else{
            $("#facebook-box").addClass('opened').find('.facebook-content').show();
        }
        e.stopPropagation();
    });
});

function rotate_banner(){
    var current_item = $("#headers-box .header-banner-box:visible");
    
    //alert(current_item.prev().length);
    var next_item = current_item.next(".header-banner-box");
    if(next_item.length == 0) {
        next_item = $("#headers-box .header-banner-box").first();
    }
    
    $("#header-banners-navigation .header-banner-navi-item").removeClass('current');
    
    $("#headers-box .header-banner-box").hide();
    next_item.fadeIn();
    $("#navi-"+next_item.attr('id')).addClass('current');
    
    setTimeout(rotate_banner, 3000);    
}
