allBran = {};

(function() {
    allBran.accordion = function(container) {
        $(container + ' dd').hide();
        $(container + ' dt').click(function() {
            var $next = $(this).next();
            var $visibleSiblings = $next.siblings('dd:visible');
            
            if ($visibleSiblings.hasClass('on')) {
                $(container + ' dt.on').removeClass('on');
                $(container + ' dd.on').removeClass('on');
            }
            
            $(this).toggleClass('on');
            
            if ($visibleSiblings.length) {
                $visibleSiblings.slideUp(400, function() {
                    $next.slideToggle(400).toggleClass('on');
                });
            } else {
                $next.slideToggle(400).toggleClass('on');
            }
        });
    };
    
    allBran.getCategory = function(urls) {
        var category;
        /* tests if current url matches one of the rewrite rules or the location.hash & displays the according category */
        /* urls definition in l10n.js */
        for (var i in urls) {
            if (location.pathname.indexOf(urls[i]) > -1 || location.hash.indexOf(i) > -1) {
                category = i;
            }
        }
        if (category === undefined) {
            category = 'all';
        }
        
        return category;
    };
    
    allBran.popup = function(elmt, options) {
        $(elmt).click(function(e) {
            e.preventDefault();
            window.open($(this).attr('href'), 'popup', options).focus();
        });
    };
    
})();

box.dom('document').ready(function() {

    /*pop ups*/
    allBran.popup('a#legals', 'scrollbars=yes,width=600,height=450');
    allBran.popup('a#credits', 'scrollbars=yes,width=420,height=370');
    allBran.popup('a#contact', 'scrollbars=yes,width=420,height=340');
    
    if (document.getElementById('fibresQuestions')) {
        allBran.accordion('#fibresQuestions');
    }
    
    var carouselConfig, category;
    
    function subNavSetActive() {
        $('#subNav .on').removeClass('on');
        $('#subNav li').eq(box.ui('carousel.rangeCarousel').current).addClass('on');
    }
    
    if (document.getElementById('recipesCarousel')) {
    
        var datas = $('#recipesCarousel .carouselWrapper li');
        
        /* gets & displays current category */
        category = allBran.getCategory(l10n.url.recipes);
        
        if (category != 'all') {
            datas.each(function() {
                if (!$(this).hasClass(category)) {
                    $(this).remove();
                }
            });
        }
        $('#recipesCarousel .carouselWrapper li:last-child').addClass('last');
        
        $('#subNav a').each(function() {
            $(this).parent().removeClass('on');
            if ($(this).attr('class').indexOf(category) > -1) {
                $(this).parent().addClass('on');
            }
        });
        
        /* carousel datas */
        carouselConfig = {
            element: '#recipesCarousel',
            horizontal: true,
            display: 4,
            duration: 600,
            paginate: true,
            buttons: true,
            startAt: 0,
            offset: 0
        };
        /* creates carsousel */
        box.ui('carousel').create(carouselConfig);
        
        $('#subNav a').click(function(e) {
            e.preventDefault();
            if ($(this).parent().hasClass('on')) {
                return false;
            } else {
                $('#subNav li').removeClass('on');
                $(this).parent().addClass('on');
                
                /* gets category */
                var category = $(this).attr('class');
                
                /* replaces formers elements */
                $('#recipesCarousel .carouselWrapper ul').html(datas);
                $('#recipesCarousel .carouselWrapper ul .last').removeClass('last');
                
                /* displays selected elements */
                if (category != 'all') {
                    datas.each(function() {
                        if (!$(this).hasClass(category)) {
                            $(this).remove();
                        }
                    });
                }
                $('#recipesCarousel .carouselWrapper li:last-child').addClass('last');
                
                /* destroys carousel instance */
                box.ui('carousel').destroy('recipesCarousel');
                /* removes added html */
                $('#recipesCarousel .pagination').remove();
                $('#recipesCarousel .next').remove();
                $('#recipesCarousel .prev').remove();
                
                /* creates a new carousel */
                box.ui('carousel').create(carouselConfig);
                /* places carousel on left */
                $('#recipesCarousel .carouselWrapper ul').css('left', 0);
            }
            
        });
    }
    
    if (document.getElementById('rangeCarousel')) {
    
        category = allBran.getCategory(l10n.url.products);
        var categoryIndex = 1;
        $('#subNav a').each(function(index) {
            if ($(this).attr('href').indexOf(category) > -1) {
                categoryIndex = index + 1;
            }
        });
        
        /* carousel datas */
        carouselConfig = {
            element: '#rangeCarousel',
            horizontal: true,
            display: 1,
            duration: 600,
            buttons: true,
            startAt: categoryIndex
        };
        
        /* creates carousel */
        box.ui('carousel').create(carouselConfig);
        subNavSetActive();
        
        $('#subNav a').each(function(index) {
            $(this).click(function(e) {
                e.preventDefault();
                box.ui('carousel.rangeCarousel').moveToItem(index + 1);
                subNavSetActive();
            });
        });
        
        $('#rangeCarousel .next, #rangeCarousel .prev').click(function(e) {
            subNavSetActive();
        });
    }
});

