$(function() {
   
    $('.box-collection .album-expand')
        .click(function() {
            var $$ = $(this).parent(); 
            if($$.hasClass('box-collection-expanded')) {
                $$.removeClass('box-collection-expanded');
            } else {
                $$.addClass('box-collection-expanded');
            }
            return false;
        }); 
        
    $('.box-collection, .box-generic, .box-video-pushed, .box-article-pushed, .box-pagette, .box-link')
        .mouseenter(function() {
            $(this).addClass("box-hover")
        })
        .mouseleave(function() {
            $(this).removeClass("box-hover")
        });  
        
    $('.carousel').each(function() {
        var $$ = $(this);
        var elWidth = 91*4;
        var elCount = Math.ceil($$.find('li').length / 4);
        var leftPosition = 0;
        var leftPositionMax = (elCount > 1) ? (elCount-1) * (- elWidth) : 0; 
        if(leftPositionMax == 0) $$.find('.prev, .next').hide();
        $$.find('.carousel-view ul').css('width', elCount * elWidth);
        
        $$.find('.prev').bind('click', function() {
            if(leftPosition != 0) {
                leftPosition = leftPosition + elWidth;
                $$.find('.carousel-view ul')
                    .animate(
                        {
                            left: leftPosition
                        }, 300);
            }
            else {
                leftPosition = leftPositionMax;
                $$.find('.carousel-view ul')
                    .animate(
                        {
                            left: leftPosition
                        }, 300);
            }
            return false;
        });
        $$.find('.next').bind('click', function() {
            if(leftPosition > leftPositionMax) {
                leftPosition = leftPosition - elWidth;
                $$.find('.carousel-view ul')
                    .animate(
                        {
                            left: leftPosition
                        }, 300);
            }
            else {
                leftPosition = 0;
                $$.find('.carousel-view ul')
                    .animate(
                        {
                            left: leftPosition
                        }, 300);                
            }
            return false;
        });

        if($$.find('.active').length) {
            var pos = $$.find('.active').position();
            leftPosition = (Math.floor(pos.left / elWidth)) * elWidth * -1;
            $$.find('.carousel-view ul').css("left", leftPosition);
        }
        
    });
    
    $("h3.to-close").addClass("closed").next().hide();
    $(".box-filter h3 .icon").each(function() {
        $$ = $(this).parent();
        $$.mouseenter(function() { $("body").css("cursor", "pointer"); })
        .mouseleave(function() { $("body").css("cursor", "auto"); })
        .click(function() { 
            if($(this).hasClass('closed')) { 
                $(this).removeClass('closed'); 
                $(this).next().show(); 
            } else {
                $(this).addClass('closed'); 
                $(this).next().hide(); 
            }
        });        
    });

    
    $(".box-confirm .icon")
        .mouseenter(function() { $("body").css("cursor", "pointer"); })
        .mouseleave(function() { $("body").css("cursor", "auto"); })
        .click(function() { $(this).parent().hide(); });
    
});